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