Set version, soname ahead of release.
[ginac.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.1)
2 set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
3
4 project(GiNaC)
5 file(STRINGS ${CMAKE_CURRENT_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_CURRENT_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 include(GNUInstallDirs)
21
22 # make check
23 enable_testing()
24 if (NOT TARGET check)
25         add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND})
26 endif()
27 if (NOT TARGET test_suite)
28         add_custom_target(test_suite)
29 endif()
30 if (WIN32)
31         if (NOT DEFINED CMAKE_RUNTIME_OUTPUT_DIRECTORY)
32                 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
33         endif()
34 endif()
35 # make info
36 if (NOT TARGET info)
37         add_custom_target(info ALL)
38 endif()
39 if (NOT TARGET html)
40         add_custom_target(html)
41 endif()
42 if (NOT TARGET pdf)
43         add_custom_target(pdf)
44 endif()
45
46 set (CMAKE_CXX_STANDARD 11)
47
48 if (NOT DEFINED CLN_SOURCE_DIR)
49         set(CLN_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cln)
50 endif()
51 if (EXISTS ${CLN_SOURCE_DIR}/CMakeLists.txt)
52         add_subdirectory(${CLN_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/cln)
53 else()
54         find_package(CLN 1.2.2 REQUIRED)
55 endif()
56
57 include(CheckIncludeFile)
58 check_include_file("unistd.h" HAVE_UNISTD_H)
59
60 if (NOT DEFINED BUILD_SHARED_LIBS)
61         if (NOT MSVC)
62                 set(BUILD_SHARED_LIBS true)
63         else()
64                 set(BUILD_SHARED_LIBS false)
65         endif()
66 endif()
67
68 # Set proper rpath so tools will Just Work(TM) after make install.
69 # Also take care to add -Wl,-rpath, stanza into the *.pc file so that
70 #
71 # g++ `pkg-config --cflags --libs ginac`
72 #
73 # will Just Work (TM), too.
74 # Distro packagers should use -DCMAKE_INSTALL_RPATH="" to avoid
75 # setting rpath on installed binaries.
76
77 # rpath for making binaries/libraries relocatable
78 set(_ginac_rpath_reloc "$ORIGIN/../lib")
79 set(_wl_rpath "${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG}")
80
81 # rpath for the pkg-config meta-data.
82 set(_ginaclib_rpath "${_wl_rpath}${_ginac_rpath_reloc}")
83 list(FIND CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_FULL_LIBDIR}" isSystemDir)
84 if ("${isSystemDir}" STREQUAL "-1")
85         list(APPEND _ginaclib_rpath "${_wl_rpath}\${libdir}")
86 endif()
87 set(GINACLIB_RPATH)
88 if (NOT CMAKE_SKIP_RPATH)
89         if (_wl_rpath)
90                 set(GINACLIB_RPATH "${_ginaclib_rpath}")
91         endif()
92 endif()
93
94 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/ginac.pc.cmake ${CMAKE_CURRENT_BINARY_DIR}/ginac.pc @ONLY)
95 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ginac.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
96
97 # rpath for libginac.so itself, ginsh, and friends
98 set(_ginac_rpath ${_ginac_rpath_reloc})
99 foreach(_d ${CLN_LIBRARY_DIRS} ${CMAKE_INSTALL_FULL_LIBDIR})
100         list(FIND CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "${_d}" isSystemDir)
101         if ("${isSystemDir}" STREQUAL "-1")
102                 list(APPEND _ginac_rpath "${_d}")
103         endif()
104 endforeach()
105 list(REMOVE_DUPLICATES _ginac_rpath)
106 string(REPLACE ";" ":" ginac_rpath "${_ginac_rpath}")
107
108 if (NOT DEFINED CMAKE_INSTALL_RPATH_USE_LINK_RPATH)
109         set(CMAKE_INSTALL_RPATH_USE_LINK_RPATH TRUE)
110 endif()
111 if (NOT DEFINED CMAKE_INSTALL_RPATH)
112         set(CMAKE_INSTALL_RPATH ${ginac_rpath})
113 endif()
114 if (APPLE AND NOT DEFINED CMAKE_INSTALL_NAME_DIR)
115         set(CMAKE_INSTALL_NAME_DIR ${CMAKE_INSTALL_FULL_LIBDIR})
116 endif()
117
118 list(FIND CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_FULL_LIBDIR}" isSystemDir)
119 if ("${isSystemDir}" STREQUAL "-1")
120         string(REPLACE ":" ";" _install_rpath "${CMAKE_INSTALL_RPATH}")
121         list(FIND _install_rpath "${CMAKE_INSTALL_FULL_LIBDIR}" _is_rpath_consistent)
122         if ("${_is_rpath_consistent}" STREQUAL "-1")
123                 message(WARNING "the libginac.so library will be installed into "
124                         "a non-standard directory (${CMAKE_INSTALL_FULL_LIBDIR}), "
125                                 "however, the rpath (${_install_rpath}) "
126                                 "does not contain that directory. Most likely "
127                                 "things won't work without extra configuration "
128                                 "(tweaking LD_LIBRARY_PATH, /etc/ld.so.conf, etc).")
129         endif()
130 endif()
131
132 include(CMakePackageConfigHelpers)
133 write_basic_package_version_file(
134         ${CMAKE_CURRENT_BINARY_DIR}/ginac-config-version.cmake
135         VERSION ${GINAC_VERSION}
136         COMPATIBILITY AnyNewerVersion
137 )
138
139 configure_package_config_file(${CMAKE_CURRENT_LIST_DIR}/cmake/ginac-config.cmake.in
140         ${CMAKE_CURRENT_BINARY_DIR}/ginac-config.cmake
141         INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ginac
142 )
143
144 install(FILES
145         ${CMAKE_CURRENT_BINARY_DIR}/ginac-config.cmake
146         ${CMAKE_CURRENT_BINARY_DIR}/ginac-config-version.cmake
147         DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ginac
148 )
149
150 include(FindFLEX)
151 include(FindBISON)
152 find_package(BISON)
153 find_package(FLEX)
154 find_package(Readline)
155 if (READLINE_FOUND)
156         set(HAVE_LIBREADLINE 1)
157         set(HAVE_READLINE_READLINE_H 1)
158         set(HAVE_READLINE_HISTORY_H 1)
159 endif()
160
161 find_package(LibDL)
162 if (LIBDL_FOUND)
163         set(HAVE_LIBDL 1)
164 endif()
165
166 find_program(MAKEINFO makeinfo)
167 find_program(FIG2DEV fig2dev)
168 find_program(PYTHON python)
169 if (NOT PYTHON)
170         find_program(PYTHON python3)
171 endif()
172 if (NOT PYTHON)
173         message(FATAL_ERROR "Python version 3 or 2 is required")
174 endif()
175
176
177 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
178
179 if (NOT LIBEXECDIR)
180         set (LIBEXECDIR "${CMAKE_INSTALL_PREFIX}/libexec")
181 endif()
182
183 add_subdirectory(ginac)
184 add_subdirectory(tools)
185 add_subdirectory(check)
186 if (BISON_FOUND AND FLEX_FOUND)
187         add_subdirectory(ginsh)
188 endif()
189 if (MAKEINFO)
190         add_subdirectory(doc)
191 endif()
192