Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
Gyorgy Szing | 9666994 | 2021-12-08 04:19:50 +0100 | [diff] [blame] | 2 | # Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved. |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | #[===[.rst: |
Gyorgy Szing | 2247d24 | 2021-09-03 16:17:25 +0200 | [diff] [blame] | 9 | NanoPB integration for cmake |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 10 | ---------------------------- |
| 11 | |
| 12 | This module will: |
| 13 | - download nanopb if not available locally |
| 14 | - build the runtime static library and the generator |
| 15 | - import the static library to the build |
| 16 | - define a function to provide access to the generator |
| 17 | |
| 18 | Note: the python module created by the generator build will be installed under |
| 19 | Python_SITELIB ("Third-party platform independent installation directory.") |
| 20 | This means the build may alter the state of your system. Please use virtualenv. |
| 21 | |
Gyorgy Szing | 2247d24 | 2021-09-03 16:17:25 +0200 | [diff] [blame] | 22 | Note: see requirements.txt for dependencies which need to be installed before |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 23 | running this module. |
| 24 | |
| 25 | #]===] |
| 26 | |
| 27 | #### Get the dependency |
| 28 | |
Gyorgy Szing | 9666994 | 2021-12-08 04:19:50 +0100 | [diff] [blame] | 29 | set(NANOPB_URL "https://github.com/nanopb/nanopb.git" |
| 30 | CACHE STRING "nanopb repository URL") |
| 31 | set(NANOPB_REFSPEC "nanopb-0.4.2" |
| 32 | CACHE STRING "nanopb git refspec") |
| 33 | set(NANOPB_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/nanopb-src" |
| 34 | CACHE PATH "nanopb source-code") |
| 35 | set(NANOPB_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/nanopb_install" |
| 36 | CACHE PATH "nanopb installation directory") |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 37 | |
| 38 | # Checking git |
| 39 | find_program(GIT_COMMAND "git") |
| 40 | if (NOT GIT_COMMAND) |
| 41 | message(FATAL_ERROR "Please install git") |
| 42 | endif() |
| 43 | |
Gyorgy Szing | 9666994 | 2021-12-08 04:19:50 +0100 | [diff] [blame] | 44 | set(GIT_OPTIONS |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 45 | GIT_REPOSITORY ${NANOPB_URL} |
| 46 | GIT_TAG ${NANOPB_REFSPEC} |
Julian Hall | a628af3 | 2022-04-01 10:08:18 +0100 | [diff] [blame] | 47 | GIT_SHALLOW FALSE |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 48 | #See the .patch file for details on why it is needed. |
| 49 | PATCH_COMMAND git stash |
| 50 | COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/fix-pyhon-name.patch |
Gyorgy Szing | 9666994 | 2021-12-08 04:19:50 +0100 | [diff] [blame] | 51 | ) |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 52 | |
Gyorgy Szing | a7e762b | 2022-03-05 00:33:57 +0000 | [diff] [blame^] | 53 | # Only pass libc settings to nanopb if needed. For environments where the standard |
| 54 | # library is not overridden, this is not needed. |
| 55 | if(TARGET stdlib::c) |
| 56 | include(${TS_ROOT}/tools/cmake/common/PropertyCopy.cmake) |
| 57 | |
| 58 | # Save libc settings |
| 59 | save_interface_target_properties(TGT stdlib::c PREFIX LIBC) |
| 60 | # Translate libc settings to cmake code fragment. Will be inserted into |
| 61 | # nanopb-init-cache.cmake.in when LazyFetch configures the file. |
| 62 | translate_interface_target_properties(PREFIX LIBC RES _cmake_fragment) |
| 63 | unset_saved_properties(LIBC) |
| 64 | endif() |
Andrew Beggs | 97a00d4 | 2021-06-15 15:45:46 +0000 | [diff] [blame] | 65 | |
Gyorgy Szing | 9666994 | 2021-12-08 04:19:50 +0100 | [diff] [blame] | 66 | include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED) |
| 67 | LazyFetch_MakeAvailable(DEP_NAME nanopb |
| 68 | FETCH_OPTIONS ${GIT_OPTIONS} |
| 69 | INSTALL_DIR ${NANOPB_INSTALL_DIR} |
| 70 | PACKAGE_DIR ${NANOPB_INSTALL_DIR} |
| 71 | CACHE_FILE "${TS_ROOT}/external/nanopb/nanopb-init-cache.cmake.in" |
| 72 | SOURCE_DIR "${NANOPB_SOURCE_DIR}" |
| 73 | ) |
Gyorgy Szing | a7e762b | 2022-03-05 00:33:57 +0000 | [diff] [blame^] | 74 | unset(_cmake_fragment) |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 75 | |
Gyorgy Szing | 9666994 | 2021-12-08 04:19:50 +0100 | [diff] [blame] | 76 | #set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${NANOPB_INSTALL_DIR}/) |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 77 | |
| 78 | #### Build access to the protobuf compiler |
| 79 | #TODO: verify protoc dependencies: python3-protobuf |
Gyorgy Szing | 9666994 | 2021-12-08 04:19:50 +0100 | [diff] [blame] | 80 | find_package(Python3 REQUIRED COMPONENTS Interpreter) |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 81 | |
| 82 | find_file(NANOPB_GENERATOR_PATH |
| 83 | NAMES nanopb_generator.py |
| 84 | PATHS ${nanopb_SOURCE_DIR}/generator |
| 85 | DOC "nanopb protobuf compiler" |
| 86 | NO_DEFAULT_PATH |
| 87 | ) |
| 88 | |
| 89 | if (NOT NANOPB_GENERATOR_PATH) |
| 90 | message(FATAL_ERROR "Nanopb generator was not found!") |
| 91 | endif() |
| 92 | |
| 93 | #[===[.rst: |
| 94 | .. cmake:command:: protobuf_generate |
| 95 | |
| 96 | .. code-block:: cmake |
| 97 | |
| 98 | protobuf_generate(SRC file.proto |
| 99 | TGT foo |
| 100 | NAMESPACE bar |
| 101 | BASE_DIR "proto/definitions") |
| 102 | |
| 103 | Run the ``nanopb_generator`` to compile a protobuf definition file into C source. |
| 104 | Generated source file will be added to the source list of ``TGT``. Protobuf |
| 105 | compilation will take part before TGT+NAMESPACE is built. |
| 106 | |
| 107 | Protobuf file names added to the same TGT must not collide. |
| 108 | |
| 109 | Inputs: |
| 110 | |
| 111 | ``SRC`` |
Gyorgy Szing | 2247d24 | 2021-09-03 16:17:25 +0200 | [diff] [blame] | 112 | Path to of the protobuf file to process. Either absolute or relative to the |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 113 | callers location. |
| 114 | |
| 115 | ``TGT`` |
| 116 | Name of target to compile generated source files. |
| 117 | |
| 118 | ``NAMESPACE`` |
| 119 | Namespace to put generated files under. Specifies include path and allows |
| 120 | separating colliding protobuf files. |
| 121 | |
| 122 | ``BASE_DIR`` |
Gyorgy Szing | 2247d24 | 2021-09-03 16:17:25 +0200 | [diff] [blame] | 123 | Base directory. Generated files are located relative to this base. |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 124 | |
| 125 | #]===] |
| 126 | function(protobuf_generate) |
| 127 | set(_options ) |
| 128 | set(_oneValueArgs SRC TGT NAMESPACE BASE_DIR) |
| 129 | set(_multiValueArgs ) |
| 130 | |
| 131 | cmake_parse_arguments(PARAMS "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN}) |
| 132 | |
| 133 | #Verify mandatory parameters |
| 134 | if (NOT DEFINED PARAMS_SRC) |
| 135 | message(FATAL_ERROR "nanopb_generate(): mandatory parameter SRC missing.") |
| 136 | endif() |
| 137 | if (NOT DEFINED PARAMS_TGT) |
| 138 | message(FATAL_ERROR "nanopb_generate(): mandatory parameter TGT missing.") |
| 139 | endif() |
| 140 | if (NOT DEFINED PARAMS_NAMESPACE) |
| 141 | message(FATAL_ERROR "nanopb_generate(): mandatory parameter NAMESPACE missing.") |
| 142 | endif() |
| 143 | if (NOT DEFINED PARAMS_BASE_DIR) |
| 144 | message(FATAL_ERROR "nanopb_generate(): mandatory parameter BASE_DIR missing.") |
| 145 | endif() |
| 146 | |
Gyorgy Szing | 2247d24 | 2021-09-03 16:17:25 +0200 | [diff] [blame] | 147 | #If SRC is not absolute make it relative to the callers location. |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 148 | if (NOT IS_ABSOLUTE ${PARAMS_SRC}) |
| 149 | set(PARAMS_SRC "${CMAKE_CURRENT_LIST_DIR}/${PARAMS_SRC}") |
| 150 | endif() |
| 151 | |
| 152 | #Calculate the output directory |
| 153 | set(_OUT_DIR_BASE ${CMAKE_BINARY_DIR}/src/${PARAMS_NAMESPACE}) |
| 154 | #Calculate output file names |
| 155 | get_filename_component(_BASENAME ${PARAMS_SRC} NAME_WE) |
| 156 | |
| 157 | #Get relative path or SRC to BASE_DIR |
| 158 | file(RELATIVE_PATH _SRC_REL ${PARAMS_BASE_DIR} ${PARAMS_SRC}) |
| 159 | get_filename_component(_OUT_DIR_REL ${_SRC_REL} DIRECTORY ) |
| 160 | |
| 161 | #Calculate output file paths |
| 162 | set(_OUT_C "${_OUT_DIR_BASE}/${_OUT_DIR_REL}/${_BASENAME}.pb.c") |
| 163 | set(_OUT_H "${_OUT_DIR_BASE}/${_OUT_DIR_REL}/${_BASENAME}.pb.h") |
| 164 | |
| 165 | #some helper variables for the purpose of readability |
| 166 | set(_nanopb_target "nanopb_generate_${PARAMS_TGT}_${PARAMS_NAMESPACE}") |
| 167 | set(_nanopb_fake_file "nanopb_generate_ff_${PARAMS_TGT}") |
| 168 | |
| 169 | if (NOT TARGET "${_nanopb_target}") |
Gyorgy Szing | cd3241a | 2021-06-30 22:41:11 +0000 | [diff] [blame] | 170 | #Tell cmake the dependency (source) file is fake. |
| 171 | set_source_files_properties("${_nanopb_fake_file}" PROPERTIES SYMBOLIC "true") |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 172 | #Create a custom target which depends on a "fake" file. |
| 173 | add_custom_target("${_nanopb_target}" |
| 174 | DEPENDS "${_nanopb_fake_file}") |
Gyorgy Szing | 2247d24 | 2021-09-03 16:17:25 +0200 | [diff] [blame] | 175 | #Add a custom command to the target to create output directory. |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 176 | add_custom_command(OUTPUT "${_nanopb_fake_file}" |
| 177 | COMMAND ${CMAKE_COMMAND} -E make_directory ${_OUT_DIR_BASE} |
| 178 | COMMENT "Generating source from protobuf definitions for target ${PARAMS_TGT}") |
| 179 | #Ensure protobuf build happens before test target. |
| 180 | add_dependencies(${PARAMS_TGT} ${_nanopb_target}) |
| 181 | #Add include path to protobuf output. |
| 182 | target_include_directories(${PARAMS_TGT} PRIVATE ${_OUT_DIR_BASE}) |
| 183 | endif() |
| 184 | |
Gyorgy Szing | 0358e08 | 2022-05-10 09:09:37 +0200 | [diff] [blame] | 185 | get_filename_component(NANOPB_GENERATOR_DIR "${NANOPB_GENERATOR_PATH}" DIRECTORY CACHE "Location of nanopb generator.") |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 186 | #Append a protobuf generator command to the nanopb_generate target. |
Gyorgy Szing | cd3241a | 2021-06-30 22:41:11 +0000 | [diff] [blame] | 187 | add_custom_command(OUTPUT "${_OUT_C}" "${_OUT_H}" |
Gyorgy Szing | 0358e08 | 2022-05-10 09:09:37 +0200 | [diff] [blame] | 188 | COMMAND |
| 189 | ${CMAKE_COMMAND} -E env PYTHONPATH=${NANOPB_GENERATOR_DIR} |
| 190 | ${Python3_EXECUTABLE} ${NANOPB_GENERATOR_PATH} |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 191 | -I ${PARAMS_BASE_DIR} |
| 192 | -D ${_OUT_DIR_BASE} |
| 193 | ${_SRC_REL} |
| 194 | DEPENDS "${PARAMS_SRC}") |
| 195 | |
| 196 | #Add generated file to the target |
| 197 | set_property(SOURCE "${_OUT_C}" PROPERTY GENERATED TRUE) |
| 198 | target_sources(${PARAMS_TGT} PRIVATE "${_OUT_C}") |
| 199 | endfunction() |
| 200 | |
| 201 | #[===[.rst: |
| 202 | .. cmake:command:: protobuf_generate_all |
| 203 | |
| 204 | .. code-block:: cmake |
| 205 | |
| 206 | protobuf_generate_all(TGT foo |
| 207 | NAMESPACE bar |
| 208 | BASE_DIR "proto/definitions") |
| 209 | |
| 210 | Generates C code from all .proto files listed in the target |
| 211 | property PROTOBUF_FILES. |
| 212 | |
| 213 | Inputs: |
| 214 | |
| 215 | ``TGT`` |
| 216 | Name of target to compile generated source files. |
| 217 | |
| 218 | ``NAMESPACE`` |
| 219 | Namespace to put generated files under. Specifies include path and allows |
| 220 | separating colliding protobuf files. |
| 221 | |
| 222 | ``BASE_DIR`` |
Gyorgy Szing | 2247d24 | 2021-09-03 16:17:25 +0200 | [diff] [blame] | 223 | Base directory. Generated files are located relative to this base. |
Julian Hall | df86fce | 2020-11-23 18:09:55 +0100 | [diff] [blame] | 224 | |
| 225 | #]===] |
| 226 | function(protobuf_generate_all) |
| 227 | set(_options ) |
| 228 | set(_oneValueArgs TGT NAMESPACE BASE_DIR) |
| 229 | set(_multiValueArgs ) |
| 230 | |
| 231 | cmake_parse_arguments(PARAMS "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN}) |
| 232 | |
| 233 | #Verify mandatory parameters |
| 234 | if (NOT DEFINED PARAMS_TGT) |
| 235 | message(FATAL_ERROR "nanopb_generate_all(): mandatory parameter TGT missing.") |
| 236 | endif() |
| 237 | if (NOT DEFINED PARAMS_NAMESPACE) |
| 238 | message(FATAL_ERROR "nanopb_generate_all(): mandatory parameter NAMESPACE missing.") |
| 239 | endif() |
| 240 | if (NOT DEFINED PARAMS_BASE_DIR) |
| 241 | message(FATAL_ERROR "nanopb_generate_all(): mandatory parameter BASE_DIR missing.") |
| 242 | endif() |
| 243 | |
| 244 | get_property(_protolist TARGET ${PARAMS_TGT} PROPERTY PROTOBUF_FILES) |
| 245 | |
| 246 | #Build of each .proto file |
| 247 | foreach(_file IN LISTS _protolist) |
| 248 | protobuf_generate( |
| 249 | TGT ${PARAMS_TGT} |
| 250 | SRC "${_file}" |
| 251 | NAMESPACE ${PARAMS_NAMESPACE} |
| 252 | BASE_DIR ${PARAMS_BASE_DIR}) |
| 253 | endforeach() |
Gyorgy Szing | 9666994 | 2021-12-08 04:19:50 +0100 | [diff] [blame] | 254 | endfunction() |