Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GCC C++20+ modules. #21

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ message(STATUS "")
message(STATUS "C++ latest standard = ${std_latest_ver}")
message(STATUS " modules support = ${cxx_modules_latest}")

set(MAPPER_FILE ${CMAKE_BINARY_DIR}/mapper.txt)

# Module library
add_module_library(hello hello.cc FALLBACK hello_fallback.cc)
add_module_library(hello hello.cc FALLBACK hello_fallback.cc MAPPER_FILE ${MAPPER_FILE})
target_include_directories(hello PUBLIC include)

# Demo application
Expand Down
15 changes: 15 additions & 0 deletions mapper.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
if(NOT(EXISTS ${MAPPER_FILE}))
# create file if doesn't exist
write_file(${MAPPER_FILE} "")
endif()
FILE(GLOB children RELATIVE ${CMAKE_CURRENT_BINARY_DIR}/gcm.cache/ ${CMAKE_CURRENT_BINARY_DIR}/gcm.cache/*)
FOREACH(child ${children})
IF(NOT (IS_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/gcm.cache/${child}))
# remove module from mapper file if exists, then append new module to mapper file
get_filename_component(inc ${child} NAME_WE)
file(READ ${MAPPER_FILE} file_data)
string(REGEX REPLACE "[ \t]*${inc}[ \t]+[^\n]+\n" "" file_data ${file_data})
set(file_data "${file_data}\n${inc} ${CMAKE_CURRENT_BINARY_DIR}/gcm.cache/${child}\n")
file(WRITE ${MAPPER_FILE} "${file_data}")
ENDIF()
ENDFOREACH()
44 changes: 43 additions & 1 deletion modules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ endfunction()
# Usage:
# add_module_library(<name> [sources...] FALLBACK [sources...] [IF enabled])
function(add_module_library name)
cmake_parse_arguments(AML "" "IF" "FALLBACK" ${ARGN})
cmake_parse_arguments(AML "" "IF;MAPPER_FILE" "FALLBACK" ${ARGN})
set(sources ${AML_UNPARSED_ARGUMENTS})
set(mapper_file ${AML_MAPPER_FILE})

add_library(${name})
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX)
Expand Down Expand Up @@ -203,6 +204,47 @@ function(add_module_library name)
endforeach ()
endif ()


if (CMAKE_COMPILER_IS_GNUCXX)
get_target_property(std ${name} CXX_STANDARD)

# Propagate -fmodule-mapper=mapper_file to targets that link with this library.
target_compile_options(
${name} INTERFACE -fmodule-mapper=${MAPPER_FILE})


# Add .gcm/.o files as sources to make sure they are built before the library.
set(sources_in ${sources})
set(sources)
set(gcms)
foreach (src ${sources_in})
get_filename_component(src_we ${src} NAME_WE)
set(obj ${src_we}.o)
set(sources ${sources} ${CMAKE_CURRENT_BINARY_DIR}/${obj})
set(gcm ${CMAKE_CURRENT_BINARY_DIR}/gcm.cache/${src_we}.gcm)
set(gcms ${gcms} ${gcm})
add_custom_command(
OUTPUT ${obj} ${gcm}
COMMAND ${CMAKE_CXX_COMPILER} $<TARGET_PROPERTY:${name},COMPILE_OPTIONS>
-std=c++${std} -fmodules-ts
-xc++ -c -o ${obj} ${CMAKE_CURRENT_SOURCE_DIR}/${src}
# Required by the generator expression above.
COMMAND_EXPAND_LISTS
DEPENDS ${src})
endforeach ()

# Generate moduler mapper `-fmodule-mapper`
add_custom_command(
OUTPUT ${mapper_file}
COMMAND ${CMAKE_COMMAND} -DCMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR} -DMAPPER_FILE=${mapper_file} -P ${CMAKE_CURRENT_LIST_DIR}/../mapper.cmake
# Required by the generator expression above.
COMMAND_EXPAND_LISTS
DEPENDS ${gcms})

set_source_files_properties(
${sources} PROPERTIES OBJECT_DEPENDS ${mapper_file})
endif ()

target_sources(${name} PRIVATE ${sources})

if (MSVC)
Expand Down