cmake: Introduce `LibmultiprocessMacros` module #95

pull hebasto wants to merge 3 commits into bitcoin-core:master from hebasto:240314-macro changing 4 files +110 −159
  1. hebasto commented at 1:03 AM on March 15, 2024: member

    This PR introduces the LibmultiprocessMacros module that is used internally and might be exported as a part of the (future) LibmultiprocessGen package (it is a subject of the follow-up PR).

    Also see a discussion in https://github.com/hebasto/bitcoin/pull/118.

  2. hebasto force-pushed on Mar 16, 2024
  3. hebasto marked this as ready for review on Mar 16, 2024
  4. hebasto commented at 4:12 PM on March 16, 2024: member
  5. hebasto renamed this:
    cmake: Introduce `MpgenMacros` module
    cmake: Introduce `LibmultiprocessMacros` module
    on Mar 16, 2024
  6. hebasto commented at 4:15 PM on March 16, 2024: member

    The PR title and description have been updated.

  7. ryanofsky assigned ryanofsky on Mar 19, 2024
  8. in cmake/LibmultiprocessMacros.cmake:23 in 028d697b11 outdated
      18 | +  set(build_include_prefix ${CMAKE_BINARY_DIR})
      19 | +  file(RELATIVE_PATH relative_path ${CMAKE_SOURCE_DIR} ${include_prefix})
      20 | +  if(relative_path)
      21 | +    string(APPEND source_include_prefix "/" "${relative_path}")
      22 | +    string(APPEND build_include_prefix "/" "${relative_path}")
      23 | +  endif()
    


    ryanofsky commented at 7:08 PM on March 28, 2024:

    In commit "cmake: Add LibmultiprocessMacros module" (028d697b110166b2e1b0114594af4377a9583c93)

    I think source_include_prefix variable is redundant here and will always be identical to include_prefix. The code is just removing ${CMAKE_SOURCE_DIR} from the beginning of include_prefix and then adding it back again, so it has no effect. I also found this function generally hard to wrap my head around so would suggest adding documentation:

    --- a/cmake/LibmultiprocessMacros.cmake
    +++ b/cmake/LibmultiprocessMacros.cmake
    @@ -2,6 +2,51 @@
     # Distributed under the MIT software license, see the accompanying
     # file COPYING or https://opensource.org/license/mit/.
     
    +# target_capnp_sources
    +#
    +# This function adds build steps to generate C++ files from Cap'n Proto files
    +# and build them as part of a specified target.
    +#
    +# Arguments:
    +#
    +#   target: The name of the CMake target (e.g., a library or executable) to
    +#     which the generated source files will be added. This target must already
    +#     be defined elsewhere in the CMake scripts.
    +#
    +#   include_prefix: absolute path indicating what portion of capnp source paths
    +#     should be used in relative #include statements in the generated c++
    +#     files. For example, if the .capnp path is /home/src/lib/schema.capnp
    +#     and include_prefix is /home/src, generated includes look like:
    +#
    +#       #include <lib/schema.capnp.h>
    +#
    +#     And if include_prefix is /home/src/lib, generated includes look like:
    +#
    +#       #include <schema.capnp.h>
    +#
    +#     The specified include_prefix should be ${CMAKE_SOURCE_DIR} or a
    +#     subdirectory of it to include files relative to the project root. It can
    +#     be ${CMAKE_CURRENT_SOURCE_DIR} to include files relative to the current
    +#     source directory.
    +#
    +# Additional Unnamed Arguments:
    +#
    +#   After `target` and `include_prefix`, all unnamed arguments are treated as
    +#   paths to `.capnp` schema files. These should be paths relative to
    +#   ${CMAKE_CURRENT_SOURCE_DIR}.
    +#
    +# Optional Keyword Arguments:
    +#
    +#   IMPORT_PATHS: Specifies additional directories to search for imported
    +#     `.capnp` files.
    +#
    +# Example:
    +#   # Assuming `my_library` is a target and `lib/` contains `.capnp` schema
    +#   # files with imports from `include/`.
    +#   target_capnp_sources(my_library "${CMAKE_SOURCE_DIR}"
    +#                        lib/schema1.capnp lib/schema2.capnp
    +#                        IMPORT_PATHS ${CMAKE_SOURCE_DIR}/include)
    +#
     function(target_capnp_sources target include_prefix)
       cmake_parse_arguments(PARSE_ARGV 2
         "TCS"           # prefix
    @@ -14,18 +59,10 @@ function(target_capnp_sources target include_prefix)
         message(FATAL_ERROR "Target 'Libmultiprocess::mpgen' does not exist.")
       endif()
     
    -  set(source_include_prefix ${CMAKE_SOURCE_DIR})
    -  set(build_include_prefix ${CMAKE_BINARY_DIR})
    -  file(RELATIVE_PATH relative_path ${CMAKE_SOURCE_DIR} ${include_prefix})
    -  if(relative_path)
    -    string(APPEND source_include_prefix "/" "${relative_path}")
    -    string(APPEND build_include_prefix "/" "${relative_path}")
    -  endif()
    -
       foreach(capnp_file IN LISTS TCS_UNPARSED_ARGUMENTS)
         add_custom_command(
           OUTPUT ${capnp_file}.c++ ${capnp_file}.h ${capnp_file}.proxy-client.c++ ${capnp_file}.proxy-types.h ${capnp_file}.proxy-server.c++ ${capnp_file}.proxy-types.c++ ${capnp_file}.proxy.h
    -      COMMAND Libmultiprocess::mpgen ${CMAKE_CURRENT_SOURCE_DIR} ${source_include_prefix} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_file} ${TCS_IMPORT_PATHS}
    +      COMMAND Libmultiprocess::mpgen ${CMAKE_CURRENT_SOURCE_DIR} ${include_prefix} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_file} ${TCS_IMPORT_PATHS}
           DEPENDS ${capnp_file}
           VERBATIM
         )
    @@ -36,7 +73,16 @@ function(target_capnp_sources target include_prefix)
           ${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-types.c++
         )
       endforeach()
    +
    +  # Translate include_prefix from a source path to a binary path and add it as a
    +  # target include directory.
    +  set(build_include_prefix ${CMAKE_BINARY_DIR})
    +  file(RELATIVE_PATH relative_path ${CMAKE_SOURCE_DIR} ${include_prefix})
    +  if(relative_path)
    +    string(APPEND build_include_prefix "/" "${relative_path}")
    +  endif()
       target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${build_include_prefix}>)
    +
       if(TARGET Libmultiprocess::multiprocess)
         target_link_libraries(${target} PRIVATE Libmultiprocess::multiprocess)
       endif()
    

    hebasto commented at 11:48 AM on March 29, 2024:

    Thanks! Done.

  9. ryanofsky approved
  10. ryanofsky commented at 7:14 PM on March 28, 2024: collaborator

    Code review ACK 45341dcea0c1172d41e16bc80bdd9c7a9d0904bf

    This looks great! Thanks for writing this. I suggested a documentation comment and a minor simplification above, but would also be happy to merge this as it is. You can let me know what you prefer.

  11. cmake: Add `LibmultiprocessMacros` module
    Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
    d9ec22f81b
  12. cmake, refactor: Use `target_capnp_sources` for `mptest` target bd2dfe27b0
  13. cmake, refactor: Use `target_capnp_sources` for examples 66643d8eaa
  14. hebasto force-pushed on Mar 29, 2024
  15. hebasto commented at 11:48 AM on March 29, 2024: member

    @ryanofsky

    I suggested a documentation comment and a minor simplification above, but would also be happy to merge this as it is. You can let me know what you prefer.

    Thank you!

    Your suggestions have been implemented.

  16. ryanofsky approved
  17. ryanofsky commented at 11:36 PM on March 29, 2024: collaborator

    Code review ACK 66643d8eaabdaf6b2d69ecc0659350175a394d5b

    Thanks for the update. I think I do want to rename LibmultiprocessMacros.cmake to TargetCapnpSources.cmake so it is named after the function it contains like other cmake modules, but I will do this in a followup

  18. ryanofsky merged this on Mar 29, 2024
  19. ryanofsky closed this on Mar 29, 2024

  20. ryanofsky referenced this in commit 003eb04d6d on Mar 30, 2024
  21. ryanofsky referenced this in commit c6a1d7fb6b on Mar 30, 2024
  22. ryanofsky referenced this in commit 3f8483b61a on Mar 30, 2024
  23. hebasto commented at 5:06 AM on March 30, 2024: member

    I think I do want to rename LibmultiprocessMacros.cmake to TargetCapnpSources.cmake so it is named after the function it contains like other cmake modules, but I will do this in a followup

    FWIW, when I chose the name, I followed the wide accepted practice for naming a CMake file, which provides the package-specific functions. For example, https://packages.ubuntu.com/jammy/amd64/qtbase5-dev/filelist

  24. hebasto deleted the branch on Mar 30, 2024
  25. ryanofsky commented at 3:18 PM on March 30, 2024: collaborator

    (EDIT: This comment was actually posted in response to https://github.com/chaincodelabs/libmultiprocess/pull/97#issuecomment-2027937177 in the other issue)

    Oops, sorry, I saw the <prefix>/(cmake|CMake)/ entry in that table and thought just putting the files in the cmake/ directory would work. I didn't realize that entry does not include the "lib" component though. I guess the only way to fix this is to repeat the package name twice in installed paths, which seems ugly to me but does seem to be the cmake convention as you pointed out https://github.com/chaincodelabs/libmultiprocess/pull/95#issuecomment-2027918956

    I do want package names to be consistent with component names, though. Will post a followup adding directory prefixes.

  26. ryanofsky referenced this in commit 3e6c61fdc2 on Jun 13, 2024
  27. ryanofsky referenced this in commit eecd63c8e2 on Jul 11, 2024
  28. ryanofsky referenced this in commit e58937ea45 on Jul 12, 2024
  29. ryanofsky referenced this in commit 48a2fc6fb1 on Jul 16, 2024
  30. ryanofsky referenced this in commit 8574f79db9 on Jul 16, 2024
  31. Sjors referenced this in commit c2dbbc5fb7 on Jul 17, 2024
  32. Sjors referenced this in commit fa0a0fa537 on Jul 17, 2024
  33. Sjors referenced this in commit 10100360b6 on Jul 18, 2024
  34. Sjors referenced this in commit 39c615dbdc on Jul 18, 2024
  35. ryanofsky referenced this in commit 33e86e04d1 on Jul 18, 2024
  36. ryanofsky referenced this in commit b3a5eb738f on Jul 18, 2024
  37. Sjors referenced this in commit 3cfcac15ba on Jul 19, 2024
  38. ryanofsky referenced this in commit 261395944e on Jul 24, 2024
  39. ryanofsky referenced this in commit c030548f20 on Jul 26, 2024
  40. ryanofsky referenced this in commit ebe93a5d0a on Sep 26, 2024
  41. bitcoin-core locked this on Jun 25, 2025

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/libmultiprocess. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-18 15:30 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me