blob: cc07ce0228dddc5dd873d81bbea4077689bf8bd5 [file] [log] [blame]
Wyatt Heplerc9e51d22020-10-29 09:12:37 -07001# Copyright 2020 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7# https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14include_guard(GLOBAL)
15
16# Declares a protocol buffers library. This function creates a library for each
17# supported protocol buffer implementation:
18#
19# ${NAME}.pwpb - pw_protobuf generated code
20# ${NAME}.nanopb - Nanopb generated code (requires Nanopb)
21#
22# This function also creates libraries for generating pw_rpc code:
23#
24# ${NAME}.nanopb_rpc - generates Nanopb pw_rpc code
25# ${NAME}.raw_rpc - generates raw pw_rpc (no protobuf library) code
26# ${NAME}.pwpb_rpc - (Not implemented) generates pw_protobuf pw_rpc code
27#
28# Args:
29#
30# NAME - the base name of the libraries to create
31# SOURCES - .proto source files
32# DEPS - dependencies on other pw_proto_library targets
Wyatt Hepler752d7d32021-03-02 09:02:23 -080033# PREFIX - prefix add to the proto files
34# STRIP_PREFIX - prefix to remove from the proto files
35# INPUTS - files to include along with the .proto files (such as Nanopb
36# .options files
Wyatt Heplerc9e51d22020-10-29 09:12:37 -070037#
38function(pw_proto_library NAME)
Wyatt Hepler752d7d32021-03-02 09:02:23 -080039 cmake_parse_arguments(PARSE_ARGV 1 arg "" "STRIP_PREFIX;PREFIX"
40 "SOURCES;INPUTS;DEPS")
Wyatt Heplerc9e51d22020-10-29 09:12:37 -070041
Wyatt Hepler752d7d32021-03-02 09:02:23 -080042 set(out_dir "${CMAKE_CURRENT_BINARY_DIR}/${NAME}")
Wyatt Heplerc9e51d22020-10-29 09:12:37 -070043
44 # Use INTERFACE libraries to track the proto include paths that are passed to
45 # protoc.
46 set(include_deps "${arg_DEPS}")
47 list(TRANSFORM include_deps APPEND ._includes)
48
49 add_library("${NAME}._includes" INTERFACE)
50 target_include_directories("${NAME}._includes" INTERFACE ".")
51 target_link_libraries("${NAME}._includes" INTERFACE ${include_deps})
52
53 # Generate a file with all include paths needed by protoc.
Wyatt Hepler752d7d32021-03-02 09:02:23 -080054 set(include_file "${out_dir}/include_paths.txt")
Wyatt Heplerc9e51d22020-10-29 09:12:37 -070055 file(GENERATE OUTPUT "${include_file}"
56 CONTENT
57 "$<TARGET_PROPERTY:${NAME}._includes,INTERFACE_INCLUDE_DIRECTORIES>")
58
Wyatt Hepler752d7d32021-03-02 09:02:23 -080059 if("${arg_STRIP_PREFIX}" STREQUAL "")
60 set(arg_STRIP_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}")
61 endif()
62
63 foreach(path IN LISTS arg_SOURCES arg_INPUTS)
64 get_filename_component(abspath "${path}" ABSOLUTE)
65 list(APPEND files_to_mirror "${abspath}")
66 endforeach()
67
68 # Mirror the sources to the output directory with the specified prefix.
69 _pw_rebase_paths(
70 sources "${out_dir}/sources/${arg_PREFIX}" "${arg_STRIP_PREFIX}" "${arg_SOURCES}" "")
71 _pw_rebase_paths(
72 inputs "${out_dir}/sources/${arg_PREFIX}" "${arg_STRIP_PREFIX}" "${arg_INPUTS}" "")
73
74 add_custom_command(
75 COMMAND
76 python
77 "$ENV{PW_ROOT}/pw_build/py/pw_build/mirror_tree.py"
78 --source-root "${arg_STRIP_PREFIX}"
79 --directory "${out_dir}/sources/${arg_PREFIX}"
80 ${files_to_mirror}
81 DEPENDS
82 "$ENV{PW_ROOT}/pw_build/py/pw_build/mirror_tree.py"
83 ${files_to_mirror}
84 ${arg_DEPS}
85 OUTPUT
86 ${sources} ${inputs}
87 )
88
Wyatt Heplerc9e51d22020-10-29 09:12:37 -070089 # Create a protobuf target for each supported protobuf library.
90 _pw_pwpb_library(
Wyatt Hepler752d7d32021-03-02 09:02:23 -080091 "${NAME}" "${sources}" "${inputs}" "${arg_DEPS}" "${include_file}" "${out_dir}")
Wyatt Heplerc9e51d22020-10-29 09:12:37 -070092 _pw_raw_rpc_library(
Wyatt Hepler752d7d32021-03-02 09:02:23 -080093 "${NAME}" "${sources}" "${inputs}" "${arg_DEPS}" "${include_file}" "${out_dir}")
Wyatt Heplerc9e51d22020-10-29 09:12:37 -070094 _pw_nanopb_library(
Wyatt Hepler752d7d32021-03-02 09:02:23 -080095 "${NAME}" "${sources}" "${inputs}" "${arg_DEPS}" "${include_file}" "${out_dir}")
Wyatt Heplerc9e51d22020-10-29 09:12:37 -070096 _pw_nanopb_rpc_library(
Wyatt Hepler752d7d32021-03-02 09:02:23 -080097 "${NAME}" "${sources}" "${inputs}" "${arg_DEPS}" "${include_file}" "${out_dir}")
Wyatt Heplerc9e51d22020-10-29 09:12:37 -070098endfunction(pw_proto_library)
99
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800100function(_pw_rebase_paths VAR OUT_DIR ROOT FILES EXTENSIONS)
101 foreach(file IN LISTS FILES)
102 get_filename_component(file "${file}" ABSOLUTE)
103 file(RELATIVE_PATH file "${ROOT}" "${file}")
104
105 if ("${EXTENSIONS}" STREQUAL "")
106 list(APPEND mirrored_files "${OUT_DIR}/${file}")
107 else()
108 foreach(ext IN LISTS EXTENSIONS)
109 get_filename_component(dir "${file}" DIRECTORY)
110 get_filename_component(name "${file}" NAME_WE)
111 list(APPEND mirrored_files "${OUT_DIR}/${dir}/${name}${ext}")
112 endforeach()
113 endif()
114 endforeach()
115
116 set("${VAR}" "${mirrored_files}" PARENT_SCOPE)
117endfunction(_pw_rebase_paths)
118
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700119# Internal function that invokes protoc through generate_protos.py.
120function(_pw_generate_protos
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800121 TARGET LANGUAGE PLUGIN OUTPUT_EXTS INCLUDE_FILE OUT_DIR SOURCES INPUTS DEPS)
122 # Determine the names of the compiled output files.
123 _pw_rebase_paths(outputs
124 "${OUT_DIR}/${LANGUAGE}" "${OUT_DIR}/sources" "${SOURCES}" "${OUTPUT_EXTS}")
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800125
126 # Export the output files to the caller's scope so it can use them if needed.
127 set(generated_outputs "${outputs}" PARENT_SCOPE)
128
129 if("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
130 get_filename_component(dir "${source_file}" DIRECTORY)
131 get_filename_component(name "${source_file}" NAME_WE)
132 set(PLUGIN "${dir}/${name}.bat")
133 endif()
134
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700135 set(script "$ENV{PW_ROOT}/pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py")
136 add_custom_command(
137 COMMAND
138 python
139 "${script}"
140 --language "${LANGUAGE}"
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800141 --plugin-path "${PLUGIN}"
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700142 --include-file "${INCLUDE_FILE}"
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800143 --compile-dir "${OUT_DIR}/sources"
144 --out-dir "${OUT_DIR}/${LANGUAGE}"
145 --sources ${SOURCES}
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700146 DEPENDS
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700147 ${script}
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800148 ${SOURCES}
149 ${INPUTS}
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700150 ${DEPS}
151 OUTPUT
152 ${outputs}
153 )
154 add_custom_target("${TARGET}" DEPENDS ${outputs})
155endfunction(_pw_generate_protos)
156
157# Internal function that creates a pwpb proto library.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800158function(_pw_pwpb_library NAME SOURCES INPUTS DEPS INCLUDE_FILE OUT_DIR)
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700159 list(TRANSFORM DEPS APPEND .pwpb)
160
161 _pw_generate_protos("${NAME}.generate.pwpb"
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800162 pwpb
163 "$ENV{PW_ROOT}/pw_protobuf/py/pw_protobuf/plugin.py"
164 ".pwpb.h"
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700165 "${INCLUDE_FILE}"
166 "${OUT_DIR}"
167 "${SOURCES}"
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800168 "${INPUTS}"
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700169 "${DEPS}"
170 )
171
172 # Create the library with the generated source files.
173 add_library("${NAME}.pwpb" INTERFACE)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800174 target_include_directories("${NAME}.pwpb" INTERFACE "${OUT_DIR}/pwpb")
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700175 target_link_libraries("${NAME}.pwpb" INTERFACE pw_protobuf ${DEPS})
176 add_dependencies("${NAME}.pwpb" "${NAME}.generate.pwpb")
177endfunction(_pw_pwpb_library)
178
179# Internal function that creates a raw_rpc proto library.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800180function(_pw_raw_rpc_library NAME SOURCES INPUTS DEPS INCLUDE_FILE OUT_DIR)
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700181 list(TRANSFORM DEPS APPEND .raw_rpc)
182
183 _pw_generate_protos("${NAME}.generate.raw_rpc"
184 raw_rpc
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800185 "$ENV{PW_ROOT}/pw_rpc/py/pw_rpc/plugin_raw.py"
186 ".raw_rpc.pb.h"
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700187 "${INCLUDE_FILE}"
188 "${OUT_DIR}"
189 "${SOURCES}"
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800190 "${INPUTS}"
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700191 "${DEPS}"
192 )
193
194 # Create the library with the generated source files.
195 add_library("${NAME}.raw_rpc" INTERFACE)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800196 target_include_directories("${NAME}.raw_rpc" INTERFACE "${OUT_DIR}/raw_rpc")
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700197 target_link_libraries("${NAME}.raw_rpc"
198 INTERFACE
199 pw_rpc.raw
200 pw_rpc.server
201 ${DEPS}
202 )
203 add_dependencies("${NAME}.raw_rpc" "${NAME}.generate.raw_rpc")
204endfunction(_pw_raw_rpc_library)
205
206# Internal function that creates a nanopb proto library.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800207function(_pw_nanopb_library NAME SOURCES INPUTS DEPS INCLUDE_FILE OUT_DIR)
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700208 list(TRANSFORM DEPS APPEND .nanopb)
209
210 set(nanopb_dir "$<TARGET_PROPERTY:$<IF:$<TARGET_EXISTS:protobuf-nanopb-static>,protobuf-nanopb-static,pw_build.empty>,SOURCE_DIR>")
211 set(nanopb_plugin
212 "$<IF:$<TARGET_EXISTS:protobuf-nanopb-static>,${nanopb_dir}/generator/protoc-gen-nanopb,COULD_NOT_FIND_protobuf-nanopb-static_TARGET_PLEASE_SET_UP_NANOPB>")
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700213
214 _pw_generate_protos("${NAME}.generate.nanopb"
215 nanopb
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800216 "${nanopb_plugin}"
217 ".pb.h;.pb.c"
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700218 "${INCLUDE_FILE}"
219 "${OUT_DIR}"
220 "${SOURCES}"
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800221 "${INPUTS}"
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700222 "${DEPS}"
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700223 )
224
225 # Create the library with the generated source files.
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800226 add_library("${NAME}.nanopb" EXCLUDE_FROM_ALL ${generated_outputs})
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800227 target_include_directories("${NAME}.nanopb" PUBLIC "${OUT_DIR}/nanopb")
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700228 target_link_libraries("${NAME}.nanopb" PUBLIC pw_third_party.nanopb ${DEPS})
229 add_dependencies("${NAME}.nanopb" "${NAME}.generate.nanopb")
230endfunction(_pw_nanopb_library)
231
232# Internal function that creates a nanopb_rpc library.
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800233function(_pw_nanopb_rpc_library NAME SOURCES INPUTS DEPS INCLUDE_FILE OUT_DIR)
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700234 # Determine the names of the output files.
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700235 list(TRANSFORM DEPS APPEND .nanopb_rpc)
236
237 _pw_generate_protos("${NAME}.generate.nanopb_rpc"
238 nanopb_rpc
Wyatt Heplerd9336a42020-11-10 09:47:30 -0800239 "$ENV{PW_ROOT}/pw_rpc/py/pw_rpc/plugin_nanopb.py"
240 ".rpc.pb.h"
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700241 "${INCLUDE_FILE}"
242 "${OUT_DIR}"
243 "${SOURCES}"
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800244 "${INPUTS}"
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700245 "${DEPS}"
246 )
247
248 # Create the library with the generated source files.
249 add_library("${NAME}.nanopb_rpc" INTERFACE)
Wyatt Hepler752d7d32021-03-02 09:02:23 -0800250 target_include_directories("${NAME}.nanopb_rpc"
251 INTERFACE
252 "${OUT_DIR}/nanopb_rpc"
253 )
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700254 target_link_libraries("${NAME}.nanopb_rpc"
255 INTERFACE
256 "${NAME}.nanopb"
Wyatt Heplerdcfa92b2020-11-10 09:47:30 -0800257 pw_rpc.nanopb.method_union
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700258 pw_rpc.server
Wyatt Heplerc9e51d22020-10-29 09:12:37 -0700259 ${DEPS}
260 )
261 add_dependencies("${NAME}.nanopb_rpc" "${NAME}.generate.nanopb_rpc")
262endfunction(_pw_nanopb_rpc_library)