Skip to content

Commit eeae796

Browse files
Benedikt Volkelsawenzel
authored andcommitted
Upgrade CMake
Add CMake config and targets to easier build other projects depending on the MCStepLogger. Use aliased/namespace library names ROOT:: when linking VMC targets. This avoids hard-coded library paths in the CMake targets. Add libraries used to build to CMake target file making it properties of the MCStepLogger targets along with its include path. This makes it straightforward to be used in other packages. Introduce namespace MCStepLogger:: for targets.
1 parent de53028 commit eeae796

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

CMakeLists.txt

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ set(MODULE_NAME "MCStepLogger")
77
CMAKE_MINIMUM_REQUIRED(VERSION 3.11.0 FATAL_ERROR)
88

99
project(${MODULE_NAME})
10-
10+
set(CMAKE_MODULE_PATH
11+
${CMAKE_MODULE_PATH}
12+
${CMAKE_SOURCE_DIR}/cmake)
1113

1214
# Install directories
1315
SET(INSTALL_BIN_DIR ${CMAKE_INSTALL_PREFIX}/bin)
1416
SET(INSTALL_MACRO_DIR ${CMAKE_INSTALL_PREFIX}/macro)
1517
SET(INSTALL_INC_DIR ${CMAKE_INSTALL_PREFIX}/include/${MODULE_NAME})
1618
SET(INSTALL_LIB_DIR ${CMAKE_INSTALL_PREFIX}/lib)
19+
SET(INSTALL_CMAKE_DIR ${CMAKE_INSTALL_PREFIX}/cmake)
1720

1821
# Source directories
1922
SET(IMP_SRC_DIR ${CMAKE_SOURCE_DIR}/src)
@@ -69,17 +72,13 @@ set(MACROS
6972
# Find ROOT and get useful functions from ROOT_USE_FILE,
7073
# e.g. ROOT_GENERATE_DICTIONARY
7174
list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})
72-
find_package(ROOT REQUIRED)
75+
find_package(ROOT REQUIRED COMPONENTS Core Hist Graf Gpad Tree VMC RIO Geom EG)
7376
include(${ROOT_USE_FILE})
74-
# Find ROOT headers
75-
include_directories(${ROOT_INCLUDE_DIRS})
7677

7778
#########
7879
# Boost #
7980
#########
80-
#find_package(Boost COMPONENTS container thread system timer program_options random filesystem chrono exception regex serialization log log_setup unit_test_framework date_time signals iostreams REQUIRED)
8181
find_package(Boost COMPONENTS program_options chrono unit_test_framework REQUIRED)
82-
include_directories(${Boost_INCLUDE_DIR})
8382

8483
# Generate ROOT dictionary
8584
SET(ROOT_DICT_LINKDEF_FILE ${IMP_SRC_DIR}/MCStepLoggerLinkDef.h)
@@ -92,24 +91,57 @@ SET(ROOT_DICT_LIB_FILES
9291
"${PROJECT_BINARY_DIR}/lib${MODULE_NAME}.rootmap"
9392
)
9493

94+
###############################
95+
# Determine CXX STD from ROOT #
96+
###############################
97+
SET(CMAKE_CXX_STANDARD 11)
98+
# Find ROOT CXX standard
99+
string(FIND ${ROOT_CXX_FLAGS} "-std=" POSITION)
100+
if (${POSITION} GREATER -1)
101+
string(SUBSTRING ${ROOT_CXX_FLAGS} ${POSITION} 11 ROOT_CXX_STD)
102+
if(${ROOT_CXX_STD} STREQUAL "-std=c++1z " OR ${ROOT_CXX_STD} STREQUAL "-std=c++17 ")
103+
SET(CMAKE_CXX_STANDARD 17)
104+
elseif(${ROOT_CXX_STD} STREQUAL "-std=c++1y " OR ${ROOT_CXX_STD} STREQUAL "-std=c++14 ")
105+
SET(CMAKE_CXX_STANDARD 14)
106+
endif()
107+
endif()
108+
message(STATUS "Build with CXX STD ${CMAKE_CXX_STANDARD}")
109+
95110
# Build a library from the sources specified above together with generated ROOT
96111
# dictionary
97112
add_library(${MODULE_NAME} SHARED ${SRCS} "${ROOT_DICT_NAME}.cxx" ${HEADERS})
98113

99114
# Link together with ROOT libs
100-
target_link_libraries(${MODULE_NAME} -lCore -lHist -lGraf -lGpad -lTree -lVMC -lRIO -lGeom -lEG)
101-
102-
# Add the executable to do analysis with the MCStepLogger output files
103-
add_executable(${EXECUTABLE_NAME} ${EXE_SRCS})
104-
target_link_libraries(${EXECUTABLE_NAME} ${MODULE_NAME} ${Boost_LIBRARIES})
115+
set(LIB_DEPS ROOT::Core ROOT::Hist ROOT::Graf ROOT::Gpad ROOT::Tree ROOT::VMC ROOT::RIO ROOT::Geom ROOT::EG)
116+
target_link_libraries(${MODULE_NAME} ${LIB_DEPS})
117+
set_target_properties(${MODULE_NAME} PROPERTIES INTERFACE_LINK_LIBRARIES "${LIB_DEPS}")
118+
target_include_directories(${MODULE_NAME} INTERFACE $<INSTALL_INTERFACE:include>)
105119

106120
# Install headers
107121
install(FILES ${HEADERS} DESTINATION ${INSTALL_INC_DIR})
108-
# Install libraries
109-
install(TARGETS ${MODULE_NAME} DESTINATION ${INSTALL_LIB_DIR})
122+
123+
# Install libraries (and add to export such that we can easily find its dependencies later)
124+
install(TARGETS ${MODULE_NAME} DESTINATION ${INSTALL_LIB_DIR} EXPORT ${CMAKE_PROJECT_NAME}Exports)
125+
110126
# Install the ROOT dictionary files
111127
install(FILES ${ROOT_DICT_LIB_FILES} DESTINATION ${INSTALL_LIB_DIR})
112-
# Install executables
113-
install(TARGETS ${EXECUTABLE_NAME} DESTINATION ${INSTALL_BIN_DIR})
128+
129+
# Install executables (and add to export such that we can easily find its dependencies later)
130+
add_executable(${EXECUTABLE_NAME} ${EXE_SRCS})
131+
set(EXE_DEPS ${MODULE_NAME} Boost::program_options Boost::chrono Boost::unit_test_framework)
132+
target_link_libraries(${EXECUTABLE_NAME} ${EXE_DEPS})
133+
install(TARGETS ${EXECUTABLE_NAME} DESTINATION ${INSTALL_BIN_DIR} EXPORT ${CMAKE_PROJECT_NAME}Exports)
134+
114135
# Install macros
115136
install(FILES ${MACROS} DESTINATION ${INSTALL_MACRO_DIR})
137+
138+
# Install exports to find targets and dependencies later
139+
install(EXPORT ${CMAKE_PROJECT_NAME}Exports NAMESPACE MCStepLogger:: FILE MCStepLoggerTargets.cmake DESTINATION ${INSTALL_CMAKE_DIR})
140+
141+
# Configure and install the config to be used by CMake of depending package
142+
configure_file(
143+
"${PROJECT_SOURCE_DIR}/cmake/MCStepLoggerConfig.cmake.in"
144+
"${PROJECT_BINARY_DIR}/MCStepLoggerConfig.cmake" @ONLY)
145+
install(FILES
146+
"${PROJECT_BINARY_DIR}/MCStepLoggerConfig.cmake"
147+
DESTINATION ${INSTALL_CMAKE_DIR})

cmake/MCStepLoggerConfig.cmake.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Compute installation prefix relative to this file
2+
get_filename_component(_prefix "${_dir}/../.." ABSOLUTE)
3+
4+
# Import targets
5+
include("${_dir}/MCStepLoggerTargets.cmake")

0 commit comments

Comments
 (0)