Skip to content

Commit

Permalink
format cmake files
Browse files Browse the repository at this point in the history
  • Loading branch information
harrypuuter committed Apr 8, 2024
1 parent f060a2e commit 5cae00b
Show file tree
Hide file tree
Showing 11 changed files with 425 additions and 280 deletions.
24 changes: 15 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(ParseArguments)

# Set the default install directory to the build directory
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR} CACHE STRING "Set default install prefix to the build directory")
set(CMAKE_INSTALL_PREFIX
${CMAKE_BINARY_DIR}
CACHE STRING "Set default install prefix to the build directory")
message(STATUS "Creating Project")
# Create the project
project(CROWN CXX)


# Print settings of the executable
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UPPER)
message(STATUS "")
message(STATUS "The executable is configured with following flags:")
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS " C++ compiler: ${CMAKE_CXX_COMPILER}")
message(STATUS " Base compiler flags: ${CMAKE_CXX_FLAGS}")
message(STATUS " Compiler flags from build type: ${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}")
message(
STATUS
" Compiler flags from build type: ${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}"
)
message(STATUS "")

message(STATUS "Finding Packages")
Expand All @@ -35,9 +39,12 @@ include(AddOnnxruntime)
include(ConfigurePython)

# installdir
if (NOT DEFINED INSTALLDIR)
message(STATUS "No -DINSTALLDIR specified, using default: ${CMAKE_CURRENT_BINARY_DIR}/bin")
set(INSTALLDIR ${CMAKE_CURRENT_BINARY_DIR}/bin)
if(NOT DEFINED INSTALLDIR)
message(
STATUS
"No -DINSTALLDIR specified, using default: ${CMAKE_CURRENT_BINARY_DIR}/bin"
)
set(INSTALLDIR ${CMAKE_CURRENT_BINARY_DIR}/bin)
endif()
set(GENERATE_CPP_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

Expand All @@ -50,7 +57,6 @@ include(CodeGeneration)
# run the build
include(Build)

# tests
# Include tests
# tests Include tests
enable_testing()
add_subdirectory(tests)
add_subdirectory(tests)
2 changes: 1 addition & 1 deletion cmake/AddBaseDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ find_package(MPI)
# add nlohmann json
find_package(nlohmann_json)
find_package(Threads)
find_package(ZLIB)
find_package(ZLIB)
53 changes: 29 additions & 24 deletions cmake/AddCorrectionlib.cmake
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
function(install_correctionlib)
execute_process(COMMAND "${Python_EXECUTABLE}" "-c" "import correctionlib; print(correctionlib.__version__)"
RESULT_VARIABLE PACKAGE_NOT_FOUND
OUTPUT_VARIABLE PACKAGE_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(${PACKAGE_NOT_FOUND} EQUAL 1)
execute_process(COMMAND ${Python_EXECUTABLE} -m pip install ${_pip_args} git+https://github.com/cms-nanoAOD/correctionlib.git)
endif()
message(STATUS "Found correctionlib !")
execute_process(
COMMAND "${Python_EXECUTABLE}" "-c"
"import correctionlib; print(correctionlib.__version__)"
RESULT_VARIABLE PACKAGE_NOT_FOUND
OUTPUT_VARIABLE PACKAGE_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(${PACKAGE_NOT_FOUND} EQUAL 1)
execute_process(
COMMAND ${Python_EXECUTABLE} -m pip install ${_pip_args}
git+https://github.com/cms-nanoAOD/correctionlib.git)
endif()
message(STATUS "Found correctionlib !")
endfunction()

# Adding correctionlib for scale factor evaluation
# for now the official pip package has some problem
# in the future "find_python_package(correctionlib correctionlib X.X)" should hopefully work
# Adding correctionlib for scale factor evaluation for now the official pip
# package has some problem in the future "find_python_package(correctionlib
# correctionlib X.X)" should hopefully work
install_correctionlib()
message(STATUS "Setting up correctionlib ...")
execute_process(COMMAND correction config --cmake
OUTPUT_VARIABLE CORRECTION_LIB_ARGS
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(
COMMAND correction config --cmake
OUTPUT_VARIABLE CORRECTION_LIB_ARGS
OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REPLACE -Dcorrectionlib_DIR= "" CORRECTIONLIBPATH ${CORRECTION_LIB_ARGS})
# if correctionlib comes from cvmfs, change the correctionlibpath accordingly
if (${CORRECTIONLIBPATH} MATCHES "^/cvmfs/")
message(STATUS "Setting up correctionlib from cvmfs ...")
set(USING_CVMFS TRUE)
find_package(correctionlib)
find_library(CORRECTION_LIB_PATH correctionlib)
if(${CORRECTIONLIBPATH} MATCHES "^/cvmfs/")
message(STATUS "Setting up correctionlib from cvmfs ...")
set(USING_CVMFS TRUE)
find_package(correctionlib)
find_library(CORRECTION_LIB_PATH correctionlib)
else()
message(STATUS "Setting up correctionlib from local setup ...")
set(USING_CVMFS FALSE)
find_package(correctionlib REQUIRED PATHS ${CORRECTIONLIBPATH})
set(CORRECTION_LIB_PATH "${CORRECTIONLIBPATH}/../lib/libcorrectionlib.so")
message(STATUS "Setting up correctionlib from local setup ...")
set(USING_CVMFS FALSE)
find_package(correctionlib REQUIRED PATHS ${CORRECTIONLIBPATH})
set(CORRECTION_LIB_PATH "${CORRECTIONLIBPATH}/../lib/libcorrectionlib.so")
endif()
set(THREADS_PREFER_PTHREAD_FLAG ON)

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
message(STATUS "Correctionlib library path: ${CORRECTION_LIB_PATH}")
message(STATUS "Correctionlib library path: ${CORRECTION_LIB_PATH}")
46 changes: 27 additions & 19 deletions cmake/AddLogging.cmake
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
message(STATUS "Including spdlog.")
# Build the logging library
include(ExternalProject)
ExternalProject_Add(spdlog
PREFIX spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog.git
GIT_SHALLOW 1
GIT_TAG v1.8.5
CMAKE_ARGS -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}
-DCMAKE_CXX_FLAGS=-fpic
LOG_DOWNLOAD 1 LOG_CONFIGURE 1 LOG_BUILD 1 LOG_INSTALL 1
BUILD_BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/lib64/libspdlog.a
BUILD_BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/lib/libspdlog.a
)
ExternalProject_Add(
spdlog
PREFIX spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog.git
GIT_SHALLOW 1
GIT_TAG v1.8.5
CMAKE_ARGS -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR} -DCMAKE_CXX_FLAGS=-fpic
LOG_DOWNLOAD 1
LOG_CONFIGURE 1
LOG_BUILD 1
LOG_INSTALL 1
BUILD_BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/lib64/libspdlog.a
BUILD_BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/lib/libspdlog.a)

message(STATUS "Configuring spdlog.")
# Make an imported target out of the build logging library
add_library(logging STATIC IMPORTED)
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include") # required because the include dir must be existent for INTERFACE_INCLUDE_DIRECTORIES
include(GNUInstallDirs) # required to populate CMAKE_INSTALL_LIBDIR with lib or lib64 required for the destination of libspdlog.a
set_target_properties(logging PROPERTIES
IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libspdlog.a"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_BINARY_DIR}/include")
add_dependencies(logging spdlog) # enforces to build spdlog before making the imported target
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include"
)# required because the include dir must be existent for
# INTERFACE_INCLUDE_DIRECTORIES
include(GNUInstallDirs) # required to populate CMAKE_INSTALL_LIBDIR with lib or
# lib64 required for the destination of libspdlog.a
set_target_properties(
logging
PROPERTIES IMPORTED_LOCATION
"${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libspdlog.a"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_BINARY_DIR}/include")
add_dependencies(logging spdlog) # enforces to build spdlog before making the
# imported target
40 changes: 21 additions & 19 deletions cmake/AddOnnxruntime.cmake
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
# Find ONNXRuntime
# first check if we have an LCG stack via LCG_VERSION environment variable
if (DEFINED ENV{LCG_VERSION})
string(REPLACE ":" ";" RUNTIME_PATH "$ENV{LD_LIBRARY_PATH}")
message(STATUS "Found LCG stack, using it to find ONNXRuntime")
find_library(ONNX_RUNTIME_LIB_PATH
NAMES onnxruntime
HINTS ${RUNTIME_PATH}
)
if (ONNX_RUNTIME_LIB_PATH)
# get the real path of the library to find the include directory
get_filename_component(ONNX_RUNTIME_LIB_PATH ${ONNX_RUNTIME_LIB_PATH} REALPATH)
get_filename_component(ONNX_RUNTIME_INCLUDE_PATH ${ONNX_RUNTIME_LIB_PATH}/../../include REALPATH)
message(STATUS "ONNXRuntime include path: ${ONNX_RUNTIME_INCLUDE_PATH}")
include_directories("${ONNX_RUNTIME_INCLUDE_PATH}/core/session")
endif()
# Find ONNXRuntime first check if we have an LCG stack via LCG_VERSION
# environment variable
if(DEFINED ENV{LCG_VERSION})
string(REPLACE ":" ";" RUNTIME_PATH "$ENV{LD_LIBRARY_PATH}")
message(STATUS "Found LCG stack, using it to find ONNXRuntime")
find_library(
ONNX_RUNTIME_LIB_PATH
NAMES onnxruntime
HINTS ${RUNTIME_PATH})
if(ONNX_RUNTIME_LIB_PATH)
# get the real path of the library to find the include directory
get_filename_component(ONNX_RUNTIME_LIB_PATH ${ONNX_RUNTIME_LIB_PATH}
REALPATH)
get_filename_component(ONNX_RUNTIME_INCLUDE_PATH
${ONNX_RUNTIME_LIB_PATH}/../../include REALPATH)
message(STATUS "ONNXRuntime include path: ${ONNX_RUNTIME_INCLUDE_PATH}")
include_directories("${ONNX_RUNTIME_INCLUDE_PATH}/core/session")
endif()

message(STATUS "ONNXRuntime library path: ${ONNX_RUNTIME_LIB_PATH}")
message(STATUS "ONNXRuntime library path: ${ONNX_RUNTIME_LIB_PATH}")
else()
message(STATUS "No LCG stack found, not adding ONNXRuntime")
endif()
message(STATUS "No LCG stack found, not adding ONNXRuntime")
endif()
49 changes: 27 additions & 22 deletions cmake/AddRoot.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
find_package(ROOT 6.26 REQUIRED COMPONENTS ROOTVecOps ROOTDataFrame RooFit GenVector)
find_package(ROOT 6.26 REQUIRED COMPONENTS ROOTVecOps ROOTDataFrame RooFit
GenVector)

message(STATUS "")
message(STATUS "Found ROOT with following settings:")
Expand All @@ -10,29 +11,33 @@ message(STATUS "")

# Add ROOT flags to compile options, e.g. we have to use the same C++ standard
# Note that the flags from the build type, e.g. CMAKE_CXX_FLAGS_RELEASE, are
# automatically appended. You can check this during build time by enabling
# the verbose make output with "VERBOSE=1 make".
# automatically appended. You can check this during build time by enabling the
# verbose make output with "VERBOSE=1 make".
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ROOT_CXX_FLAGS}")

# Use -fconcepts with g++ to silence following warning:
# warning: use of 'auto' in parameter declaration only available with '-fconcepts
if (CMAKE_CXX_COMPILER_ID STREQUAL GNU)
message(STATUS "Attach -fconcepts to the compiler flags to silence warnings.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fconcepts")
# Use -fconcepts with g++ to silence following warning: warning: use of 'auto'
# in parameter declaration only available with '-fconcepts
if(CMAKE_CXX_COMPILER_ID STREQUAL GNU)
message(STATUS "Attach -fconcepts to the compiler flags to silence warnings.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fconcepts")
endif()

# Find the C++ standard from ROOT and set it as the standard of this project
# We require the C++ standard 17 or 20 and don't want to fall back to lower versions.
# Find the C++ standard from ROOT and set it as the standard of this project We
# require the C++ standard 17 or 20 and don't want to fall back to lower
# versions.
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if (${ROOT_CXX_FLAGS} MATCHES "\\-std\\=c\\+\\+17")
message(STATUS "Set c++17 as the C++ standard.")
set(CMAKE_CXX_STANDARD 17)
elseif (${ROOT_CXX_FLAGS} MATCHES "\\-std\\=c\\+\\+20")
message(STATUS "Set c++20 as the C++ standard.")
set(CMAKE_CXX_STANDARD 20)
elseif (${ROOT_CXX_FLAGS} MATCHES "\\-std\\=c\\+\\+14")
message(STATUS "c++14 found, setting c++17 as the C++ standard.")
set(CMAKE_CXX_STANDARD 17)
else ()
message(FATAL_ERROR "The standard c++17 or higher is required but not found in the ROOT flags: ${ROOT_CXX_FLAGS}")
endif()
if(${ROOT_CXX_FLAGS} MATCHES "\\-std\\=c\\+\\+17")
message(STATUS "Set c++17 as the C++ standard.")
set(CMAKE_CXX_STANDARD 17)
elseif(${ROOT_CXX_FLAGS} MATCHES "\\-std\\=c\\+\\+20")
message(STATUS "Set c++20 as the C++ standard.")
set(CMAKE_CXX_STANDARD 20)
elseif(${ROOT_CXX_FLAGS} MATCHES "\\-std\\=c\\+\\+14")
message(STATUS "c++14 found, setting c++17 as the C++ standard.")
set(CMAKE_CXX_STANDARD 17)
else()
message(
FATAL_ERROR
"The standard c++17 or higher is required but not found in the ROOT flags: ${ROOT_CXX_FLAGS}"
)
endif()
Loading

0 comments on commit 5cae00b

Please sign in to comment.