Skip to content

Commit

Permalink
Set FMT_USE_MODULES again
Browse files Browse the repository at this point in the history
Refactory the CMakeLists.txt files

Fix build problems with test module fmt

Try to fix CI problems too
  • Loading branch information
ClausKlein committed Jan 2, 2025
1 parent be999c2 commit e7be420
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 188 deletions.
3 changes: 2 additions & 1 deletion .CMakeUserPresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"name": "dev",
"configurePreset": "dev",
"configuration": "Release",
"jobs": 12,
"jobs": 1,
"verbose": true,
"targets": [
"install"
]
Expand Down
253 changes: 133 additions & 120 deletions .TODO.txt

Large diffs are not rendered by default.

79 changes: 48 additions & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ 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)
if(CMAKE_CXX_SCAN_FOR_MODULES)
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD 0e5b6991-d74f-4b3d-a41c-cf096e0b2508)
endif()

project(fmt
VERSION 11.1.1.1
Expand All @@ -16,33 +18,28 @@ project(fmt

include(cmake/variables.cmake)

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

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

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})
if(UNIX)
option(FMT_USE_MODULES "Export a CXX_MODULE fmt if possible" ${PROJECT_IS_TOP_LEVEL})
endif()
# TODO: option(FMT_IMPORT_STD "Import the module std" ${PROJECT_IS_TOP_LEVEL})
option(FMT_SEPARATE_COMPILATION "build fmt lib too" ${CMAKE_CXX_SCAN_FOR_MODULES})
option(FMT_USE_MODULES "Export a CXX_MODULE fmt if possible" ${CMAKE_CXX_SCAN_FOR_MODULES})
option(FMT_IMPORT_STD "Import the module std" ${FMT_USE_MODULES})

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

if(APPLE)
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.0)
execute_process(OUTPUT_VARIABLE LLVM_PREFIX COMMAND brew --prefix llvm@19 COMMAND_ECHO STDOUT)
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 20.0)
execute_process(OUTPUT_VARIABLE LLVM_PREFIX COMMAND brew --prefix llvm@20 COMMAND_ECHO STDOUT)
string(STRIP ${LLVM_PREFIX} LLVM_PREFIX)
message(STATUS "LLVM_PREFIX=${LLVM_PREFIX}")

Expand All @@ -62,16 +59,34 @@ if(FMT_USE_MODULES)
add_link_options(-stdlib=libc++)
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()
# Build the stdlib module
function(add_stdlib_module NAME)
add_library(${NAME})
# cmake-format: off
target_sources(${NAME} PUBLIC
FILE_SET CXX_MODULES
BASE_DIRS ${LLVM_LIBC_SOURCE}
FILES
${LLVM_LIBC_SOURCE}/std.cppm
${LLVM_LIBC_SOURCE}/std.compat.cppm
)
# cmake-format: on
target_compile_features(${NAME} PUBLIC cxx_std_23)
target_compile_definitions(${NAME} PUBLIC _LIBCPP_HAS_NO_LOCALIZATION)
target_compile_options(${NAME} PRIVATE -Wno-reserved-module-identifier)
endfunction()

if(CMAKE_CXX_MODULE_STD AND ADD_STDLIB_MODULES)
# FIXME: NO! list(APPEND CPPdefinitions FMT_IMPORT_STD)
list(APPEND CPPdefinitions HAS_STDLIB_MODULES)
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()

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}")
Expand All @@ -81,6 +96,10 @@ else()
set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ${PROJECT_IS_TOP_LEVEL})
endif()

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

# ---- add dependency libraries ----

include(cmake/CPM.cmake)
Expand All @@ -91,7 +110,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 @@ -132,15 +151,14 @@ 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})
add_library(fmt::fmt_module ALIAS fmt)
# cmake-format: off
target_sources(fmt PUBLIC
FILE_SET public_modules
Expand All @@ -153,13 +171,12 @@ if(FMT_SEPARATE_COMPILATION)
# cmake-format: on
else()
add_library(fmt::fmt ALIAS fmt)
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
target_compile_definitions(fmt PUBLIC ${CPPdefinitions})
target_sources(fmt PUBLIC ${_fmt_all_sources})
set_target_properties(fmt PROPERTIES CXX_SCAN_FOR_MODULES OFF)
target_sources(fmt PRIVATE ${_fmt_all_sources})
endif()

if(FMT_WARNINGS_AS_ERRORS AND FMT_DEVELOPER_MODE AND UNIX)
target_compile_options(fmt PUBLIC -Werror)
target_compile_options(fmt PRIVATE -Werror)
endif()
endif()

Expand Down
4 changes: 2 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ check: all

test:
cmake --preset ci-${hostSystemName}
cmake --build build
cmake --build build -- -v -j 1
cmake --install build --prefix $(CURDIR)/stagedir
cmake -G Ninja -B build/tests -S tests -D CMAKE_PREFIX_PATH=$(CURDIR)/stagedir
cmake --build build/tests
cmake --build build/tests -- -v -j 1
ctest --test-dir build/tests

example:
Expand Down
6 changes: 5 additions & 1 deletion cmake/dev-mode.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ if(FMT_BUILD_TESTING)
enable_testing()

add_subdirectory(tests)
add_subdirectory(module/tests)

if(CMAKE_CXX_MODULE_STD)
add_subdirectory(module/tests)
endif()

endif()

option(FMT_BUILD_MCSS_DOCS "Build documentation using Doxygen and m.css" OFF)
Expand Down
7 changes: 4 additions & 3 deletions cmake/install-rules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ mark_as_advanced(FMT_INSTALL_CMAKEDIR)
install(TARGETS fmt-header-only EXPORT fmtTargets FILE_SET HEADERS)

if(FMT_USE_MODULES)
install(TARGETS fmt EXPORT fmtTargets FILE_SET public_headers FILE_SET public_modules
DESTINATION ${FMT_INSTALL_CMAKEDIR}/module
install(TARGETS fmt EXPORT fmtTargets FILE_SET public_headers #
FILE_SET public_modules DESTINATION ${FMT_INSTALL_CMAKEDIR}/module
)
else()
install(FILES module/fmt.cppm DESTINATION ${FMT_INSTALL_CMAKEDIR}/module)
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
18 changes: 10 additions & 8 deletions module/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ if(PROJECT_IS_TOP_LEVEL)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
if(CMAKE_GENERATOR STREQUAL "Ninja")
Expand All @@ -32,12 +33,8 @@ if(PROJECT_IS_TOP_LEVEL)
message(STATUS "CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES=${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}")
endif()
if(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})
endif()
# Build the fmt module
add_fmt_module(fmt-module)
add_fmt_module(fmt_module)
endif()
endif()

Expand All @@ -48,9 +45,14 @@ 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})
# XXX: NOT USED! target_compile_definitions(module_${_file} PUBLIC USE_MODULES)
if(CMAKE_CXX_MODULE_STD)
target_link_libraries(module_${_file} fmt_module)
elseif(ADD_STDLIB_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})
endif()
target_link_libraries(module_${_file} fmt_module ${ADD_STDLIB_MODULES})
else()
target_link_libraries(module_${_file} fmt::fmt-header-only)
endif()
Expand Down
38 changes: 16 additions & 22 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ 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)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(fmt 11.1.1.1 EXACT REQUIRED HINT ../stagedir/lib)

add_subdirectory(../module/tests module_test)
endif()

# ---- Tests ----
Expand All @@ -21,32 +27,20 @@ if(EXISTS ${fmt_SOURCE_DIR}/test/header-only-test.cc AND NOT EXISTS ${CMAKE_CURR
endif()
add_executable(header-only-test header-only-test.cc)

# Build the stdlib module
function(add_stdlib_module NAME)
add_library(${NAME})
# cmake-format: off
target_sources(${NAME} PUBLIC
FILE_SET CXX_MODULES
BASE_DIRS ${LLVM_LIBC_SOURCE}
FILES
${LLVM_LIBC_SOURCE}/std.cppm
${LLVM_LIBC_SOURCE}/std.compat.cppm
)
# cmake-format: on
target_compile_features(${NAME} PUBLIC cxx_std_23)
target_compile_definitions(${NAME} PUBLIC _LIBCPP_HAS_NO_LOCALIZATION)
target_compile_options(${NAME} PRIVATE -Wno-reserved-module-identifier)
endfunction()

if(FMT_USE_MODULES)
# Build the stdlib modules if possible
# /usr/local/Cellar/llvm/19.1.6/share/libc++/v1/std.cppm
# /usr/local/Cellar/llvm/19.1.6/share/libc++/v1/std.compat.cppm
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})
target_link_libraries(header-only-test ${ADD_STDLIB_MODULES})
endif()
if(TARGET fmt::fmt_module)
target_link_libraries(header-only-test fmt::fmt_module)
else()
target_link_libraries(header-only-test fmt::fmt)
endif()
target_link_libraries(header-only-test ${ADD_STDLIB_MODULES} fmt::fmt-module)
else()
target_link_libraries(header-only-test fmt::fmt-header-only)
endif()
Expand Down Expand Up @@ -80,8 +74,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=${CMAKE_CXX_MODULE_STD}"
"-DCMAKE_EXPERIMENTAL_CXX_IMPORT_STD=${CMAKE_EXPERIMENTAL_CXX_IMPORT_STD}"
)
endif()
# cmake-format: on
Expand Down

0 comments on commit e7be420

Please sign in to comment.