chiark / gitweb /
Cleaner way to fix ctest (#144)
[nlopt.git] / CMakeLists.txt
index 1cf877f9684476d04fa2c73823d5ea6fd642aa5b..48644780edc2ab27db9f0b4c1ceece11054442bf 100644 (file)
@@ -1,31 +1,32 @@
 #==============================================================================\r
 # NLOPT CMake file\r
-# \r
-# NLopt is a free/open-source library for nonlinear optimization, providing \r
-# a common interface for a number of different free optimization routines \r
-# available online as well as original implementations of various other \r
+#\r
+# NLopt is a free/open-source library for nonlinear optimization, providing\r
+# a common interface for a number of different free optimization routines\r
+# available online as well as original implementations of various other\r
 # algorithms\r
-# WEBSITE: http://ab-initio.mit.edu/wiki/index.php/NLopt \r
+# WEBSITE: http://ab-initio.mit.edu/wiki/index.php/NLopt\r
 # AUTHOR: Steven G. Johnson\r
 #\r
 # This CMakeLists.txt file was created to compile NLOPT with the CMAKE utility.\r
 # Benoit Scherrer, 2010 CRL, Harvard Medical School\r
-# Copyright (c) 2008-2009 Children's Hospital Boston \r
+# Copyright (c) 2008-2009 Children's Hospital Boston\r
 #==============================================================================\r
-cmake_minimum_required (VERSION 2.6)\r
+cmake_minimum_required (VERSION 3.0)\r
 \r
 if (NOT DEFINED CMAKE_BUILD_TYPE)\r
-  set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type")\r
+  set (CMAKE_BUILD_TYPE Release CACHE STRING "Build type")\r
 endif ()\r
 \r
-project (nlopt C)\r
+project (nlopt)\r
 \r
 list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)\r
 \r
 option (WITH_CXX "enable cxx routines" OFF)\r
-option (BUILD_SHARED_LIBS "Build NLOPT as a shared library" ON)\r
+option (BUILD_SHARED_LIBS "Build NLopt as a shared library" ON)\r
 option (BUILD_PYTHON "build python bindings" ON)\r
 option (BUILD_OCTAVE "build octave bindings" ON)\r
+option (BUILD_MATLAB "build matlab bindings" ON)\r
 option (BUILD_GUILE "build guile bindings" ON)\r
 option (USE_SWIG "use SWIG to build bindings" ON)\r
 \r
@@ -34,26 +35,24 @@ if (WITH_CXX)
   set (NLOPT_SUFFIX _cxx)\r
 endif ()\r
 \r
-if (WITH_CXX OR BUILD_PYTHON)\r
-  enable_language (CXX)\r
-endif ()\r
-\r
+include (GNUInstallDirs)\r
 \r
 # Offer the user the choice of overriding the installation directories\r
-set (INSTALL_LIB_DIR     lib${LIB_SUFFIX} CACHE PATH "Installation directory for libraries")\r
-set (INSTALL_BIN_DIR     bin              CACHE PATH "Installation directory for executables")\r
-set (INSTALL_INCLUDE_DIR include          CACHE PATH "Installation directory for header files")\r
-set (INSTALL_DATA_DIR    share/nlopt       CACHE PATH "Installation directory for data files")\r
+set (INSTALL_LIB_DIR     ${CMAKE_INSTALL_LIBDIR}        CACHE PATH "Installation directory for libraries")\r
+set (INSTALL_BIN_DIR     ${CMAKE_INSTALL_BINDIR}        CACHE PATH "Installation directory for executables")\r
+set (INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}    CACHE PATH "Installation directory for header files")\r
+set (INSTALL_DATA_DIR    ${CMAKE_INSTALL_DATADIR}/nlopt CACHE PATH "Installation directory for data files")\r
+set (INSTALL_MAN_DIR     ${CMAKE_INSTALL_MANDIR}        CACHE PATH "Installation directory for man documentation")\r
 set (INSTALL_CMAKE_DIR   ${INSTALL_LIB_DIR}/cmake/nlopt CACHE PATH "Installation directory for cmake config files")\r
 \r
 # Make relative paths absolute (needed later on)\r
-foreach(p LIB BIN INCLUDE DATA CMAKE)\r
-    set(var INSTALL_${p}_DIR)\r
-    set(RELATIVE_INSTALL_${p}_DIR ${INSTALL_${p}_DIR})\r
-    if(NOT IS_ABSOLUTE "${${var}}")\r
-        set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")\r
-    endif()\r
-endforeach()\r
+foreach (p LIB BIN INCLUDE DATA CMAKE)\r
+  set (var INSTALL_${p}_DIR)\r
+  set (RELATIVE_INSTALL_${p}_DIR ${INSTALL_${p}_DIR})\r
+  if (NOT IS_ABSOLUTE "${${var}}")\r
+    set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")\r
+  endif ()\r
+endforeach ()\r
 \r
 \r
 set (CMAKE_INSTALL_RPATH ${INSTALL_LIB_DIR})\r
@@ -67,6 +66,9 @@ include (CheckIncludeFiles)
 include (CheckFunctionExists)\r
 include (CheckTypeSize)\r
 include (CheckCCompilerFlag)\r
+include (CheckCXXSymbolExists)\r
+include (CheckCXXCompilerFlag)\r
+include (CheckLibraryExists)\r
 \r
 #==============================================================================\r
 # COMPILATION CHECKINGS and CONFIGURATION GENERATION\r
@@ -100,15 +102,20 @@ set (HAVE_UINT32_T ${SIZEOF_UINT32_T})
 check_type_size ("unsigned int" SIZEOF_UNSIGNED_INT)\r
 check_type_size ("unsigned long" SIZEOF_UNSIGNED_LONG)\r
 \r
+check_library_exists ("m" sqrt "" HAVE_LIBM)\r
+if (HAVE_LIBM)\r
+  set (M_LIBRARY m)\r
+endif()\r
+\r
 if (NOT DEFINED HAVE_FPCLASSIFY)\r
   message(STATUS "Looking for fpclassify")\r
-  file (WRITE ${CMAKE_BINARY_DIR}/fpclassify.c "#include <math.h>\n")\r
-  file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c "int main(void) {\n")\r
-  file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c "if (!fpclassify(3.14159)) fpclassify(2.7183);\n")\r
-  file (APPEND ${CMAKE_BINARY_DIR}/fpclassify.c "  return 0; }\n")\r
+  file (WRITE ${PROJECT_BINARY_DIR}/fpclassify.c "#include <math.h>\n")\r
+  file (APPEND ${PROJECT_BINARY_DIR}/fpclassify.c "int main(void) {\n")\r
+  file (APPEND ${PROJECT_BINARY_DIR}/fpclassify.c "if (!fpclassify(3.14159)) fpclassify(2.7183);\n")\r
+  file (APPEND ${PROJECT_BINARY_DIR}/fpclassify.c "  return 0; }\n")\r
   try_compile (HAVE_FPCLASSIFY\r
-  ${CMAKE_BINARY_DIR}/build_fpclassify\r
-  ${CMAKE_BINARY_DIR}/fpclassify.c\r
+  ${PROJECT_BINARY_DIR}/build_fpclassify\r
+  ${PROJECT_BINARY_DIR}/fpclassify.c\r
   CMAKE_FLAGS -DLINK_LIBRARIES=m)\r
   message(STATUS "Looking for fpclassify - ${HAVE_FPCLASSIFY}")\r
 endif ()\r
@@ -129,6 +136,16 @@ if (WITH_THREADLOCAL AND NOT DEFINED HAVE_THREAD_LOCAL_STORAGE)
   endforeach()\r
 endif ()\r
 \r
+if (WITH_CXX OR BUILD_PYTHON OR BUILD_GUILE OR BUILD_OCTAVE)\r
+  check_cxx_symbol_exists (_LIBCPP_VERSION string SYSTEM_HAS_LIBCPP)\r
+  if (SYSTEM_HAS_LIBCPP)\r
+    check_cxx_compiler_flag ("-std=c++11" SUPPORTS_STDCXX11)\r
+    if (SUPPORTS_STDCXX11)\r
+      set (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")\r
+    endif ()\r
+  endif ()\r
+endif ()\r
+\r
 #==============================================================================\r
 # version\r
 #==============================================================================\r
@@ -139,10 +156,10 @@ set (NLOPT_VERSION_STRING ${NLOPT_MAJOR_VERSION}.${NLOPT_MINOR_VERSION}.${NLOPT_
 message (STATUS "NLopt version ${NLOPT_VERSION_STRING}")\r
 \r
 #==============================================================================\r
-# CREATE config.h\r
+# CREATE nlopt_config.h\r
 #==============================================================================\r
 \r
-configure_file (${CMAKE_CURRENT_SOURCE_DIR}/config.cmake.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h IMMEDIATE)\r
+configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt_config.h IMMEDIATE)\r
 \r
 # pkgconfig file\r
 if (UNIX OR MINGW)\r
@@ -150,78 +167,82 @@ if (UNIX OR MINGW)
   install (FILES ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc DESTINATION ${RELATIVE_INSTALL_LIB_DIR}/pkgconfig)\r
 endif ()\r
 \r
-#==============================================================================\r
-# INCLUDE DIRECTORIES\r
-#==============================================================================\r
-set (${INCLUDE_DIRECTORIES} "")\r
-include_directories (\r
-  ${CMAKE_BINARY_DIR}/api\r
-  ${CMAKE_BINARY_DIR}\r
-  stogo \r
-  util \r
-  direct \r
-  cdirect \r
-  praxis \r
-  luksan \r
-  crs \r
-  mlsl \r
-  mma \r
-  cobyla \r
-  newuoa \r
-  neldermead \r
-  auglag \r
-  bobyqa \r
-  isres \r
-  slsqp\r
-  esch\r
-  api)\r
-\r
-\r
 #==============================================================================\r
 # nlopt LIBRARY TARGET (SHARED OR STATIC)\r
 #==============================================================================\r
 \r
-SET ( NLOPT_HEADERS \r
-  api/nlopt.h ${CMAKE_BINARY_DIR}/api/nlopt.hpp ${CMAKE_BINARY_DIR}/api/nlopt.f\r
+configure_file (api/nlopt.h ${PROJECT_BINARY_DIR}/api/nlopt.h COPYONLY)\r
+\r
+set (NLOPT_HEADERS\r
+  ${PROJECT_BINARY_DIR}/api/nlopt.h ${PROJECT_BINARY_DIR}/api/nlopt.hpp ${PROJECT_BINARY_DIR}/api/nlopt.f\r
 )\r
 \r
-SET ( NLOPT_SOURCES   \r
-       direct/DIRect.c direct/direct_wrap.c direct/DIRserial.c direct/DIRsubrout.c direct/direct-internal.h direct/direct.h\r
-       cdirect/cdirect.c cdirect/hybrid.c cdirect/cdirect.h\r
-       praxis/praxis.c praxis/praxis.h\r
-       luksan/plis.c luksan/plip.c luksan/pnet.c luksan/mssubs.c luksan/pssubs.c luksan/luksan.h\r
-       crs/crs.c crs/crs.h\r
-       mlsl/mlsl.c mlsl/mlsl.h\r
-       mma/mma.c mma/mma.h mma/ccsa_quadratic.c\r
-       cobyla/cobyla.c cobyla/cobyla.h\r
-       newuoa/newuoa.c newuoa/newuoa.h \r
-       neldermead/nldrmd.c neldermead/neldermead.h neldermead/sbplx.c   \r
-       auglag/auglag.c auglag/auglag.h\r
-       bobyqa/bobyqa.c bobyqa/bobyqa.h\r
-       isres/isres.c isres/isres.h \r
-       slsqp/slsqp.c slsqp/slsqp.h \r
-       esch/esch.c esch/esch.h\r
-       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
-       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
-        )\r
+set (NLOPT_SOURCES\r
+  direct/DIRect.c direct/direct_wrap.c direct/DIRserial.c direct/DIRsubrout.c direct/direct-internal.h direct/direct.h\r
+  cdirect/cdirect.c cdirect/hybrid.c cdirect/cdirect.h\r
+  praxis/praxis.c praxis/praxis.h\r
+  luksan/plis.c luksan/plip.c luksan/pnet.c luksan/mssubs.c luksan/pssubs.c luksan/luksan.h\r
+  crs/crs.c crs/crs.h\r
+  mlsl/mlsl.c mlsl/mlsl.h\r
+  mma/mma.c mma/mma.h mma/ccsa_quadratic.c\r
+  cobyla/cobyla.c cobyla/cobyla.h\r
+  newuoa/newuoa.c newuoa/newuoa.h\r
+  neldermead/nldrmd.c neldermead/neldermead.h neldermead/sbplx.c\r
+  auglag/auglag.c auglag/auglag.h\r
+  bobyqa/bobyqa.c bobyqa/bobyqa.h\r
+  isres/isres.c isres/isres.h\r
+  slsqp/slsqp.c slsqp/slsqp.h\r
+  esch/esch.c esch/esch.h\r
+  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
+  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
+)\r
 \r
 if (WITH_CXX)\r
   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
 endif ()\r
 \r
-\r
-if (BUILD_SHARED_LIBS)\r
-  add_definitions (-DNLOPT_DLL)\r
-  add_definitions (-DNLOPT_DLL_EXPORT)\r
-endif ()\r
-\r
-install (FILES ${NLOPT_HEADERS} DESTINATION include)\r
+install (FILES ${NLOPT_HEADERS} DESTINATION ${INSTALL_INCLUDE_DIR})\r
 \r
 set (nlopt_lib nlopt${NLOPT_SUFFIX})\r
 add_library (${nlopt_lib} ${NLOPT_SOURCES})\r
+target_link_libraries (${nlopt_lib} ${M_LIBRARY})\r
+\r
 set_target_properties (${nlopt_lib} PROPERTIES SOVERSION 0)\r
 set_target_properties (${nlopt_lib} PROPERTIES VERSION 0.9.0)\r
 \r
+#==============================================================================\r
+# INCLUDE DIRECTORIES\r
+#==============================================================================\r
+target_include_directories (${nlopt_lib} PRIVATE\r
+  ${PROJECT_BINARY_DIR}/api\r
+  ${PROJECT_BINARY_DIR}\r
+  stogo\r
+  util\r
+  direct\r
+  cdirect\r
+  praxis\r
+  luksan\r
+  crs\r
+  mlsl\r
+  mma\r
+  cobyla\r
+  newuoa\r
+  neldermead\r
+  auglag\r
+  bobyqa\r
+  isres\r
+  slsqp\r
+  esch\r
+  api)\r
+\r
+get_target_property (NLOPT_PRIVATE_INCLUDE_DIRS ${nlopt_lib} INCLUDE_DIRECTORIES)\r
+target_include_directories (${nlopt_lib} INTERFACE "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/api>" "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")\r
+\r
+if (BUILD_SHARED_LIBS)\r
+  target_compile_definitions (${nlopt_lib} PUBLIC -DNLOPT_DLL)\r
+  target_compile_definitions (${nlopt_lib} PRIVATE -DNLOPT_DLL_EXPORT)\r
+endif ()\r
+\r
 # pass -fPIC in case swig module is built with static library\r
 if (NOT BUILD_SHARED_LIBS)\r
   check_c_compiler_flag (-fPIC HAS_FPIC)\r
@@ -246,7 +267,7 @@ if (BUILD_PYTHON)
   find_package (NumPy)\r
 endif ()\r
 \r
-if (PYTHONINTERP_FOUND)\r
+if (NOT DEFINED INSTALL_PYTHON_DIR AND PYTHONINTERP_FOUND)\r
   execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}'))"\r
                     OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH\r
                     OUTPUT_STRIP_TRAILING_WHITESPACE )\r
@@ -254,7 +275,7 @@ if (PYTHONINTERP_FOUND)
   get_filename_component (_ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE)\r
   file (RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH})\r
 \r
-  set (PYTHON_MODULE_PATH ${_REL_PYTHON_MODULE_PATH})\r
+  set (INSTALL_PYTHON_DIR ${_REL_PYTHON_MODULE_PATH})\r
 \r
 endif ()\r
 \r
@@ -272,7 +293,11 @@ if (BUILD_OCTAVE)
   find_package (Octave)\r
 endif ()\r
 \r
-if (OCTAVE_FOUND)\r
+if (BUILD_MATLAB)\r
+  find_package (Matlab)\r
+endif ()\r
+\r
+if (OCTAVE_FOUND OR Matlab_FOUND)\r
   add_subdirectory (octave)\r
 endif ()\r
 \r
@@ -295,16 +320,17 @@ set (CPACK_SOURCE_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${NLOPT_VERSION_STRING
 include (CPack)\r
 \r
 # configuration files\r
-export (TARGETS ${nlopt_lib} FILE ${PROJECT_BINARY_DIR}/NLoptLibraryDepends.cmake)\r
+export (TARGETS ${nlopt_lib} NAMESPACE NLopt:: FILE ${PROJECT_BINARY_DIR}/NLoptLibraryDepends.cmake)\r
 \r
 # Install the export set for use with the install-tree\r
 install(EXPORT NLoptLibraryDepends\r
+        NAMESPACE NLopt::\r
         DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}\r
         COMPONENT Development)\r
 \r
 # Create a NLOPTConfig.cmake file for the use from the install tree\r
 # and install it\r
-set (NLOPT_LIBRARIES ${nlopt_lib})\r
+set (NLOPT_LIBRARIES "NLopt::${nlopt_lib}")\r
 \r
 set (NLOPT_CMAKE_DIR "${INSTALL_CMAKE_DIR}")\r
 file (RELATIVE_PATH rel_include_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_INCLUDE_DIR}")\r