comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/cmake/CapnProto/CapnProtoMacros.cmake @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 68:5028fdace37b
1 # CAPNP_GENERATE_CPP ===========================================================
2 #
3 # Example usage:
4 # find_package(CapnProto)
5 # capnp_generate_cpp(CAPNP_SRCS CAPNP_HDRS schema.capnp)
6 # add_executable(foo main.cpp ${CAPNP_SRCS})
7 # target_link_libraries(foo PRIVATE CapnProto::capnp-rpc)
8 # target_include_directories(foo PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
9 #
10 # If you are not using the RPC features you can use 'CapnProto::capnp' in the
11 # target_link_libraries call
12 #
13 # Configuration variables (optional):
14 # CAPNPC_OUTPUT_DIR
15 # Directory to place compiled schema sources (default: CMAKE_CURRENT_BINARY_DIR).
16 # CAPNPC_IMPORT_DIRS
17 # List of additional include directories for the schema compiler.
18 # (CAPNPC_SRC_PREFIX and CAPNP_INCLUDE_DIRECTORY are always included.)
19 # CAPNPC_SRC_PREFIX
20 # Schema file source prefix (default: CMAKE_CURRENT_SOURCE_DIR).
21 # CAPNPC_FLAGS
22 # Additional flags to pass to the schema compiler.
23 #
24 # TODO: convert to cmake_parse_arguments
25
26 function(CAPNP_GENERATE_CPP SOURCES HEADERS)
27 if(NOT ARGN)
28 message(SEND_ERROR "CAPNP_GENERATE_CPP() called without any source files.")
29 endif()
30 set(tool_depends ${EMPTY_STRING})
31 #Use cmake targets available
32 if(TARGET capnp_tool)
33 if(NOT CAPNP_EXECUTABLE)
34 set(CAPNP_EXECUTABLE $<TARGET_FILE:capnp_tool>)
35 endif()
36 if(NOT CAPNPC_CXX_EXECUTABLE)
37 get_target_property(CAPNPC_CXX_EXECUTABLE capnpc_cpp CAPNPC_CXX_EXECUTABLE)
38 endif()
39 if(NOT CAPNP_INCLUDE_DIRECTORY)
40 get_target_property(CAPNP_INCLUDE_DIRECTORY capnp_tool CAPNP_INCLUDE_DIRECTORY)
41 endif()
42 list(APPEND tool_depends capnp_tool capnpc_cpp)
43 endif()
44 if(NOT CAPNP_EXECUTABLE)
45 message(SEND_ERROR "Could not locate capnp executable (CAPNP_EXECUTABLE).")
46 endif()
47 if(NOT CAPNPC_CXX_EXECUTABLE)
48 message(SEND_ERROR "Could not locate capnpc-c++ executable (CAPNPC_CXX_EXECUTABLE).")
49 endif()
50 if(NOT CAPNP_INCLUDE_DIRECTORY)
51 message(SEND_ERROR "Could not locate capnp header files (CAPNP_INCLUDE_DIRECTORY).")
52 endif()
53
54 if(DEFINED CAPNPC_OUTPUT_DIR)
55 # Prepend a ':' to get the format for the '-o' flag right
56 set(output_dir ":${CAPNPC_OUTPUT_DIR}")
57 else()
58 set(output_dir ":.")
59 endif()
60
61 if(NOT DEFINED CAPNPC_SRC_PREFIX)
62 set(CAPNPC_SRC_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}")
63 endif()
64 get_filename_component(CAPNPC_SRC_PREFIX "${CAPNPC_SRC_PREFIX}" ABSOLUTE)
65
66 # Default compiler includes. Note that in capnp's own test usage of capnp_generate_cpp(), these
67 # two variables will end up evaluating to the same directory. However, it's difficult to
68 # deduplicate them because if CAPNP_INCLUDE_DIRECTORY came from the capnp_tool target property,
69 # then it must be a generator expression in order to handle usages in both the build tree and the
70 # install tree. This vastly overcomplicates duplication detection, so the duplication doesn't seem
71 # worth fixing.
72 set(include_path -I "${CAPNPC_SRC_PREFIX}" -I "${CAPNP_INCLUDE_DIRECTORY}")
73
74 if(DEFINED CAPNPC_IMPORT_DIRS)
75 # Append each directory as a series of '-I' flags in ${include_path}
76 foreach(directory ${CAPNPC_IMPORT_DIRS})
77 get_filename_component(absolute_path "${directory}" ABSOLUTE)
78 list(APPEND include_path -I "${absolute_path}")
79 endforeach()
80 endif()
81
82 set(${SOURCES})
83 set(${HEADERS})
84 foreach(schema_file ${ARGN})
85 get_filename_component(file_path "${schema_file}" ABSOLUTE)
86 get_filename_component(file_dir "${file_path}" PATH)
87 if(NOT EXISTS "${file_path}")
88 message(FATAL_ERROR "Cap'n Proto schema file '${file_path}' does not exist!")
89 endif()
90
91 # Figure out where the output files will go
92 if (NOT DEFINED CAPNPC_OUTPUT_DIR)
93 set(CAPNPC_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/")
94 endif()
95 # Output files are placed in CAPNPC_OUTPUT_DIR, at a location as if they were
96 # relative to CAPNPC_SRC_PREFIX.
97 string(LENGTH "${CAPNPC_SRC_PREFIX}" prefix_len)
98 string(SUBSTRING "${file_path}" 0 ${prefix_len} output_prefix)
99 if(NOT "${CAPNPC_SRC_PREFIX}" STREQUAL "${output_prefix}")
100 message(SEND_ERROR "Could not determine output path for '${schema_file}' ('${file_path}') with source prefix '${CAPNPC_SRC_PREFIX}' into '${CAPNPC_OUTPUT_DIR}'.")
101 endif()
102
103 string(SUBSTRING "${file_path}" ${prefix_len} -1 output_path)
104 set(output_base "${CAPNPC_OUTPUT_DIR}${output_path}")
105
106 add_custom_command(
107 OUTPUT "${output_base}.c++" "${output_base}.h"
108 COMMAND "${CAPNP_EXECUTABLE}"
109 ARGS compile
110 -o ${CAPNPC_CXX_EXECUTABLE}${output_dir}
111 --src-prefix ${CAPNPC_SRC_PREFIX}
112 ${include_path}
113 ${CAPNPC_FLAGS}
114 ${file_path}
115 DEPENDS "${schema_file}" ${tool_depends}
116 COMMENT "Compiling Cap'n Proto schema ${schema_file}"
117 VERBATIM
118 )
119
120 list(APPEND ${SOURCES} "${output_base}.c++")
121 list(APPEND ${HEADERS} "${output_base}.h")
122 endforeach()
123
124 set_source_files_properties(${${SOURCES}} ${${HEADERS}} PROPERTIES GENERATED TRUE)
125 set(${SOURCES} ${${SOURCES}} PARENT_SCOPE)
126 set(${HEADERS} ${${HEADERS}} PARENT_SCOPE)
127 endfunction()