Update add_module_library

This commit is contained in:
Victor Zverovich 2023-04-13 17:09:31 -07:00
parent 4b5ae0b0ef
commit d9c19940a3
2 changed files with 38 additions and 46 deletions

View File

@ -56,57 +56,53 @@ function(add_module_library name)
return() return()
endif () endif ()
# Check if modules are supported.
set(have_modules FALSE)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND
CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16.0)
set(have_modules TRUE)
endif ()
if (NOT have_modules)
message(FATAL_ERROR "Modules not supported.")
endif ()
# Modules require C++20. # Modules require C++20.
target_compile_features(${name} PUBLIC cxx_std_20) target_compile_features(${name} PUBLIC cxx_std_20)
if (CMAKE_COMPILER_IS_GNUCXX)
target_compile_options(${name} PUBLIC -fmodules-ts)
endif ()
# `std` is affected by CMake options and may be higher than C++20. # `std` is affected by CMake options and may be higher than C++20.
get_target_property(std ${name} CXX_STANDARD) get_target_property(std ${name} CXX_STANDARD)
set(pcms) if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
foreach (src ${sources}) set(pcms)
get_filename_component(pcm ${src} NAME_WE) foreach (src ${sources})
set(pcm ${pcm}.pcm) get_filename_component(pcm ${src} NAME_WE)
set(pcm ${pcm}.pcm)
# Propagate -fmodule-file=*.pcm to targets that link with this library. # Propagate -fmodule-file=*.pcm to targets that link with this library.
target_compile_options(${name} PUBLIC -fmodule-file=${pcm}) target_compile_options(${name} PUBLIC -fmodule-file=${pcm})
# Use an absolute path to prevent target_link_libraries prepending -l to it. # Use an absolute path to prevent target_link_libraries prepending -l
set(pcms ${pcms} ${CMAKE_CURRENT_BINARY_DIR}/${pcm}) # to it.
add_custom_command( set(pcms ${pcms} ${CMAKE_CURRENT_BINARY_DIR}/${pcm})
OUTPUT ${pcm} add_custom_command(
COMMAND ${CMAKE_CXX_COMPILER} OUTPUT ${pcm}
-std=c++${std} -x c++-module --precompile -c COMMAND ${CMAKE_CXX_COMPILER}
-o ${pcm} ${CMAKE_CURRENT_SOURCE_DIR}/${src} -std=c++${std} -x c++-module --precompile -c
"-I$<JOIN:$<TARGET_PROPERTY:${name},INCLUDE_DIRECTORIES>,;-I>" -o ${pcm} ${CMAKE_CURRENT_SOURCE_DIR}/${src}
# Required by the -I generator expression above. "-I$<JOIN:$<TARGET_PROPERTY:${name},INCLUDE_DIRECTORIES>,;-I>"
COMMAND_EXPAND_LISTS # Required by the -I generator expression above.
DEPENDS ${src}) COMMAND_EXPAND_LISTS
endforeach () DEPENDS ${src})
endforeach ()
# Add .pcm files as sources to make sure they are built before the library. # Add .pcm files as sources to make sure they are built before the library.
set(files) set(sources)
foreach (pcm ${pcms}) foreach (pcm ${pcms})
get_filename_component(pcm_we ${pcm} NAME_WE) get_filename_component(pcm_we ${pcm} NAME_WE)
set(obj ${pcm_we}.o) set(obj ${pcm_we}.o)
# Use an absolute path to prevent target_link_libraries prepending -l. # Use an absolute path to prevent target_link_libraries prepending -l.
set(files ${files} ${pcm} ${CMAKE_CURRENT_BINARY_DIR}/${obj}) set(sources ${sources} ${pcm} ${CMAKE_CURRENT_BINARY_DIR}/${obj})
add_custom_command( add_custom_command(
OUTPUT ${obj} OUTPUT ${obj}
COMMAND ${CMAKE_CXX_COMPILER} $<TARGET_PROPERTY:${name},COMPILE_OPTIONS> COMMAND ${CMAKE_CXX_COMPILER} $<TARGET_PROPERTY:${name},COMPILE_OPTIONS>
-c -o ${obj} ${pcm} -c -o ${obj} ${pcm}
DEPENDS ${pcm}) DEPENDS ${pcm})
endforeach () endforeach ()
target_sources(${name} PRIVATE ${files}) endif ()
target_sources(${name} PRIVATE ${sources})
endfunction() endfunction()
include(CMakeParseArguments) include(CMakeParseArguments)

View File

@ -76,10 +76,6 @@ export module fmt;
#include "fmt/printf.h" #include "fmt/printf.h"
#include "fmt/xchar.h" #include "fmt/xchar.h"
#if !(defined(__cpp_modules) || FMT_CLANG_VERSION >= 1600)
# error modules not supported
#endif
// gcc doesn't yet implement private module fragments // gcc doesn't yet implement private module fragments
#if !FMT_GCC_VERSION #if !FMT_GCC_VERSION
module : private; module : private;