Skip to content

Commit

Permalink
CMake: Simplify paths logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jschueller committed Oct 26, 2024
1 parent 9886bd0 commit d2a1c44
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 56 deletions.
75 changes: 35 additions & 40 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,21 @@ endif ()
include (GNUInstallDirs)

# Offer the user the choice of overriding the installation directories
set (INSTALL_LIB_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "Installation directory for libraries")
set (INSTALL_BIN_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Installation directory for executables")
set (INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Installation directory for header files")
set (INSTALL_DATA_DIR ${CMAKE_INSTALL_DATADIR}/nlopt CACHE PATH "Installation directory for data files")
set (INSTALL_MAN_DIR ${CMAKE_INSTALL_MANDIR} CACHE PATH "Installation directory for man documentation")
set (INSTALL_CMAKE_DIR ${INSTALL_LIB_DIR}/cmake/nlopt CACHE PATH "Installation directory for cmake config files")

# Make relative paths absolute (needed later on)
foreach (p LIB BIN INCLUDE DATA CMAKE)
set (var INSTALL_${p}_DIR)
set (RELATIVE_INSTALL_${p}_DIR ${INSTALL_${p}_DIR})
if (NOT IS_ABSOLUTE "${${var}}")
set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
endif ()
endforeach ()


set (CMAKE_INSTALL_RPATH ${INSTALL_LIB_DIR} CACHE PATH "rpath")
set (NLOPT_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR} CACHE STRING "Installation directory for libraries")
set (NLOPT_INSTALL_BINDIR ${CMAKE_INSTALL_BINDIR} CACHE STRING "Installation directory for executables")
set (NLOPT_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE STRING "Installation directory for header files")
set (NLOPT_INSTALL_DATADIR ${CMAKE_INSTALL_DATADIR}/nlopt CACHE STRING "Installation directory for data files")
set (NLOPT_INSTALL_MANDIR ${CMAKE_INSTALL_MANDIR} CACHE STRING "Installation directory for man documentation")
set (NLOPT_INSTALL_CMAKEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/nlopt CACHE STRING "Installation directory for cmake config files")

set (CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/${NLOPT_INSTALL_LIBDIR} CACHE PATH "rpath")
set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOL "use link path")

if(POLICY CMP0042)
# Set MACOSX_RPATH to ON
cmake_policy(SET CMP0042 NEW)
endif()

if (POLICY CMP0086)
# UseSWIG honors SWIG_MODULE_NAME via -module flag
cmake_policy(SET CMP0086 NEW)
Expand Down Expand Up @@ -176,7 +168,7 @@ configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt_config.h.in ${CMAKE_CURRENT_BI

# pkgconfig file
configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt.pc.in ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc @ONLY)
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc DESTINATION ${RELATIVE_INSTALL_LIB_DIR}/pkgconfig)
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/nlopt.pc DESTINATION ${NLOPT_INSTALL_LIBDIR}/pkgconfig)

#==============================================================================
# nlopt LIBRARY TARGET (SHARED OR STATIC)
Expand Down Expand Up @@ -255,7 +247,7 @@ set_property(SOURCE src/algs/ags/solver.cc src/algs/ags/local_optimizer.cc src/a
PROPERTY SKIP_UNITY_BUILD_INCLUSION ON)
endif ()

install (FILES ${NLOPT_HEADERS} DESTINATION ${RELATIVE_INSTALL_INCLUDE_DIR})
install (FILES ${NLOPT_HEADERS} DESTINATION ${NLOPT_INSTALL_INCLUDEDIR})

set (nlopt_lib nlopt)
add_library (${nlopt_lib} ${NLOPT_SOURCES})
Expand Down Expand Up @@ -296,9 +288,14 @@ target_include_directories (${nlopt_lib} INTERFACE "$<BUILD_INTERFACE:${PROJECT_

if (BUILD_SHARED_LIBS)
target_compile_definitions (${nlopt_lib} PUBLIC NLOPT_DLL)
target_compile_definitions (${nlopt_lib} PRIVATE NLOPT_DLL_EXPORT)

if (WIN32)
# needed to switch between dllexport/dllimport
target_compile_definitions (${nlopt_lib} PRIVATE NLOPT_DLL_EXPORT)
endif ()
endif ()

# needed for the language plugins
set_target_properties (${nlopt_lib} PROPERTIES POSITION_INDEPENDENT_CODE ON)

# for consistent floating-point operations across architectures
Expand All @@ -309,31 +306,36 @@ endif ()

install ( TARGETS ${nlopt_lib}
EXPORT NLoptLibraryDepends
RUNTIME DESTINATION ${RELATIVE_INSTALL_BIN_DIR}
LIBRARY DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
ARCHIVE DESTINATION ${RELATIVE_INSTALL_LIB_DIR}
RUNTIME DESTINATION ${NLOPT_INSTALL_BINDIR}
LIBRARY DESTINATION ${NLOPT_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${NLOPT_INSTALL_LIBDIR}
)

if (MSVC AND BUILD_SHARED_LIBS)
install (FILES $<TARGET_PDB_FILE:${nlopt_lib}> DESTINATION ${RELATIVE_INSTALL_BIN_DIR} CONFIGURATIONS Debug RelWithDebInfo COMPONENT Debug)
install (FILES $<TARGET_PDB_FILE:${nlopt_lib}> DESTINATION ${NLOPT_INSTALL_BINDIR} CONFIGURATIONS Debug RelWithDebInfo COMPONENT Debug)
endif ()

add_subdirectory (src/api)
# install man
if (UNIX)
install (FILES src/api/nlopt.3 src/api/nlopt_minimize.3 src/api/nlopt_minimize_constrained.3 DESTINATION ${NLOPT_INSTALL_MANDIR}/man3)
endif ()

if (NLOPT_PYTHON)
if (CMAKE_VERSION VERSION_LESS 3.24)
find_package (Python COMPONENTS Interpreter Development)
if (CMAKE_VERSION VERSION_LESS 3.26)
find_package (Python 3.6 COMPONENTS Interpreter Development)
else ()
find_package (Python COMPONENTS Interpreter Development.Module)
find_package (Python 3.6 COMPONENTS Interpreter Development.Module Development.SABIModule)
endif ()

find_package (NumPy)
if (Python_FOUND)
find_package (NumPy)
endif ()
endif ()

if (WIN32)
set (INSTALL_PYTHON_DIR Lib/site-packages CACHE STRING "site packages dir")
else ()
set (INSTALL_PYTHON_DIR ${INSTALL_LIB_DIR}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages CACHE STRING "site packages dir")
set (INSTALL_PYTHON_DIR ${NLOPT_INSTALL_LIBDIR}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages CACHE STRING "site packages dir")
endif ()

if (NLOPT_GUILE)
Expand Down Expand Up @@ -385,24 +387,17 @@ export (TARGETS ${nlopt_lib} NAMESPACE NLopt:: FILE ${PROJECT_BINARY_DIR}/NLoptL
# Install the export set for use with the install-tree
install(EXPORT NLoptLibraryDepends
NAMESPACE NLopt::
DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
DESTINATION ${NLOPT_INSTALL_CMAKEDIR}
COMPONENT Development)

# Create a NLOPTConfig.cmake file for the use from the install tree
# and install it
set (NLOPT_LIBRARIES "NLopt::${nlopt_lib}")

set (NLOPT_CMAKE_DIR "${INSTALL_CMAKE_DIR}")
file (RELATIVE_PATH rel_include_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_INCLUDE_DIR}")
list (APPEND RELATIVE_NLOPT_INCLUDE_DIRS ${rel_include_dir})

file (RELATIVE_PATH rel_lib_dir "${NLOPT_CMAKE_DIR}" "${INSTALL_LIB_DIR}")
list (APPEND RELATIVE_NLOPT_LIB_DIR ${rel_lib_dir})

configure_file (cmake/NLoptConfig.cmake.in NLoptConfig.cmake @ONLY)
configure_file (cmake/NLoptConfigVersion.cmake.in NLoptConfigVersion.cmake @ONLY)
install (FILES
${CMAKE_CURRENT_BINARY_DIR}/NLoptConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/NLoptConfigVersion.cmake
DESTINATION ${RELATIVE_INSTALL_CMAKE_DIR}
DESTINATION ${NLOPT_INSTALL_CMAKEDIR}
COMPONENT Development)
4 changes: 2 additions & 2 deletions cmake/NLoptConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

set (NLOPT_VERSION "@NLOPT_VERSION_STRING@")

set (NLOPT_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/@RELATIVE_NLOPT_INCLUDE_DIRS@")
set (NLOPT_LIBRARY_DIRS "${CMAKE_CURRENT_LIST_DIR}/@RELATIVE_NLOPT_LIB_DIR@")
set (NLOPT_INCLUDE_DIRS "@CMAKE_INSTALL_PREFIX@/@NLOPT_INSTALL_INCLUDEDIR@")
set (NLOPT_LIBRARY_DIRS "@CMAKE_INSTALL_PREFIX@/@NLOPT_INSTALL_LIBDIR@")

# Allows loading NLOPT settings from another project
set (NLOPT_CONFIG_FILE "${CMAKE_CURRENT_LIST_FILE}")
Expand Down
2 changes: 1 addition & 1 deletion nlopt.pc.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
libdir=${exec_prefix}/@RELATIVE_INSTALL_LIB_DIR@
libdir=${exec_prefix}/@NLOPT_INSTALL_LIBDIR@
includedir=${prefix}/include

Name: NLopt
Expand Down
5 changes: 0 additions & 5 deletions src/api/CMakeLists.txt

This file was deleted.

12 changes: 4 additions & 8 deletions src/octave/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@ if (Matlab_FOUND AND Matlab_MX_LIBRARY)
set_target_properties (nlopt_optimize-mex PROPERTIES LINKER_LANGUAGE CXX)
endif ()

if (NOT DEFINED INSTALL_MEX_DIR)
set (INSTALL_MEX_DIR ${INSTALL_LIB_DIR}/matlab)
endif ()
install (TARGETS nlopt_optimize-mex DESTINATION ${INSTALL_MEX_DIR})
set (NLOPT_INSTALL_MEXDIR ${NLOPT_INSTALL_LIBDIR}/matlab CACHE STRING "mex dir")
install (TARGETS nlopt_optimize-mex DESTINATION ${NLOPT_INSTALL_MEXDIR})

if (NOT DEFINED INSTALL_M_DIR)
set (INSTALL_M_DIR ${INSTALL_MEX_DIR})
endif ()
install (FILES ${M_DATA} DESTINATION ${INSTALL_M_DIR})
set (NLOPT_INSTALL_MDIR ${NLOPT_INSTALL_LIBDIR}/matlab CACHE STRING "m dir")
install (FILES ${M_DATA} DESTINATION ${NLOPT_INSTALL_MDIR})
endif ()


Expand Down

0 comments on commit d2a1c44

Please sign in to comment.