Skip to content

Commit

Permalink
Use the Key4hepConfig flag to set the standard, compiler flags and
Browse files Browse the repository at this point in the history
rpath magic.
  • Loading branch information
jmcarcell committed Sep 8, 2024
1 parent b679e64 commit fdc2304
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 102 deletions.
39 changes: 3 additions & 36 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,14 @@ SET( ${PROJECT_NAME}_VERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME
find_package(podio 1.0 REQUIRED HINTS $ENV{PODIO})
find_package(ROOT REQUIRED COMPONENTS RIO Tree Physics)

include(cmake/Key4hepConfig.cmake)

#--- Define basic build settings -----------------------------------------------
# - Use GNU-style hierarchy for installing build products
include(GNUInstallDirs)

# - Define a default build type when using a single-mode tool like make/ninja
# If you're using a build tool that supports multiple modes (Visual Studio,
# Xcode), this setting has no effect.
# HSF recommend RelWithDebInfo (optimized with debugging symbols) as this is
# generally the mode used by system packaging (rpm, deb, spack, macports).
# However, it can be overriden by passing ``-DCMAKE_BUILD_TYPE=<type>`` when
# invoking CMake
if(NOT CMAKE_CONFIGURATION_TYPES)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RelWithDebInfo
CACHE STRING "Choose the type of build, options are: None Release MinSizeRel Debug RelWithDebInfo"
FORCE
)
else()
set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}"
CACHE STRING "Choose the type of build, options are: None Release MinSizeRel Debug RelWithDebInfo"
FORCE
)
endif()
endif()

# - Define the C++ Standard to use (Simplest Possible)
set(CMAKE_CXX_STANDARD 20 CACHE STRING "")

if(NOT CMAKE_CXX_STANDARD MATCHES "20|23")
message(FATAL_ERROR "Unsupported C++ standard: ${CMAKE_CXX_STANDARD}")
endif()

message (STATUS "C++ standard: ${CMAKE_CXX_STANDARD}")

option(EDM4HEP_SET_RPATH "Link libraries with built-in RPATH (run-time search path)" ON)
include(cmake/EDM4HEPBuild.cmake)
edm4hep_set_compiler_flags()
edm4hep_set_rpath()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDROP_CGAL")

edm4hep_set_linker_flags()

#--- Declare options -----------------------------------------------------------

Expand Down
70 changes: 4 additions & 66 deletions cmake/EDM4HEPBuild.cmake
Original file line number Diff line number Diff line change
@@ -1,75 +1,13 @@
#---------------------------------------------------------------------------------------------------
#---edm4hep_set_rpath
#---edm4hep_set_linker_flags
#
# Set optional rpath on linux and mandatory rpath on mac
# Set linker flags
#
#---------------------------------------------------------------------------------------------------
macro(edm4hep_set_rpath)
# When building, don't use the install RPATH already (but later on when installing)
set(CMAKE_SKIP_BUILD_RPATH FALSE) # don't skip the full RPATH for the build tree
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) # use always the build RPATH for the build tree
set(CMAKE_MACOSX_RPATH TRUE) # use RPATH for MacOSX
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) # point to directories outside the build tree to the install RPATH

option(USE_RUNPATH_INSTEAD_OF_RPATH "Use runpath instead of rpath (allow specifying liked libraries via LD_LIBRARY_PATH)" OFF)
if(USE_RUNPATH_INSTEAD_OF_RPATH)
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--enable-new-dtags")
endif()

# Check whether to add RPATH to the installation (the build tree always has the RPATH enabled)
if(APPLE)
set(CMAKE_INSTALL_NAME_DIR "@rpath")
set(CMAKE_INSTALL_RPATH "@loader_path/../lib") # self relative LIBDIR
# the RPATH to be used when installing, but only if it's not a system directory
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
if("${isSystemDir}" STREQUAL "-1")
set(CMAKE_INSTALL_RPATH "@loader_path/../lib")
endif("${isSystemDir}" STREQUAL "-1")
elseif(EDM4HEP_SET_RPATH)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${LIBDIR}") # install LIBDIR
# the RPATH to be used when installing, but only if it's not a system directory
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
if("${isSystemDir}" STREQUAL "-1")
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${LIBDIR}")
endif("${isSystemDir}" STREQUAL "-1")
else()
set(CMAKE_SKIP_INSTALL_RPATH TRUE) # skip the full RPATH for the install tree
endif()
endmacro(edm4hep_set_rpath)

#---------------------------------------------------------------------------------------------------
#---edm4hep_set_compiler_flags
#
# Set compiler and linker flags
#
#---------------------------------------------------------------------------------------------------

macro(edm4hep_set_compiler_flags)
macro(edm4hep_set_linker_flags)
include(CheckCXXCompilerFlag)

SET(COMPILER_FLAGS -fPIC -Wall -Wextra -Wpedantic -Wno-unused-variable -Wno-unused-parameter)

# AppleClang/Clang specific warning flags
if(CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$")
set ( COMPILER_FLAGS ${COMPILER_FLAGS} -Winconsistent-missing-override -Wheader-hygiene )
endif()

FOREACH( FLAG ${COMPILER_FLAGS} )
## need to replace the minus or plus signs from the variables, because it is passed
## as a macro to g++ which causes a warning about no whitespace after macro
## definition
STRING(REPLACE "-" "_" FLAG_WORD ${FLAG} )
STRING(REPLACE "+" "P" FLAG_WORD ${FLAG_WORD} )

CHECK_CXX_COMPILER_FLAG( "${FLAG}" CXX_FLAG_WORKS_${FLAG_WORD} )
IF( ${CXX_FLAG_WORKS_${FLAG_WORD}} )
message( STATUS "Adding ${FLAG} to CXX_FLAGS" )
SET ( CMAKE_CXX_FLAGS "${FLAG} ${CMAKE_CXX_FLAGS} ")
ELSE()
message( STATUS "NOT Adding ${FLAG} to CXX_FLAGS" )
ENDIF()
ENDFOREACH()

# resolve which linker we use
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -Wl,--version OUTPUT_VARIABLE stdout ERROR_QUIET)
if("${stdout}" MATCHES "GNU ")
Expand All @@ -94,4 +32,4 @@ macro(edm4hep_set_compiler_flags)
MESSAGE( WARNING "No known linker (GNU or Apple) has been detected, pass no flags to linker" )
endif()

endmacro(edm4hep_set_compiler_flags)
endmacro()

0 comments on commit fdc2304

Please sign in to comment.