Skip to content

Commit

Permalink
Fix build problems with test module fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ClausKlein committed Jan 2, 2025
1 parent b8eac3a commit 98c866a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
22 changes: 20 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ if(FMT_USE_MODULES)
# 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 @@ -58,6 +58,24 @@ if(FMT_USE_MODULES)
add_compile_options(-stdlib=libc++)
add_link_options(-stdlib=libc++)
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()

endif()

# Tell CMake that we explicitly want `import std`.
Expand Down
9 changes: 5 additions & 4 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,10 +33,6 @@ 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)
endif()
Expand All @@ -51,6 +48,10 @@ foreach(_file ${ALL_TESTS})
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)
Expand Down
23 changes: 4 additions & 19 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ if(PROJECT_IS_TOP_LEVEL)
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 @@ -25,23 +27,6 @@ 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
Expand Down Expand Up @@ -85,8 +70,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 98c866a

Please sign in to comment.