0

I have a custom target for emit module interface. How can I link custom target with libraries?

function(add_module TARGET)
    set_source_files_properties(${ARGN} PROPERTIES LANGUAGE CXX)

    add_custom_target(${TARGET}.pcm
        COMMAND
            ${CMAKE_CXX_COMPILER}
            -std=c++20
            -fmodules
            -fimplicit-modules
            -fbuiltin-module-map
            -fimplicit-module-maps
            -c "${CMAKE_CURRENT_SOURCE_DIR}/${ARGN}"
            -Xclang -emit-module-interface
            -o "${PREBUILT_MODULE_PATH}/${TARGET}.pcm"
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    )
    add_library(${TARGET} ${ARGN})
    add_dependencies(${TARGET} ${TARGET}.pcm)
endfunction()
add_module(raytrace src/raytrace.cpp)
target_link_libraries(raytrace stl) // not working
target_link_libraries(raytrace.pcm stl) // (1) 

(1) produces

CMake Error at CMakeLists.txt:75 (target_link_libraries):
  Utility target "raytrace.pcm" must not be used as the target of a
  target_link_libraries call.

stl is interface library added via add_subdirectory(stl)

Max Pasichnyk
  • 134
  • 1
  • 6
  • What do you mean by `// not working` comment for the line? It should link the library `raytrace` with the `stl`. – Tsyvarev Jan 17 '21 at 10:23
  • 1
    `"${CMAKE_CURRENT_SOURCE_DIR}/${ARGN}"`: Why not declare an additional parameter. If there are more than 2 parameters passed to the function this would probably result in issues. – fabian Jan 17 '21 at 11:44
  • @Tsyvarev raytrace.pcm can't find any headers from stl interface library – Max Pasichnyk Jan 17 '21 at 13:55
  • 1
    "raytrace.pcm can't find any headers from stl interface library" - This seems to be logical, as your **custom** target, which creates `raytrace.pcm`, doesn't specify include directories. CMake commands like `target_include_libraries` or `target_include_directories` affects only on command lines, **generated by CMake** itself. CMake never modifies COMMAND for custom target. – Tsyvarev Jan 17 '21 at 14:28
  • @Tsyvarev My question exactly about this. How to specify target_include_libraries for custom target, or how to create module properly, without custom target? – Max Pasichnyk Jan 17 '21 at 15:37
  • 2
    "how to create module properly, without custom target?" - According to your code, you have already check the question https://stackoverflow.com/questions/57300495/how-to-use-c20-modules-with-cmake, and its answers states that currently you cannot do that. "How to specify target_include_libraries for custom target" - In your custom COMMAND you could try to use `$` generator expression, which extracts include directories from your library target. Like in that question: https://stackoverflow.com/questions/65450808. – Tsyvarev Jan 17 '21 at 16:20
  • @Tsyvarev Thanks, It's working! But now I have one more problem /home/maxim/CLionProjects/minecraft/src/main.cpp:29:1: fatal error: module '_Builtin_stddef_max_align_t' is defined in both '/home/maxim/.cache/clang/ModuleCache/2Y5MCJ5ENFIL7/_Builtin_stddef_max_align_t-1HITC614MVKMY.pcm' and '/home/maxim/.cache/clang/ModuleCache/XFMJMT840WAT/_Builtin_stddef_max_align_t-1HITC614MVKMY.pcm' – Max Pasichnyk Jan 21 '21 at 12:33
  • I would suggest to inspect **actual command line** with the compiler invocation, which is generated by CMake, and find out which its parameter(s) is wrong. – Tsyvarev Jan 21 '21 at 13:20

0 Answers0