Use native c++ module support from CMake

also fix some clang compilation issues when using c++ modules
This commit is contained in:
LoveSy 2024-06-04 21:35:43 +08:00 committed by Victor Zverovich
parent fba06f0ee1
commit a9b8517638
2 changed files with 48 additions and 40 deletions

View File

@ -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. # Fallback for using newer policies on CMake <3.12.
if (${CMAKE_VERSION} VERSION_LESS 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) target_compile_options(${name} PUBLIC -fmodules-ts)
endif () endif ()
# `std` is affected by CMake options and may be higher than C++20. if (CMAKE_VERSION VERSION_LESS 3.28)
get_target_property(std ${name} CXX_STANDARD) # `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") if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(pcms) set(pcms)
foreach (src ${sources}) foreach (src ${sources})
get_filename_component(pcm ${src} NAME_WE) get_filename_component(pcm ${src} NAME_WE)
set(pcm ${pcm}.pcm) 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( target_compile_options(
${name} PUBLIC -fmodule-file=${CMAKE_CURRENT_BINARY_DIR}/${pcm}) ${name} PUBLIC -fmodule-file=${CMAKE_CURRENT_BINARY_DIR}/${pcm})
# Use an absolute path to prevent target_link_libraries prepending -l # Use an absolute path to prevent target_link_libraries prepending -l
# to it. # to it.
set(pcms ${pcms} ${CMAKE_CURRENT_BINARY_DIR}/${pcm}) set(pcms ${pcms} ${CMAKE_CURRENT_BINARY_DIR}/${pcm})
add_custom_command( add_custom_command(
OUTPUT ${pcm} OUTPUT ${pcm}
COMMAND ${CMAKE_CXX_COMPILER} COMMAND ${CMAKE_CXX_COMPILER}
-std=c++${std} -x c++-module --precompile -c -std=c++${std} -x c++-module --precompile -c
-o ${pcm} ${CMAKE_CURRENT_SOURCE_DIR}/${src} -o ${pcm} ${CMAKE_CURRENT_SOURCE_DIR}/${src}
"-I$<JOIN:$<TARGET_PROPERTY:${name},INCLUDE_DIRECTORIES>,;-I>" "-I$<JOIN:$<TARGET_PROPERTY:${name},INCLUDE_DIRECTORIES>,;-I>"
# Required by the -I generator expression above. # Required by the -I generator expression above.
COMMAND_EXPAND_LISTS COMMAND_EXPAND_LISTS
DEPENDS ${src}) DEPENDS ${src})
endforeach () 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(sources) 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(sources ${sources} ${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 ()
endif () endif ()
target_sources(${name} PRIVATE ${sources}) target_sources(${name} PRIVATE ${sources})
else()
target_sources(${name} PUBLIC FILE_SET CXX_MODULES FILES ${sources})
endif()
endfunction() endfunction()
include(CMakeParseArguments) include(CMakeParseArguments)

View File

@ -1069,6 +1069,8 @@ template <typename Locale> class format_facet : public Locale::facet {
} }
}; };
FMT_END_EXPORT
namespace detail { namespace detail {
// Returns true if value is negative, false otherwise. // 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; const char* message) noexcept;
} // namespace detail } // namespace detail
FMT_BEGIN_EXPORT
FMT_API auto vsystem_error(int error_code, string_view format_str, FMT_API auto vsystem_error(int error_code, string_view format_str,
format_args args) -> std::system_error; 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; extern template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t;
#endif // FMT_HEADER_ONLY #endif // FMT_HEADER_ONLY
FMT_END_EXPORT
template <typename T, typename Char, type TYPE> template <typename T, typename Char, type TYPE>
template <typename FormatContext> template <typename FormatContext>
FMT_CONSTEXPR FMT_INLINE auto native_formatter<T, Char, TYPE>::format( FMT_CONSTEXPR FMT_INLINE auto native_formatter<T, Char, TYPE>::format(
@ -4300,7 +4305,6 @@ FMT_CONSTEXPR FMT_INLINE auto native_formatter<T, Char, TYPE>::format(
return write<Char>(ctx.out(), val, specs, ctx.locale()); return write<Char>(ctx.out(), val, specs, ctx.locale());
} }
FMT_END_EXPORT
} // namespace detail } // namespace detail
FMT_BEGIN_EXPORT FMT_BEGIN_EXPORT