1 #==============================================================================
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
8 # WEBSITE: http://ab-initio.mit.edu/wiki/index.php/NLopt
9 # AUTHOR: Steven G. Johnson
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 2.8.11)
17 if (NOT DEFINED CMAKE_BUILD_TYPE)
18 set (CMAKE_BUILD_TYPE Release CACHE STRING "Build type")
23 #==============================================================================
25 set (NLOPT_MAJOR_VERSION "2")
26 set (NLOPT_MINOR_VERSION "5")
27 set (NLOPT_BUGFIX_VERSION "0")
28 set (NLOPT_VERSION_STRING ${NLOPT_MAJOR_VERSION}.${NLOPT_MINOR_VERSION}.${NLOPT_BUGFIX_VERSION})
29 message (STATUS "NLopt version ${NLOPT_VERSION_STRING}")
31 # This is the ABI version number, which differes from the API version above
32 # (it indicates ABI compatibility), but they are typically incremented together.
36 #==============================================================================
38 list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
40 option (NLOPT_CXX "enable cxx routines" ON)
41 option (NLOPT_FORTRAN "enable fotran tests" OFF)
42 option (BUILD_SHARED_LIBS "Build NLopt as a shared library" ON)
43 option (NLOPT_PYTHON "build python bindings" ON)
44 option (NLOPT_OCTAVE "build octave bindings" ON)
45 option (NLOPT_MATLAB "build matlab bindings" ON)
46 option (NLOPT_GUILE "build guile bindings" ON)
47 option (NLOPT_SWIG "use SWIG to build bindings" ON)
49 if (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
50 option (NLOPT_TESTS "build unit tests" ON)
52 option (NLOPT_TESTS "build unit tests" OFF)
56 enable_language (Fortran)
59 include (GNUInstallDirs)
61 # Offer the user the choice of overriding the installation directories
62 set (INSTALL_LIB_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "Installation directory for libraries")
63 set (INSTALL_BIN_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Installation directory for executables")
64 set (INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Installation directory for header files")
65 set (INSTALL_DATA_DIR ${CMAKE_INSTALL_DATADIR}/nlopt CACHE PATH "Installation directory for data files")
66 set (INSTALL_MAN_DIR ${CMAKE_INSTALL_MANDIR} CACHE PATH "Installation directory for man documentation")
67 set (INSTALL_CMAKE_DIR ${INSTALL_LIB_DIR}/cmake/nlopt CACHE PATH "Installation directory for cmake config files")
69 # Make relative paths absolute (needed later on)
70 foreach (p LIB BIN INCLUDE DATA CMAKE)
71 set (var INSTALL_${p}_DIR)
72 set (RELATIVE_INSTALL_${p}_DIR ${INSTALL_${p}_DIR})
73 if (NOT IS_ABSOLUTE "${${var}}")
74 set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
79 set (CMAKE_INSTALL_RPATH ${INSTALL_LIB_DIR})
80 set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
82 # Set MACOSX_RPATH to ON
83 cmake_policy(SET CMP0042 NEW)
86 include (CheckIncludeFiles)
87 include (CheckFunctionExists)
88 include (CheckTypeSize)
89 include (CheckCCompilerFlag)
90 include (CheckCXXSymbolExists)
91 include (CheckCXXCompilerFlag)
92 include (CheckLibraryExists)
94 #==============================================================================
95 # COMPILATION CHECKINGS and CONFIGURATION GENERATION
96 #==============================================================================
97 check_include_file (dlfcn.h HAVE_DLFCN_H)
98 check_include_file (getopt.h HAVE_GETOPT_H)
99 check_include_file (unistd.h HAVE_UNISTD_H)
100 check_include_file (string.h HAVE_STRING_H)
101 check_include_file (strings.h HAVE_STRINGS_H)
102 check_include_file (inttypes.h HAVE_INTTYPES_H)
103 check_include_file (memory.h HAVE_MEMORY_H)
104 check_include_file (stdlib.h HAVE_STDLIB_H)
105 check_include_file (stdint.h HAVE_STDINT_H)
106 check_include_file (time.h HAVE_TIME_H)
107 check_include_file (sys/types.h HAVE_SYS_TYPES_H)
108 check_include_file (sys/stat.h HAVE_SYS_STAT_H)
109 check_include_file (sys/time.h HAVE_SYS_TIME_H)
110 if (HAVE_TIME_H AND HAVE_SYS_TIME_H)
111 set (TIME_WITH_SYS_TIME TRUE)
113 check_function_exists (getpid HAVE_GETPID)
114 check_function_exists (syscall HAVE_GETTID_SYSCALL)
115 check_function_exists (isinf HAVE_ISINF)
116 check_function_exists (isnan HAVE_ISNAN)
117 check_function_exists (gettimeofday HAVE_GETTIMEOFDAY)
118 check_function_exists (qsort_r HAVE_QSORT_R)
119 check_function_exists (time HAVE_TIME)
120 check_function_exists (copysign HAVE_COPYSIGN)
121 check_type_size ("uint32_t" SIZEOF_UINT32_T)
122 set (HAVE_UINT32_T ${SIZEOF_UINT32_T})
123 check_type_size ("unsigned int" SIZEOF_UNSIGNED_INT)
124 check_type_size ("unsigned long" SIZEOF_UNSIGNED_LONG)
126 check_library_exists ("m" sqrt "" HAVE_LIBM)
131 if (NOT DEFINED HAVE_FPCLASSIFY)
132 message(STATUS "Looking for fpclassify")
133 file (WRITE ${PROJECT_BINARY_DIR}/fpclassify.c "#include <math.h>\n")
134 file (APPEND ${PROJECT_BINARY_DIR}/fpclassify.c "int main(void) {\n")
135 file (APPEND ${PROJECT_BINARY_DIR}/fpclassify.c "if (!fpclassify(3.14159)) fpclassify(2.7183);\n")
136 file (APPEND ${PROJECT_BINARY_DIR}/fpclassify.c " return 0; }\n")
137 try_compile (HAVE_FPCLASSIFY
138 ${PROJECT_BINARY_DIR}/build_fpclassify
139 ${PROJECT_BINARY_DIR}/fpclassify.c
140 CMAKE_FLAGS -DLINK_LIBRARIES=m)
141 message(STATUS "Looking for fpclassify - ${HAVE_FPCLASSIFY}")
144 option (WITH_THREADLOCAL "check thread local keyword" ON)
145 if (WITH_THREADLOCAL AND NOT DEFINED THREADLOCAL)
146 foreach (_THREADLOCAL_KEY "__thread" "__declspec(thread)")
147 unset (HAVE_THREAD_LOCAL_STORAGE CACHE)
148 check_c_source_compiles("
149 ${_THREADLOCAL_KEY} int tls;
153 }" HAVE_THREAD_LOCAL_STORAGE)
154 if (HAVE_THREAD_LOCAL_STORAGE)
155 set (THREADLOCAL ${_THREADLOCAL_KEY} CACHE STRING "Thread local keyword")
161 if (NLOPT_CXX OR NLOPT_PYTHON OR NLOPT_GUILE OR NLOPT_OCTAVE)
162 check_cxx_symbol_exists (__cplusplus ciso646 SYSTEM_HAS_CXX)
164 check_cxx_compiler_flag ("-std=c++11" SUPPORTS_STDCXX11)
165 if (SUPPORTS_STDCXX11)
166 set (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
172 message (FATAL_ERROR "The compiler doesn't support CXX.")
176 #==============================================================================
177 # CREATE nlopt_config.h
178 #==============================================================================
180 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt_config.h IMMEDIATE)
184 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt.pc.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc @ONLY)
185 install (FILES ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc DESTINATION ${RELATIVE_INSTALL_LIB_DIR}/pkgconfig)
188 #==============================================================================
189 # nlopt LIBRARY TARGET (SHARED OR STATIC)
190 #==============================================================================
192 configure_file (src/api/nlopt.h ${PROJECT_BINARY_DIR}/src/api/nlopt.h COPYONLY)
195 ${PROJECT_BINARY_DIR}/src/api/nlopt.h ${PROJECT_BINARY_DIR}/src/api/nlopt.hpp ${PROJECT_BINARY_DIR}/src/api/nlopt.f
199 src/algs/direct/DIRect.c src/algs/direct/direct_wrap.c src/algs/direct/DIRserial.c src/algs/direct/DIRsubrout.c src/algs/direct/direct-internal.h src/algs/direct/direct.h
200 src/algs/cdirect/cdirect.c src/algs/cdirect/hybrid.c src/algs/cdirect/cdirect.h
201 src/algs/praxis/praxis.c src/algs/praxis/praxis.h
202 src/algs/luksan/plis.c src/algs/luksan/plip.c src/algs/luksan/pnet.c src/algs/luksan/mssubs.c src/algs/luksan/pssubs.c src/algs/luksan/luksan.h
203 src/algs/crs/crs.c src/algs/crs/crs.h
204 src/algs/mlsl/mlsl.c src/algs/mlsl/mlsl.h
205 src/algs/mma/mma.c src/algs/mma/mma.h src/algs/mma/ccsa_quadratic.c
206 src/algs/cobyla/cobyla.c src/algs/cobyla/cobyla.h
207 src/algs/newuoa/newuoa.c src/algs/newuoa/newuoa.h
208 src/algs/neldermead/nldrmd.c src/algs/neldermead/neldermead.h src/algs/neldermead/sbplx.c
209 src/algs/auglag/auglag.c src/algs/auglag/auglag.h
210 src/algs/bobyqa/bobyqa.c src/algs/bobyqa/bobyqa.h
211 src/algs/isres/isres.c src/algs/isres/isres.h
212 src/algs/slsqp/slsqp.c src/algs/slsqp/slsqp.h
213 src/algs/esch/esch.c src/algs/esch/esch.h
214 src/api/general.c src/api/options.c src/api/optimize.c src/api/deprecated.c src/api/nlopt-internal.h src/api/nlopt.h src/api/f77api.c src/api/f77funcs.h src/api/f77funcs_.h src/api/nlopt.hpp src/api/nlopt-in.hpp
215 src/util/mt19937ar.c src/util/sobolseq.c src/util/soboldata.h src/util/timer.c src/util/stop.c src/util/nlopt-util.h src/util/redblack.c src/util/redblack.h src/util/qsort_r.c src/util/rescale.c
219 list (APPEND NLOPT_SOURCES src/algs/stogo/global.cc src/algs/stogo/linalg.cc src/algs/stogo/local.cc src/algs/stogo/stogo.cc src/algs/stogo/tools.cc
220 src/algs/stogo/global.h src/algs/stogo/linalg.h src/algs/stogo/local.h src/algs/stogo/stogo_config.h src/algs/stogo/stogo.h src/algs/stogo/tools.h)
223 list (APPEND NLOPT_SOURCES src/algs/ags/data_types.hpp src/algs/ags/evolvent.hpp src/algs/ags/evolvent.cc src/algs/ags/solver.hpp src/algs/ags/solver.cc
224 src/algs/ags/local_optimizer.hpp src/algs/ags/local_optimizer.cc src/algs/ags/ags.h src/algs/ags/ags.cc)
227 install (FILES ${NLOPT_HEADERS} DESTINATION ${RELATIVE_INSTALL_INCLUDE_DIR})
229 set (nlopt_lib nlopt)
230 add_library (${nlopt_lib} ${NLOPT_SOURCES})
231 target_link_libraries (${nlopt_lib} ${M_LIBRARY})
233 set_target_properties (${nlopt_lib} PROPERTIES SOVERSION ${SO_MAJOR})
234 set_target_properties (${nlopt_lib} PROPERTIES VERSION "${SO_MAJOR}.${SO_MINOR}.${SO_PATCH}")
236 #==============================================================================
237 # INCLUDE DIRECTORIES
238 #==============================================================================
239 target_include_directories (${nlopt_lib} PRIVATE
240 ${PROJECT_BINARY_DIR}/src/api
241 ${PROJECT_BINARY_DIR}
262 get_target_property (NLOPT_PRIVATE_INCLUDE_DIRS ${nlopt_lib} INCLUDE_DIRECTORIES)
263 target_include_directories (${nlopt_lib} INTERFACE "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/src/api>" "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
265 if (BUILD_SHARED_LIBS)
266 target_compile_definitions (${nlopt_lib} PUBLIC -DNLOPT_DLL)
267 target_compile_definitions (${nlopt_lib} PRIVATE -DNLOPT_DLL_EXPORT)
270 # pass -fPIC in case swig module is built with static library
271 if (NOT BUILD_SHARED_LIBS)
272 check_c_compiler_flag (-fPIC HAS_FPIC)
274 set (CMAKE_C_FLAGS "-fPIC ${CMAKE_C_FLAGS}")
275 set (CMAKE_CXX_FLAGS "-fPIC ${CMAKE_CXX_FLAGS}")
279 install ( TARGETS ${nlopt_lib}
280 EXPORT NLoptLibraryDepends
281 RUNTIME DESTINATION ${RELATIVE_INSTALL_BIN_DIR}
282 LIBRARY DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
283 ARCHIVE DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
286 if (MSVC AND BUILD_SHARED_LIBS AND NOT CMAKE_VERSION VERSION_LESS 3.1)
287 install (FILES $<TARGET_PDB_FILE:${nlopt_lib}> DESTINATION ${RELATIVE_INSTALL_BIN_DIR} CONFIGURATIONS Debug RelWithDebInfo COMPONENT Debug)
290 add_subdirectory (src/api)
293 find_package (PythonInterp)
294 find_package (PythonLibs)
298 if (NOT DEFINED INSTALL_PYTHON_DIR AND PYTHONINTERP_FOUND)
299 execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}'))"
300 OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
301 OUTPUT_STRIP_TRAILING_WHITESPACE )
303 get_filename_component (_ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE)
304 file (RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
306 set (INSTALL_PYTHON_DIR ${_REL_PYTHON_MODULE_PATH})
317 add_subdirectory (src/swig)
322 find_package (Octave)
326 find_package (Matlab COMPONENTS MX_LIBRARY)
329 if (OCTAVE_FOUND OR Matlab_FOUND)
330 add_subdirectory (src/octave)
335 add_subdirectory (test)
338 set (CPACK_PACKAGE_NAME "${CMAKE_PROJECT_NAME}")
339 set (CPACK_PACKAGE_VERSION_MAJOR "${NLOPT_MAJOR_VERSION}")
340 set (CPACK_PACKAGE_VERSION_MINOR "${NLOPT_MINOR_VERSION}")
341 set (CPACK_PACKAGE_VERSION_PATCH "${NLOPT_BUGFIX_VERSION}")
342 set (CPACK_SOURCE_GENERATOR "TBZ2;TGZ" )
343 set (CPACK_BINARY_STGZ "OFF" )
344 set (CPACK_BINARY_TBZ2 "ON" )
345 set (CPACK_BINARY_TGZ "ON" )
346 set (CPACK_BINARY_TZ "OFF" )
347 set (CPACK_SOURCE_IGNORE_FILES ".git;/build;.*~;${CPACK_SOURCE_IGNORE_FILES}")
349 set (CPACK_SOURCE_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${NLOPT_VERSION_STRING})
353 # configuration files
354 export (TARGETS ${nlopt_lib} NAMESPACE NLopt:: FILE ${PROJECT_BINARY_DIR}/NLoptLibraryDepends.cmake)
356 # Install the export set for use with the install-tree
357 install(EXPORT NLoptLibraryDepends
359 DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
360 COMPONENT Development)
362 # Create a NLOPTConfig.cmake file for the use from the install tree
364 set (NLOPT_LIBRARIES "NLopt::${nlopt_lib}")
366 set (NLOPT_CMAKE_DIR "${INSTALL_CMAKE_DIR}")
367 file (RELATIVE_PATH rel_include_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_INCLUDE_DIR}")
368 list (APPEND RELATIVE_NLOPT_INCLUDE_DIRS ${rel_include_dir})
370 file (RELATIVE_PATH rel_lib_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_LIB_DIR}")
371 list (APPEND RELATIVE_NLOPT_LIB_DIR ${rel_lib_dir})
373 configure_file (cmake/NLoptConfig.cmake.in NLoptConfig.cmake @ONLY)
374 configure_file (cmake/NLoptConfigVersion.cmake.in NLoptConfigVersion.cmake @ONLY)
376 ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfig.cmake
377 ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfigVersion.cmake
378 DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
379 COMPONENT Development)