1 #==============================================================================
\r
4 # NLopt is a free/open-source library for nonlinear optimization, providing
\r
5 # a common interface for a number of different free optimization routines
\r
6 # available online as well as original implementations of various other
\r
8 # WEBSITE: http://ab-initio.mit.edu/wiki/index.php/NLopt
\r
9 # AUTHOR: Steven G. Johnson
\r
11 # This CMakeLists.txt file was created to compile NLOPT with the CMAKE utility.
\r
12 # Benoit Scherrer, 2010 CRL, Harvard Medical School
\r
13 # Copyright (c) 2008-2009 Children's Hospital Boston
\r
14 #==============================================================================
\r
15 cmake_minimum_required (VERSION 2.8.5)
\r
17 if (NOT DEFINED CMAKE_BUILD_TYPE)
\r
18 set (CMAKE_BUILD_TYPE Release CACHE STRING "Build type")
\r
23 list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
\r
25 option (WITH_CXX "enable cxx routines" OFF)
\r
26 option (BUILD_SHARED_LIBS "Build NLopt as a shared library" ON)
\r
27 option (BUILD_PYTHON "build python bindings" ON)
\r
28 option (BUILD_OCTAVE "build octave bindings" ON)
\r
29 option (BUILD_MATLAB "build matlab bindings" ON)
\r
30 option (BUILD_GUILE "build guile bindings" ON)
\r
31 option (USE_SWIG "use SWIG to build bindings" ON)
\r
35 set (NLOPT_SUFFIX _cxx)
\r
38 include (GNUInstallDirs)
\r
40 # Offer the user the choice of overriding the installation directories
\r
41 set (INSTALL_LIB_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "Installation directory for libraries")
\r
42 set (INSTALL_BIN_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Installation directory for executables")
\r
43 set (INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Installation directory for header files")
\r
44 set (INSTALL_DATA_DIR ${CMAKE_INSTALL_DATADIR}/nlopt CACHE PATH "Installation directory for data files")
\r
45 set (INSTALL_MAN_DIR ${CMAKE_INSTALL_MANDIR} CACHE PATH "Installation directory for man documentation")
\r
46 set (INSTALL_CMAKE_DIR ${INSTALL_LIB_DIR}/cmake/nlopt CACHE PATH "Installation directory for cmake config files")
\r
48 # Make relative paths absolute (needed later on)
\r
49 foreach (p LIB BIN INCLUDE DATA CMAKE)
\r
50 set (var INSTALL_${p}_DIR)
\r
51 set (RELATIVE_INSTALL_${p}_DIR ${INSTALL_${p}_DIR})
\r
52 if (NOT IS_ABSOLUTE "${${var}}")
\r
53 set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
\r
58 set (CMAKE_INSTALL_RPATH ${INSTALL_LIB_DIR})
\r
59 set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
\r
61 # Set MACOSX_RPATH to ON
\r
62 cmake_policy(SET CMP0042 NEW)
\r
65 include (CheckIncludeFiles)
\r
66 include (CheckFunctionExists)
\r
67 include (CheckTypeSize)
\r
68 include (CheckCCompilerFlag)
\r
69 include (CheckCXXSymbolExists)
\r
70 include (CheckCXXCompilerFlag)
\r
71 include (CheckLibraryExists)
\r
73 #==============================================================================
\r
74 # COMPILATION CHECKINGS and CONFIGURATION GENERATION
\r
75 #==============================================================================
\r
76 check_include_file (dlfcn.h HAVE_DLFCN_H)
\r
77 check_include_file (getopt.h HAVE_GETOPT_H)
\r
78 check_include_file (unistd.h HAVE_UNISTD_H)
\r
79 check_include_file (string.h HAVE_STRING_H)
\r
80 check_include_file (strings.h HAVE_STRINGS_H)
\r
81 check_include_file (inttypes.h HAVE_INTTYPES_H)
\r
82 check_include_file (memory.h HAVE_MEMORY_H)
\r
83 check_include_file (stdlib.h HAVE_STDLIB_H)
\r
84 check_include_file (stdint.h HAVE_STDINT_H)
\r
85 check_include_file (time.h HAVE_TIME_H)
\r
86 check_include_file (sys/types.h HAVE_SYS_TYPES_H)
\r
87 check_include_file (sys/stat.h HAVE_SYS_STAT_H)
\r
88 check_include_file (sys/time.h HAVE_SYS_TIME_H)
\r
89 if (HAVE_TIME_H AND HAVE_SYS_TIME_H)
\r
90 set (TIME_WITH_SYS_TIME TRUE)
\r
92 check_function_exists (getpid HAVE_GETPID)
\r
93 check_function_exists (syscall HAVE_GETTID_SYSCALL)
\r
94 check_function_exists (isinf HAVE_ISINF)
\r
95 check_function_exists (isnan HAVE_ISNAN)
\r
96 check_function_exists (gettimeofday HAVE_GETTIMEOFDAY)
\r
97 check_function_exists (qsort_r HAVE_QSORT_R)
\r
98 check_function_exists (time HAVE_TIME)
\r
99 check_function_exists (copysign HAVE_COPYSIGN)
\r
100 check_type_size ("uint32_t" SIZEOF_UINT32_T)
\r
101 set (HAVE_UINT32_T ${SIZEOF_UINT32_T})
\r
102 check_type_size ("unsigned int" SIZEOF_UNSIGNED_INT)
\r
103 check_type_size ("unsigned long" SIZEOF_UNSIGNED_LONG)
\r
105 check_library_exists ("m" sqrt "" HAVE_LIBM)
\r
110 if (NOT DEFINED HAVE_FPCLASSIFY)
\r
111 message(STATUS "Looking for fpclassify")
\r
112 file (WRITE ${CMAKE_BINARY_DIR}/fpclassify.c "#include <math.h>\n")
\r
113 file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c "int main(void) {\n")
\r
114 file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c "if (!fpclassify(3.14159)) fpclassify(2.7183);\n")
\r
115 file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c " return 0; }\n")
\r
116 try_compile (HAVE_FPCLASSIFY
\r
117 ${CMAKE_BINARY_DIR}/build_fpclassify
\r
118 ${CMAKE_BINARY_DIR}/fpclassify.c
\r
119 CMAKE_FLAGS -DLINK_LIBRARIES=m)
\r
120 message(STATUS "Looking for fpclassify - ${HAVE_FPCLASSIFY}")
\r
123 option (WITH_THREADLOCAL "check thread local keyword" ON)
\r
124 if (WITH_THREADLOCAL AND NOT DEFINED HAVE_THREAD_LOCAL_STORAGE)
\r
125 foreach (_THREADLOCAL_KEY "__thread" "__declspec(thread)")
\r
126 unset (HAVE_THREAD_LOCAL_STORAGE CACHE)
\r
127 check_c_source_compiles("
\r
128 ${_THREADLOCAL_KEY} int tls;
\r
132 }" HAVE_THREAD_LOCAL_STORAGE)
\r
133 if (${HAVE_THREAD_LOCAL_STORAGE})
\r
134 set (THREADLOCAL ${_THREADLOCAL_KEY})
\r
139 if (WITH_CXX OR BUILD_PYTHON OR BUILD_GUILE OR BUILD_OCTAVE)
\r
140 check_cxx_symbol_exists (_LIBCPP_VERSION string SYSTEM_HAS_LIBCPP)
\r
141 if (SYSTEM_HAS_LIBCPP)
\r
142 check_cxx_compiler_flag ("-std=c++11" SUPPORTS_STDCXX11)
\r
143 if (SUPPORTS_STDCXX11)
\r
144 set (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
\r
149 #==============================================================================
\r
151 #==============================================================================
\r
152 set (NLOPT_MAJOR_VERSION "2")
\r
153 set (NLOPT_MINOR_VERSION "5")
\r
154 set (NLOPT_BUGFIX_VERSION "0")
\r
155 set (NLOPT_VERSION_STRING ${NLOPT_MAJOR_VERSION}.${NLOPT_MINOR_VERSION}.${NLOPT_BUGFIX_VERSION})
\r
156 message (STATUS "NLopt version ${NLOPT_VERSION_STRING}")
\r
158 #==============================================================================
\r
159 # CREATE nlopt_config.h
\r
160 #==============================================================================
\r
162 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt_config.h IMMEDIATE)
\r
166 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt.pc.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc @ONLY)
\r
167 install (FILES ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc DESTINATION ${RELATIVE_INSTALL_LIB_DIR}/pkgconfig)
\r
170 #==============================================================================
\r
171 # INCLUDE DIRECTORIES
\r
172 #==============================================================================
\r
173 set (${INCLUDE_DIRECTORIES} "")
\r
174 include_directories (
\r
175 ${CMAKE_BINARY_DIR}/api
\r
176 ${CMAKE_BINARY_DIR}
\r
197 #==============================================================================
\r
198 # nlopt LIBRARY TARGET (SHARED OR STATIC)
\r
199 #==============================================================================
\r
201 set (NLOPT_HEADERS
\r
202 api/nlopt.h ${CMAKE_BINARY_DIR}/api/nlopt.hpp ${CMAKE_BINARY_DIR}/api/nlopt.f
\r
205 set (NLOPT_SOURCES
\r
206 direct/DIRect.c direct/direct_wrap.c direct/DIRserial.c direct/DIRsubrout.c direct/direct-internal.h direct/direct.h
\r
207 cdirect/cdirect.c cdirect/hybrid.c cdirect/cdirect.h
\r
208 praxis/praxis.c praxis/praxis.h
\r
209 luksan/plis.c luksan/plip.c luksan/pnet.c luksan/mssubs.c luksan/pssubs.c luksan/luksan.h
\r
210 crs/crs.c crs/crs.h
\r
211 mlsl/mlsl.c mlsl/mlsl.h
\r
212 mma/mma.c mma/mma.h mma/ccsa_quadratic.c
\r
213 cobyla/cobyla.c cobyla/cobyla.h
\r
214 newuoa/newuoa.c newuoa/newuoa.h
\r
215 neldermead/nldrmd.c neldermead/neldermead.h neldermead/sbplx.c
\r
216 auglag/auglag.c auglag/auglag.h
\r
217 bobyqa/bobyqa.c bobyqa/bobyqa.h
\r
218 isres/isres.c isres/isres.h
\r
219 slsqp/slsqp.c slsqp/slsqp.h
\r
220 esch/esch.c esch/esch.h
\r
221 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
\r
222 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
\r
226 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)
\r
230 if (BUILD_SHARED_LIBS)
\r
231 add_definitions (-DNLOPT_DLL)
\r
232 add_definitions (-DNLOPT_DLL_EXPORT)
\r
235 install (FILES ${NLOPT_HEADERS} DESTINATION ${INSTALL_INCLUDE_DIR})
\r
237 set (nlopt_lib nlopt${NLOPT_SUFFIX})
\r
238 add_library (${nlopt_lib} ${NLOPT_SOURCES})
\r
239 target_link_libraries (${nlopt_lib} ${M_LIBRARY})
\r
241 set_target_properties (${nlopt_lib} PROPERTIES SOVERSION 0)
\r
242 set_target_properties (${nlopt_lib} PROPERTIES VERSION 0.9.0)
\r
244 # pass -fPIC in case swig module is built with static library
\r
245 if (NOT BUILD_SHARED_LIBS)
\r
246 check_c_compiler_flag (-fPIC HAS_FPIC)
\r
248 set (CMAKE_C_FLAGS "-fPIC ${CMAKE_C_FLAGS}")
\r
249 set (CMAKE_CXX_FLAGS "-fPIC ${CMAKE_CXX_FLAGS}")
\r
253 install ( TARGETS ${nlopt_lib}
\r
254 EXPORT NLoptLibraryDepends
\r
255 RUNTIME DESTINATION ${RELATIVE_INSTALL_BIN_DIR}
\r
256 LIBRARY DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
\r
257 ARCHIVE DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
\r
260 add_subdirectory (api)
\r
263 find_package (PythonInterp)
\r
264 find_package (PythonLibs)
\r
265 find_package (NumPy)
\r
268 if (NOT DEFINED INSTALL_PYTHON_DIR AND PYTHONINTERP_FOUND)
\r
269 execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}'))"
\r
270 OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
\r
271 OUTPUT_STRIP_TRAILING_WHITESPACE )
\r
273 get_filename_component (_ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE)
\r
274 file (RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
\r
276 set (INSTALL_PYTHON_DIR ${_REL_PYTHON_MODULE_PATH})
\r
281 find_package (Guile)
\r
285 find_package (SWIG)
\r
288 add_subdirectory (swig)
\r
291 find_package (Octave)
\r
295 find_package (Matlab)
\r
298 if (OCTAVE_FOUND OR Matlab_FOUND)
\r
299 add_subdirectory (octave)
\r
303 add_subdirectory (test)
\r
305 set (CPACK_PACKAGE_NAME "${CMAKE_PROJECT_NAME}")
\r
306 set (CPACK_PACKAGE_VERSION_MAJOR "${NLOPT_MAJOR_VERSION}")
\r
307 set (CPACK_PACKAGE_VERSION_MINOR "${NLOPT_MINOR_VERSION}")
\r
308 set (CPACK_PACKAGE_VERSION_PATCH "${NLOPT_BUGFIX_VERSION}")
\r
309 set (CPACK_SOURCE_GENERATOR "TBZ2;TGZ" )
\r
310 set (CPACK_BINARY_STGZ "OFF" )
\r
311 set (CPACK_BINARY_TBZ2 "ON" )
\r
312 set (CPACK_BINARY_TGZ "ON" )
\r
313 set (CPACK_BINARY_TZ "OFF" )
\r
314 set (CPACK_SOURCE_IGNORE_FILES ".git;/build;.*~;${CPACK_SOURCE_IGNORE_FILES}")
\r
316 set (CPACK_SOURCE_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${NLOPT_VERSION_STRING})
\r
320 # configuration files
\r
321 export (TARGETS ${nlopt_lib} FILE ${PROJECT_BINARY_DIR}/NLoptLibraryDepends.cmake)
\r
323 # Install the export set for use with the install-tree
\r
324 install(EXPORT NLoptLibraryDepends
\r
325 DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
\r
326 COMPONENT Development)
\r
328 # Create a NLOPTConfig.cmake file for the use from the install tree
\r
330 set (NLOPT_LIBRARIES ${nlopt_lib})
\r
332 set (NLOPT_CMAKE_DIR "${INSTALL_CMAKE_DIR}")
\r
333 file (RELATIVE_PATH rel_include_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_INCLUDE_DIR}")
\r
334 list (APPEND RELATIVE_NLOPT_INCLUDE_DIRS ${rel_include_dir})
\r
336 file (RELATIVE_PATH rel_lib_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_LIB_DIR}")
\r
337 list (APPEND RELATIVE_NLOPT_LIB_DIR ${rel_lib_dir})
\r
339 configure_file (cmake/NLoptConfig.cmake.in NLoptConfig.cmake @ONLY)
\r
340 configure_file (cmake/NLoptConfigVersion.cmake.in NLoptConfigVersion.cmake @ONLY)
\r
342 ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfig.cmake
\r
343 ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfigVersion.cmake
\r
344 DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
\r
345 COMPONENT Development)
\r