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 | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 42 | set(out_dir "${CMAKE_CURRENT_BINARY_DIR}/${NAME}") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 43 | |
| 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 Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 54 | set(include_file "${out_dir}/include_paths.txt") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 55 | file(GENERATE OUTPUT "${include_file}" |
| 56 | CONTENT |
| 57 | "$<TARGET_PROPERTY:${NAME}._includes,INTERFACE_INCLUDE_DIRECTORIES>") |
| 58 | |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 59 | 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 Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 89 | # Create a protobuf target for each supported protobuf library. |
| 90 | _pw_pwpb_library( |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 91 | "${NAME}" "${sources}" "${inputs}" "${arg_DEPS}" "${include_file}" "${out_dir}") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 92 | _pw_raw_rpc_library( |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 93 | "${NAME}" "${sources}" "${inputs}" "${arg_DEPS}" "${include_file}" "${out_dir}") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 94 | _pw_nanopb_library( |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 95 | "${NAME}" "${sources}" "${inputs}" "${arg_DEPS}" "${include_file}" "${out_dir}") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 96 | _pw_nanopb_rpc_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 | endfunction(pw_proto_library) |
| 99 | |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 100 | function(_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) |
| 117 | endfunction(_pw_rebase_paths) |
| 118 | |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 119 | # Internal function that invokes protoc through generate_protos.py. |
| 120 | function(_pw_generate_protos |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 121 | 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 Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 125 | |
| 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 Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 135 | 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 Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 141 | --plugin-path "${PLUGIN}" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 142 | --include-file "${INCLUDE_FILE}" |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 143 | --compile-dir "${OUT_DIR}/sources" |
| 144 | --out-dir "${OUT_DIR}/${LANGUAGE}" |
| 145 | --sources ${SOURCES} |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 146 | DEPENDS |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 147 | ${script} |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 148 | ${SOURCES} |
| 149 | ${INPUTS} |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 150 | ${DEPS} |
| 151 | OUTPUT |
| 152 | ${outputs} |
| 153 | ) |
| 154 | add_custom_target("${TARGET}" DEPENDS ${outputs}) |
| 155 | endfunction(_pw_generate_protos) |
| 156 | |
| 157 | # Internal function that creates a pwpb proto library. |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 158 | function(_pw_pwpb_library NAME SOURCES INPUTS DEPS INCLUDE_FILE OUT_DIR) |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 159 | list(TRANSFORM DEPS APPEND .pwpb) |
| 160 | |
| 161 | _pw_generate_protos("${NAME}.generate.pwpb" |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 162 | pwpb |
| 163 | "$ENV{PW_ROOT}/pw_protobuf/py/pw_protobuf/plugin.py" |
| 164 | ".pwpb.h" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 165 | "${INCLUDE_FILE}" |
| 166 | "${OUT_DIR}" |
| 167 | "${SOURCES}" |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 168 | "${INPUTS}" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 169 | "${DEPS}" |
| 170 | ) |
| 171 | |
| 172 | # Create the library with the generated source files. |
| 173 | add_library("${NAME}.pwpb" INTERFACE) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 174 | target_include_directories("${NAME}.pwpb" INTERFACE "${OUT_DIR}/pwpb") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 175 | target_link_libraries("${NAME}.pwpb" INTERFACE pw_protobuf ${DEPS}) |
| 176 | add_dependencies("${NAME}.pwpb" "${NAME}.generate.pwpb") |
| 177 | endfunction(_pw_pwpb_library) |
| 178 | |
| 179 | # Internal function that creates a raw_rpc proto library. |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 180 | 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] | 181 | list(TRANSFORM DEPS APPEND .raw_rpc) |
| 182 | |
| 183 | _pw_generate_protos("${NAME}.generate.raw_rpc" |
| 184 | raw_rpc |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 185 | "$ENV{PW_ROOT}/pw_rpc/py/pw_rpc/plugin_raw.py" |
| 186 | ".raw_rpc.pb.h" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 187 | "${INCLUDE_FILE}" |
| 188 | "${OUT_DIR}" |
| 189 | "${SOURCES}" |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 190 | "${INPUTS}" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 191 | "${DEPS}" |
| 192 | ) |
| 193 | |
| 194 | # Create the library with the generated source files. |
| 195 | add_library("${NAME}.raw_rpc" INTERFACE) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 196 | target_include_directories("${NAME}.raw_rpc" INTERFACE "${OUT_DIR}/raw_rpc") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 197 | 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") |
| 204 | endfunction(_pw_raw_rpc_library) |
| 205 | |
| 206 | # Internal function that creates a nanopb proto library. |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 207 | function(_pw_nanopb_library NAME SOURCES INPUTS DEPS INCLUDE_FILE OUT_DIR) |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 208 | 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 Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 213 | |
| 214 | _pw_generate_protos("${NAME}.generate.nanopb" |
| 215 | nanopb |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 216 | "${nanopb_plugin}" |
| 217 | ".pb.h;.pb.c" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 218 | "${INCLUDE_FILE}" |
| 219 | "${OUT_DIR}" |
| 220 | "${SOURCES}" |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 221 | "${INPUTS}" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 222 | "${DEPS}" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 223 | ) |
| 224 | |
| 225 | # Create the library with the generated source files. |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 226 | add_library("${NAME}.nanopb" EXCLUDE_FROM_ALL ${generated_outputs}) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 227 | target_include_directories("${NAME}.nanopb" PUBLIC "${OUT_DIR}/nanopb") |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 228 | target_link_libraries("${NAME}.nanopb" PUBLIC pw_third_party.nanopb ${DEPS}) |
| 229 | add_dependencies("${NAME}.nanopb" "${NAME}.generate.nanopb") |
| 230 | endfunction(_pw_nanopb_library) |
| 231 | |
| 232 | # Internal function that creates a nanopb_rpc library. |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 233 | 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] | 234 | # Determine the names of the output files. |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 235 | list(TRANSFORM DEPS APPEND .nanopb_rpc) |
| 236 | |
| 237 | _pw_generate_protos("${NAME}.generate.nanopb_rpc" |
| 238 | nanopb_rpc |
Wyatt Hepler | d9336a4 | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 239 | "$ENV{PW_ROOT}/pw_rpc/py/pw_rpc/plugin_nanopb.py" |
| 240 | ".rpc.pb.h" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 241 | "${INCLUDE_FILE}" |
| 242 | "${OUT_DIR}" |
| 243 | "${SOURCES}" |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 244 | "${INPUTS}" |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 245 | "${DEPS}" |
| 246 | ) |
| 247 | |
| 248 | # Create the library with the generated source files. |
| 249 | add_library("${NAME}.nanopb_rpc" INTERFACE) |
Wyatt Hepler | 752d7d3 | 2021-03-02 09:02:23 -0800 | [diff] [blame^] | 250 | target_include_directories("${NAME}.nanopb_rpc" |
| 251 | INTERFACE |
| 252 | "${OUT_DIR}/nanopb_rpc" |
| 253 | ) |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 254 | target_link_libraries("${NAME}.nanopb_rpc" |
| 255 | INTERFACE |
| 256 | "${NAME}.nanopb" |
Wyatt Hepler | dcfa92b | 2020-11-10 09:47:30 -0800 | [diff] [blame] | 257 | pw_rpc.nanopb.method_union |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 258 | pw_rpc.server |
Wyatt Hepler | c9e51d2 | 2020-10-29 09:12:37 -0700 | [diff] [blame] | 259 | ${DEPS} |
| 260 | ) |
| 261 | add_dependencies("${NAME}.nanopb_rpc" "${NAME}.generate.nanopb_rpc") |
| 262 | endfunction(_pw_nanopb_rpc_library) |