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 if (WITH_CXX OR BUILD_PYTHON OR BUILD_GUILE OR BUILD_OCTAVE)
\r
39 enable_language (CXX)
\r
42 include (GNUInstallDirs)
\r
44 # Offer the user the choice of overriding the installation directories
\r
45 set (INSTALL_LIB_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "Installation directory for libraries")
\r
46 set (INSTALL_BIN_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Installation directory for executables")
\r
47 set (INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Installation directory for header files")
\r
48 set (INSTALL_DATA_DIR ${CMAKE_INSTALL_DATADIR}/nlopt CACHE PATH "Installation directory for data files")
\r
49 set (INSTALL_MAN_DIR ${CMAKE_INSTALL_MANDIR} CACHE PATH "Installation directory for man documentation")
\r
50 set (INSTALL_CMAKE_DIR ${INSTALL_LIB_DIR}/cmake/nlopt CACHE PATH "Installation directory for cmake config files")
\r
52 # Make relative paths absolute (needed later on)
\r
53 foreach (p LIB BIN INCLUDE DATA CMAKE)
\r
54 set (var INSTALL_${p}_DIR)
\r
55 set (RELATIVE_INSTALL_${p}_DIR ${INSTALL_${p}_DIR})
\r
56 if (NOT IS_ABSOLUTE "${${var}}")
\r
57 set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
\r
62 set (CMAKE_INSTALL_RPATH ${INSTALL_LIB_DIR})
\r
63 set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
\r
65 # Set MACOSX_RPATH to ON
\r
66 cmake_policy(SET CMP0042 NEW)
\r
69 include (CheckIncludeFiles)
\r
70 include (CheckFunctionExists)
\r
71 include (CheckTypeSize)
\r
72 include (CheckCCompilerFlag)
\r
73 include (CheckCXXSymbolExists)
\r
74 include (CheckCXXCompilerFlag)
\r
75 include (CheckLibraryExists)
\r
77 #==============================================================================
\r
78 # COMPILATION CHECKINGS and CONFIGURATION GENERATION
\r
79 #==============================================================================
\r
80 check_include_file (dlfcn.h HAVE_DLFCN_H)
\r
81 check_include_file (getopt.h HAVE_GETOPT_H)
\r
82 check_include_file (unistd.h HAVE_UNISTD_H)
\r
83 check_include_file (string.h HAVE_STRING_H)
\r
84 check_include_file (strings.h HAVE_STRINGS_H)
\r
85 check_include_file (inttypes.h HAVE_INTTYPES_H)
\r
86 check_include_file (memory.h HAVE_MEMORY_H)
\r
87 check_include_file (stdlib.h HAVE_STDLIB_H)
\r
88 check_include_file (stdint.h HAVE_STDINT_H)
\r
89 check_include_file (time.h HAVE_TIME_H)
\r
90 check_include_file (sys/types.h HAVE_SYS_TYPES_H)
\r
91 check_include_file (sys/stat.h HAVE_SYS_STAT_H)
\r
92 check_include_file (sys/time.h HAVE_SYS_TIME_H)
\r
93 if (HAVE_TIME_H AND HAVE_SYS_TIME_H)
\r
94 set (TIME_WITH_SYS_TIME TRUE)
\r
96 check_function_exists (getpid HAVE_GETPID)
\r
97 check_function_exists (syscall HAVE_GETTID_SYSCALL)
\r
98 check_function_exists (isinf HAVE_ISINF)
\r
99 check_function_exists (isnan HAVE_ISNAN)
\r
100 check_function_exists (gettimeofday HAVE_GETTIMEOFDAY)
\r
101 check_function_exists (qsort_r HAVE_QSORT_R)
\r
102 check_function_exists (time HAVE_TIME)
\r
103 check_function_exists (copysign HAVE_COPYSIGN)
\r
104 check_type_size ("uint32_t" SIZEOF_UINT32_T)
\r
105 set (HAVE_UINT32_T ${SIZEOF_UINT32_T})
\r
106 check_type_size ("unsigned int" SIZEOF_UNSIGNED_INT)
\r
107 check_type_size ("unsigned long" SIZEOF_UNSIGNED_LONG)
\r
109 check_library_exists ("m" sqrt "" HAVE_LIBM)
\r
114 if (NOT DEFINED HAVE_FPCLASSIFY)
\r
115 message(STATUS "Looking for fpclassify")
\r
116 file (WRITE ${CMAKE_BINARY_DIR}/fpclassify.c "#include <math.h>\n")
\r
117 file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c "int main(void) {\n")
\r
118 file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c "if (!fpclassify(3.14159)) fpclassify(2.7183);\n")
\r
119 file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c " return 0; }\n")
\r
120 try_compile (HAVE_FPCLASSIFY
\r
121 ${CMAKE_BINARY_DIR}/build_fpclassify
\r
122 ${CMAKE_BINARY_DIR}/fpclassify.c
\r
123 CMAKE_FLAGS -DLINK_LIBRARIES=m)
\r
124 message(STATUS "Looking for fpclassify - ${HAVE_FPCLASSIFY}")
\r
127 option (WITH_THREADLOCAL "check thread local keyword" ON)
\r
128 if (WITH_THREADLOCAL AND NOT DEFINED HAVE_THREAD_LOCAL_STORAGE)
\r
129 foreach (_THREADLOCAL_KEY "__thread" "__declspec(thread)")
\r
130 unset (HAVE_THREAD_LOCAL_STORAGE CACHE)
\r
131 check_c_source_compiles("
\r
132 ${_THREADLOCAL_KEY} int tls;
\r
136 }" HAVE_THREAD_LOCAL_STORAGE)
\r
137 if (${HAVE_THREAD_LOCAL_STORAGE})
\r
138 set (THREADLOCAL ${_THREADLOCAL_KEY})
\r
143 if (WITH_CXX OR BUILD_PYTHON OR BUILD_GUILE OR BUILD_OCTAVE)
\r
144 check_cxx_symbol_exists (_LIBCPP_VERSION string SYSTEM_HAS_LIBCPP)
\r
145 if (SYSTEM_HAS_LIBCPP)
\r
146 check_cxx_compiler_flag ("-std=c++11" SUPPORTS_STDCXX11)
\r
147 if (SUPPORTS_STDCXX11)
\r
148 set (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
\r
153 #==============================================================================
\r
155 #==============================================================================
\r
156 set (NLOPT_MAJOR_VERSION "2")
\r
157 set (NLOPT_MINOR_VERSION "5")
\r
158 set (NLOPT_BUGFIX_VERSION "0")
\r
159 set (NLOPT_VERSION_STRING ${NLOPT_MAJOR_VERSION}.${NLOPT_MINOR_VERSION}.${NLOPT_BUGFIX_VERSION})
\r
160 message (STATUS "NLopt version ${NLOPT_VERSION_STRING}")
\r
162 #==============================================================================
\r
163 # CREATE nlopt_config.h
\r
164 #==============================================================================
\r
166 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt_config.h IMMEDIATE)
\r
170 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt.pc.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc @ONLY)
\r
171 install (FILES ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc DESTINATION ${RELATIVE_INSTALL_LIB_DIR}/pkgconfig)
\r
174 #==============================================================================
\r
175 # INCLUDE DIRECTORIES
\r
176 #==============================================================================
\r
177 set (${INCLUDE_DIRECTORIES} "")
\r
178 include_directories (
\r
179 ${CMAKE_BINARY_DIR}/api
\r
180 ${CMAKE_BINARY_DIR}
\r
201 #==============================================================================
\r
202 # nlopt LIBRARY TARGET (SHARED OR STATIC)
\r
203 #==============================================================================
\r
205 set (NLOPT_HEADERS
\r
206 api/nlopt.h ${CMAKE_BINARY_DIR}/api/nlopt.hpp ${CMAKE_BINARY_DIR}/api/nlopt.f
\r
209 set (NLOPT_SOURCES
\r
210 direct/DIRect.c direct/direct_wrap.c direct/DIRserial.c direct/DIRsubrout.c direct/direct-internal.h direct/direct.h
\r
211 cdirect/cdirect.c cdirect/hybrid.c cdirect/cdirect.h
\r
212 praxis/praxis.c praxis/praxis.h
\r
213 luksan/plis.c luksan/plip.c luksan/pnet.c luksan/mssubs.c luksan/pssubs.c luksan/luksan.h
\r
214 crs/crs.c crs/crs.h
\r
215 mlsl/mlsl.c mlsl/mlsl.h
\r
216 mma/mma.c mma/mma.h mma/ccsa_quadratic.c
\r
217 cobyla/cobyla.c cobyla/cobyla.h
\r
218 newuoa/newuoa.c newuoa/newuoa.h
\r
219 neldermead/nldrmd.c neldermead/neldermead.h neldermead/sbplx.c
\r
220 auglag/auglag.c auglag/auglag.h
\r
221 bobyqa/bobyqa.c bobyqa/bobyqa.h
\r
222 isres/isres.c isres/isres.h
\r
223 slsqp/slsqp.c slsqp/slsqp.h
\r
224 esch/esch.c esch/esch.h
\r
225 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
226 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
230 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
234 if (BUILD_SHARED_LIBS)
\r
235 add_definitions (-DNLOPT_DLL)
\r
236 add_definitions (-DNLOPT_DLL_EXPORT)
\r
239 install (FILES ${NLOPT_HEADERS} DESTINATION ${INSTALL_INCLUDE_DIR})
\r
241 set (nlopt_lib nlopt${NLOPT_SUFFIX})
\r
242 add_library (${nlopt_lib} ${NLOPT_SOURCES})
\r
243 target_link_libraries (${nlopt_lib} ${M_LIBRARY})
\r
245 set_target_properties (${nlopt_lib} PROPERTIES SOVERSION 0)
\r
246 set_target_properties (${nlopt_lib} PROPERTIES VERSION 0.9.0)
\r
248 # pass -fPIC in case swig module is built with static library
\r
249 if (NOT BUILD_SHARED_LIBS)
\r
250 check_c_compiler_flag (-fPIC HAS_FPIC)
\r
252 set (CMAKE_C_FLAGS "-fPIC ${CMAKE_C_FLAGS}")
\r
253 set (CMAKE_CXX_FLAGS "-fPIC ${CMAKE_CXX_FLAGS}")
\r
257 install ( TARGETS ${nlopt_lib}
\r
258 EXPORT NLoptLibraryDepends
\r
259 RUNTIME DESTINATION ${RELATIVE_INSTALL_BIN_DIR}
\r
260 LIBRARY DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
\r
261 ARCHIVE DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
\r
264 add_subdirectory (api)
\r
267 find_package (PythonInterp)
\r
268 find_package (PythonLibs)
\r
269 find_package (NumPy)
\r
272 if (NOT DEFINED INSTALL_PYTHON_DIR AND PYTHONINTERP_FOUND)
\r
273 execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}'))"
\r
274 OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
\r
275 OUTPUT_STRIP_TRAILING_WHITESPACE )
\r
277 get_filename_component (_ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE)
\r
278 file (RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
\r
280 set (INSTALL_PYTHON_DIR ${_REL_PYTHON_MODULE_PATH})
\r
285 find_package (Guile)
\r
289 find_package (SWIG)
\r
292 add_subdirectory (swig)
\r
295 find_package (Octave)
\r
299 find_package (Matlab)
\r
302 if (OCTAVE_FOUND OR Matlab_FOUND)
\r
303 add_subdirectory (octave)
\r
307 add_subdirectory (test)
\r
309 set (CPACK_PACKAGE_NAME "${CMAKE_PROJECT_NAME}")
\r
310 set (CPACK_PACKAGE_VERSION_MAJOR "${NLOPT_MAJOR_VERSION}")
\r
311 set (CPACK_PACKAGE_VERSION_MINOR "${NLOPT_MINOR_VERSION}")
\r
312 set (CPACK_PACKAGE_VERSION_PATCH "${NLOPT_BUGFIX_VERSION}")
\r
313 set (CPACK_SOURCE_GENERATOR "TBZ2;TGZ" )
\r
314 set (CPACK_BINARY_STGZ "OFF" )
\r
315 set (CPACK_BINARY_TBZ2 "ON" )
\r
316 set (CPACK_BINARY_TGZ "ON" )
\r
317 set (CPACK_BINARY_TZ "OFF" )
\r
318 set (CPACK_SOURCE_IGNORE_FILES ".git;/build;.*~;${CPACK_SOURCE_IGNORE_FILES}")
\r
320 set (CPACK_SOURCE_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${NLOPT_VERSION_STRING})
\r
324 # configuration files
\r
325 export (TARGETS ${nlopt_lib} FILE ${PROJECT_BINARY_DIR}/NLoptLibraryDepends.cmake)
\r
327 # Install the export set for use with the install-tree
\r
328 install(EXPORT NLoptLibraryDepends
\r
329 DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
\r
330 COMPONENT Development)
\r
332 # Create a NLOPTConfig.cmake file for the use from the install tree
\r
334 set (NLOPT_LIBRARIES ${nlopt_lib})
\r
336 set (NLOPT_CMAKE_DIR "${INSTALL_CMAKE_DIR}")
\r
337 file (RELATIVE_PATH rel_include_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_INCLUDE_DIR}")
\r
338 list (APPEND RELATIVE_NLOPT_INCLUDE_DIRS ${rel_include_dir})
\r
340 file (RELATIVE_PATH rel_lib_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_LIB_DIR}")
\r
341 list (APPEND RELATIVE_NLOPT_LIB_DIR ${rel_lib_dir})
\r
343 configure_file (cmake/NLoptConfig.cmake.in NLoptConfig.cmake @ONLY)
\r
344 configure_file (cmake/NLoptConfigVersion.cmake.in NLoptConfigVersion.cmake @ONLY)
\r
346 ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfig.cmake
\r
347 ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfigVersion.cmake
\r
348 DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
\r
349 COMPONENT Development)
\r