Mercurial > hg > sv-dependency-builds
comparison src/capnproto-git-20161025/c++/cmake/CapnProtoMacros.cmake @ 133:1ac99bfc383d
Add Cap'n Proto source
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Tue, 25 Oct 2016 11:17:01 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
132:42a73082be24 | 133:1ac99bfc383d |
---|---|
1 # CAPNP_GENERATE_CPP =========================================================== | |
2 # | |
3 # Example usage: | |
4 # find_package(CapnProto) | |
5 # capnp_generate_cpp(CAPNP_SRCS CAPNP_HDRS schema.capnp) | |
6 # include_directories(${CMAKE_CURRENT_BINARY_DIR}) | |
7 # add_executable(foo main.cpp ${CAPNP_SRCS}) | |
8 # target_link_libraries(foo CapnProto::capnp-rpc) | |
9 # | |
10 # If you are using not using the RPC features you can use | |
11 # 'CapnProto::capnp' in 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 # (CMAKE_CURRENT_SOURCE_DIR 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 set(CAPNP_EXECUTABLE capnp_tool) | |
34 GET_TARGET_PROPERTY(CAPNPC_CXX_EXECUTABLE capnpc_cpp CAPNPC_CXX_EXECUTABLE) | |
35 GET_TARGET_PROPERTY(CAPNP_INCLUDE_DIRECTORY capnp_tool CAPNP_INCLUDE_DIRECTORY) | |
36 list(APPEND tool_depends capnp_tool capnpc_cpp) | |
37 endif() | |
38 if(NOT CAPNP_EXECUTABLE) | |
39 message(SEND_ERROR "Could not locate capnp executable (CAPNP_EXECUTABLE).") | |
40 endif() | |
41 if(NOT CAPNPC_CXX_EXECUTABLE) | |
42 message(SEND_ERROR "Could not locate capnpc-c++ executable (CAPNPC_CXX_EXECUTABLE).") | |
43 endif() | |
44 if(NOT CAPNP_INCLUDE_DIRECTORY) | |
45 message(SEND_ERROR "Could not locate capnp header files (CAPNP_INCLUDE_DIRECTORY).") | |
46 endif() | |
47 | |
48 # Default compiler includes | |
49 set(include_path -I ${CMAKE_CURRENT_SOURCE_DIR} -I ${CAPNP_INCLUDE_DIRECTORY}) | |
50 | |
51 if(DEFINED CAPNPC_IMPORT_DIRS) | |
52 # Append each directory as a series of '-I' flags in ${include_path} | |
53 foreach(directory ${CAPNPC_IMPORT_DIRS}) | |
54 get_filename_component(absolute_path "${directory}" ABSOLUTE) | |
55 list(APPEND include_path -I ${absolute_path}) | |
56 endforeach() | |
57 endif() | |
58 | |
59 if(DEFINED CAPNPC_OUTPUT_DIR) | |
60 # Prepend a ':' to get the format for the '-o' flag right | |
61 set(output_dir ":${CAPNPC_OUTPUT_DIR}") | |
62 else() | |
63 set(output_dir ":.") | |
64 endif() | |
65 | |
66 if(NOT DEFINED CAPNPC_SRC_PREFIX) | |
67 set(CAPNPC_SRC_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}") | |
68 endif() | |
69 get_filename_component(CAPNPC_SRC_PREFIX "${CAPNPC_SRC_PREFIX}" ABSOLUTE) | |
70 | |
71 set(${SOURCES}) | |
72 set(${HEADERS}) | |
73 foreach(schema_file ${ARGN}) | |
74 if(NOT EXISTS "${CAPNPC_SRC_PREFIX}/${schema_file}") | |
75 message(FATAL_ERROR "Cap'n Proto schema file '${CAPNPC_SRC_PREFIX}/${schema_file}' does not exist!") | |
76 endif() | |
77 get_filename_component(file_path "${schema_file}" ABSOLUTE) | |
78 get_filename_component(file_dir "${file_path}" PATH) | |
79 | |
80 # Figure out where the output files will go | |
81 if (NOT DEFINED CAPNPC_OUTPUT_DIR) | |
82 set(CAPNPC_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/") | |
83 endif() | |
84 # Output files are placed in CAPNPC_OUTPUT_DIR, at a location as if they were | |
85 # relative to CAPNPC_SRC_PREFIX. | |
86 string(LENGTH "${CAPNPC_SRC_PREFIX}" prefix_len) | |
87 string(SUBSTRING "${file_path}" 0 ${prefix_len} output_prefix) | |
88 if(NOT "${CAPNPC_SRC_PREFIX}" STREQUAL "${output_prefix}") | |
89 message(SEND_ERROR "Could not determine output path for '${schema_file}' ('${file_path}') with source prefix '${CAPNPC_SRC_PREFIX}' into '${CAPNPC_OUTPUT_DIR}'.") | |
90 endif() | |
91 | |
92 string(SUBSTRING "${file_path}" ${prefix_len} -1 output_path) | |
93 set(output_base "${CAPNPC_OUTPUT_DIR}${output_path}") | |
94 | |
95 add_custom_command( | |
96 OUTPUT "${output_base}.c++" "${output_base}.h" | |
97 COMMAND "${CAPNP_EXECUTABLE}" | |
98 ARGS compile | |
99 -o ${CAPNPC_CXX_EXECUTABLE}${output_dir} | |
100 --src-prefix ${CAPNPC_SRC_PREFIX} | |
101 ${include_path} | |
102 ${CAPNPC_FLAGS} | |
103 ${file_path} | |
104 DEPENDS "${schema_file}" ${tool_depends} | |
105 COMMENT "Compiling Cap'n Proto schema ${schema_file}" | |
106 VERBATIM | |
107 ) | |
108 | |
109 list(APPEND ${SOURCES} "${output_base}.c++") | |
110 list(APPEND ${HEADERS} "${output_base}.h") | |
111 endforeach() | |
112 | |
113 set_source_files_properties(${${SOURCES}} ${${HEADERS}} PROPERTIES GENERATED TRUE) | |
114 set(${SOURCES} ${${SOURCES}} PARENT_SCOPE) | |
115 set(${HEADERS} ${${HEADERS}} PARENT_SCOPE) | |
116 endfunction() |