chiark / gitweb /
Use Unix eols instead of CRLFs (#148)
[nlopt.git] / CMakeLists.txt
1 #==============================================================================
2 # NLOPT CMake file
3 #
4 # NLopt is a free/open-source library for nonlinear optimization, providing
5 # a common interface for a number of different free optimization routines
6 # available online as well as original implementations of various other
7 # algorithms
8 # WEBSITE: http://ab-initio.mit.edu/wiki/index.php/NLopt
9 # AUTHOR: Steven G. Johnson
10 #
11 # This CMakeLists.txt file was created to compile NLOPT with the CMAKE utility.
12 # Benoit Scherrer, 2010 CRL, Harvard Medical School
13 # Copyright (c) 2008-2009 Children's Hospital Boston
14 #==============================================================================
15 cmake_minimum_required (VERSION 3.0)
16
17 if (NOT DEFINED CMAKE_BUILD_TYPE)
18   set (CMAKE_BUILD_TYPE Release CACHE STRING "Build type")
19 endif ()
20
21 project (nlopt)
22
23 list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
24
25 option (WITH_CXX "enable cxx routines" OFF)
26 option (BUILD_SHARED_LIBS "Build NLopt as a shared library" ON)
27 option (BUILD_PYTHON "build python bindings" ON)
28 option (BUILD_OCTAVE "build octave bindings" ON)
29 option (BUILD_MATLAB "build matlab bindings" ON)
30 option (BUILD_GUILE "build guile bindings" ON)
31 option (USE_SWIG "use SWIG to build bindings" ON)
32
33 set (NLOPT_SUFFIX)
34 if (WITH_CXX)
35   set (NLOPT_SUFFIX _cxx)
36 endif ()
37
38 include (GNUInstallDirs)
39
40 # Offer the user the choice of overriding the installation directories
41 set (INSTALL_LIB_DIR     ${CMAKE_INSTALL_LIBDIR}        CACHE PATH "Installation directory for libraries")
42 set (INSTALL_BIN_DIR     ${CMAKE_INSTALL_BINDIR}        CACHE PATH "Installation directory for executables")
43 set (INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}    CACHE PATH "Installation directory for header files")
44 set (INSTALL_DATA_DIR    ${CMAKE_INSTALL_DATADIR}/nlopt CACHE PATH "Installation directory for data files")
45 set (INSTALL_MAN_DIR     ${CMAKE_INSTALL_MANDIR}        CACHE PATH "Installation directory for man documentation")
46 set (INSTALL_CMAKE_DIR   ${INSTALL_LIB_DIR}/cmake/nlopt CACHE PATH "Installation directory for cmake config files")
47
48 # Make relative paths absolute (needed later on)
49 foreach (p LIB BIN INCLUDE DATA CMAKE)
50   set (var INSTALL_${p}_DIR)
51   set (RELATIVE_INSTALL_${p}_DIR ${INSTALL_${p}_DIR})
52   if (NOT IS_ABSOLUTE "${${var}}")
53     set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
54   endif ()
55 endforeach ()
56
57
58 set (CMAKE_INSTALL_RPATH ${INSTALL_LIB_DIR})
59 set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
60 if(POLICY CMP0042)
61   # Set MACOSX_RPATH to ON
62   cmake_policy(SET CMP0042 NEW)
63 endif()
64
65 include (CheckIncludeFiles)
66 include (CheckFunctionExists)
67 include (CheckTypeSize)
68 include (CheckCCompilerFlag)
69 include (CheckCXXSymbolExists)
70 include (CheckCXXCompilerFlag)
71 include (CheckLibraryExists)
72
73 #==============================================================================
74 # COMPILATION CHECKINGS and CONFIGURATION GENERATION
75 #==============================================================================
76 check_include_file (dlfcn.h HAVE_DLFCN_H)
77 check_include_file (getopt.h HAVE_GETOPT_H)
78 check_include_file (unistd.h HAVE_UNISTD_H)
79 check_include_file (string.h HAVE_STRING_H)
80 check_include_file (strings.h HAVE_STRINGS_H)
81 check_include_file (inttypes.h HAVE_INTTYPES_H)
82 check_include_file (memory.h HAVE_MEMORY_H)
83 check_include_file (stdlib.h HAVE_STDLIB_H)
84 check_include_file (stdint.h HAVE_STDINT_H)
85 check_include_file (time.h HAVE_TIME_H)
86 check_include_file (sys/types.h HAVE_SYS_TYPES_H)
87 check_include_file (sys/stat.h HAVE_SYS_STAT_H)
88 check_include_file (sys/time.h HAVE_SYS_TIME_H)
89 if (HAVE_TIME_H AND HAVE_SYS_TIME_H)
90   set (TIME_WITH_SYS_TIME TRUE)
91 endif ()
92 check_function_exists (getpid HAVE_GETPID)
93 check_function_exists (syscall HAVE_GETTID_SYSCALL)
94 check_function_exists (isinf HAVE_ISINF)
95 check_function_exists (isnan HAVE_ISNAN)
96 check_function_exists (gettimeofday HAVE_GETTIMEOFDAY)
97 check_function_exists (qsort_r HAVE_QSORT_R)
98 check_function_exists (time HAVE_TIME)
99 check_function_exists (copysign HAVE_COPYSIGN)
100 check_type_size ("uint32_t" SIZEOF_UINT32_T)
101 set (HAVE_UINT32_T ${SIZEOF_UINT32_T})
102 check_type_size ("unsigned int" SIZEOF_UNSIGNED_INT)
103 check_type_size ("unsigned long" SIZEOF_UNSIGNED_LONG)
104
105 check_library_exists ("m" sqrt "" HAVE_LIBM)
106 if (HAVE_LIBM)
107   set (M_LIBRARY m)
108 endif()
109
110 if (NOT DEFINED HAVE_FPCLASSIFY)
111   message(STATUS "Looking for fpclassify")
112   file (WRITE ${PROJECT_BINARY_DIR}/fpclassify.c "#include <math.h>\n")
113   file (APPEND ${PROJECT_BINARY_DIR}/fpclassify.c "int main(void) {\n")
114   file (APPEND ${PROJECT_BINARY_DIR}/fpclassify.c "if (!fpclassify(3.14159)) fpclassify(2.7183);\n")
115   file (APPEND ${PROJECT_BINARY_DIR}/fpclassify.c "  return 0; }\n")
116   try_compile (HAVE_FPCLASSIFY
117   ${PROJECT_BINARY_DIR}/build_fpclassify
118   ${PROJECT_BINARY_DIR}/fpclassify.c
119   CMAKE_FLAGS -DLINK_LIBRARIES=m)
120   message(STATUS "Looking for fpclassify - ${HAVE_FPCLASSIFY}")
121 endif ()
122
123 option (WITH_THREADLOCAL "check thread local keyword" ON)
124 if (WITH_THREADLOCAL AND NOT DEFINED HAVE_THREAD_LOCAL_STORAGE)
125   foreach (_THREADLOCAL_KEY "__thread" "__declspec(thread)")
126     unset (HAVE_THREAD_LOCAL_STORAGE CACHE)
127     check_c_source_compiles("
128     ${_THREADLOCAL_KEY} int tls;
129
130     int main(void) {
131         return 0;
132     }" HAVE_THREAD_LOCAL_STORAGE)
133     if (${HAVE_THREAD_LOCAL_STORAGE})
134       set (THREADLOCAL ${_THREADLOCAL_KEY})
135     endif ()
136   endforeach()
137 endif ()
138
139 if (WITH_CXX OR BUILD_PYTHON OR BUILD_GUILE OR BUILD_OCTAVE)
140   check_cxx_symbol_exists (_LIBCPP_VERSION string SYSTEM_HAS_LIBCPP)
141   if (SYSTEM_HAS_LIBCPP)
142     check_cxx_compiler_flag ("-std=c++11" SUPPORTS_STDCXX11)
143     if (SUPPORTS_STDCXX11)
144       set (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
145     endif ()
146   endif ()
147 endif ()
148
149 #==============================================================================
150 # version
151 #==============================================================================
152 set (NLOPT_MAJOR_VERSION "2")
153 set (NLOPT_MINOR_VERSION "5")
154 set (NLOPT_BUGFIX_VERSION "0")
155 set (NLOPT_VERSION_STRING ${NLOPT_MAJOR_VERSION}.${NLOPT_MINOR_VERSION}.${NLOPT_BUGFIX_VERSION})
156 message (STATUS "NLopt version ${NLOPT_VERSION_STRING}")
157
158 #==============================================================================
159 # CREATE nlopt_config.h
160 #==============================================================================
161
162 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt_config.h IMMEDIATE)
163
164 # pkgconfig file
165 if (UNIX OR MINGW)
166   configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt.pc.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc @ONLY)
167   install (FILES ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc DESTINATION ${RELATIVE_INSTALL_LIB_DIR}/pkgconfig)
168 endif ()
169
170 #==============================================================================
171 # nlopt LIBRARY TARGET (SHARED OR STATIC)
172 #==============================================================================
173
174 configure_file (api/nlopt.h ${PROJECT_BINARY_DIR}/api/nlopt.h COPYONLY)
175
176 set (NLOPT_HEADERS
177   ${PROJECT_BINARY_DIR}/api/nlopt.h ${PROJECT_BINARY_DIR}/api/nlopt.hpp ${PROJECT_BINARY_DIR}/api/nlopt.f
178 )
179
180 set (NLOPT_SOURCES
181   direct/DIRect.c direct/direct_wrap.c direct/DIRserial.c direct/DIRsubrout.c direct/direct-internal.h direct/direct.h
182   cdirect/cdirect.c cdirect/hybrid.c cdirect/cdirect.h
183   praxis/praxis.c praxis/praxis.h
184   luksan/plis.c luksan/plip.c luksan/pnet.c luksan/mssubs.c luksan/pssubs.c luksan/luksan.h
185   crs/crs.c crs/crs.h
186   mlsl/mlsl.c mlsl/mlsl.h
187   mma/mma.c mma/mma.h mma/ccsa_quadratic.c
188   cobyla/cobyla.c cobyla/cobyla.h
189   newuoa/newuoa.c newuoa/newuoa.h
190   neldermead/nldrmd.c neldermead/neldermead.h neldermead/sbplx.c
191   auglag/auglag.c auglag/auglag.h
192   bobyqa/bobyqa.c bobyqa/bobyqa.h
193   isres/isres.c isres/isres.h
194   slsqp/slsqp.c slsqp/slsqp.h
195   esch/esch.c esch/esch.h
196   api/general.c api/options.c api/optimize.c api/deprecated.c api/nlopt-internal.h api/nlopt.h api/f77api.c api/f77funcs.h api/f77funcs_.h api/nlopt.hpp api/nlopt-in.hpp
197   util/mt19937ar.c util/sobolseq.c util/soboldata.h util/timer.c util/stop.c util/nlopt-util.h util/redblack.c util/redblack.h util/qsort_r.c util/rescale.c
198 )
199
200 if (WITH_CXX)
201   list (APPEND NLOPT_SOURCES stogo/global.cc stogo/linalg.cc stogo/local.cc stogo/stogo.cc stogo/tools.cc stogo/global.h stogo/linalg.h stogo/local.h stogo/stogo_config.h stogo/stogo.h stogo/tools.h)
202 endif ()
203
204 install (FILES ${NLOPT_HEADERS} DESTINATION ${INSTALL_INCLUDE_DIR})
205
206 set (nlopt_lib nlopt${NLOPT_SUFFIX})
207 add_library (${nlopt_lib} ${NLOPT_SOURCES})
208 target_link_libraries (${nlopt_lib} ${M_LIBRARY})
209
210 set_target_properties (${nlopt_lib} PROPERTIES SOVERSION 0)
211 set_target_properties (${nlopt_lib} PROPERTIES VERSION 0.9.0)
212
213 #==============================================================================
214 # INCLUDE DIRECTORIES
215 #==============================================================================
216 target_include_directories (${nlopt_lib} PRIVATE
217   ${PROJECT_BINARY_DIR}/api
218   ${PROJECT_BINARY_DIR}
219   stogo
220   util
221   direct
222   cdirect
223   praxis
224   luksan
225   crs
226   mlsl
227   mma
228   cobyla
229   newuoa
230   neldermead
231   auglag
232   bobyqa
233   isres
234   slsqp
235   esch
236   api)
237
238 get_target_property (NLOPT_PRIVATE_INCLUDE_DIRS ${nlopt_lib} INCLUDE_DIRECTORIES)
239 target_include_directories (${nlopt_lib} INTERFACE "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/api>" "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
240
241 if (BUILD_SHARED_LIBS)
242   target_compile_definitions (${nlopt_lib} PUBLIC -DNLOPT_DLL)
243   target_compile_definitions (${nlopt_lib} PRIVATE -DNLOPT_DLL_EXPORT)
244 endif ()
245
246 # pass -fPIC in case swig module is built with static library
247 if (NOT BUILD_SHARED_LIBS)
248   check_c_compiler_flag (-fPIC HAS_FPIC)
249   if (HAS_FPIC)
250     set (CMAKE_C_FLAGS "-fPIC ${CMAKE_C_FLAGS}")
251     set (CMAKE_CXX_FLAGS "-fPIC ${CMAKE_CXX_FLAGS}")
252   endif ()
253 endif ()
254
255 install ( TARGETS ${nlopt_lib}
256           EXPORT  NLoptLibraryDepends
257           RUNTIME DESTINATION ${RELATIVE_INSTALL_BIN_DIR}
258           LIBRARY DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
259           ARCHIVE DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
260         )
261
262 if (MSVC AND BUILD_SHARED_LIBS AND NOT CMAKE_VERSION VERSION_LESS 3.1)
263   install (FILES $<TARGET_PDB_FILE:${nlopt_lib}> DESTINATION ${RELATIVE_INSTALL_BIN_DIR} CONFIGURATIONS Debug RelWithDebInfo COMPONENT Debug)
264 endif ()
265
266 add_subdirectory (api)
267
268 if (BUILD_PYTHON)
269   find_package (PythonInterp)
270   find_package (PythonLibs)
271   find_package (NumPy)
272 endif ()
273
274 if (NOT DEFINED INSTALL_PYTHON_DIR AND PYTHONINTERP_FOUND)
275   execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}'))"
276                     OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
277                     OUTPUT_STRIP_TRAILING_WHITESPACE )
278
279   get_filename_component (_ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE)
280   file (RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
281
282   set (INSTALL_PYTHON_DIR ${_REL_PYTHON_MODULE_PATH})
283
284 endif ()
285
286 if (BUILD_GUILE)
287   find_package (Guile)
288 endif ()
289
290 if (USE_SWIG)
291   find_package (SWIG)
292 endif ()
293
294 add_subdirectory (swig)
295
296 if (BUILD_OCTAVE)
297   find_package (Octave)
298 endif ()
299
300 if (BUILD_MATLAB)
301   find_package (Matlab)
302 endif ()
303
304 if (OCTAVE_FOUND OR Matlab_FOUND)
305   add_subdirectory (octave)
306 endif ()
307
308 enable_testing ()
309 add_subdirectory (test)
310
311 set (CPACK_PACKAGE_NAME          "${CMAKE_PROJECT_NAME}")
312 set (CPACK_PACKAGE_VERSION_MAJOR "${NLOPT_MAJOR_VERSION}")
313 set (CPACK_PACKAGE_VERSION_MINOR "${NLOPT_MINOR_VERSION}")
314 set (CPACK_PACKAGE_VERSION_PATCH "${NLOPT_BUGFIX_VERSION}")
315 set (CPACK_SOURCE_GENERATOR      "TBZ2;TGZ"    )
316 set (CPACK_BINARY_STGZ           "OFF"        )
317 set (CPACK_BINARY_TBZ2           "ON"         )
318 set (CPACK_BINARY_TGZ            "ON"         )
319 set (CPACK_BINARY_TZ             "OFF"        )
320 set (CPACK_SOURCE_IGNORE_FILES ".git;/build;.*~;${CPACK_SOURCE_IGNORE_FILES}")
321
322 set (CPACK_SOURCE_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${NLOPT_VERSION_STRING})
323
324 include (CPack)
325
326 # configuration files
327 export (TARGETS ${nlopt_lib} NAMESPACE NLopt:: FILE ${PROJECT_BINARY_DIR}/NLoptLibraryDepends.cmake)
328
329 # Install the export set for use with the install-tree
330 install(EXPORT NLoptLibraryDepends
331         NAMESPACE NLopt::
332         DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
333         COMPONENT Development)
334
335 # Create a NLOPTConfig.cmake file for the use from the install tree
336 # and install it
337 set (NLOPT_LIBRARIES "NLopt::${nlopt_lib}")
338
339 set (NLOPT_CMAKE_DIR "${INSTALL_CMAKE_DIR}")
340 file (RELATIVE_PATH rel_include_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_INCLUDE_DIR}")
341 list (APPEND RELATIVE_NLOPT_INCLUDE_DIRS ${rel_include_dir})
342
343 file (RELATIVE_PATH rel_lib_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_LIB_DIR}")
344 list (APPEND RELATIVE_NLOPT_LIB_DIR ${rel_lib_dir})
345
346 configure_file (cmake/NLoptConfig.cmake.in NLoptConfig.cmake @ONLY)
347 configure_file (cmake/NLoptConfigVersion.cmake.in NLoptConfigVersion.cmake @ONLY)
348 install (FILES
349           ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfig.cmake
350           ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfigVersion.cmake
351          DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
352          COMPONENT Development)
353