Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 1 | # 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. |
| 14 | include_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 Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 33 | # 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 Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 37 | # |
| 38 | function(pw_proto_library NAME) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 39 | cmake_parse_arguments(PARSE_ARGV 1 arg "" "STRIP_PREFIX;PREFIX" |
| 40 | "SOURCES;INPUTS;DEPS") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 41 | |
Wyatt Hepler | 1573b82 | 2021-03-11 17:32:26 -0800 | [diff] [blame] | 42 | if("${arg_SOURCES}" STREQUAL "") |
| 43 | message(FATAL_ERROR |
| 44 | "pw_proto_library requires at least one .proto file in SOURCES. No " |
| 45 | "SOURCES were listed for ${NAME}.") |
| 46 | endif() |
| 47 | |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 48 | set(out_dir "${CMAKE_CURRENT_BINARY_DIR}/${NAME}") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 49 | |
| 50 | # Use INTERFACE libraries to track the proto include paths that are passed to |
| 51 | # protoc. |
| 52 | set(include_deps "${arg_DEPS}") |
| 53 | list(TRANSFORM include_deps APPEND ._includes) |
| 54 | |
| 55 | add_library("${NAME}._includes" INTERFACE) |
| 56 | target_include_directories("${NAME}._includes" INTERFACE ".") |
| 57 | target_link_libraries("${NAME}._includes" INTERFACE ${include_deps}) |
| 58 | |
| 59 | # Generate a file with all include paths needed by protoc. |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 60 | set(include_file "${out_dir}/include_paths.txt") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 61 | file(GENERATE OUTPUT "${include_file}" |
| 62 | CONTENT |
| 63 | "$<TARGET_PROPERTY:${NAME}._includes,INTERFACE_INCLUDE_DIRECTORIES>") |
| 64 | |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 65 | if("${arg_STRIP_PREFIX}" STREQUAL "") |
| 66 | set(arg_STRIP_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}") |
| 67 | endif() |
| 68 | |
| 69 | foreach(path IN LISTS arg_SOURCES arg_INPUTS) |
| 70 | get_filename_component(abspath "${path}" ABSOLUTE) |
| 71 | list(APPEND files_to_mirror "${abspath}") |
| 72 | endforeach() |
| 73 | |
| 74 | # Mirror the sources to the output directory with the specified prefix. |
| 75 | _pw_rebase_paths( |
| 76 | sources "${out_dir}/sources/${arg_PREFIX}" "${arg_STRIP_PREFIX}" "${arg_SOURCES}" "") |
| 77 | _pw_rebase_paths( |
| 78 | inputs "${out_dir}/sources/${arg_PREFIX}" "${arg_STRIP_PREFIX}" "${arg_INPUTS}" "") |
| 79 | |
| 80 | add_custom_command( |
| 81 | COMMAND |
Wyatt Hepler | 8f357f4 | 2021-03-17 15:37:09 -0700 | [diff] [blame^] | 82 | python3 |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 83 | "$ENV{PW_ROOT}/pw_build/py/pw_build/mirror_tree.py" |
| 84 | --source-root "${arg_STRIP_PREFIX}" |
| 85 | --directory "${out_dir}/sources/${arg_PREFIX}" |
| 86 | ${files_to_mirror} |
| 87 | DEPENDS |
| 88 | "$ENV{PW_ROOT}/pw_build/py/pw_build/mirror_tree.py" |
| 89 | ${files_to_mirror} |
| 90 | ${arg_DEPS} |
| 91 | OUTPUT |
| 92 | ${sources} ${inputs} |
| 93 | ) |
| 94 | |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 95 | # Create a protobuf target for each supported protobuf library. |
| 96 | _pw_pwpb_library( |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 97 | "${NAME}" "${sources}" "${inputs}" "${arg_DEPS}" "${include_file}" "${out_dir}") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 98 | _pw_raw_rpc_library( |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 99 | "${NAME}" "${sources}" "${inputs}" "${arg_DEPS}" "${include_file}" "${out_dir}") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 100 | _pw_nanopb_library( |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 101 | "${NAME}" "${sources}" "${inputs}" "${arg_DEPS}" "${include_file}" "${out_dir}") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 102 | _pw_nanopb_rpc_library( |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 103 | "${NAME}" "${sources}" "${inputs}" "${arg_DEPS}" "${include_file}" "${out_dir}") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 104 | endfunction(pw_proto_library) |
| 105 | |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 106 | function(_pw_rebase_paths VAR OUT_DIR ROOT FILES EXTENSIONS) |
| 107 | foreach(file IN LISTS FILES) |
| 108 | get_filename_component(file "${file}" ABSOLUTE) |
| 109 | file(RELATIVE_PATH file "${ROOT}" "${file}") |
| 110 | |
| 111 | if ("${EXTENSIONS}" STREQUAL "") |
| 112 | list(APPEND mirrored_files "${OUT_DIR}/${file}") |
| 113 | else() |
| 114 | foreach(ext IN LISTS EXTENSIONS) |
| 115 | get_filename_component(dir "${file}" DIRECTORY) |
| 116 | get_filename_component(name "${file}" NAME_WE) |
| 117 | list(APPEND mirrored_files "${OUT_DIR}/${dir}/${name}${ext}") |
| 118 | endforeach() |
| 119 | endif() |
| 120 | endforeach() |
| 121 | |
| 122 | set("${VAR}" "${mirrored_files}" PARENT_SCOPE) |
| 123 | endfunction(_pw_rebase_paths) |
| 124 | |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 125 | # Internal function that invokes protoc through generate_protos.py. |
| 126 | function(_pw_generate_protos |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 127 | TARGET LANGUAGE PLUGIN OUTPUT_EXTS INCLUDE_FILE OUT_DIR SOURCES INPUTS DEPS) |
| 128 | # Determine the names of the compiled output files. |
| 129 | _pw_rebase_paths(outputs |
| 130 | "${OUT_DIR}/${LANGUAGE}" "${OUT_DIR}/sources" "${SOURCES}" "${OUTPUT_EXTS}") |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 131 | |
| 132 | # Export the output files to the caller's scope so it can use them if needed. |
| 133 | set(generated_outputs "${outputs}" PARENT_SCOPE) |
| 134 | |
| 135 | if("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows") |
| 136 | get_filename_component(dir "${source_file}" DIRECTORY) |
| 137 | get_filename_component(name "${source_file}" NAME_WE) |
| 138 | set(PLUGIN "${dir}/${name}.bat") |
| 139 | endif() |
| 140 | |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 141 | set(script "$ENV{PW_ROOT}/pw_protobuf_compiler/py/pw_protobuf_compiler/generate_protos.py") |
| 142 | add_custom_command( |
| 143 | COMMAND |
Wyatt Hepler | 8f357f4 | 2021-03-17 15:37:09 -0700 | [diff] [blame^] | 144 | python3 |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 145 | "${script}" |
| 146 | --language "${LANGUAGE}" |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 147 | --plugin-path "${PLUGIN}" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 148 | --include-file "${INCLUDE_FILE}" |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 149 | --compile-dir "${OUT_DIR}/sources" |
| 150 | --out-dir "${OUT_DIR}/${LANGUAGE}" |
| 151 | --sources ${SOURCES} |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 152 | DEPENDS |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 153 | ${script} |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 154 | ${SOURCES} |
| 155 | ${INPUTS} |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 156 | ${DEPS} |
| 157 | OUTPUT |
| 158 | ${outputs} |
| 159 | ) |
| 160 | add_custom_target("${TARGET}" DEPENDS ${outputs}) |
| 161 | endfunction(_pw_generate_protos) |
| 162 | |
| 163 | # Internal function that creates a pwpb proto library. |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 164 | function(_pw_pwpb_library NAME SOURCES INPUTS DEPS INCLUDE_FILE OUT_DIR) |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 165 | list(TRANSFORM DEPS APPEND .pwpb) |
| 166 | |
| 167 | _pw_generate_protos("${NAME}.generate.pwpb" |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 168 | pwpb |
| 169 | "$ENV{PW_ROOT}/pw_protobuf/py/pw_protobuf/plugin.py" |
| 170 | ".pwpb.h" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 171 | "${INCLUDE_FILE}" |
| 172 | "${OUT_DIR}" |
| 173 | "${SOURCES}" |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 174 | "${INPUTS}" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 175 | "${DEPS}" |
| 176 | ) |
| 177 | |
| 178 | # Create the library with the generated source files. |
| 179 | add_library("${NAME}.pwpb" INTERFACE) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 180 | target_include_directories("${NAME}.pwpb" INTERFACE "${OUT_DIR}/pwpb") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 181 | target_link_libraries("${NAME}.pwpb" INTERFACE pw_protobuf ${DEPS}) |
| 182 | add_dependencies("${NAME}.pwpb" "${NAME}.generate.pwpb") |
| 183 | endfunction(_pw_pwpb_library) |
| 184 | |
| 185 | # Internal function that creates a raw_rpc proto library. |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 186 | function(_pw_raw_rpc_library NAME SOURCES INPUTS DEPS INCLUDE_FILE OUT_DIR) |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 187 | list(TRANSFORM DEPS APPEND .raw_rpc) |
| 188 | |
| 189 | _pw_generate_protos("${NAME}.generate.raw_rpc" |
| 190 | raw_rpc |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 191 | "$ENV{PW_ROOT}/pw_rpc/py/pw_rpc/plugin_raw.py" |
| 192 | ".raw_rpc.pb.h" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 193 | "${INCLUDE_FILE}" |
| 194 | "${OUT_DIR}" |
| 195 | "${SOURCES}" |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 196 | "${INPUTS}" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 197 | "${DEPS}" |
| 198 | ) |
| 199 | |
| 200 | # Create the library with the generated source files. |
| 201 | add_library("${NAME}.raw_rpc" INTERFACE) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 202 | target_include_directories("${NAME}.raw_rpc" INTERFACE "${OUT_DIR}/raw_rpc") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 203 | target_link_libraries("${NAME}.raw_rpc" |
| 204 | INTERFACE |
| 205 | pw_rpc.raw |
| 206 | pw_rpc.server |
| 207 | ${DEPS} |
| 208 | ) |
| 209 | add_dependencies("${NAME}.raw_rpc" "${NAME}.generate.raw_rpc") |
| 210 | endfunction(_pw_raw_rpc_library) |
| 211 | |
| 212 | # Internal function that creates a nanopb proto library. |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 213 | function(_pw_nanopb_library NAME SOURCES INPUTS DEPS INCLUDE_FILE OUT_DIR) |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 214 | list(TRANSFORM DEPS APPEND .nanopb) |
| 215 | |
Wyatt Hepler | 8f357f4 | 2021-03-17 15:37:09 -0700 | [diff] [blame^] | 216 | if("${dir_pw_third_party_nanopb}" STREQUAL "") |
| 217 | add_custom_target("${NAME}.generate.nanopb" |
| 218 | cmake -E echo |
| 219 | ERROR: Attempting to use pw_proto_library, but |
| 220 | dir_pw_third_party_nanopb is not set. Set dir_pw_third_party_nanopb |
| 221 | to the path to the Nanopb repository. |
| 222 | COMMAND |
| 223 | cmake -E false |
| 224 | DEPENDS |
| 225 | ${DEPS} |
| 226 | SOURCES |
| 227 | ${SOURCES} |
| 228 | ) |
| 229 | set(generated_outputs $<TARGET_PROPERTY:pw_build.empty,SOURCES>) |
| 230 | else() |
| 231 | _pw_generate_protos("${NAME}.generate.nanopb" |
| 232 | nanopb |
| 233 | "${dir_pw_third_party_nanopb}/generator/protoc-gen-nanopb" |
| 234 | ".pb.h;.pb.c" |
| 235 | "${INCLUDE_FILE}" |
| 236 | "${OUT_DIR}" |
| 237 | "${SOURCES}" |
| 238 | "${INPUTS}" |
| 239 | "${DEPS}" |
| 240 | ) |
| 241 | endif() |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 242 | |
| 243 | # Create the library with the generated source files. |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 244 | add_library("${NAME}.nanopb" EXCLUDE_FROM_ALL ${generated_outputs}) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 245 | target_include_directories("${NAME}.nanopb" PUBLIC "${OUT_DIR}/nanopb") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 246 | target_link_libraries("${NAME}.nanopb" PUBLIC pw_third_party.nanopb ${DEPS}) |
| 247 | add_dependencies("${NAME}.nanopb" "${NAME}.generate.nanopb") |
| 248 | endfunction(_pw_nanopb_library) |
| 249 | |
| 250 | # Internal function that creates a nanopb_rpc library. |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 251 | function(_pw_nanopb_rpc_library NAME SOURCES INPUTS DEPS INCLUDE_FILE OUT_DIR) |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 252 | # Determine the names of the output files. |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 253 | list(TRANSFORM DEPS APPEND .nanopb_rpc) |
| 254 | |
| 255 | _pw_generate_protos("${NAME}.generate.nanopb_rpc" |
| 256 | nanopb_rpc |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 257 | "$ENV{PW_ROOT}/pw_rpc/py/pw_rpc/plugin_nanopb.py" |
| 258 | ".rpc.pb.h" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 259 | "${INCLUDE_FILE}" |
| 260 | "${OUT_DIR}" |
| 261 | "${SOURCES}" |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 262 | "${INPUTS}" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 263 | "${DEPS}" |
| 264 | ) |
| 265 | |
| 266 | # Create the library with the generated source files. |
| 267 | add_library("${NAME}.nanopb_rpc" INTERFACE) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame] | 268 | target_include_directories("${NAME}.nanopb_rpc" |
| 269 | INTERFACE |
| 270 | "${OUT_DIR}/nanopb_rpc" |
| 271 | ) |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 272 | target_link_libraries("${NAME}.nanopb_rpc" |
| 273 | INTERFACE |
| 274 | "${NAME}.nanopb" |
Wyatt Hepler | dcfa92b | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 275 | pw_rpc.nanopb.method_union |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 276 | pw_rpc.server |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 277 | ${DEPS} |
| 278 | ) |
| 279 | add_dependencies("${NAME}.nanopb_rpc" "${NAME}.generate.nanopb_rpc") |
| 280 | endfunction(_pw_nanopb_rpc_library) |