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.6)
\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)
\r
39 enable_language (CXX)
\r
43 # Offer the user the choice of overriding the installation directories
\r
44 set (INSTALL_LIB_DIR lib${LIB_SUFFIX} CACHE PATH "Installation directory for libraries")
\r
45 set (INSTALL_BIN_DIR bin CACHE PATH "Installation directory for executables")
\r
46 set (INSTALL_INCLUDE_DIR include CACHE PATH "Installation directory for header files")
\r
47 set (INSTALL_DATA_DIR share/nlopt CACHE PATH "Installation directory for data files")
\r
48 set (INSTALL_CMAKE_DIR ${INSTALL_LIB_DIR}/cmake/nlopt CACHE PATH "Installation directory for cmake config files")
\r
50 # Make relative paths absolute (needed later on)
\r
51 foreach (p LIB BIN INCLUDE DATA CMAKE)
\r
52 set (var INSTALL_${p}_DIR)
\r
53 set (RELATIVE_INSTALL_${p}_DIR ${INSTALL_${p}_DIR})
\r
54 if (NOT IS_ABSOLUTE "${${var}}")
\r
55 set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
\r
60 set (CMAKE_INSTALL_RPATH ${INSTALL_LIB_DIR})
\r
61 set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
\r
63 # Set MACOSX_RPATH to ON
\r
64 cmake_policy(SET CMP0042 NEW)
\r
67 include (CheckIncludeFiles)
\r
68 include (CheckFunctionExists)
\r
69 include (CheckTypeSize)
\r
70 include (CheckCCompilerFlag)
\r
72 #==============================================================================
\r
73 # COMPILATION CHECKINGS and CONFIGURATION GENERATION
\r
74 #==============================================================================
\r
75 check_include_file (dlfcn.h HAVE_DLFCN_H)
\r
76 check_include_file (getopt.h HAVE_GETOPT_H)
\r
77 check_include_file (unistd.h HAVE_UNISTD_H)
\r
78 check_include_file (string.h HAVE_STRING_H)
\r
79 check_include_file (strings.h HAVE_STRINGS_H)
\r
80 check_include_file (inttypes.h HAVE_INTTYPES_H)
\r
81 check_include_file (memory.h HAVE_MEMORY_H)
\r
82 check_include_file (stdlib.h HAVE_STDLIB_H)
\r
83 check_include_file (stdint.h HAVE_STDINT_H)
\r
84 check_include_file (time.h HAVE_TIME_H)
\r
85 check_include_file (sys/types.h HAVE_SYS_TYPES_H)
\r
86 check_include_file (sys/stat.h HAVE_SYS_STAT_H)
\r
87 check_include_file (sys/time.h HAVE_SYS_TIME_H)
\r
88 if (HAVE_TIME_H AND HAVE_SYS_TIME_H)
\r
89 set (TIME_WITH_SYS_TIME TRUE)
\r
91 check_function_exists (getpid HAVE_GETPID)
\r
92 check_function_exists (syscall HAVE_GETTID_SYSCALL)
\r
93 check_function_exists (isinf HAVE_ISINF)
\r
94 check_function_exists (isnan HAVE_ISNAN)
\r
95 check_function_exists (gettimeofday HAVE_GETTIMEOFDAY)
\r
96 check_function_exists (qsort_r HAVE_QSORT_R)
\r
97 check_function_exists (time HAVE_TIME)
\r
98 check_function_exists (copysign HAVE_COPYSIGN)
\r
99 check_type_size ("uint32_t" SIZEOF_UINT32_T)
\r
100 set (HAVE_UINT32_T ${SIZEOF_UINT32_T})
\r
101 check_type_size ("unsigned int" SIZEOF_UNSIGNED_INT)
\r
102 check_type_size ("unsigned long" SIZEOF_UNSIGNED_LONG)
\r
104 if (NOT DEFINED HAVE_FPCLASSIFY)
\r
105 message(STATUS "Looking for fpclassify")
\r
106 file (WRITE ${CMAKE_BINARY_DIR}/fpclassify.c "#include <math.h>\n")
\r
107 file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c "int main(void) {\n")
\r
108 file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c "if (!fpclassify(3.14159)) fpclassify(2.7183);\n")
\r
109 file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c " return 0; }\n")
\r
110 try_compile (HAVE_FPCLASSIFY
\r
111 ${CMAKE_BINARY_DIR}/build_fpclassify
\r
112 ${CMAKE_BINARY_DIR}/fpclassify.c
\r
113 CMAKE_FLAGS -DLINK_LIBRARIES=m)
\r
114 message(STATUS "Looking for fpclassify - ${HAVE_FPCLASSIFY}")
\r
117 option (WITH_THREADLOCAL "check thread local keyword" ON)
\r
118 if (WITH_THREADLOCAL AND NOT DEFINED HAVE_THREAD_LOCAL_STORAGE)
\r
119 foreach (_THREADLOCAL_KEY "__thread" "__declspec(thread)")
\r
120 unset (HAVE_THREAD_LOCAL_STORAGE CACHE)
\r
121 check_c_source_compiles("
\r
122 ${_THREADLOCAL_KEY} int tls;
\r
126 }" HAVE_THREAD_LOCAL_STORAGE)
\r
127 if (${HAVE_THREAD_LOCAL_STORAGE})
\r
128 set (THREADLOCAL ${_THREADLOCAL_KEY})
\r
133 #==============================================================================
\r
135 #==============================================================================
\r
136 set (NLOPT_MAJOR_VERSION "2")
\r
137 set (NLOPT_MINOR_VERSION "5")
\r
138 set (NLOPT_BUGFIX_VERSION "0")
\r
139 set (NLOPT_VERSION_STRING ${NLOPT_MAJOR_VERSION}.${NLOPT_MINOR_VERSION}.${NLOPT_BUGFIX_VERSION})
\r
140 message (STATUS "NLopt version ${NLOPT_VERSION_STRING}")
\r
142 #==============================================================================
\r
144 #==============================================================================
\r
146 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/config.cmake.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h IMMEDIATE)
\r
150 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt.pc.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc @ONLY)
\r
151 install (FILES ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc DESTINATION ${RELATIVE_INSTALL_LIB_DIR}/pkgconfig)
\r
154 #==============================================================================
\r
155 # INCLUDE DIRECTORIES
\r
156 #==============================================================================
\r
157 set (${INCLUDE_DIRECTORIES} "")
\r
158 include_directories (
\r
159 ${CMAKE_BINARY_DIR}/api
\r
160 ${CMAKE_BINARY_DIR}
\r
181 #==============================================================================
\r
182 # nlopt LIBRARY TARGET (SHARED OR STATIC)
\r
183 #==============================================================================
\r
185 set (NLOPT_HEADERS
\r
186 api/nlopt.h ${CMAKE_BINARY_DIR}/api/nlopt.hpp ${CMAKE_BINARY_DIR}/api/nlopt.f
\r
189 set (NLOPT_SOURCES
\r
190 direct/DIRect.c direct/direct_wrap.c direct/DIRserial.c direct/DIRsubrout.c direct/direct-internal.h direct/direct.h
\r
191 cdirect/cdirect.c cdirect/hybrid.c cdirect/cdirect.h
\r
192 praxis/praxis.c praxis/praxis.h
\r
193 luksan/plis.c luksan/plip.c luksan/pnet.c luksan/mssubs.c luksan/pssubs.c luksan/luksan.h
\r
194 crs/crs.c crs/crs.h
\r
195 mlsl/mlsl.c mlsl/mlsl.h
\r
196 mma/mma.c mma/mma.h mma/ccsa_quadratic.c
\r
197 cobyla/cobyla.c cobyla/cobyla.h
\r
198 newuoa/newuoa.c newuoa/newuoa.h
\r
199 neldermead/nldrmd.c neldermead/neldermead.h neldermead/sbplx.c
\r
200 auglag/auglag.c auglag/auglag.h
\r
201 bobyqa/bobyqa.c bobyqa/bobyqa.h
\r
202 isres/isres.c isres/isres.h
\r
203 slsqp/slsqp.c slsqp/slsqp.h
\r
204 esch/esch.c esch/esch.h
\r
205 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
206 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
210 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
214 if (BUILD_SHARED_LIBS)
\r
215 add_definitions (-DNLOPT_DLL)
\r
216 add_definitions (-DNLOPT_DLL_EXPORT)
\r
219 install (FILES ${NLOPT_HEADERS} DESTINATION include)
\r
221 set (nlopt_lib nlopt${NLOPT_SUFFIX})
\r
222 add_library (${nlopt_lib} ${NLOPT_SOURCES})
\r
223 set_target_properties (${nlopt_lib} PROPERTIES SOVERSION 0)
\r
224 set_target_properties (${nlopt_lib} PROPERTIES VERSION 0.9.0)
\r
226 # pass -fPIC in case swig module is built with static library
\r
227 if (NOT BUILD_SHARED_LIBS)
\r
228 check_c_compiler_flag (-fPIC HAS_FPIC)
\r
230 set (CMAKE_C_FLAGS "-fPIC ${CMAKE_C_FLAGS}")
\r
231 set (CMAKE_CXX_FLAGS "-fPIC ${CMAKE_CXX_FLAGS}")
\r
235 install ( TARGETS ${nlopt_lib}
\r
236 EXPORT NLoptLibraryDepends
\r
237 RUNTIME DESTINATION ${RELATIVE_INSTALL_BIN_DIR}
\r
238 LIBRARY DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
\r
239 ARCHIVE DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
\r
242 add_subdirectory (api)
\r
245 find_package (PythonInterp)
\r
246 find_package (PythonLibs)
\r
247 find_package (NumPy)
\r
250 if (PYTHONINTERP_FOUND)
\r
251 execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}'))"
\r
252 OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
\r
253 OUTPUT_STRIP_TRAILING_WHITESPACE )
\r
255 get_filename_component (_ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE)
\r
256 file (RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
\r
258 set (PYTHON_MODULE_PATH ${_REL_PYTHON_MODULE_PATH})
\r
263 find_package (Guile)
\r
267 find_package (SWIG)
\r
270 add_subdirectory (swig)
\r
273 find_package (Octave)
\r
277 find_package (Matlab)
\r
280 if (OCTAVE_FOUND OR Matlab_FOUND)
\r
281 add_subdirectory (octave)
\r
285 add_subdirectory (test)
\r
287 set (CPACK_PACKAGE_NAME "${CMAKE_PROJECT_NAME}")
\r
288 set (CPACK_PACKAGE_VERSION_MAJOR "${NLOPT_MAJOR_VERSION}")
\r
289 set (CPACK_PACKAGE_VERSION_MINOR "${NLOPT_MINOR_VERSION}")
\r
290 set (CPACK_PACKAGE_VERSION_PATCH "${NLOPT_BUGFIX_VERSION}")
\r
291 set (CPACK_SOURCE_GENERATOR "TBZ2;TGZ" )
\r
292 set (CPACK_BINARY_STGZ "OFF" )
\r
293 set (CPACK_BINARY_TBZ2 "ON" )
\r
294 set (CPACK_BINARY_TGZ "ON" )
\r
295 set (CPACK_BINARY_TZ "OFF" )
\r
296 set (CPACK_SOURCE_IGNORE_FILES ".git;/build;.*~;${CPACK_SOURCE_IGNORE_FILES}")
\r
298 set (CPACK_SOURCE_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${NLOPT_VERSION_STRING})
\r
302 # configuration files
\r
303 export (TARGETS ${nlopt_lib} FILE ${PROJECT_BINARY_DIR}/NLoptLibraryDepends.cmake)
\r
305 # Install the export set for use with the install-tree
\r
306 install(EXPORT NLoptLibraryDepends
\r
307 DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
\r
308 COMPONENT Development)
\r
310 # Create a NLOPTConfig.cmake file for the use from the install tree
\r
312 set (NLOPT_LIBRARIES ${nlopt_lib})
\r
314 set (NLOPT_CMAKE_DIR "${INSTALL_CMAKE_DIR}")
\r
315 file (RELATIVE_PATH rel_include_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_INCLUDE_DIR}")
\r
316 list (APPEND RELATIVE_NLOPT_INCLUDE_DIRS ${rel_include_dir})
\r
318 file (RELATIVE_PATH rel_lib_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_LIB_DIR}")
\r
319 list (APPEND RELATIVE_NLOPT_LIB_DIR ${rel_lib_dir})
\r
321 configure_file (cmake/NLoptConfig.cmake.in NLoptConfig.cmake @ONLY)
\r
322 configure_file (cmake/NLoptConfigVersion.cmake.in NLoptConfigVersion.cmake @ONLY)
\r
324 ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfig.cmake
\r
325 ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfigVersion.cmake
\r
326 DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
\r
327 COMPONENT Development)
\r