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
76 #==============================================================================
\r
77 # COMPILATION CHECKINGS and CONFIGURATION GENERATION
\r
78 #==============================================================================
\r
79 check_include_file (dlfcn.h HAVE_DLFCN_H)
\r
80 check_include_file (getopt.h HAVE_GETOPT_H)
\r
81 check_include_file (unistd.h HAVE_UNISTD_H)
\r
82 check_include_file (string.h HAVE_STRING_H)
\r
83 check_include_file (strings.h HAVE_STRINGS_H)
\r
84 check_include_file (inttypes.h HAVE_INTTYPES_H)
\r
85 check_include_file (memory.h HAVE_MEMORY_H)
\r
86 check_include_file (stdlib.h HAVE_STDLIB_H)
\r
87 check_include_file (stdint.h HAVE_STDINT_H)
\r
88 check_include_file (time.h HAVE_TIME_H)
\r
89 check_include_file (sys/types.h HAVE_SYS_TYPES_H)
\r
90 check_include_file (sys/stat.h HAVE_SYS_STAT_H)
\r
91 check_include_file (sys/time.h HAVE_SYS_TIME_H)
\r
92 if (HAVE_TIME_H AND HAVE_SYS_TIME_H)
\r
93 set (TIME_WITH_SYS_TIME TRUE)
\r
95 check_function_exists (getpid HAVE_GETPID)
\r
96 check_function_exists (syscall HAVE_GETTID_SYSCALL)
\r
97 check_function_exists (isinf HAVE_ISINF)
\r
98 check_function_exists (isnan HAVE_ISNAN)
\r
99 check_function_exists (gettimeofday HAVE_GETTIMEOFDAY)
\r
100 check_function_exists (qsort_r HAVE_QSORT_R)
\r
101 check_function_exists (time HAVE_TIME)
\r
102 check_function_exists (copysign HAVE_COPYSIGN)
\r
103 check_type_size ("uint32_t" SIZEOF_UINT32_T)
\r
104 set (HAVE_UINT32_T ${SIZEOF_UINT32_T})
\r
105 check_type_size ("unsigned int" SIZEOF_UNSIGNED_INT)
\r
106 check_type_size ("unsigned long" SIZEOF_UNSIGNED_LONG)
\r
108 if (NOT DEFINED HAVE_FPCLASSIFY)
\r
109 message(STATUS "Looking for fpclassify")
\r
110 file (WRITE ${CMAKE_BINARY_DIR}/fpclassify.c "#include <math.h>\n")
\r
111 file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c "int main(void) {\n")
\r
112 file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c "if (!fpclassify(3.14159)) fpclassify(2.7183);\n")
\r
113 file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c " return 0; }\n")
\r
114 try_compile (HAVE_FPCLASSIFY
\r
115 ${CMAKE_BINARY_DIR}/build_fpclassify
\r
116 ${CMAKE_BINARY_DIR}/fpclassify.c
\r
117 CMAKE_FLAGS -DLINK_LIBRARIES=m)
\r
118 message(STATUS "Looking for fpclassify - ${HAVE_FPCLASSIFY}")
\r
121 option (WITH_THREADLOCAL "check thread local keyword" ON)
\r
122 if (WITH_THREADLOCAL AND NOT DEFINED HAVE_THREAD_LOCAL_STORAGE)
\r
123 foreach (_THREADLOCAL_KEY "__thread" "__declspec(thread)")
\r
124 unset (HAVE_THREAD_LOCAL_STORAGE CACHE)
\r
125 check_c_source_compiles("
\r
126 ${_THREADLOCAL_KEY} int tls;
\r
130 }" HAVE_THREAD_LOCAL_STORAGE)
\r
131 if (${HAVE_THREAD_LOCAL_STORAGE})
\r
132 set (THREADLOCAL ${_THREADLOCAL_KEY})
\r
137 if (WITH_CXX OR BUILD_PYTHON OR BUILD_GUILE OR BUILD_OCTAVE)
\r
138 check_cxx_symbol_exists (_LIBCPP_VERSION string SYSTEM_HAS_LIBCPP)
\r
139 if (SYSTEM_HAS_LIBCPP)
\r
140 check_cxx_compiler_flag ("-std=c++11" SUPPORTS_STDCXX11)
\r
141 if (SUPPORTS_STDCXX11)
\r
142 set (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
\r
147 #==============================================================================
\r
149 #==============================================================================
\r
150 set (NLOPT_MAJOR_VERSION "2")
\r
151 set (NLOPT_MINOR_VERSION "5")
\r
152 set (NLOPT_BUGFIX_VERSION "0")
\r
153 set (NLOPT_VERSION_STRING ${NLOPT_MAJOR_VERSION}.${NLOPT_MINOR_VERSION}.${NLOPT_BUGFIX_VERSION})
\r
154 message (STATUS "NLopt version ${NLOPT_VERSION_STRING}")
\r
156 #==============================================================================
\r
157 # CREATE nlopt_config.h
\r
158 #==============================================================================
\r
160 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt_config.h IMMEDIATE)
\r
164 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt.pc.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc @ONLY)
\r
165 install (FILES ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc DESTINATION ${RELATIVE_INSTALL_LIB_DIR}/pkgconfig)
\r
168 #==============================================================================
\r
169 # INCLUDE DIRECTORIES
\r
170 #==============================================================================
\r
171 set (${INCLUDE_DIRECTORIES} "")
\r
172 include_directories (
\r
173 ${CMAKE_BINARY_DIR}/api
\r
174 ${CMAKE_BINARY_DIR}
\r
195 #==============================================================================
\r
196 # nlopt LIBRARY TARGET (SHARED OR STATIC)
\r
197 #==============================================================================
\r
199 set (NLOPT_HEADERS
\r
200 api/nlopt.h ${CMAKE_BINARY_DIR}/api/nlopt.hpp ${CMAKE_BINARY_DIR}/api/nlopt.f
\r
203 set (NLOPT_SOURCES
\r
204 direct/DIRect.c direct/direct_wrap.c direct/DIRserial.c direct/DIRsubrout.c direct/direct-internal.h direct/direct.h
\r
205 cdirect/cdirect.c cdirect/hybrid.c cdirect/cdirect.h
\r
206 praxis/praxis.c praxis/praxis.h
\r
207 luksan/plis.c luksan/plip.c luksan/pnet.c luksan/mssubs.c luksan/pssubs.c luksan/luksan.h
\r
208 crs/crs.c crs/crs.h
\r
209 mlsl/mlsl.c mlsl/mlsl.h
\r
210 mma/mma.c mma/mma.h mma/ccsa_quadratic.c
\r
211 cobyla/cobyla.c cobyla/cobyla.h
\r
212 newuoa/newuoa.c newuoa/newuoa.h
\r
213 neldermead/nldrmd.c neldermead/neldermead.h neldermead/sbplx.c
\r
214 auglag/auglag.c auglag/auglag.h
\r
215 bobyqa/bobyqa.c bobyqa/bobyqa.h
\r
216 isres/isres.c isres/isres.h
\r
217 slsqp/slsqp.c slsqp/slsqp.h
\r
218 esch/esch.c esch/esch.h
\r
219 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
220 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
224 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
228 if (BUILD_SHARED_LIBS)
\r
229 add_definitions (-DNLOPT_DLL)
\r
230 add_definitions (-DNLOPT_DLL_EXPORT)
\r
233 install (FILES ${NLOPT_HEADERS} DESTINATION ${INSTALL_INCLUDE_DIR})
\r
235 set (nlopt_lib nlopt${NLOPT_SUFFIX})
\r
236 add_library (${nlopt_lib} ${NLOPT_SOURCES})
\r
237 set_target_properties (${nlopt_lib} PROPERTIES SOVERSION 0)
\r
238 set_target_properties (${nlopt_lib} PROPERTIES VERSION 0.9.0)
\r
240 # pass -fPIC in case swig module is built with static library
\r
241 if (NOT BUILD_SHARED_LIBS)
\r
242 check_c_compiler_flag (-fPIC HAS_FPIC)
\r
244 set (CMAKE_C_FLAGS "-fPIC ${CMAKE_C_FLAGS}")
\r
245 set (CMAKE_CXX_FLAGS "-fPIC ${CMAKE_CXX_FLAGS}")
\r
249 install ( TARGETS ${nlopt_lib}
\r
250 EXPORT NLoptLibraryDepends
\r
251 RUNTIME DESTINATION ${RELATIVE_INSTALL_BIN_DIR}
\r
252 LIBRARY DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
\r
253 ARCHIVE DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
\r
256 add_subdirectory (api)
\r
259 find_package (PythonInterp)
\r
260 find_package (PythonLibs)
\r
261 find_package (NumPy)
\r
264 if (NOT DEFINED INSTALL_PYTHON_DIR AND PYTHONINTERP_FOUND)
\r
265 execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}'))"
\r
266 OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
\r
267 OUTPUT_STRIP_TRAILING_WHITESPACE )
\r
269 get_filename_component (_ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE)
\r
270 file (RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
\r
272 set (INSTALL_PYTHON_DIR ${_REL_PYTHON_MODULE_PATH})
\r
277 find_package (Guile)
\r
281 find_package (SWIG)
\r
284 add_subdirectory (swig)
\r
287 find_package (Octave)
\r
291 find_package (Matlab)
\r
294 if (OCTAVE_FOUND OR Matlab_FOUND)
\r
295 add_subdirectory (octave)
\r
299 add_subdirectory (test)
\r
301 set (CPACK_PACKAGE_NAME "${CMAKE_PROJECT_NAME}")
\r
302 set (CPACK_PACKAGE_VERSION_MAJOR "${NLOPT_MAJOR_VERSION}")
\r
303 set (CPACK_PACKAGE_VERSION_MINOR "${NLOPT_MINOR_VERSION}")
\r
304 set (CPACK_PACKAGE_VERSION_PATCH "${NLOPT_BUGFIX_VERSION}")
\r
305 set (CPACK_SOURCE_GENERATOR "TBZ2;TGZ" )
\r
306 set (CPACK_BINARY_STGZ "OFF" )
\r
307 set (CPACK_BINARY_TBZ2 "ON" )
\r
308 set (CPACK_BINARY_TGZ "ON" )
\r
309 set (CPACK_BINARY_TZ "OFF" )
\r
310 set (CPACK_SOURCE_IGNORE_FILES ".git;/build;.*~;${CPACK_SOURCE_IGNORE_FILES}")
\r
312 set (CPACK_SOURCE_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${NLOPT_VERSION_STRING})
\r
316 # configuration files
\r
317 export (TARGETS ${nlopt_lib} FILE ${PROJECT_BINARY_DIR}/NLoptLibraryDepends.cmake)
\r
319 # Install the export set for use with the install-tree
\r
320 install(EXPORT NLoptLibraryDepends
\r
321 DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
\r
322 COMPONENT Development)
\r
324 # Create a NLOPTConfig.cmake file for the use from the install tree
\r
326 set (NLOPT_LIBRARIES ${nlopt_lib})
\r
328 set (NLOPT_CMAKE_DIR "${INSTALL_CMAKE_DIR}")
\r
329 file (RELATIVE_PATH rel_include_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_INCLUDE_DIR}")
\r
330 list (APPEND RELATIVE_NLOPT_INCLUDE_DIRS ${rel_include_dir})
\r
332 file (RELATIVE_PATH rel_lib_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_LIB_DIR}")
\r
333 list (APPEND RELATIVE_NLOPT_LIB_DIR ${rel_lib_dir})
\r
335 configure_file (cmake/NLoptConfig.cmake.in NLoptConfig.cmake @ONLY)
\r
336 configure_file (cmake/NLoptConfigVersion.cmake.in NLoptConfigVersion.cmake @ONLY)
\r
338 ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfig.cmake
\r
339 ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfigVersion.cmake
\r
340 DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
\r
341 COMPONENT Development)
\r