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