Skip to content

Commit

Permalink
ENH: use the new CMake mechanism to specify MSVC's static or DLL CRT
Browse files Browse the repository at this point in the history
  • Loading branch information
dzenanz authored and hjmjohnson committed Dec 30, 2021
1 parent d6778a3 commit 4b010e7
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CMake/ITKConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ set(ITK_BUILD_SHARED "@ITK_BUILD_SHARED_LIBS@")
# Export the library build type (SHARED or STATIC) for external builds
set(ITK_LIBRARY_BUILD_TYPE "@ITK_LIBRARY_BUILD_TYPE@")

# Whether ITK links to MSVC's static CRT (/MT and /MTd).
set(ITK_MSVC_STATIC_CRT "@ITK_MSVC_STATIC_RUNTIME_LIBRARY_value@")

# Whether ITK examples were built.
set(ITK_BUILD_EXAMPLES "@BUILD_EXAMPLES@")

Expand Down
16 changes: 16 additions & 0 deletions CMake/ITKModuleExternal.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ endif()
if(NOT ITK_VERSION VERSION_GREATER "5.1")
message(FATAL_ERROR "Requires ITK 5.1 or later to work.")
endif()
if(MSVC AND ${CMAKE_MINIMUM_REQUIRED_VERSION} LESS 3.16.3)
message(STATUS "cmake_minimum_required of ${CMAKE_MINIMUM_REQUIRED_VERSION} is not enough.")
message(WARNING "cmake_minimum_required must be at least 3.16.3")
message(STATUS "This is needed to allow proper setting of CMAKE_MSVC_RUNTIME_LIBRARY.")
message(STATUS "Do not be surprised if you run into link errors of the style:
error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_Static' doesn't match value 'MDd_Dynamic' in module.obj")
endif()
if(NOT EXISTS ${ITK_CMAKE_DIR}/ITKModuleMacros.cmake)
message(FATAL_ERROR "Modules can only be built against an ITK build tree; they cannot be built against an ITK install tree.")
endif()
Expand Down Expand Up @@ -138,6 +145,15 @@ option(BUILD_SHARED_LIBS "Build ITK with shared libraries." ${ITK_BUILD_SHARED})
if(NOT CMAKE_POSITION_INDEPENDENT_CODE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()
if(MSVC)
if(ITK_MSVC_STATIC_CRT)
message(STATUS "ITK is setting ${PROJECT_NAME}'s MSVC_RUNTIME_LIBRARY to static")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
else()
message(STATUS "ITK is setting ${PROJECT_NAME}'s MSVC_RUNTIME_LIBRARY to dynamic")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()
endif()

# Add the ITK_MODULES_DIR to the CMAKE_MODULE_PATH and then use the binary
# directory for the project to write out new ones to.
Expand Down
10 changes: 10 additions & 0 deletions CMake/UseITK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${ITK_REQUIRED_LINK_
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${ITK_REQUIRED_LINK_FLAGS}")
include(${ITK_CMAKE_DIR}/ITKInitializeCXXStandard.cmake)

if(MSVC)
if(ITK_MSVC_STATIC_CRT)
message(STATUS "ITK is setting ${PROJECT_NAME}'s MSVC_RUNTIME_LIBRARY to static")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
else()
message(STATUS "ITK is setting ${PROJECT_NAME}'s MSVC_RUNTIME_LIBRARY to dynamic")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()
endif()

# Add include directories needed to use ITK.
include_directories(BEFORE ${ITK_INCLUDE_DIRS})

Expand Down
14 changes: 13 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ foreach(pnew "CMP0120")
cmake_policy(SET ${pnew} NEW)
endif()
endforeach()
foreach(pold "CMP0091")
foreach(pold "")
if(POLICY ${pold})
cmake_policy(SET ${pold} OLD)
endif()
Expand Down Expand Up @@ -121,6 +121,18 @@ endif()
include(CTest)
mark_as_advanced(CLEAR BUILD_TESTING)

option(ITK_MSVC_STATIC_RUNTIME_LIBRARY "Link to MSVC's static CRT (/MT and /MTd).
OFF (default) means link to regular, dynamic CRT (/MD and /MDd)." OFF)
mark_as_advanced(ITK_MSVC_STATIC_RUNTIME_LIBRARY)
set(ITK_MSVC_STATIC_RUNTIME_LIBRARY_value ${ITK_MSVC_STATIC_RUNTIME_LIBRARY})
if(ITK_MSVC_STATIC_RUNTIME_LIBRARY)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
message(STATUS "Using MSVC's static CRT (/MT and /MTd)")
else()
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
message(STATUS "Using MSVC's dynamic CRT (/MD and /MDd)")
endif()

include(ITKDownloadSetup)
include(PreventInSourceBuilds)
include(PreventInBuildInstalls)
Expand Down
1 change: 1 addition & 0 deletions Modules/ThirdParty/DCMTK/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ endforeach()
CMAKE_ARGS
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
-DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS}
-DCMAKE_MSVC_RUNTIME_LIBRARY:STRING=${CMAKE_MSVC_RUNTIME_LIBRARY}
-DDCMTK_ENABLE_BUILTIN_DICTIONARY:BOOL=ON
-DDCMTK_WITH_DOXYGEN:BOOL=OFF
-DDCMTK_WITH_SNDFILE:BOOL=OFF
Expand Down
2 changes: 1 addition & 1 deletion Modules/ThirdParty/Expat/src/expat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mark_as_advanced(EXPAT_LARGE_SIZE)
option(EXPAT_MIN_SIZE "Get a smaller (but slower) parser (in particular avoid multiple copies of the tokenizer)" OFF)
mark_as_advanced(EXPAT_MIN_SIZE)
if(MSVC OR _EXPAT_HELP)
set(EXPAT_MSVC_STATIC_CRT OFF CACHE BOOL "Use /MT flag (static CRT) when compiling in MSVC")
set(EXPAT_MSVC_STATIC_CRT ${ITK_MSVC_STATIC_RUNTIME_LIBRARY} CACHE INTERNAL "Use /MT flag (static CRT) when compiling in MSVC")
endif()

#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
option(
gtest_force_shared_crt
"Use shared (DLL) run-time lib even when Google Test is built as static lib."
OFF)
ON) # ITK handles whether to build static CRT
# See how gtest_force_shared_crt is used in cmake/internal_utils.cmake

option(gtest_build_tests "Build all of gtest's own tests." OFF)

Expand Down

0 comments on commit 4b010e7

Please sign in to comment.