chiark / gitweb /
Remove set_numevals method
[nlopt.git] / CMakeLists.txt
1 #==============================================================================
2 # NLOPT CMake file
3 #
4 # NLopt is a free/open-source library for nonlinear optimization, providing
5 # a common interface for a number of different free optimization routines
6 # available online as well as original implementations of various other
7 # algorithms
8 # WEBSITE: http://ab-initio.mit.edu/wiki/index.php/NLopt
9 # AUTHOR: Steven G. Johnson
10 #
11 # This CMakeLists.txt file was created to compile NLOPT with the CMAKE utility.
12 # Benoit Scherrer, 2010 CRL, Harvard Medical School
13 # Copyright (c) 2008-2009 Children's Hospital Boston
14 #==============================================================================
15 cmake_minimum_required (VERSION 2.8.11)
16
17 if (NOT DEFINED CMAKE_BUILD_TYPE)
18   set (CMAKE_BUILD_TYPE Release CACHE STRING "Build type")
19 endif ()
20
21 project (nlopt)
22
23 list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
24
25 option (NLOPT_CXX "enable cxx routines" OFF)
26 option (BUILD_SHARED_LIBS "Build NLopt as a shared library" ON)
27 option (NLOPT_PYTHON "build python bindings" ON)
28 option (NLOPT_OCTAVE "build octave bindings" ON)
29 option (NLOPT_MATLAB "build matlab bindings" ON)
30 option (NLOPT_GUILE "build guile bindings" ON)
31 option (NLOPT_SWIG "use SWIG to build bindings" ON)
32 option (NLOPT_LINK_PYTHON "link Python libs" ON)
33
34 set (NLOPT_SUFFIX)
35 if (NLOPT_CXX)
36   set (NLOPT_SUFFIX _cxx)
37 endif ()
38
39 include (GNUInstallDirs)
40
41 # Offer the user the choice of overriding the installation directories
42 set (INSTALL_LIB_DIR     ${CMAKE_INSTALL_LIBDIR}        CACHE PATH "Installation directory for libraries")
43 set (INSTALL_BIN_DIR     ${CMAKE_INSTALL_BINDIR}        CACHE PATH "Installation directory for executables")
44 set (INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}    CACHE PATH "Installation directory for header files")
45 set (INSTALL_DATA_DIR    ${CMAKE_INSTALL_DATADIR}/nlopt CACHE PATH "Installation directory for data files")
46 set (INSTALL_MAN_DIR     ${CMAKE_INSTALL_MANDIR}        CACHE PATH "Installation directory for man documentation")
47 set (INSTALL_CMAKE_DIR   ${INSTALL_LIB_DIR}/cmake/nlopt CACHE PATH "Installation directory for cmake config files")
48
49 # Make relative paths absolute (needed later on)
50 foreach (p LIB BIN INCLUDE DATA CMAKE)
51   set (var INSTALL_${p}_DIR)
52   set (RELATIVE_INSTALL_${p}_DIR ${INSTALL_${p}_DIR})
53   if (NOT IS_ABSOLUTE "${${var}}")
54     set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
55   endif ()
56 endforeach ()
57
58
59 set (CMAKE_INSTALL_RPATH ${INSTALL_LIB_DIR})
60 set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
61 if(POLICY CMP0042)
62   # Set MACOSX_RPATH to ON
63   cmake_policy(SET CMP0042 NEW)
64 endif()
65
66 include (CheckIncludeFiles)
67 include (CheckFunctionExists)
68 include (CheckTypeSize)
69 include (CheckCCompilerFlag)
70 include (CheckCXXSymbolExists)
71 include (CheckCXXCompilerFlag)
72 include (CheckLibraryExists)
73
74 #==============================================================================
75 # COMPILATION CHECKINGS and CONFIGURATION GENERATION
76 #==============================================================================
77 check_include_file (dlfcn.h HAVE_DLFCN_H)
78 check_include_file (getopt.h HAVE_GETOPT_H)
79 check_include_file (unistd.h HAVE_UNISTD_H)
80 check_include_file (string.h HAVE_STRING_H)
81 check_include_file (strings.h HAVE_STRINGS_H)
82 check_include_file (inttypes.h HAVE_INTTYPES_H)
83 check_include_file (memory.h HAVE_MEMORY_H)
84 check_include_file (stdlib.h HAVE_STDLIB_H)
85 check_include_file (stdint.h HAVE_STDINT_H)
86 check_include_file (time.h HAVE_TIME_H)
87 check_include_file (sys/types.h HAVE_SYS_TYPES_H)
88 check_include_file (sys/stat.h HAVE_SYS_STAT_H)
89 check_include_file (sys/time.h HAVE_SYS_TIME_H)
90 if (HAVE_TIME_H AND HAVE_SYS_TIME_H)
91   set (TIME_WITH_SYS_TIME TRUE)
92 endif ()
93 check_function_exists (getpid HAVE_GETPID)
94 check_function_exists (syscall HAVE_GETTID_SYSCALL)
95 check_function_exists (isinf HAVE_ISINF)
96 check_function_exists (isnan HAVE_ISNAN)
97 check_function_exists (gettimeofday HAVE_GETTIMEOFDAY)
98 check_function_exists (qsort_r HAVE_QSORT_R)
99 check_function_exists (time HAVE_TIME)
100 check_function_exists (copysign HAVE_COPYSIGN)
101 check_type_size ("uint32_t" SIZEOF_UINT32_T)
102 set (HAVE_UINT32_T ${SIZEOF_UINT32_T})
103 check_type_size ("unsigned int" SIZEOF_UNSIGNED_INT)
104 check_type_size ("unsigned long" SIZEOF_UNSIGNED_LONG)
105
106 check_library_exists ("m" sqrt "" HAVE_LIBM)
107 if (HAVE_LIBM)
108   set (M_LIBRARY m)
109 endif()
110
111 if (NOT DEFINED HAVE_FPCLASSIFY)
112   message(STATUS "Looking for fpclassify")
113   file (WRITE ${PROJECT_BINARY_DIR}/fpclassify.c "#include <math.h>\n")
114   file (APPEND ${PROJECT_BINARY_DIR}/fpclassify.c "int main(void) {\n")
115   file (APPEND ${PROJECT_BINARY_DIR}/fpclassify.c "if (!fpclassify(3.14159)) fpclassify(2.7183);\n")
116   file (APPEND ${PROJECT_BINARY_DIR}/fpclassify.c "  return 0; }\n")
117   try_compile (HAVE_FPCLASSIFY
118   ${PROJECT_BINARY_DIR}/build_fpclassify
119   ${PROJECT_BINARY_DIR}/fpclassify.c
120   CMAKE_FLAGS -DLINK_LIBRARIES=m)
121   message(STATUS "Looking for fpclassify - ${HAVE_FPCLASSIFY}")
122 endif ()
123
124 option (WITH_THREADLOCAL "check thread local keyword" ON)
125 if (WITH_THREADLOCAL AND NOT DEFINED HAVE_THREAD_LOCAL_STORAGE)
126   foreach (_THREADLOCAL_KEY "__thread" "__declspec(thread)")
127     unset (HAVE_THREAD_LOCAL_STORAGE CACHE)
128     check_c_source_compiles("
129     ${_THREADLOCAL_KEY} int tls;
130
131     int main(void) {
132         return 0;
133     }" HAVE_THREAD_LOCAL_STORAGE)
134     if (${HAVE_THREAD_LOCAL_STORAGE})
135       set (THREADLOCAL ${_THREADLOCAL_KEY})
136     endif ()
137   endforeach()
138 endif ()
139
140 if (NLOPT_CXX OR NLOPT_PYTHON OR NLOPT_GUILE OR NLOPT_OCTAVE)
141   check_cxx_symbol_exists (_LIBCPP_VERSION string SYSTEM_HAS_LIBCPP)
142   if (SYSTEM_HAS_LIBCPP)
143     check_cxx_compiler_flag ("-std=c++11" SUPPORTS_STDCXX11)
144     if (SUPPORTS_STDCXX11)
145       set (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
146     endif ()
147   endif ()
148 endif ()
149
150 #==============================================================================
151 # version
152 #==============================================================================
153 set (NLOPT_MAJOR_VERSION "2")
154 set (NLOPT_MINOR_VERSION "5")
155 set (NLOPT_BUGFIX_VERSION "0")
156 set (NLOPT_VERSION_STRING ${NLOPT_MAJOR_VERSION}.${NLOPT_MINOR_VERSION}.${NLOPT_BUGFIX_VERSION})
157 message (STATUS "NLopt version ${NLOPT_VERSION_STRING}")
158
159 #==============================================================================
160 # CREATE nlopt_config.h
161 #==============================================================================
162
163 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt_config.h IMMEDIATE)
164
165 # pkgconfig file
166 if (UNIX OR MINGW)
167   configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt.pc.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc @ONLY)
168   install (FILES ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc DESTINATION ${RELATIVE_INSTALL_LIB_DIR}/pkgconfig)
169 endif ()
170
171 #==============================================================================
172 # nlopt LIBRARY TARGET (SHARED OR STATIC)
173 #==============================================================================
174
175 configure_file (src/api/nlopt.h ${PROJECT_BINARY_DIR}/src/api/nlopt.h COPYONLY)
176
177 set (NLOPT_HEADERS
178   ${PROJECT_BINARY_DIR}/src/api/nlopt.h ${PROJECT_BINARY_DIR}/src/api/nlopt.hpp ${PROJECT_BINARY_DIR}/src/api/nlopt.f
179 )
180
181 set (NLOPT_SOURCES
182   src/algs/direct/DIRect.c src/algs/direct/direct_wrap.c src/algs/direct/DIRserial.c src/algs/direct/DIRsubrout.c src/algs/direct/direct-internal.h src/algs/direct/direct.h
183   src/algs/cdirect/cdirect.c src/algs/cdirect/hybrid.c src/algs/cdirect/cdirect.h
184   src/algs/praxis/praxis.c src/algs/praxis/praxis.h
185   src/algs/luksan/plis.c src/algs/luksan/plip.c src/algs/luksan/pnet.c src/algs/luksan/mssubs.c src/algs/luksan/pssubs.c src/algs/luksan/luksan.h
186   src/algs/crs/crs.c src/algs/crs/crs.h
187   src/algs/mlsl/mlsl.c src/algs/mlsl/mlsl.h
188   src/algs/mma/mma.c src/algs/mma/mma.h src/algs/mma/ccsa_quadratic.c
189   src/algs/cobyla/cobyla.c src/algs/cobyla/cobyla.h
190   src/algs/newuoa/newuoa.c src/algs/newuoa/newuoa.h
191   src/algs/neldermead/nldrmd.c src/algs/neldermead/neldermead.h src/algs/neldermead/sbplx.c
192   src/algs/auglag/auglag.c src/algs/auglag/auglag.h
193   src/algs/bobyqa/bobyqa.c src/algs/bobyqa/bobyqa.h
194   src/algs/isres/isres.c src/algs/isres/isres.h
195   src/algs/slsqp/slsqp.c src/algs/slsqp/slsqp.h
196   src/algs/esch/esch.c src/algs/esch/esch.h
197   src/api/general.c src/api/options.c src/api/optimize.c src/api/deprecated.c src/api/nlopt-internal.h src/api/nlopt.h src/api/f77api.c src/api/f77funcs.h src/api/f77funcs_.h src/api/nlopt.hpp src/api/nlopt-in.hpp
198   src/util/mt19937ar.c src/util/sobolseq.c src/util/soboldata.h src/util/timer.c src/util/stop.c src/util/nlopt-util.h src/util/redblack.c src/util/redblack.h src/util/qsort_r.c src/util/rescale.c
199 )
200
201 if (NLOPT_CXX)
202   list (APPEND NLOPT_SOURCES src/algs/stogo/global.cc src/algs/stogo/linalg.cc src/algs/stogo/local.cc src/algs/stogo/stogo.cc src/algs/stogo/tools.cc
203         src/algs/stogo/global.h src/algs/stogo/linalg.h src/algs/stogo/local.h src/algs/stogo/stogo_config.h src/algs/stogo/stogo.h src/algs/stogo/tools.h)
204 endif ()
205
206 install (FILES ${NLOPT_HEADERS} DESTINATION ${RELATIVE_INSTALL_INCLUDE_DIR})
207
208 set (nlopt_lib nlopt${NLOPT_SUFFIX})
209 add_library (${nlopt_lib} ${NLOPT_SOURCES})
210 target_link_libraries (${nlopt_lib} ${M_LIBRARY})
211
212 set_target_properties (${nlopt_lib} PROPERTIES SOVERSION 0)
213 set_target_properties (${nlopt_lib} PROPERTIES VERSION 0.9.0)
214
215 #==============================================================================
216 # INCLUDE DIRECTORIES
217 #==============================================================================
218 target_include_directories (${nlopt_lib} PRIVATE
219   ${PROJECT_BINARY_DIR}/src/api
220   ${PROJECT_BINARY_DIR}
221   src/algs/stogo
222   src/util
223   src/algs/direct
224   src/algs/cdirect
225   src/algs/praxis
226   src/algs/luksan
227   src/algs/crs
228   src/algs/mlsl
229   src/algs/mma
230   src/algs/cobyla
231   src/algs/newuoa
232   src/algs/neldermead
233   src/algs/auglag
234   src/algs/bobyqa
235   src/algs/isres
236   src/algs/slsqp
237   src/algs/esch
238   src/api)
239
240 get_target_property (NLOPT_PRIVATE_INCLUDE_DIRS ${nlopt_lib} INCLUDE_DIRECTORIES)
241 target_include_directories (${nlopt_lib} INTERFACE "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/src/api>" "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
242
243 if (BUILD_SHARED_LIBS)
244   target_compile_definitions (${nlopt_lib} PUBLIC -DNLOPT_DLL)
245   target_compile_definitions (${nlopt_lib} PRIVATE -DNLOPT_DLL_EXPORT)
246 endif ()
247
248 # pass -fPIC in case swig module is built with static library
249 if (NOT BUILD_SHARED_LIBS)
250   check_c_compiler_flag (-fPIC HAS_FPIC)
251   if (HAS_FPIC)
252     set (CMAKE_C_FLAGS "-fPIC ${CMAKE_C_FLAGS}")
253     set (CMAKE_CXX_FLAGS "-fPIC ${CMAKE_CXX_FLAGS}")
254   endif ()
255 endif ()
256
257 install ( TARGETS ${nlopt_lib}
258           EXPORT  NLoptLibraryDepends
259           RUNTIME DESTINATION ${RELATIVE_INSTALL_BIN_DIR}
260           LIBRARY DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
261           ARCHIVE DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
262         )
263
264 if (MSVC AND BUILD_SHARED_LIBS AND NOT CMAKE_VERSION VERSION_LESS 3.1)
265   install (FILES $<TARGET_PDB_FILE:${nlopt_lib}> DESTINATION ${RELATIVE_INSTALL_BIN_DIR} CONFIGURATIONS Debug RelWithDebInfo COMPONENT Debug)
266 endif ()
267
268 add_subdirectory (src/api)
269
270 if (NLOPT_PYTHON)
271   find_package (PythonInterp)
272   find_package (PythonLibs)
273   find_package (NumPy)
274 endif ()
275
276 if (NOT DEFINED INSTALL_PYTHON_DIR AND PYTHONINTERP_FOUND)
277   execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}'))"
278                     OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH
279                     OUTPUT_STRIP_TRAILING_WHITESPACE )
280
281   get_filename_component (_ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE)
282   file (RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})
283
284   set (INSTALL_PYTHON_DIR ${_REL_PYTHON_MODULE_PATH})
285
286 endif ()
287
288 if (NLOPT_GUILE)
289   find_package (Guile)
290 endif ()
291
292 if (NLOPT_SWIG)
293   find_package (SWIG)
294   if (SWIG_FOUND)
295     add_subdirectory (src/swig)
296   endif ()
297 endif ()
298
299 if (NLOPT_OCTAVE)
300   find_package (Octave)
301 endif ()
302
303 if (NLOPT_MATLAB)
304   find_package (Matlab COMPONENTS MX_LIBRARY)
305 endif ()
306
307 if (OCTAVE_FOUND OR Matlab_FOUND)
308   add_subdirectory (src/octave)
309 endif ()
310
311 enable_testing ()
312 add_subdirectory (test)
313
314 set (CPACK_PACKAGE_NAME          "${CMAKE_PROJECT_NAME}")
315 set (CPACK_PACKAGE_VERSION_MAJOR "${NLOPT_MAJOR_VERSION}")
316 set (CPACK_PACKAGE_VERSION_MINOR "${NLOPT_MINOR_VERSION}")
317 set (CPACK_PACKAGE_VERSION_PATCH "${NLOPT_BUGFIX_VERSION}")
318 set (CPACK_SOURCE_GENERATOR      "TBZ2;TGZ"    )
319 set (CPACK_BINARY_STGZ           "OFF"        )
320 set (CPACK_BINARY_TBZ2           "ON"         )
321 set (CPACK_BINARY_TGZ            "ON"         )
322 set (CPACK_BINARY_TZ             "OFF"        )
323 set (CPACK_SOURCE_IGNORE_FILES ".git;/build;.*~;${CPACK_SOURCE_IGNORE_FILES}")
324
325 set (CPACK_SOURCE_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${NLOPT_VERSION_STRING})
326
327 include (CPack)
328
329 # configuration files
330 export (TARGETS ${nlopt_lib} NAMESPACE NLopt:: FILE ${PROJECT_BINARY_DIR}/NLoptLibraryDepends.cmake)
331
332 # Install the export set for use with the install-tree
333 install(EXPORT NLoptLibraryDepends
334         NAMESPACE NLopt::
335         DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
336         COMPONENT Development)
337
338 # Create a NLOPTConfig.cmake file for the use from the install tree
339 # and install it
340 set (NLOPT_LIBRARIES "NLopt::${nlopt_lib}")
341
342 set (NLOPT_CMAKE_DIR "${INSTALL_CMAKE_DIR}")
343 file (RELATIVE_PATH rel_include_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_INCLUDE_DIR}")
344 list (APPEND RELATIVE_NLOPT_INCLUDE_DIRS ${rel_include_dir})
345
346 file (RELATIVE_PATH rel_lib_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_LIB_DIR}")
347 list (APPEND RELATIVE_NLOPT_LIB_DIR ${rel_lib_dir})
348
349 configure_file (cmake/NLoptConfig.cmake.in NLoptConfig.cmake @ONLY)
350 configure_file (cmake/NLoptConfigVersion.cmake.in NLoptConfigVersion.cmake @ONLY)
351 install (FILES
352           ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfig.cmake
353           ${CMAKE_CURRENT_BINARY_DIR}/NLoptConfigVersion.cmake
354          DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
355          COMPONENT Development)