Skip to content

Commit

Permalink
Use CXX_MODULE_STD
Browse files Browse the repository at this point in the history
  • Loading branch information
ClausKlein committed Jan 2, 2025
1 parent 48c0954 commit ccb4bc0
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Executing workflow step 1 of 5: configure preset "dev"
Preset CMake variables:

CMAKE_BUILD_TYPE="Debug"
CMAKE_CXX_EXTENSIONS="OFF"
CMAKE_CXX_EXTENSIONS="ON"
CMAKE_CXX_FLAGS="-fstack-protector-strong -Wall -Wextra -Wpedantic -Wno-conversion -Wno-sign-conversion -Wcast-qual -Wformat=2 -Wundef -Werror=float-equal -Wshadow -Wcast-align -Wunused -Wnull-dereference -Wdouble-promotion -Wno-implicit-fallthrough -Wextra-semi -Woverloaded-virtual -Wnon-virtual-dtor -Wno-old-style-cast"
CMAKE_CXX_STANDARD="20"
CMAKE_CXX_STANDARD_REQUIRED="ON"
Expand Down
54 changes: 29 additions & 25 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include(cmake/prelude.cmake)

# Set experimental flag to enable `import std` support from CMake.
# This must be enabled before C++ language support.
# TODO: set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD 0e5b6991-d74f-4b3d-a41c-cf096e0b2508)
#XXX set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD 0e5b6991-d74f-4b3d-a41c-cf096e0b2508)

project(fmt
VERSION 11.1.1.1
Expand All @@ -16,21 +16,24 @@ project(fmt

include(cmake/variables.cmake)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

set(CMAKE_DEBUG_POSTFIX D)
# to prevent -Winclude-angled-in-module-purview in fmt/os.h
# see /Users/clausklein/.cache/CPM/fmt/297020319ba1d5d14e1599714def59a1788711c5/include/fmt/os.h:33
set(CPPdefinitions FMT_USE_FCNTL=0)
set(ADD_STDLIB_MODULES)

option(FMT_SEPARATE_COMPILATION "build fmt lib too" ${PROJECT_IS_TOP_LEVEL})
option(FMT_USE_MODULES "Export a CXX_MODULE fmt if possible" ${PROJECT_IS_TOP_LEVEL})
option(FMT_IMPORT_STD "Import the module std" ${PROJECT_IS_TOP_LEVEL})
option(FMT_SEPARATE_COMPILATION "build fmt lib too" ON)
option(FMT_USE_MODULES "Export a CXX_MODULE fmt if possible" ON)
option(FMT_IMPORT_STD "Import the module std" ON)

if(FMT_USE_MODULES)
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
list(APPEND CPPdefinitions FMT_MODULE)
# FIXME: NO! list(APPEND CPPdefinitions FMT_ATTACH_TO_GLOBAL_MODULE)

Expand All @@ -55,27 +58,29 @@ if(FMT_USE_MODULES)
add_compile_options(-stdlib=libc++)
add_link_options(-stdlib=libc++)
endif()
endif()

# Tell CMake that we explicitly want `import std`.
# This will initialize the property on all targets declared after this to 1
if(CMAKE_CXX_COMPILER_IMPORT_STD)
set(CMAKE_CXX_MODULE_STD ON)
endif()
# Tell CMake that we explicitly want `import std`.
# This will initialize the property on all targets declared after this to 1
if(CMAKE_CXX_COMPILER_IMPORT_STD)
set(CMAKE_CXX_MODULE_STD ON)
#XXX unset(ADD_STDLIB_MODULES)
endif()

if(CMAKE_CXX_MODULE_STD AND ADD_STDLIB_MODULES)
# FIXME: NO! list(APPEND CPPdefinitions FMT_IMPORT_STD)
list(APPEND CPPdefinitions HAS_STDLIB_MODULES)
endif()
if(CMAKE_CXX_MODULE_STD OR ADD_STDLIB_MODULES)
# FIXME: NO! list(APPEND CPPdefinitions FMT_IMPORT_STD)
list(APPEND CPPdefinitions HAS_STDLIB_MODULES)
endif()

message(STATUS "CMAKE_CXX_COMPILER_IMPORT_STD=${CMAKE_CXX_COMPILER_IMPORT_STD}")

message(STATUS "CMAKE_CXX_SCAN_FOR_MODULES=${CMAKE_CXX_SCAN_FOR_MODULES}")
message(STATUS "CMAKE_CXX_MODULE_STD=${CMAKE_CXX_MODULE_STD}")
message(STATUS "CPPdefinitions=${CPPdefinitions}")
else()
set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ${PROJECT_IS_TOP_LEVEL})
endif()

if(ADD_STDLIB_MODULES AND CMAKE_BUILD_TYPE STREQUAL Release)
if(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD AND CMAKE_BUILD_TYPE STREQUAL Release)
include(cmake/example.cmake)
endif()

Expand All @@ -89,7 +94,7 @@ cpmaddpackage(
GITHUB_REPOSITORY fmtlib/fmt
GIT_TAG 11.1.1
VERSION 11.1.1
DOWNLOAD_ONLY YES
DOWNLOAD_ONLY ON
)
# cmake-format: on

Expand Down Expand Up @@ -130,15 +135,15 @@ if(FMT_SEPARATE_COMPILATION)
fmt PUBLIC FILE_SET public_headers TYPE HEADERS BASE_DIRS ${fmt_SOURCE_DIR}/include FILES ${_fmt_public_header}
)

target_compile_definitions(fmt PUBLIC ${CPPdefinitions})
target_compile_options(fmt PUBLIC $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/utf-8>)
target_compile_features(
fmt INTERFACE "$<$<COMPILE_FEATURES:cxx_std_23>:cxx_std_23>" "$<$<NOT:$<COMPILE_FEATURES:cxx_std_23>>:cxx_std_20>"
)

target_compile_options(fmt PUBLIC $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/utf-8>)

if(FMT_USE_MODULES)
add_library(fmt::fmt-module ALIAS fmt)
target_compile_definitions(fmt PUBLIC ${CPPdefinitions})
message(STATUS "FMT_USE_MODULES=${FMT_USE_MODULES}")
add_library(fmt::fmt_module ALIAS fmt)
# cmake-format: off
target_sources(fmt PUBLIC
FILE_SET public_modules
Expand All @@ -152,8 +157,7 @@ if(FMT_SEPARATE_COMPILATION)
else()
add_library(fmt::fmt ALIAS fmt)
set_target_properties(fmt PROPERTIES CXX_SCAN_FOR_MODULES OFF)
target_compile_definitions(fmt PUBLIC ${CPPdefinitions})
target_sources(fmt PUBLIC ${_fmt_all_sources})
target_sources(fmt PRIVATE ${_fmt_all_sources})
endif()

if(FMT_WARNINGS_AS_ERRORS AND FMT_DEVELOPER_MODE AND UNIX)
Expand Down
6 changes: 3 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ check: all
-ninja -C build/dev spell-check

test:
cmake --preset ci-${hostSystemName}
cmake --build build
cmake --install build --prefix $(CURDIR)/stagedir
# cmake --preset ci-${hostSystemName} --fresh
# cmake --build build
# cmake --install build --prefix $(CURDIR)/stagedir
cmake -G Ninja -B build/tests -S tests -D CMAKE_PREFIX_PATH=$(CURDIR)/stagedir
cmake --build build/tests
ctest --test-dir build/tests
Expand Down
2 changes: 1 addition & 1 deletion cmake/dev-mode.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ option(FMT_BUILD_TESTING "Use ctest" ON)
if(FMT_BUILD_TESTING)
enable_testing()

add_subdirectory(tests)
# XXX add_subdirectory(tests)
add_subdirectory(module/tests)
endif()

Expand Down
2 changes: 1 addition & 1 deletion cmake/install-rules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ if(FMT_USE_MODULES)
install(TARGETS fmt EXPORT fmtTargets FILE_SET public_headers #
FILE_SET public_modules DESTINATION ${FMT_INSTALL_CMAKEDIR}/module
)
install(FILES module/fmt.cppm DESTINATION ${FMT_INSTALL_CMAKEDIR}/module)
else()
install(TARGETS fmt EXPORT fmtTargets FILE_SET public_headers)
endif()
install(FILES module/fmt.cppm DESTINATION ${FMT_INSTALL_CMAKEDIR}/module)
install(FILES ${_fmt_all_sources} DESTINATION ${FMT_INSTALL_CMAKEDIR}/module)

write_basic_package_version_file("${_package}ConfigVersion.cmake" COMPATIBILITY SameMajorVersion ARCH_INDEPENDENT)
Expand Down
10 changes: 7 additions & 3 deletions module/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ if(PROJECT_IS_TOP_LEVEL)
if(NOT CMAKE_CXX_COMPILER_IMPORT_STD AND ADD_STDLIB_MODULES)
message(WARNING "ADD_STDLIB_MODULES=${ADD_STDLIB_MODULES}")
add_stdlib_module(${ADD_STDLIB_MODULES})
else()
unset(ADD_STDLIB_MODULES)
endif()
# Build the fmt module
add_fmt_module(fmt-module)
add_fmt_module(fmt_module)
endif()
endif()

Expand All @@ -48,8 +50,10 @@ message(STATUS "Module tests to be built: ${ALL_TESTS}")
foreach(_file ${ALL_TESTS})
add_executable(module_${_file})
target_sources(module_${_file} PRIVATE ../../tests/${_file}.cc)
if(USE_MODULES)
target_link_libraries(module_${_file} fmt-module ${ADD_STDLIB_MODULES})
if(CMAKE_CXX_MODULE_STD)
target_link_libraries(module_${_file} PUBLIC fmt_module)
elseif(ADD_STDLIB_MODULES)
target_link_libraries(module_${_file} PUBLIC fmt_module ${ADD_STDLIB_MODULES})
else()
target_link_libraries(module_${_file} fmt::fmt-header-only)
endif()
Expand Down
18 changes: 13 additions & 5 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
cmake_minimum_required(VERSION 3.30...3.31)

# Set experimental flag to enable `import std` support from CMake.
# This must be enabled before C++ language support.
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD 0e5b6991-d74f-4b3d-a41c-cf096e0b2508)

project(fmt_test LANGUAGES CXX)

include(../cmake/folders.cmake OPTIONAL)
Expand All @@ -10,8 +14,10 @@ include(../cmake/windows-set-path.cmake OPTIONAL)
if(PROJECT_IS_TOP_LEVEL)
enable_testing()

find_package(fmt 11.1.1.1 EXACT REQUIRED)
include(../cmake/CPM.cmake)
#XXX find_package(fmt 11.1.1.1 EXACT REQUIRED)

set(ADD_STDLIB_MODULES stdlib)
add_subdirectory(.. fmt_module)
endif()

# ---- Tests ----
Expand Down Expand Up @@ -45,8 +51,10 @@ if(FMT_USE_MODULES)
if(NOT CMAKE_CXX_COMPILER_IMPORT_STD AND ADD_STDLIB_MODULES)
message(WARNING "ADD_STDLIB_MODULES=${ADD_STDLIB_MODULES}")
add_stdlib_module(${ADD_STDLIB_MODULES})
else()
unset(ADD_STDLIB_MODULES)
endif()
target_link_libraries(header-only-test ${ADD_STDLIB_MODULES} fmt::fmt-module)
target_link_libraries(header-only-test PUBLIC fmt::fmt_module ${ADD_STDLIB_MODULES})
else()
target_link_libraries(header-only-test fmt::fmt-header-only)
endif()
Expand Down Expand Up @@ -80,8 +88,8 @@ if(NOT PROJECT_IS_TOP_LEVEL)
"-DADD_STDLIB_MODULES=${ADD_STDLIB_MODULES}"
"-DLLVM_LIBC_SOURCE=${LLVM_LIBC_SOURCE}"
"-DLLVM_ROOT=${LLVM_ROOT}"
# TODO: "-DCMAKE_CXX_MODULE_STD=ON"
# TODO: "-DCMAKE_EXPERIMENTAL_CXX_IMPORT_STD=0e5b6991-d74f-4b3d-a41c-cf096e0b2508"
"-DCMAKE_CXX_MODULE_STD=ON"
"-DCMAKE_EXPERIMENTAL_CXX_IMPORT_STD=0e5b6991-d74f-4b3d-a41c-cf096e0b2508"
)
endif()
# cmake-format: on
Expand Down

0 comments on commit ccb4bc0

Please sign in to comment.