[build] Force C++11 compilation when using CMake.
[ginac.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.1)
2 set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
3
4 project(GiNaC)
5 file(STRINGS ${CMAKE_SOURCE_DIR}/ginac/version.h _ginac_vinfo REGEX "^#define[\t ]+GINACLIB_.*_VERSION.*")
6 string(REGEX REPLACE "^.*GINACLIB_MAJOR_VERSION[ \t]+([0-9]+).*" "\\1" GINAC_MAJOR_VERSION "${_ginac_vinfo}")
7 string(REGEX REPLACE "^.*GINACLIB_MINOR_VERSION[ \t]+([0-9]+).*" "\\1" GINAC_MINOR_VERSION "${_ginac_vinfo}")
8 string(REGEX REPLACE "^.*GINACLIB_MICRO_VERSION[ \t]+([0-9]+).*" "\\1" GINAC_MICRO_VERSION "${_ginac_vinfo}")
9 set(GINAC_VERSION "${GINAC_MAJOR_VERSION}.${GINAC_MINOR_VERSION}.${GINAC_MICRO_VERSION}")
10
11 # Library versioning info
12 file(STRINGS ${CMAKE_SOURCE_DIR}/ginac/version.h _ginac_vinfo REGEX "^#define[\t ]+GINAC_LT_.*")
13 string(REGEX REPLACE "^.*GINAC_LT_CURRENT[ \t]+([0-9]+).*" "\\1" ginac_lt_current "${_ginac_vinfo}")
14 string(REGEX REPLACE "^.*GINAC_LT_AGE[ \t]+([0-9]+).*" "\\1" ginac_lt_age "${_ginac_vinfo}")
15 string(REGEX REPLACE "^.*GINAC_LT_REVISION[ \t]+([0-9]+).*" "\\1" ginac_lt_revision "${_ginac_vinfo}")
16 # XXX: it looks like we need to set this for every platform?
17 math(EXPR ginaclib_soversion "${ginac_lt_current} - ${ginac_lt_age}")
18 set(ginaclib_version ${ginaclib_soversion}.${ginac_lt_age}.${ginac_lt_revision})
19
20 # make check
21 enable_testing()
22 add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND})
23 # make info
24 add_custom_target(info ALL)
25 add_custom_target(html)
26 add_custom_target(pdf)
27
28 set (CMAKE_CXX_STANDARD 11)
29
30 find_package(CLN 1.2.2 REQUIRED)
31 include_directories(${CLN_INCLUDE_DIR})
32
33 include(CheckIncludeFile)
34 check_include_file("unistd.h" HAVE_UNISTD_H)
35
36 include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/ginac)
37
38 # This macro implements some very special logic how to deal with the cache.
39 # By default the various install locations inherit their value from their
40 #"parent" variable, so if you set CMAKE_INSTALL_PREFIX, then
41 # EXEC_INSTALL_PREFIX, BIN_INSTALL_DIR, LIB_INSTALL_DIR, etc will calculate
42 # their value by appending subdirs to CMAKE_INSTALL_PREFIX.
43 # This would work just fine without using the cache.
44 # But if somebody wants e.g. a different EXEC_INSTALL_PREFIX this value
45 # has to go into the cache, otherwise it will be forgotten on the next cmake
46 # run. Once a variable is in the cache, it doesn't depend on its "parent"
47 # variables anymore and you can only change it by editing it directly.
48 # This macro helps in this regard, because as long as you don't set one
49 # of the variables explicitly to some location, the value will be computed
50 # from parents of the variable in question. So modifying CMAKE_INSTALL_PREFIX
51 # later on will have the desired effect.
52 # But once you decide to set e.g. EXEC_INSTALL_PREFIX to some special
53 # location this will go into the cache and it will no longer depend on
54 # CMAKE_INSTALL_PREFIX.
55
56 macro(_set_fancy _var _value _comment)
57         set(predefinedvalue "${_value}")
58         if ("${CMAKE_INSTALL_PREFIX}" STREQUAL "${GINAC_INSTALL_DIR}" AND DEFINED GINAC_${_var})
59                 set(predefinedvalue "${GINAC_${_var}}")
60         endif()
61         if (NOT DEFINED ${_var})
62                 set(${_var} ${predefinedvalue})
63         else()
64                 set(${_var} "${${_var}}" CACHE PATH "${_comment}")
65         endif()
66 endmacro(_set_fancy)
67
68 _set_fancy(EXEC_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}"
69            "Base directory for libraries and executables")
70 _set_fancy(LIB_INSTALL_DIR "${EXEC_INSTALL_PREFIX}/lib"
71            "Libraries installation directory")
72 _set_fancy(BIN_INSTALL_DIR "${EXEC_INSTALL_PREFIX}/bin"
73            "Binaries installation directory")
74 _set_fancy(SHARE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/share"
75            "Base directory for architecture independent files")
76 _set_fancy(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include"
77            "Headers installation directory")
78
79 if (NOT DEFINED BUILD_SHARED_LIBS)
80         if (NOT MSVC)
81                 set(BUILD_SHARED_LIBS true)
82         else()
83                 set(BUILD_SHARED_LIBS false)
84         endif()
85 endif()
86
87 # Set proper rpath so tools will Just Work(TM) after make install.
88 # Also take care to add -Wl,-rpath, stanza into the *.pc file so that
89 #
90 # g++ `pkg-config --cflags --libs ginac`
91 #
92 # will Just Work (TM), too.
93 # Distro packagers should use -DCMAKE_INSTALL_RPATH="" to avoid
94 # setting rpath on installed binaries.
95
96 # rpath for making binaries/libraries relocatable
97 set(_ginac_rpath_reloc "$ORIGIN/../lib")
98 set(_wl_rpath "${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG}")
99
100 # rpath for the pkg-config meta-data.
101 set(_ginaclib_rpath "${_wl_rpath}${_ginac_rpath_reloc}")
102 list(FIND CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "${LIB_INSTALL_DIR}" isSystemDir)
103 if ("${isSystemDir}" STREQUAL "-1")
104         list(APPEND _ginaclib_rpath "${_wl_rpath}\${libdir}")
105 endif()
106 set(GINACLIB_RPATH)
107 if (NOT CMAKE_SKIP_RPATH)
108         if (_wl_rpath)
109                 set(GINACLIB_RPATH "${_ginaclib_rpath}")
110         endif()
111 endif()
112
113 configure_file(${CMAKE_SOURCE_DIR}/ginac.pc.cmake ${CMAKE_BINARY_DIR}/ginac.pc @ONLY)
114 install(FILES ${CMAKE_BINARY_DIR}/ginac.pc DESTINATION "${LIB_INSTALL_DIR}/pkgconfig")
115
116 # rpath for libginac.so itself, ginsh, and friends
117 set(_ginac_rpath ${_ginac_rpath_reloc})
118 foreach(_d ${CLN_LIBRARY_DIRS} ${LIB_INSTALL_DIR})
119         list(FIND CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "${_d}" isSystemDir)
120         if ("${isSystemDir}" STREQUAL "-1")
121                 list(APPEND _ginac_rpath "${_d}")
122         endif()
123 endforeach()
124 list(REMOVE_DUPLICATES _ginac_rpath)
125 string(REPLACE ";" ":" ginac_rpath "${_ginac_rpath}")
126
127 if (NOT DEFINED CMAKE_INSTALL_RPATH_USE_LINK_RPATH)
128         set(CMAKE_INSTALL_RPATH_USE_LINK_RPATH TRUE)
129 endif()
130 if (NOT DEFINED CMAKE_INSTALL_RPATH)
131         set(CMAKE_INSTALL_RPATH ${ginac_rpath})
132 endif()
133 if (APPLE AND NOT DEFINED CMAKE_INSTALL_NAME_DIR)
134         set(CMAKE_INSTALL_NAME_DIR ${LIB_INSTALL_DIR})
135 endif()
136
137 list(FIND CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "${LIB_INSTALL_DIR}" isSystemDir)
138 if ("${isSystemDir}" STREQUAL "-1")
139         string(REPLACE ":" ";" _install_rpath "${CMAKE_INSTALL_RPATH}")
140         list(FIND _install_rpath "${LIB_INSTALL_DIR}" _is_rpath_consistent)
141         if ("${_is_rpath_consistent}" STREQUAL "-1")
142                 message(WARNING "the libginac.so library will be installed into "
143                                 "a non-standard directory (${LIB_INSTALL_DIR}), "
144                                 "however, the rpath (${_install_rpath}) "
145                                 "does not contain that directory. Most likely "
146                                 "things won't work without extra configuration "
147                                 "(tweaking LD_LIBRARY_PATH, /etc/ld.so.conf, etc).")
148         endif()
149 endif()
150
151
152 include(FindFLEX)
153 include(FindBISON)
154 find_package(BISON)
155 find_package(FLEX)
156 find_package(Readline)
157 if (READLINE_FOUND)
158         set(HAVE_LIBREADLINE 1)
159         set(HAVE_READLINE_READLINE_H 1)
160         set(HAVE_READLINE_HISTORY_H 1)
161 endif()
162
163 find_package(LibDL)
164 if (LIBDL_FOUND)
165         set(HAVE_LIBDL 1)
166 endif()
167
168 find_program(MAKEINFO makeinfo)
169 find_program(FIG2DEV fig2dev)
170
171 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
172 add_definitions(-DHAVE_CONFIG_H)
173
174 add_subdirectory(ginac)
175 add_subdirectory(tools)
176 add_subdirectory(check)
177 if (BISON_FOUND AND FLEX_FOUND)
178         add_subdirectory(ginsh)
179 endif()
180 if (MAKEINFO)
181         add_subdirectory(doc)
182 endif()
183