Skip to content
Snippets Groups Projects
Commit 3d098064 authored by Benjamin Morgan's avatar Benjamin Morgan
Browse files

Simplify build/use of builtin/system JSON

- Make builtin "imported" target depend on JSONExt external project.
  - Subprojects only then need to link to the nlohmann_json target
    whether builtin/external is used
- Use REQUIRED to find system nlohmann_json as modern installs will
  always provide a CMake config script.
parent 4b9cb47e
No related branches found
No related tags found
1 merge request!184Modernisation of CMake scripting to simplify build of FullSimLight
...@@ -20,9 +20,7 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) ...@@ -20,9 +20,7 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
"Framework finding behaviour on macOS" ) "Framework finding behaviour on macOS" )
# Set up how the project handle some of its dependenices. Either by picking them # Set up how the project handle some of its dependenices. Either by picking them
# up from the environment, or building them itself. # up from the environment, or building them itself.
if( NOT TARGET JSONExt ) include( SetupJSON )
include( SetupJSON )
endif()
# Set a flag to steer the of the subpackages # Set a flag to steer the of the subpackages
set( ${PROJECT_NAME}_INDIVIDUAL_BUILD ON ) set( ${PROJECT_NAME}_INDIVIDUAL_BUILD ON )
find_package (FullSimLight REQUIRED) find_package (FullSimLight REQUIRED)
...@@ -80,18 +78,7 @@ set(PROJECT_SOURCES ${sources} ${headers} ${uis} ) ...@@ -80,18 +78,7 @@ set(PROJECT_SOURCES ${sources} ${headers} ${uis} )
add_executable(fsl ${PROJECT_SOURCES} ) add_executable(fsl ${PROJECT_SOURCES} )
target_link_libraries(fsl PRIVATE Qt5::Widgets nlohmann_json::nlohmann_json)
target_link_libraries(fsl PRIVATE Qt5::Widgets)
if( GEOMODEL_USE_BUILTIN_JSON )
add_dependencies(fsl JSONExt )
endif()
if( nlohmann_json_FOUND )
target_link_libraries( fsl PRIVATE nlohmann_json::nlohmann_json )
endif()
# Install the executable. # Install the executable.
install( TARGETS fsl install( TARGETS fsl
......
...@@ -27,9 +27,7 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) ...@@ -27,9 +27,7 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
"Framework finding behaviour on macOS" ) "Framework finding behaviour on macOS" )
# Set up how the project handle some of its dependenices. Either by picking them # Set up how the project handle some of its dependenices. Either by picking them
# up from the environment, or building them itself. # up from the environment, or building them itself.
if( NOT TARGET JSONExt ) include( SetupJSON )
include( SetupJSON )
endif()
if( NOT TARGET XercesCBuiltIn ) if( NOT TARGET XercesCBuiltIn )
include( SetupXercesC ) include( SetupXercesC )
endif() endif()
...@@ -138,19 +136,6 @@ add_executable(gm2gdml geoModel2GDML.cc ${sources} ${headers}) ...@@ -138,19 +136,6 @@ add_executable(gm2gdml geoModel2GDML.cc ${sources} ${headers})
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Add extra 'include' directories # Add extra 'include' directories
# #
# If the in-house build of the nlohmann_json library is used, add explicit dependency
if( GEOMODEL_USE_BUILTIN_JSON )
add_dependencies( fullSimLight JSONExt )
add_dependencies( gmclash JSONExt )
add_dependencies( gmmasscalc JSONExt )
add_dependencies( fillHistogramExample JSONExt )
add_dependencies( gmgeantino JSONExt )
add_dependencies( gm2gdml JSONExt )
# Acquire Installation Directory of JSONExt
ExternalProject_Get_Property (JSONExt install_dir)
# Include the installed 'include' PATH
include_directories (${install_dir}/include)
endif()
# If the in-house build of the Xerces-C library is used, add explicit dependency: for Geant4 # If the in-house build of the Xerces-C library is used, add explicit dependency: for Geant4
if( GEOMODEL_USE_BUILTIN_XERCESC ) if( GEOMODEL_USE_BUILTIN_XERCESC )
add_dependencies( fullSimLight XercesCBuiltIn ) add_dependencies( fullSimLight XercesCBuiltIn )
...@@ -196,20 +181,11 @@ else() # if built as a part of GeoModel ...@@ -196,20 +181,11 @@ else() # if built as a part of GeoModel
endif() endif()
# targets that need 'nlohmann_json' # targets that need 'nlohmann_json'
# NOTE: We link to `nlohmann_json` only if we use a version of nlohmann_json target_link_libraries( gmclash PRIVATE nlohmann_json::nlohmann_json )
# that provides a CMake config file (i.e., either built from source, or also target_link_libraries( gmmasscalc PRIVATE nlohmann_json::nlohmann_json )
# installed with Homebrew on macOS). target_link_libraries( fullSimLight PRIVATE nlohmann_json::nlohmann_json )
# This is not needed if the single-header library is installed in a regular target_link_libraries( gmgeantino PRIVATE nlohmann_json::nlohmann_json )
# system include folder (e.g., '/usr/local/include', '/usr/include', ...) target_link_libraries( gm2gdml PRIVATE nlohmann_json::nlohmann_json )
if( nlohmann_json_FOUND )
target_link_libraries( gmclash PRIVATE nlohmann_json::nlohmann_json )
target_link_libraries( gmmasscalc PRIVATE nlohmann_json::nlohmann_json )
target_link_libraries( fullSimLight PRIVATE nlohmann_json::nlohmann_json )
target_link_libraries( gmgeantino PRIVATE nlohmann_json::nlohmann_json )
target_link_libraries( gm2gdml PRIVATE nlohmann_json::nlohmann_json )
else()
message(STATUS "WARNING - 'nlohmann_json' not found by CMake!! Anyway, if you installed the single header file in a standard system include dir, I will be able to use it.")
endif()
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Add profiling test targets # Add profiling test targets
......
...@@ -27,9 +27,7 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) ...@@ -27,9 +27,7 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
"Framework finding behaviour on macOS" ) "Framework finding behaviour on macOS" )
# Set up how the project handle some of its dependenices. Either by picking them # Set up how the project handle some of its dependenices. Either by picking them
# up from the environment, or building them itself. # up from the environment, or building them itself.
if( NOT TARGET JSONExt ) include( SetupJSON )
include( SetupJSON )
endif()
if( NOT TARGET XercesCBuiltIn ) if( NOT TARGET XercesCBuiltIn )
include( SetupXercesC ) include( SetupXercesC )
endif() endif()
......
...@@ -6,20 +6,7 @@ file( GLOB HEADERS GeoModelJSONParser/*.h ) ...@@ -6,20 +6,7 @@ file( GLOB HEADERS GeoModelJSONParser/*.h )
# Create the library. # Create the library.
add_library( GeoModelJSONParser SHARED ${HEADERS} ${SOURCES} ) add_library( GeoModelJSONParser SHARED ${HEADERS} ${SOURCES} )
target_link_libraries( GeoModelJSONParser PUBLIC nlohmann_json::nlohmann_json )
# If the in-house build of the nlohmann_json library is used, add explicit dependency
if( GEOMODEL_USE_BUILTIN_JSON )
add_dependencies( GeoModelJSONParser JSONExt )
endif()
# We link to `nlohmann_json` only if we use a version of nlohmann_json
# that provides a CMake config file (i.e., either built from source, or also
# installed with Homebrew on macOS).
# This is not needed if the single-header library is installed in a regular
# system include folder (e.g., '/usr/local/include', '/usr/include', ...)
if ( nlohmann_json_FOUND )
target_link_libraries( GeoModelJSONParser PUBLIC nlohmann_json::nlohmann_json )
endif()
target_include_directories( GeoModelJSONParser PUBLIC target_include_directories( GeoModelJSONParser PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
......
...@@ -21,9 +21,6 @@ set_target_properties( GXClashPointSystems PROPERTIES ...@@ -21,9 +21,6 @@ set_target_properties( GXClashPointSystems PROPERTIES
if( GEOMODEL_USE_BUILTIN_COIN3D ) if( GEOMODEL_USE_BUILTIN_COIN3D )
add_dependencies( GXClashPointSystems Coin3DBuiltIn ) add_dependencies( GXClashPointSystems Coin3DBuiltIn )
endif() endif()
if( GEOMODEL_USE_BUILTIN_JSON )
add_dependencies( GXClashPointSystems JSONExt )
endif()
# Set up an alias with the same name that you would get by "finding" a pre-built # Set up an alias with the same name that you would get by "finding" a pre-built
# version of the library. # version of the library.
......
...@@ -26,9 +26,6 @@ set_target_properties( GXGeometrySystems PROPERTIES ...@@ -26,9 +26,6 @@ set_target_properties( GXGeometrySystems PROPERTIES
if( GEOMODEL_USE_BUILTIN_COIN3D ) if( GEOMODEL_USE_BUILTIN_COIN3D )
add_dependencies( GXGeometrySystems Coin3DBuiltIn ) add_dependencies( GXGeometrySystems Coin3DBuiltIn )
endif() endif()
if( GEOMODEL_USE_BUILTIN_JSON )
add_dependencies( GXGeometrySystems JSONExt )
endif()
# Set up an alias with the same name that you would get by "finding" a pre-built # Set up an alias with the same name that you would get by "finding" a pre-built
# version of the library. # version of the library.
......
...@@ -6,75 +6,54 @@ ...@@ -6,75 +6,54 @@
# #
# Make sure that this file is only included once. # Make sure that this file is only included once.
get_property( _jsonSetUp GLOBAL PROPERTY GEOMODEL_JSON_SET_UP SET ) include_guard(GLOBAL)
if( _jsonSetUp )
unset( _jsonSetUp )
return()
endif()
set_property( GLOBAL PROPERTY GEOMODEL_JSON_SET_UP TRUE )
# Configuration option for how "nlohmann_json" should be used. # Configuration option for how "nlohmann_json" should be used.
option( GEOMODEL_USE_BUILTIN_JSON option(GEOMODEL_USE_BUILTIN_JSON "Download and compile a version of nlohmann_json during the build" OFF)
"Download and compile a version of nlohmann_json during the build" FALSE )
# Now do what was requested. # Now do what was requested.
if( GEOMODEL_USE_BUILTIN_JSON ) if(GEOMODEL_USE_BUILTIN_JSON)
# Tell the user what's happening. # Tell the user what's happening.
message( STATUS "${BoldMagenta}'GEOMODEL_USE_BUILTIN_JSON' was set to 'true' ==> Building nlohmann_json as part of the project${ColourReset}" ) message( STATUS "${BoldMagenta}'GEOMODEL_USE_BUILTIN_JSON' was set to 'true' ==> Building nlohmann_json as part of the project${ColourReset}" )
# The include directory and library that will be produced. # The include directory and library that will be produced.
set( nlohmann_json_INCLUDE_DIR set(nlohmann_json_INCLUDE_DIR "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/JSONInstall/${CMAKE_INSTALL_INCLUDEDIR}" )
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/JSONInstall/${CMAKE_INSTALL_INCLUDEDIR}" ) set(nlohmann_json_INCLUDE_DIRS "${nlohmann_json_INCLUDE_DIR}" )
set( nlohmann_json_INCLUDE_DIRS "${nlohmann_json_INCLUDE_DIR}" ) set(nlohmann_json_VERSION "3.6.1" )
set( nlohmann_json_VERSION "3.6.1" ) set(nlohmann_json_FOUND TRUE )
set( nlohmann_json_FOUND TRUE )
message(STATUS "Installing the built-in 'nlohmann_json' in: ${nlohmann_json_INCLUDE_DIR}") message(STATUS "Installing the built-in 'nlohmann_json' in: ${nlohmann_json_INCLUDE_DIR}")
# Create the include directory already, otherwise CMake refuses to # Create the include directory already, otherwise CMake refuses to
# create the imported target. # create the imported target.
file( MAKE_DIRECTORY "${nlohmann_json_INCLUDE_DIR}" ) file(MAKE_DIRECTORY "${nlohmann_json_INCLUDE_DIR}")
# Build/install nlohmann_json using ExternalProject_Add(...). # Build/install nlohmann_json using ExternalProject_Add(...).
include( ExternalProject ) include( ExternalProject )
ExternalProject_Add( JSONExt ExternalProject_Add(JSONExt
PREFIX ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/JSONBuild PREFIX ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/JSONBuild
INSTALL_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/JSONInstall INSTALL_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/JSONInstall
URL "https://cern.ch/lcgpackages/tarFiles/sources/json-${nlohmann_json_VERSION}.tar.gz" URL "https://cern.ch/lcgpackages/tarFiles/sources/json-${nlohmann_json_VERSION}.tar.gz"
URL_MD5 "c53592d55e7fec787cf0a406d36098a3" URL_MD5 "c53592d55e7fec787cf0a406d36098a3"
CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
-DCMAKE_CXX_STANDARD:STRING=${CMAKE_CXX_STANDARD} -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
-DJSON_BuildTests:BOOL=OFF -DJSON_MultipleHeaders:BOOL=ON -DCMAKE_CXX_STANDARD:STRING=${CMAKE_CXX_STANDARD}
BUILD_BYPRODUCTS "${nlohmann_json_INCLUDE_DIR}" ) -DJSON_BuildTests:BOOL=OFF
install( DIRECTORY -DJSON_MultipleHeaders:BOOL=ON
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/JSONInstall/ BUILD_BYPRODUCTS "${nlohmann_json_INCLUDE_DIR}" )
DESTINATION . install(DIRECTORY ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/JSONInstall/
COMPONENT Development DESTINATION .
USE_SOURCE_PERMISSIONS ) COMPONENT Development
USE_SOURCE_PERMISSIONS)
# Set up nlohmann_json's imported target. # Set up nlohmann_json's imported target.
add_library( nlohmann_json::nlohmann_json INTERFACE IMPORTED ) add_library(nlohmann_json::nlohmann_json INTERFACE IMPORTED)
set_target_properties( nlohmann_json::nlohmann_json PROPERTIES set_property(TARGET nlohmann_json::nlohmann_json PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${nlohmann_json_INCLUDE_DIR}")
INTERFACE_INCLUDE_DIRECTORIES "${nlohmann_json_INCLUDE_DIR}" )
# Auto handle dependency
add_dependencies(nlohmann_json::nlohmann_json JSONExt)
else() else()
# Find an existing installation of nlohmann_json. # Find an existing installation of nlohmann_json.
find_package( nlohmann_json QUIET ) find_package(nlohmann_json REQUIRED)
if( nlohmann_json_FOUND )
# If it was found, tell the user about it.
get_target_property( _incPaths nlohmann_json::nlohmann_json
INTERFACE_INCLUDE_DIRECTORIES )
list( GET _incPaths 0 _incPath )
message( STATUS "Found nlohmann_json: ${_incPath}" )
unset( _incPaths )
unset( _incPath )
else()
# If it was not found, it may still be that the necessary header is
# available on the build system. So just set up a dummy library that would
# allow the configuration to succeed.
message( WARNING "Creating a dummy nlohmann_json::nlohmann_json target "
"in case the headers are avalable in some public place..." )
add_library( nlohmann_json::nlohmann_json INTERFACE IMPORTED )
endif()
endif() endif()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment