1 # Copyright 2020 The Pigweed Authors
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
7 # https://www.apache.org/licenses/LICENSE-2.0
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
16 # Declares a protocol buffers library. This function creates a library for each
17 # supported protocol buffer implementation:
19 # ${NAME}.pwpb - pw_protobuf generated code
20 # ${NAME}.nanopb - Nanopb generated code (requires Nanopb)
22 # This function also creates libraries for generating pw_rpc code:
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
30 # NAME - the base name of the libraries to create
31 # SOURCES - .proto source files
32 # DEPS - dependencies on other pw_proto_library targets
34 function(pw_proto_library NAME)
35 cmake_parse_arguments(PARSE_ARGV 1 arg "" "" "SOURCES;DEPS")
37 set(out_dir "${CMAKE_CURRENT_BINARY_DIR}/protos")
39 # Use INTERFACE libraries to track the proto include paths that are passed to
41 set(include_deps "${arg_DEPS}")
42 list(TRANSFORM include_deps APPEND ._includes)
44 add_library("${NAME}._includes" INTERFACE)
45 target_include_directories("${NAME}._includes" INTERFACE ".")
46 target_link_libraries("${NAME}._includes" INTERFACE ${include_deps})
48 # Generate a file with all include paths needed by protoc.
49 set(include_file "${out_dir}/${NAME}.include_paths.txt")
50 file(GENERATE OUTPUT "${include_file}"
52 "$<TARGET_PROPERTY:${NAME}._includes,INTERFACE_INCLUDE_DIRECTORIES>")
54 # Create a protobuf target for each supported protobuf library.
56 "${NAME}" "${arg_SOURCES}" "${arg_DEPS}" "${include_file}" "${out_dir}")
58 "${NAME}" "${arg_SOURCES}" "${arg_DEPS}" "${include_file}" "${out_dir}")
60 "${NAME}" "${arg_SOURCES}" "${arg_DEPS}" "${include_file}" "${out_dir}")
61 _pw_nanopb_rpc_library(
62 "${NAME}" "${arg_SOURCES}" "${arg_DEPS}" "${include_file}" "${out_dir}")
63 endfunction(pw_proto_library)
65 # Internal function that invokes protoc through generate_protos.py.
66 function(_pw_generate_protos
67 TARGET LANGUAGE PLUGIN OUTPUT_EXTS INCLUDE_FILE OUT_DIR SOURCES DEPS)
68 # Determine the names of the output files.
69 foreach(extension IN LISTS OUTPUT_EXTS)
70 foreach(source_file IN LISTS SOURCES)
71 get_filename_component(dir "${source_file}" DIRECTORY)
72 get_filename_component(name "${source_file}" NAME_WE)
73 list(APPEND outputs "${OUT_DIR}/${dir}/${name}${extension}")
77 # Export the output files to the caller's scope so it can use them if needed.
78 set(generated_outputs "${outputs}" PARENT_SCOPE)
80 if("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
81 get_filename_component(dir "${source_file}" DIRECTORY)
82 get_filename_component(name "${source_file}" NAME_WE)
83 set(PLUGIN "${dir}/${name}.bat")
86 set(script "$ENV{PW_ROOT}/pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py")
91 --language "${LANGUAGE}"
92 --plugin-path "${PLUGIN}"
93 --include-path "${CMAKE_CURRENT_SOURCE_DIR}"
94 --include-file "${INCLUDE_FILE}"
95 --out-dir "${OUT_DIR}"
105 add_custom_target("${TARGET}" DEPENDS ${outputs})
106 endfunction(_pw_generate_protos)
108 # Internal function that creates a pwpb proto library.
109 function(_pw_pwpb_library NAME SOURCES DEPS INCLUDE_FILE OUT_DIR)
110 list(TRANSFORM DEPS APPEND .pwpb)
112 _pw_generate_protos("${NAME}.generate.pwpb"
114 "$ENV{PW_ROOT}/pw_protobuf/py/pw_protobuf/plugin.py"
122 # Create the library with the generated source files.
123 add_library("${NAME}.pwpb" INTERFACE)
124 target_include_directories("${NAME}.pwpb" INTERFACE "${OUT_DIR}")
125 target_link_libraries("${NAME}.pwpb" INTERFACE pw_protobuf ${DEPS})
126 add_dependencies("${NAME}.pwpb" "${NAME}.generate.pwpb")
127 endfunction(_pw_pwpb_library)
129 # Internal function that creates a raw_rpc proto library.
130 function(_pw_raw_rpc_library NAME SOURCES DEPS INCLUDE_FILE OUT_DIR)
131 list(TRANSFORM DEPS APPEND .raw_rpc)
133 _pw_generate_protos("${NAME}.generate.raw_rpc"
135 "$ENV{PW_ROOT}/pw_rpc/py/pw_rpc/plugin_raw.py"
143 # Create the library with the generated source files.
144 add_library("${NAME}.raw_rpc" INTERFACE)
145 target_include_directories("${NAME}.raw_rpc" INTERFACE "${OUT_DIR}")
146 target_link_libraries("${NAME}.raw_rpc"
152 add_dependencies("${NAME}.raw_rpc" "${NAME}.generate.raw_rpc")
153 endfunction(_pw_raw_rpc_library)
155 # Internal function that creates a nanopb proto library.
156 function(_pw_nanopb_library NAME SOURCES DEPS INCLUDE_FILE OUT_DIR)
157 list(TRANSFORM DEPS APPEND .nanopb)
159 set(nanopb_dir "$<TARGET_PROPERTY:$<IF:$<TARGET_EXISTS:protobuf-nanopb-static>,protobuf-nanopb-static,pw_build.empty>,SOURCE_DIR>")
161 "$<IF:$<TARGET_EXISTS:protobuf-nanopb-static>,${nanopb_dir}/generator/protoc-gen-nanopb,COULD_NOT_FIND_protobuf-nanopb-static_TARGET_PLEASE_SET_UP_NANOPB>")
163 _pw_generate_protos("${NAME}.generate.nanopb"
173 # Create the library with the generated source files.
174 add_library("${NAME}.nanopb" EXCLUDE_FROM_ALL ${generated_outputs})
175 target_include_directories("${NAME}.nanopb" PUBLIC "${OUT_DIR}")
176 target_link_libraries("${NAME}.nanopb" PUBLIC pw_third_party.nanopb ${DEPS})
177 add_dependencies("${NAME}.nanopb" "${NAME}.generate.nanopb")
178 endfunction(_pw_nanopb_library)
180 # Internal function that creates a nanopb_rpc library.
181 function(_pw_nanopb_rpc_library NAME SOURCES DEPS INCLUDE_FILE OUT_DIR)
182 # Determine the names of the output files.
183 list(TRANSFORM DEPS APPEND .nanopb_rpc)
185 _pw_generate_protos("${NAME}.generate.nanopb_rpc"
187 "$ENV{PW_ROOT}/pw_rpc/py/pw_rpc/plugin_nanopb.py"
195 # Create the library with the generated source files.
196 add_library("${NAME}.nanopb_rpc" INTERFACE)
197 target_include_directories("${NAME}.nanopb_rpc" INTERFACE "${OUT_DIR}")
198 target_link_libraries("${NAME}.nanopb_rpc"
201 pw_rpc.nanopb.method_union
205 add_dependencies("${NAME}.nanopb_rpc" "${NAME}.generate.nanopb_rpc")
206 endfunction(_pw_nanopb_rpc_library)