Skip to content

Commit

Permalink
CMake: Set RPATHs on Installed Targets (#1105)
Browse files Browse the repository at this point in the history
* CMake: Clean Up C++ Std Control

* CMake: Add RPATH Handling

Handle RPATHs by default.
  • Loading branch information
ax3l authored Sep 16, 2021
1 parent 9ec5642 commit 5bf5bc7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 36 deletions.
86 changes: 50 additions & 36 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ openpmd_option(ADIOS2 "ADIOS2 backend (.bp files)" AUTO)
openpmd_option(PYTHON "Enable Python bindings" AUTO)

option(openPMD_INSTALL "Add installation targets" ON)
option(openPMD_INSTALL_RPATH "Add RPATHs to installed binaries" ON)
option(openPMD_HAVE_PKGCONFIG "Generate a .pc file for pkg-config" ON)
option(openPMD_USE_INTERNAL_VARIANT "Use internally shipped MPark.Variant" ON)
option(openPMD_USE_INTERNAL_CATCH "Use internally shipped Catch2" ON)
Expand Down Expand Up @@ -136,6 +137,18 @@ option(openPMD_BUILD_CLI_TOOLS "Build the command line tools" ${BUILD_CLI_TOOLS}
option(openPMD_BUILD_EXAMPLES "Build the examples" ${BUILD_EXAMPLES})


# Helper Functions ############################################################
#
# C++ standard: requirements for a target
function(openpmd_cxx_required target)
target_compile_features(${target} PUBLIC cxx_std_14)
set_target_properties(${target} PROPERTIES
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
)
endfunction()


# Dependencies ################################################################
#
# external library: MPI (optional)
Expand Down Expand Up @@ -445,12 +458,8 @@ add_library(openPMD ${_openpmd_lib_type} ${CORE_SOURCE} ${IO_SOURCE})
add_library(openPMD::openPMD ALIAS openPMD)

# properties
target_compile_features(openPMD
PUBLIC cxx_std_14
)
openpmd_cxx_required(openPMD)
set_target_properties(openPMD PROPERTIES
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
POSITION_INDEPENDENT_CODE ON
WINDOWS_EXPORT_ALL_SYMBOLS ON
)
Expand Down Expand Up @@ -527,12 +536,8 @@ endif()
if(openPMD_HAVE_ADIOS1)
add_library(openPMD.ADIOS1.Serial SHARED ${IO_ADIOS1_SEQUENTIAL_SOURCE})
add_library(openPMD.ADIOS1.Parallel SHARED ${IO_ADIOS1_SOURCE})
target_compile_features(openPMD.ADIOS1.Serial
PUBLIC cxx_std_14
)
target_compile_features(openPMD.ADIOS1.Parallel
PUBLIC cxx_std_14
)
openpmd_cxx_required(openPMD.ADIOS1.Serial)
openpmd_cxx_required(openPMD.ADIOS1.Parallel)
target_compile_options(openPMD.ADIOS1.Serial PUBLIC ${_msvc_options})
target_compile_options(openPMD.ADIOS1.Parallel PUBLIC ${_msvc_options})
target_link_libraries(openPMD.ADIOS1.Serial PUBLIC openPMD::thirdparty::mpark_variant)
Expand All @@ -551,8 +556,6 @@ if(openPMD_HAVE_ADIOS1)
endif()

set_target_properties(openPMD.ADIOS1.Serial PROPERTIES
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
POSITION_INDEPENDENT_CODE ON
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON
Expand All @@ -578,8 +581,6 @@ if(openPMD_HAVE_ADIOS1)

if(openPMD_HAVE_MPI)
set_target_properties(openPMD.ADIOS1.Parallel PROPERTIES
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
POSITION_INDEPENDENT_CODE ON
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN 1
Expand Down Expand Up @@ -800,11 +801,9 @@ if(openPMD_BUILD_TESTING)
test/CatchRunner.cpp) # Always MPI_Init with Serial Fallback
add_library(CatchMain ${_openpmd_lib_type}
test/CatchMain.cpp) # Serial only
target_compile_features(CatchRunner PUBLIC cxx_std_14)
target_compile_features(CatchMain PUBLIC cxx_std_14)
openpmd_cxx_required(CatchRunner)
openpmd_cxx_required(CatchMain)
set_target_properties(CatchRunner CatchMain PROPERTIES
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
POSITION_INDEPENDENT_CODE ON
WINDOWS_EXPORT_ALL_SYMBOLS ON
)
Expand All @@ -819,6 +818,7 @@ if(openPMD_BUILD_TESTING)

foreach(testname ${openPMD_TEST_NAMES})
add_executable(${testname}Tests test/${testname}Test.cpp)
openpmd_cxx_required(${testname}Tests)

if(openPMD_USE_INVASIVE_TESTS)
target_compile_definitions(${testname}Tests PRIVATE openPMD_USE_INVASIVE_TESTS=1)
Expand All @@ -829,22 +829,14 @@ if(openPMD_BUILD_TESTING)
else()
target_link_libraries(${testname}Tests PRIVATE CatchMain)
endif()

set_target_properties(${testname}Tests PROPERTIES
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
)
endforeach()
endif()

if(openPMD_BUILD_CLI_TOOLS)
foreach(toolname ${openPMD_CLI_TOOL_NAMES})
add_executable(openpmd-${toolname} src/cli/${toolname}.cpp)
openpmd_cxx_required(openpmd-${toolname})
target_link_libraries(openpmd-${toolname} PRIVATE openPMD)
set_target_properties(openpmd-${toolname} PROPERTIES
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
)
endforeach()
endif()

Expand All @@ -853,19 +845,13 @@ if(openPMD_BUILD_EXAMPLES)
if(${examplename} MATCHES ".+parallel$")
if(openPMD_HAVE_MPI)
add_executable(${examplename} examples/${examplename}.cpp)
openpmd_cxx_required(${examplename})
target_link_libraries(${examplename} PRIVATE openPMD)
set_target_properties(${examplename} PROPERTIES
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
)
endif()
else()
add_executable(${examplename} examples/${examplename}.cpp)
openpmd_cxx_required(${examplename})
target_link_libraries(${examplename} PRIVATE openPMD)
set_target_properties(${examplename} PROPERTIES
CXX_EXTENSIONS OFF
CXX_STANDARD_REQUIRED ON
)
endif()
endforeach()
endif()
Expand Down Expand Up @@ -978,6 +964,33 @@ if(openPMD_INSTALL)
endforeach()
endif()

if(openPMD_INSTALL_RPATH)
set(openPMD_INSTALL_RPATH_TARGET_NAMES ${openPMD_INSTALL_TARGET_NAMES})
if(openPMD_HAVE_PYTHON)
list(APPEND openPMD_INSTALL_RPATH_TARGET_NAMES openPMD.py)
endif()
if(NOT DEFINED CMAKE_INSTALL_RPATH)
if(APPLE)
set_target_properties(${openPMD_INSTALL_RPATH_TARGET_NAMES} PROPERTIES
INSTALL_RPATH "@loader_path"
)
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
set_target_properties(${openPMD_INSTALL_RPATH_TARGET_NAMES} PROPERTIES
INSTALL_RPATH "$ORIGIN"
)
endif()
# Windows: has no RPath concept, all interdependent `.dll`s must be in
# %PATH% or in the same dir as the calling executable
endif()

if(NOT DEFINED CMAKE_INSTALL_RPATH_USE_LINK_PATH)
# those are appended AFTER the paths in INSTALL_RPATH
set_target_properties(${openPMD_INSTALL_RPATH_TARGET_NAMES} PROPERTIES
INSTALL_RPATH_USE_LINK_PATH ON
)
endif()
endif()

install(TARGETS ${openPMD_INSTALL_TARGET_NAMES}
EXPORT openPMDTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand Down Expand Up @@ -1370,6 +1383,7 @@ message(" C++ Compiler: ${CMAKE_CXX_COMPILER_ID} "
message(" ${CMAKE_CXX_COMPILER}")
message("")
if(openPMD_INSTALL)
message(" Install with RPATHs: ${openPMD_INSTALL_RPATH}")
message(" Installation prefix: ${CMAKE_INSTALL_PREFIX}")
message(" bin: ${CMAKE_INSTALL_BINDIR}")
message(" lib: ${CMAKE_INSTALL_LIBDIR}")
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ CMake controls options with prefixed `-D`, e.g. `-DopenPMD_USE_MPI=OFF`:
| `openPMD_USE_INVASIVE_TESTS` | ON/**OFF** | Enable unit tests that modify source code <sup>1</sup> |
| `openPMD_USE_VERIFY` | **ON**/OFF | Enable internal VERIFY (assert) macro independent of build type <sup>2</sup> |
| `openPMD_INSTALL` | **ON**/OFF | Add installation targets |
| `openPMD_INSTALL_RPATH` | **ON**/OFF | Add RPATHs to installed binaries |
| `Python_EXECUTABLE` | (newest found) | Path to Python executable |

<sup>1</sup> *e.g. changes C++ visibility keywords, breaks MSVC*
Expand Down
1 change: 1 addition & 0 deletions docs/source/dev/buildoptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ CMake Option Values Description
``openPMD_USE_INVASIVE_TESTS`` ON/**OFF** Enable unit tests that modify source code :sup:`1`
``openPMD_USE_VERIFY`` **ON**/OFF Enable internal VERIFY (assert) macro independent of build type :sup:`2`
``openPMD_INSTALL`` **ON**/OFF Add installation targets
``openPMD_INSTALL_RPATH`` **ON**/OFF Add RPATHs to installed binaries
``Python_EXECUTABLE`` (newest found) Path to Python executable
============================== =============== ========================================================================

Expand Down

0 comments on commit 5bf5bc7

Please sign in to comment.