comparison src/capnproto-git-20161025/c++/cmake/FindCapnProto.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 #
2 # Finds the Cap'n Proto libraries, and compiles schema files.
3 #
4 # Configuration variables (optional):
5 # CAPNPC_OUTPUT_DIR
6 # Directory to place compiled schema sources (default: the same directory as the schema file).
7 # CAPNPC_IMPORT_DIRS
8 # List of additional include directories for the schema compiler.
9 # (CMAKE_CURRENT_SOURCE_DIR and CAPNP_INCLUDE_DIRS are always included.)
10 # CAPNPC_SRC_PREFIX
11 # Schema file source prefix (default: CMAKE_CURRENT_SOURCE_DIR).
12 # CAPNPC_FLAGS
13 # Additional flags to pass to the schema compiler.
14 #
15 # Variables that are discovered:
16 # CAPNP_EXECUTABLE
17 # Path to the `capnp` tool (can be set to override).
18 # CAPNPC_CXX_EXECUTABLE
19 # Path to the `capnpc-c++` tool (can be set to override).
20 # CAPNP_INCLUDE_DIRS
21 # Include directories for the library's headers (can be set to override).
22 # CANP_LIBRARIES
23 # The Cap'n Proto library paths.
24 # CAPNP_LIBRARIES_LITE
25 # Paths to only the 'lite' libraries.
26 # CAPNP_DEFINITIONS
27 # Compiler definitions required for building with the library.
28 # CAPNP_FOUND
29 # Set if the libraries have been located.
30 #
31 # Example usage:
32 #
33 # find_package(CapnProto REQUIRED)
34 # include_directories(${CAPNP_INCLUDE_DIRS})
35 # add_definitions(${CAPNP_DEFINITIONS})
36 #
37 # capnp_generate_cpp(CAPNP_SRCS CAPNP_HDRS schema.capnp)
38 # add_executable(a a.cc ${CAPNP_SRCS} ${CAPNP_HDRS})
39 # target_link_library(a ${CAPNP_LIBRARIES})
40 #
41 # For out-of-source builds:
42 #
43 # set(CAPNPC_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
44 # include_directories(${CAPNPC_OUTPUT_DIR})
45 # capnp_generate_cpp(...)
46 #
47
48 # CAPNP_GENERATE_CPP ===========================================================
49
50 function(CAPNP_GENERATE_CPP SOURCES HEADERS)
51 if(NOT ARGN)
52 message(SEND_ERROR "CAPNP_GENERATE_CPP() called without any source files.")
53 endif()
54 if(NOT CAPNP_EXECUTABLE)
55 message(SEND_ERROR "Could not locate capnp executable (CAPNP_EXECUTABLE).")
56 endif()
57 if(NOT CAPNPC_CXX_EXECUTABLE)
58 message(SEND_ERROR "Could not locate capnpc-c++ executable (CAPNPC_CXX_EXECUTABLE).")
59 endif()
60 if(NOT CAPNP_INCLUDE_DIRS)
61 message(SEND_ERROR "Could not locate capnp header files (CAPNP_INCLUDE_DIRS).")
62 endif()
63
64 # Default compiler includes
65 set(include_path -I ${CMAKE_CURRENT_SOURCE_DIR} -I ${CAPNP_INCLUDE_DIRS})
66
67 if(DEFINED CAPNPC_IMPORT_DIRS)
68 # Append each directory as a series of '-I' flags in ${include_path}
69 foreach(directory ${CAPNPC_IMPORT_DIRS})
70 get_filename_component(absolute_path "${directory}" ABSOLUTE)
71 list(APPEND include_path -I ${absolute_path})
72 endforeach()
73 endif()
74
75 if(DEFINED CAPNPC_OUTPUT_DIR)
76 # Prepend a ':' to get the format for the '-o' flag right
77 set(output_dir ":${CAPNPC_OUTPUT_DIR}")
78 else()
79 set(output_dir ":.")
80 endif()
81
82 if(NOT DEFINED CAPNPC_SRC_PREFIX)
83 set(CAPNPC_SRC_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}")
84 endif()
85 get_filename_component(CAPNPC_SRC_PREFIX "${CAPNPC_SRC_PREFIX}" ABSOLUTE)
86
87 set(${SOURCES})
88 set(${HEADERS})
89 foreach(schema_file ${ARGN})
90 get_filename_component(file_path "${schema_file}" ABSOLUTE)
91 get_filename_component(file_dir "${file_path}" PATH)
92
93 # Figure out where the output files will go
94 if (NOT DEFINED CAPNPC_OUTPUT_DIR)
95 set(output_base "${file_path}")
96 else()
97 # Output files are placed in CAPNPC_OUTPUT_DIR, at a location as if they were
98 # relative to CAPNPC_SRC_PREFIX.
99 string(LENGTH "${CAPNPC_SRC_PREFIX}" prefix_len)
100 string(SUBSTRING "${file_path}" 0 ${prefix_len} output_prefix)
101 if(NOT "${CAPNPC_SRC_PREFIX}" STREQUAL "${output_prefix}")
102 message(SEND_ERROR "Could not determine output path for '${schema_file}' ('${file_path}') with source prefix '${CAPNPC_SRC_PREFIX}' into '${CAPNPC_OUTPUT_DIR}'.")
103 endif()
104
105 string(SUBSTRING "${file_path}" ${prefix_len} -1 output_path)
106 set(output_base "${CAPNPC_OUTPUT_DIR}${output_path}")
107 endif()
108
109 add_custom_command(
110 OUTPUT "${output_base}.c++" "${output_base}.h"
111 COMMAND "${CAPNP_EXECUTABLE}"
112 ARGS compile
113 -o ${CAPNPC_CXX_EXECUTABLE}${output_dir}
114 --src-prefix ${CAPNPC_SRC_PREFIX}
115 ${include_path}
116 ${CAPNPC_FLAGS}
117 ${file_path}
118 DEPENDS "${schema_file}"
119 COMMENT "Compiling Cap'n Proto schema ${schema_file}"
120 VERBATIM
121 )
122 list(APPEND ${SOURCES} "${output_base}.c++")
123 list(APPEND ${HEADERS} "${output_base}.h")
124 endforeach()
125
126 set_source_files_properties(${${SOURCES}} ${${HEADERS}} PROPERTIES GENERATED TRUE)
127 set(${SOURCES} ${${SOURCES}} PARENT_SCOPE)
128 set(${HEADERS} ${${HEADERS}} PARENT_SCOPE)
129 endfunction()
130
131 # Find Libraries/Paths =========================================================
132
133 # Use pkg-config to get path hints and definitions
134 find_package(PkgConfig QUIET)
135 pkg_check_modules(PKGCONFIG_CAPNP capnp)
136 pkg_check_modules(PKGCONFIG_CAPNP_RPC capnp-rpc QUIET)
137 pkg_check_modules(PKGCONFIG_CAPNP_JSON capnp-json QUIET)
138
139 find_library(CAPNP_LIB_KJ kj
140 HINTS "${PKGCONFIG_CAPNP_LIBDIR}" ${PKGCONFIG_CAPNP_LIBRARY_DIRS}
141 )
142 find_library(CAPNP_LIB_KJ-ASYNC kj-async
143 HINTS "${PKGCONFIG_CAPNP_RPC_LIBDIR}" ${PKGCONFIG_CAPNP_RPC_LIBRARY_DIRS}
144 )
145 find_library(CAPNP_LIB_CAPNP capnp
146 HINTS "${PKGCONFIG_CAPNP_LIBDIR}" ${PKGCONFIG_CAPNP_LIBRARY_DIRS}
147 )
148 find_library(CAPNP_LIB_CAPNP-RPC capnp-rpc
149 HINTS "${PKGCONFIG_CAPNP_RPC_LIBDIR}" ${PKGCONFIG_CAPNP_RPC_LIBRARY_DIRS}
150 )
151 find_library(CAPNP_LIB_CAPNP-JSON capnp-json
152 HINTS "${PKGCONFIG_CAPNP_JSON_LIBDIR}" ${PKGCONFIG_CAPNP_JSON_LIBRARY_DIRS}
153 )
154 mark_as_advanced(CAPNP_LIB_KJ CAPNP_LIB_KJ-ASYNC CAPNP_LIB_CAPNP CAPNP_LIB_CAPNP-RPC CAPNP_LIB_CAPNP-JSON)
155 set(CAPNP_LIBRARIES_LITE
156 ${CAPNP_LIB_CAPNP}
157 ${CAPNP_LIB_KJ}
158 )
159 set(CAPNP_LIBRARIES
160 ${CAPNP_LIB_CAPNP-JSON}
161 ${CAPNP_LIB_CAPNP-RPC}
162 ${CAPNP_LIB_CAPNP}
163 ${CAPNP_LIB_KJ-ASYNC}
164 ${CAPNP_LIB_KJ}
165 )
166
167 # Was only the 'lite' library found?
168 if(CAPNP_LIB_CAPNP AND NOT CAPNP_LIB_CAPNP-RPC)
169 set(CAPNP_DEFINITIONS -DCAPNP_LITE)
170 else()
171 set(CAPNP_DEFINITIONS)
172 endif()
173
174 find_path(CAPNP_INCLUDE_DIRS capnp/generated-header-support.h
175 HINTS "${PKGCONFIG_CAPNP_INCLUDEDIR}" ${PKGCONFIG_CAPNP_INCLUDE_DIRS}
176 )
177
178 find_program(CAPNP_EXECUTABLE
179 NAMES capnp
180 DOC "Cap'n Proto Command-line Tool"
181 HINTS "${PKGCONFIG_CAPNP_PREFIX}/bin"
182 )
183
184 find_program(CAPNPC_CXX_EXECUTABLE
185 NAMES capnpc-c++
186 DOC "Capn'n Proto C++ Compiler"
187 HINTS "${PKGCONFIG_CAPNP_PREFIX}/bin"
188 )
189
190 # Only *require* the include directory, libkj, and libcapnp. If compiling with
191 # CAPNP_LITE, nothing else will be found.
192 include(FindPackageHandleStandardArgs)
193 find_package_handle_standard_args(CAPNP DEFAULT_MSG
194 CAPNP_INCLUDE_DIRS
195 CAPNP_LIB_KJ
196 CAPNP_LIB_CAPNP
197 )