Add Separate CMake Target for C++20 Modules (#4685)

* Add Separate CMake Target for C++20 Modules

In the same vein as there is the `fmt::fmt-header-only`, `fmt::fmt` and
`fmt::fmt_c` targets, I propose the addition of a new target
`fmt::fmt-module` which will be for the compilation of the FMT_MODULE
library option.

The new target will have the properties requried for Compiling,
Installing and using the C++20 functionality in CMake

The `add_module_library` function is marked as deprecated as its
functionality is superseded.

Updated the logic for setting the FMT_USE_CMAKE_MODULE flag to check the
versions for Ninja and MSVC according the CMAKE Documents and setting
the FMT_MODULE flag based on this

* Add Separate CMake Target for C++20 Modules

In the same vein as there is the `fmt::fmt-header-only`, `fmt::fmt` and
`fmt::fmt_c` targets, I propose the addition of a new target
`fmt::fmt-module` which will be for the compilation of the FMT_MODULE
library option.

The new target will have the properties requried for Compiling,
Installing and using the C++20 functionality in CMake

Updated the logic for setting the FMT_USE_CMAKE_MODULE flag to check the
versions for Ninja and MSVC according the CMAKE Documents and setting
the FMT_MODULE flag based on this

Fixed the test/CMakeLists.txt file which used the FMT_MODULE flag to
separate the module and non-module library testing, in particular
disableing the module version.

The module testing still needs to be fixed, but the expected behavior of
testing the non-modular version is working.

---------

Co-authored-by: Mathew Benson <mathew@benson.co.ke>
Co-authored-by: ClausKlein <claus.klein@arcormail.de>
This commit is contained in:
Mathew Benson
2026-03-02 20:11:31 +03:00
committed by GitHub
parent fca0445565
commit 4e1b170b44
4 changed files with 126 additions and 54 deletions
+18 -18
View File
@@ -41,10 +41,6 @@ function(add_fmt_test name)
add_test(NAME ${name} COMMAND ${name})
endfunction()
if (FMT_MODULE)
return ()
endif ()
add_fmt_test(args-test)
add_fmt_test(base-test)
add_fmt_test(assert-test)
@@ -86,23 +82,27 @@ add_executable(perf-sanity perf-sanity.cc)
target_link_libraries(perf-sanity fmt::fmt)
if (FMT_MODULE)
# The module-test.cc needs some work and is not working yet.
# For now We simply just return
# so that the other tests are not affected.
return()
# The tests need {fmt} to be compiled as traditional library
# because of visibility of implementation details.
# If module support is present the module tests require a
# test-only module to be built from {fmt}
add_library(test-module OBJECT ${CMAKE_SOURCE_DIR}/src/fmt.cc)
target_compile_features(test-module PUBLIC cxx_std_11)
target_include_directories(test-module PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
enable_module(test-module)
#add_library(test-module OBJECT ${CMAKE_SOURCE_DIR}/src/fmt.cc)
#target_compile_features(test-module PUBLIC cxx_std_11)
#target_include_directories(test-module PUBLIC
# $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
#enable_module(test-module)
add_fmt_test(module-test MODULE test-main.cc)
if (MSVC)
target_compile_options(test-module PRIVATE /utf-8 /Zc:__cplusplus
/Zc:externConstexpr /Zc:inline)
target_compile_options(module-test PRIVATE /utf-8 /Zc:__cplusplus
/Zc:externConstexpr /Zc:inline)
endif ()
#add_fmt_test(module-test MODULE test-main.cc)
#if (MSVC)
# target_compile_options(test-module PRIVATE /utf-8 /Zc:__cplusplus
# /Zc:externConstexpr /Zc:inline)
# target_compile_options(module-test PRIVATE /utf-8 /Zc:__cplusplus
# /Zc:externConstexpr /Zc:inline)
#endif ()
endif ()
if (NOT DEFINED MSVC_STATIC_RUNTIME AND MSVC)
@@ -112,9 +112,9 @@ if (NOT DEFINED MSVC_STATIC_RUNTIME AND MSVC)
if (${flag_var} MATCHES "^(/|-)(MT|MTd)")
set(MSVC_STATIC_RUNTIME ON)
break()
endif()
endif ()
endforeach()
endif()
endif ()
if (NOT MSVC_STATIC_RUNTIME)
add_executable(posix-mock-test