From e0bdbd180a7f0c840177cb5933dd17b82357144b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Dumas?= Date: Tue, 22 Nov 2016 13:23:44 +0100 Subject: [PATCH] Prefer target_include_directories in CMake build script --- CMakeLists.txt | 90 +++++++++++++++++++++++---------------------- swig/CMakeLists.txt | 1 + test/CMakeLists.txt | 3 +- 3 files changed, 49 insertions(+), 45 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 97a152f..4996bb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,18 +1,18 @@ #============================================================================== # NLOPT CMake file -# -# NLopt is a free/open-source library for nonlinear optimization, providing -# a common interface for a number of different free optimization routines -# available online as well as original implementations of various other +# +# NLopt is a free/open-source library for nonlinear optimization, providing +# a common interface for a number of different free optimization routines +# available online as well as original implementations of various other # algorithms -# WEBSITE: http://ab-initio.mit.edu/wiki/index.php/NLopt +# WEBSITE: http://ab-initio.mit.edu/wiki/index.php/NLopt # AUTHOR: Steven G. Johnson # # This CMakeLists.txt file was created to compile NLOPT with the CMAKE utility. # Benoit Scherrer, 2010 CRL, Harvard Medical School -# Copyright (c) 2008-2009 Children's Hospital Boston +# Copyright (c) 2008-2009 Children's Hospital Boston #============================================================================== -cmake_minimum_required (VERSION 2.8.5) +cmake_minimum_required (VERSION 3.0) if (NOT DEFINED CMAKE_BUILD_TYPE) set (CMAKE_BUILD_TYPE Release CACHE STRING "Build type") @@ -167,42 +167,17 @@ if (UNIX OR MINGW) install (FILES ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc DESTINATION ${RELATIVE_INSTALL_LIB_DIR}/pkgconfig) endif () -#============================================================================== -# INCLUDE DIRECTORIES -#============================================================================== -set (${INCLUDE_DIRECTORIES} "") -include_directories ( - ${PROJECT_BINARY_DIR}/api - ${PROJECT_BINARY_DIR} - stogo - util - direct - cdirect - praxis - luksan - crs - mlsl - mma - cobyla - newuoa - neldermead - auglag - bobyqa - isres - slsqp - esch - api) - - #============================================================================== # nlopt LIBRARY TARGET (SHARED OR STATIC) #============================================================================== -set (NLOPT_HEADERS - api/nlopt.h ${PROJECT_BINARY_DIR}/api/nlopt.hpp ${PROJECT_BINARY_DIR}/api/nlopt.f +configure_file (api/nlopt.h ${PROJECT_BINARY_DIR}/api/nlopt.h COPYONLY) + +set (NLOPT_HEADERS + ${PROJECT_BINARY_DIR}/api/nlopt.h ${PROJECT_BINARY_DIR}/api/nlopt.hpp ${PROJECT_BINARY_DIR}/api/nlopt.f ) -set (NLOPT_SOURCES +set (NLOPT_SOURCES direct/DIRect.c direct/direct_wrap.c direct/DIRserial.c direct/DIRsubrout.c direct/direct-internal.h direct/direct.h cdirect/cdirect.c cdirect/hybrid.c cdirect/cdirect.h praxis/praxis.c praxis/praxis.h @@ -212,7 +187,7 @@ set (NLOPT_SOURCES mma/mma.c mma/mma.h mma/ccsa_quadratic.c cobyla/cobyla.c cobyla/cobyla.h newuoa/newuoa.c newuoa/newuoa.h - neldermead/nldrmd.c neldermead/neldermead.h neldermead/sbplx.c + neldermead/nldrmd.c neldermead/neldermead.h neldermead/sbplx.c auglag/auglag.c auglag/auglag.h bobyqa/bobyqa.c bobyqa/bobyqa.h isres/isres.c isres/isres.h @@ -226,12 +201,6 @@ if (WITH_CXX) 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) endif () - -if (BUILD_SHARED_LIBS) - add_definitions (-DNLOPT_DLL) - add_definitions (-DNLOPT_DLL_EXPORT) -endif () - install (FILES ${NLOPT_HEADERS} DESTINATION ${INSTALL_INCLUDE_DIR}) set (nlopt_lib nlopt${NLOPT_SUFFIX}) @@ -241,6 +210,39 @@ target_link_libraries (${nlopt_lib} ${M_LIBRARY}) set_target_properties (${nlopt_lib} PROPERTIES SOVERSION 0) set_target_properties (${nlopt_lib} PROPERTIES VERSION 0.9.0) +#============================================================================== +# INCLUDE DIRECTORIES +#============================================================================== +target_include_directories (${nlopt_lib} PRIVATE + ${PROJECT_BINARY_DIR}/api + ${PROJECT_BINARY_DIR} + stogo + util + direct + cdirect + praxis + luksan + crs + mlsl + mma + cobyla + newuoa + neldermead + auglag + bobyqa + isres + slsqp + esch + api) + +get_target_property (NLOPT_PRIVATE_INCLUDE_DIRS ${nlopt_lib} INCLUDE_DIRECTORIES) +target_include_directories (${nlopt_lib} INTERFACE "$") + +if (BUILD_SHARED_LIBS) + target_compile_definitions (${nlopt_lib} INTERFACE -DNLOPT_DLL) + target_compile_definitions (${nlopt_lib} INTERFACE -DNLOPT_DLL_EXPORT) +endif () + # pass -fPIC in case swig module is built with static library if (NOT BUILD_SHARED_LIBS) check_c_compiler_flag (-fPIC HAS_FPIC) diff --git a/swig/CMakeLists.txt b/swig/CMakeLists.txt index d51ea03..faba14c 100644 --- a/swig/CMakeLists.txt +++ b/swig/CMakeLists.txt @@ -28,6 +28,7 @@ if (NUMPY_FOUND AND PYTHONLIBS_FOUND AND (SWIG_FOUND OR (EXISTS ${CMAKE_CURRENT_ set_source_files_properties (nlopt.i PROPERTIES CPLUSPLUS ON) set (SWIG_MODULE_nlopt_EXTRA_DEPS nlopt-python.i numpy.i) + include_directories (${NLOPT_PRIVATE_INCLUDE_DIRS}) swig_add_module (nlopt python nlopt.i) swig_link_libraries (nlopt ${nlopt_lib}) swig_link_libraries (nlopt ${PYTHON_LIBRARIES}) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c1990fd..e17d127 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -3,6 +3,7 @@ add_custom_target (tests) # have to add timer.c and mt19937ar.c as symbols are declared extern add_executable (testopt testfuncs.c testfuncs.h testopt.cpp ${PROJECT_SOURCE_DIR}/util/timer.c ${PROJECT_SOURCE_DIR}/util/mt19937ar.c) target_link_libraries (testopt ${nlopt_lib}) +target_include_directories (testopt PRIVATE ${NLOPT_PRIVATE_INCLUDE_DIRS}) add_dependencies (tests testopt) foreach (algo_index RANGE 29)# 42 @@ -46,4 +47,4 @@ if (GUILE_FOUND AND ((SWIG_FOUND AND SWIG_VERSION VERSION_GREATER 2.0.9) OR (EXI ) add_test (NAME test_guile COMMAND ${GUILE_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/t_guile.scm) set_tests_properties (test_guile PROPERTIES ENVIRONMENT "${GUILECHECK_ENVIRONMENT}") -endif () \ No newline at end of file +endif () -- 2.30.2