From cdc755cfc59e7a106618cf917e879353151f0e1a Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Wed, 19 Apr 2023 18:17:06 -0400 Subject: [PATCH] COMP: Fix initialization of build-type to support multi-config CMake generator This commit introduces the module "dcm2niixInitializeBuildType" It allows to consistently manage the initialization and setting of build type CMake variables. It sets the variable EXTERNAL_PROJECT_BUILD_TYPE_CMAKE_ARGS based on the CMake generator being used: * If a multi-config generator (e.g Visual Studio) is used, it sets the variable with CMAKE_CONFIGURATION_TYPES. * If a single-config generator (e.g Unix Makefiles) is used, it sets the variable with CMAKE_BUILD_TYPE. Adapted from https://github.com/Slicer/Slicer/blob/5.2/CMake/SlicerInitializeBuildType.cmake --- SuperBuild/External-CLOUDFLARE-ZLIB.cmake | 2 +- SuperBuild/External-OPENJPEG.cmake | 2 +- SuperBuild/External-YAML-CPP.cmake | 2 +- SuperBuild/SuperBuild.cmake | 10 ++--- cmake/dcm2niixInitializeBuildType.cmake | 47 +++++++++++++++++++++++ 5 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 cmake/dcm2niixInitializeBuildType.cmake diff --git a/SuperBuild/External-CLOUDFLARE-ZLIB.cmake b/SuperBuild/External-CLOUDFLARE-ZLIB.cmake index 44d7c93c..4c6a6acb 100644 --- a/SuperBuild/External-CLOUDFLARE-ZLIB.cmake +++ b/SuperBuild/External-CLOUDFLARE-ZLIB.cmake @@ -7,8 +7,8 @@ ExternalProject_Add(zlib BINARY_DIR cloudflare-zlib-build CMAKE_ARGS -Wno-dev + ${EXTERNAL_PROJECT_BUILD_TYPE_CMAKE_ARGS} ${OSX_ARCHITECTURES} - -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} # Compiler settings -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER} diff --git a/SuperBuild/External-OPENJPEG.cmake b/SuperBuild/External-OPENJPEG.cmake index 6d3afcb3..1d99ac28 100644 --- a/SuperBuild/External-OPENJPEG.cmake +++ b/SuperBuild/External-OPENJPEG.cmake @@ -8,8 +8,8 @@ ExternalProject_Add(openjpeg CMAKE_ARGS -Wno-dev --no-warn-unused-cli + ${EXTERNAL_PROJECT_BUILD_TYPE_CMAKE_ARGS} ${OSX_ARCHITECTURES} - -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} # Compiler settings -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER} # Not used -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER} diff --git a/SuperBuild/External-YAML-CPP.cmake b/SuperBuild/External-YAML-CPP.cmake index 5febe95b..ed85ca76 100644 --- a/SuperBuild/External-YAML-CPP.cmake +++ b/SuperBuild/External-YAML-CPP.cmake @@ -8,8 +8,8 @@ ExternalProject_Add(yaml-cpp CMAKE_ARGS -Wno-dev --no-warn-unused-cli + ${EXTERNAL_PROJECT_BUILD_TYPE_CMAKE_ARGS} ${OSX_ARCHITECTURES} - -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} # Compiler settings -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER} diff --git a/SuperBuild/SuperBuild.cmake b/SuperBuild/SuperBuild.cmake index d093bc2f..98f20c6c 100644 --- a/SuperBuild/SuperBuild.cmake +++ b/SuperBuild/SuperBuild.cmake @@ -4,12 +4,8 @@ if(NOT GIT_FOUND) message(FATAL_ERROR "Cannot find Git. Git is required for Superbuild") endif() -# Basic CMake build settings -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE "Release" CACHE STRING - "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE) - set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug;Release;RelWithDebInfo;MinSizeRel") -endif() +include(${CMAKE_SOURCE_DIR}/cmake/dcm2niixInitializeBuildType.cmake) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) option(USE_STATIC_RUNTIME "Use static runtime" ON) @@ -151,8 +147,8 @@ ExternalProject_Add(console CMAKE_ARGS -Wno-dev --no-warn-unused-cli + ${EXTERNAL_PROJECT_BUILD_TYPE_CMAKE_ARGS} ${OSX_ARCHITECTURES} - -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} # Install directories -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR} # Compiler settings diff --git a/cmake/dcm2niixInitializeBuildType.cmake b/cmake/dcm2niixInitializeBuildType.cmake new file mode 100644 index 00000000..b21ba4a8 --- /dev/null +++ b/cmake/dcm2niixInitializeBuildType.cmake @@ -0,0 +1,47 @@ +# This module allows to consistently manage the initialization and setting of build type +# CMake variables. +# +# It sets the variable EXTERNAL_PROJECT_BUILD_TYPE_CMAKE_ARGS based on the CMake generator +# being used: +# * If a multi-config generator (e.g Visual Studio) is used, it sets the variable with +# CMAKE_CONFIGURATION_TYPES. +# * If a single-config generator (e.g Unix Makefiles) is used, it sets the variable with +# CMAKE_BUILD_TYPE. +# +# Adapted from https://github.com/Slicer/Slicer/blob/5.2/CMake/SlicerInitializeBuildType.cmake + +# Default build type to use if none was specified +if(NOT DEFINED dcm2niix_DEFAULT_BUILD_TYPE) + set(dcm2niix_DEFAULT_BUILD_TYPE "Release") +endif() + +# Set a default build type if none was specified +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + + message(STATUS "Setting build type to '${dcm2niix_DEFAULT_BUILD_TYPE}' as none was specified.") + + set(CMAKE_BUILD_TYPE ${dcm2niix_DEFAULT_BUILD_TYPE} CACHE STRING "Choose the type of build." FORCE) + mark_as_advanced(CMAKE_BUILD_TYPE) + + # Set the possible values of build type for cmake-gui + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS + "Debug" + "Release" + "MinSizeRel" + "RelWithDebInfo" + ) +endif() + +# Pass variables to dependent projects +if(COMMAND ExternalProject_Add) + if(NOT CMAKE_CONFIGURATION_TYPES) + set(EXTERNAL_PROJECT_BUILD_TYPE_CMAKE_ARGS + -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} + ) + else() + set(EXTERNAL_PROJECT_BUILD_TYPE_CMAKE_ARGS + -DCMAKE_CONFIGURATION_TYPES:STRING=${CMAKE_CONFIGURATION_TYPES} + ) + endif() +endif() +