From a9b851763854ffb21e9569f18e0f34b4f661fbeb Mon Sep 17 00:00:00 2001 From: LoveSy Date: Tue, 4 Jun 2024 21:35:43 +0800 Subject: [PATCH] Use native c++ module support from CMake also fix some clang compilation issues when using c++ modules --- CMakeLists.txt | 90 +++++++++++++++++++++++--------------------- include/fmt/format.h | 6 ++- 2 files changed, 52 insertions(+), 44 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b79776fa42e..bd9896676d83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.8...3.26) +cmake_minimum_required(VERSION 3.8...3.28) # Fallback for using newer policies on CMake <3.12. if (${CMAKE_VERSION} VERSION_LESS 3.12) @@ -62,48 +62,52 @@ function(add_module_library name) target_compile_options(${name} PUBLIC -fmodules-ts) endif () - # `std` is affected by CMake options and may be higher than C++20. - get_target_property(std ${name} CXX_STANDARD) - - if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(pcms) - foreach (src ${sources}) - get_filename_component(pcm ${src} NAME_WE) - set(pcm ${pcm}.pcm) - - # Propagate -fmodule-file=*.pcm to targets that link with this library. - target_compile_options( - ${name} PUBLIC -fmodule-file=${CMAKE_CURRENT_BINARY_DIR}/${pcm}) - - # Use an absolute path to prevent target_link_libraries prepending -l - # to it. - set(pcms ${pcms} ${CMAKE_CURRENT_BINARY_DIR}/${pcm}) - add_custom_command( - OUTPUT ${pcm} - COMMAND ${CMAKE_CXX_COMPILER} - -std=c++${std} -x c++-module --precompile -c - -o ${pcm} ${CMAKE_CURRENT_SOURCE_DIR}/${src} - "-I$,;-I>" - # Required by the -I generator expression above. - COMMAND_EXPAND_LISTS - DEPENDS ${src}) - endforeach () - - # Add .pcm files as sources to make sure they are built before the library. - set(sources) - foreach (pcm ${pcms}) - get_filename_component(pcm_we ${pcm} NAME_WE) - set(obj ${pcm_we}.o) - # Use an absolute path to prevent target_link_libraries prepending -l. - set(sources ${sources} ${pcm} ${CMAKE_CURRENT_BINARY_DIR}/${obj}) - add_custom_command( - OUTPUT ${obj} - COMMAND ${CMAKE_CXX_COMPILER} $ - -c -o ${obj} ${pcm} - DEPENDS ${pcm}) - endforeach () - endif () - target_sources(${name} PRIVATE ${sources}) + if (CMAKE_VERSION VERSION_LESS 3.28) + # `std` is affected by CMake options and may be higher than C++20. + get_target_property(std ${name} CXX_STANDARD) + + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(pcms) + foreach (src ${sources}) + get_filename_component(pcm ${src} NAME_WE) + set(pcm ${pcm}.pcm) + + # Propagate -fmodule-file=*.pcm to targets that link with this library. + target_compile_options( + ${name} PUBLIC -fmodule-file=${CMAKE_CURRENT_BINARY_DIR}/${pcm}) + + # Use an absolute path to prevent target_link_libraries prepending -l + # to it. + set(pcms ${pcms} ${CMAKE_CURRENT_BINARY_DIR}/${pcm}) + add_custom_command( + OUTPUT ${pcm} + COMMAND ${CMAKE_CXX_COMPILER} + -std=c++${std} -x c++-module --precompile -c + -o ${pcm} ${CMAKE_CURRENT_SOURCE_DIR}/${src} + "-I$,;-I>" + # Required by the -I generator expression above. + COMMAND_EXPAND_LISTS + DEPENDS ${src}) + endforeach () + + # Add .pcm files as sources to make sure they are built before the library. + set(sources) + foreach (pcm ${pcms}) + get_filename_component(pcm_we ${pcm} NAME_WE) + set(obj ${pcm_we}.o) + # Use an absolute path to prevent target_link_libraries prepending -l. + set(sources ${sources} ${pcm} ${CMAKE_CURRENT_BINARY_DIR}/${obj}) + add_custom_command( + OUTPUT ${obj} + COMMAND ${CMAKE_CXX_COMPILER} $ + -c -o ${obj} ${pcm} + DEPENDS ${pcm}) + endforeach () + endif () + target_sources(${name} PRIVATE ${sources}) + else() + target_sources(${name} PUBLIC FILE_SET CXX_MODULES FILES ${sources}) + endif() endfunction() include(CMakeParseArguments) diff --git a/include/fmt/format.h b/include/fmt/format.h index d3e196c94cd7..fa8debc13b71 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1069,6 +1069,8 @@ template class format_facet : public Locale::facet { } }; +FMT_END_EXPORT + namespace detail { // Returns true if value is negative, false otherwise. @@ -3865,6 +3867,7 @@ FMT_API void report_error(format_func func, int error_code, const char* message) noexcept; } // namespace detail +FMT_BEGIN_EXPORT FMT_API auto vsystem_error(int error_code, string_view format_str, format_args args) -> std::system_error; @@ -4285,6 +4288,8 @@ extern template FMT_API auto decimal_point_impl(locale_ref) -> char; extern template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t; #endif // FMT_HEADER_ONLY +FMT_END_EXPORT + template template FMT_CONSTEXPR FMT_INLINE auto native_formatter::format( @@ -4300,7 +4305,6 @@ FMT_CONSTEXPR FMT_INLINE auto native_formatter::format( return write(ctx.out(), val, specs, ctx.locale()); } -FMT_END_EXPORT } // namespace detail FMT_BEGIN_EXPORT