blob: f41b710b6188e2a0061d3139484f6bd9633344fa [file] [log] [blame]
Julian Halldf86fce2020-11-23 18:09:55 +01001#-------------------------------------------------------------------------------
Gyorgy Szing28bca0b2023-02-15 11:17:07 +01002# Copyright (c) 2020-2023, Arm Limited and Contributors. All rights reserved.
Julian Halldf86fce2020-11-23 18:09:55 +01003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8#[===[.rst:
Gyorgy Szing2247d242021-09-03 16:17:25 +02009NanoPB integration for cmake
Julian Halldf86fce2020-11-23 18:09:55 +010010----------------------------
11
12This module will:
Ross Burton39a5bc72023-09-29 16:21:26 +010013 - use LazyFetch to download nanopb and build the static library and the generator.
14 Usual LazyFetch configuration to use prefetched source or prebuilt binaries apply.
15 - run find_module() to import the static library
16 - run find_executable() import the generator to the build (extend PYTHONPATH) and
17 define a cmake function to provide access to the generator to build nanopb files.
Julian Halldf86fce2020-11-23 18:09:55 +010018
Ross Burton39a5bc72023-09-29 16:21:26 +010019Note: see requirements.txt for dependencies which need to be installed in the build
20environment to use this module.
Julian Halldf86fce2020-11-23 18:09:55 +010021
22#]===]
23
24#### Get the dependency
25
Gyorgy Szing96669942021-12-08 04:19:50 +010026set(NANOPB_URL "https://github.com/nanopb/nanopb.git"
27 CACHE STRING "nanopb repository URL")
Ross Burton39a5bc72023-09-29 16:21:26 +010028set(NANOPB_REFSPEC "nanopb-0.4.8"
Gyorgy Szing96669942021-12-08 04:19:50 +010029 CACHE STRING "nanopb git refspec")
30set(NANOPB_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/nanopb-src"
31 CACHE PATH "nanopb source-code")
32set(NANOPB_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/nanopb_install"
33 CACHE PATH "nanopb installation directory")
Gyorgy Szing34aaf212022-10-20 07:26:23 +020034set(NANOPB_BUILD_TYPE "Release"
35 CACHE STRING "nanopb build type")
Julian Halldf86fce2020-11-23 18:09:55 +010036
37# Checking git
38find_program(GIT_COMMAND "git")
39if (NOT GIT_COMMAND)
40 message(FATAL_ERROR "Please install git")
41endif()
42
Gyorgy Szing96669942021-12-08 04:19:50 +010043set(GIT_OPTIONS
Julian Halldf86fce2020-11-23 18:09:55 +010044 GIT_REPOSITORY ${NANOPB_URL}
45 GIT_TAG ${NANOPB_REFSPEC}
Julian Halla628af32022-04-01 10:08:18 +010046 GIT_SHALLOW FALSE
Gyorgy Szing96669942021-12-08 04:19:50 +010047 )
Julian Halldf86fce2020-11-23 18:09:55 +010048
Gyorgy Szinga7e762b2022-03-05 00:33:57 +000049# Only pass libc settings to nanopb if needed. For environments where the standard
50# library is not overridden, this is not needed.
51if(TARGET stdlib::c)
52 include(${TS_ROOT}/tools/cmake/common/PropertyCopy.cmake)
53
54 # Save libc settings
55 save_interface_target_properties(TGT stdlib::c PREFIX LIBC)
56 # Translate libc settings to cmake code fragment. Will be inserted into
57 # nanopb-init-cache.cmake.in when LazyFetch configures the file.
58 translate_interface_target_properties(PREFIX LIBC RES _cmake_fragment)
59 unset_saved_properties(LIBC)
60endif()
Andrew Beggs97a00d42021-06-15 15:45:46 +000061
Ross Burton39a5bc72023-09-29 16:21:26 +010062# Nanopb build depends on python. Discover python here and pass the result to
63# nanopb build through the initial cache file.
64find_package(Python3 REQUIRED COMPONENTS Interpreter)
65
66# Use LazyFetch to manage the external dependency.
Gyorgy Szing96669942021-12-08 04:19:50 +010067include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
68LazyFetch_MakeAvailable(DEP_NAME nanopb
69 FETCH_OPTIONS ${GIT_OPTIONS}
70 INSTALL_DIR ${NANOPB_INSTALL_DIR}
71 PACKAGE_DIR ${NANOPB_INSTALL_DIR}
72 CACHE_FILE "${TS_ROOT}/external/nanopb/nanopb-init-cache.cmake.in"
73 SOURCE_DIR "${NANOPB_SOURCE_DIR}"
74 )
Gyorgy Szinga7e762b2022-03-05 00:33:57 +000075unset(_cmake_fragment)
Julian Halldf86fce2020-11-23 18:09:55 +010076
Gyorgy Szing28bca0b2023-02-15 11:17:07 +010077if(TARGET stdlib::c)
78 target_link_libraries(nanopb::protobuf-nanopb-static INTERFACE stdlib::c)
79endif()
Julian Halldf86fce2020-11-23 18:09:55 +010080
81#### Build access to the protobuf compiler
Ross Burton39a5bc72023-09-29 16:21:26 +010082find_program(NANOPB_GENERATOR_PATH
Julian Halldf86fce2020-11-23 18:09:55 +010083 NAMES nanopb_generator.py
Ross Burton39a5bc72023-09-29 16:21:26 +010084 HINTS ${NANOPB_INSTALL_DIR}/bin ${NANOPB_INSTALL_DIR}/sbin
Julian Halldf86fce2020-11-23 18:09:55 +010085 DOC "nanopb protobuf compiler"
86 NO_DEFAULT_PATH
87 )
88
89if (NOT NANOPB_GENERATOR_PATH)
90 message(FATAL_ERROR "Nanopb generator was not found!")
91endif()
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 Szing2247d242021-09-03 16:17:25 +0200112 Path to of the protobuf file to process. Either absolute or relative to the
Julian Halldf86fce2020-11-23 18:09:55 +0100113 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 Szing2247d242021-09-03 16:17:25 +0200123 Base directory. Generated files are located relative to this base.
Julian Halldf86fce2020-11-23 18:09:55 +0100124
125#]===]
126function(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 Szing2247d242021-09-03 16:17:25 +0200147 #If SRC is not absolute make it relative to the callers location.
Julian Halldf86fce2020-11-23 18:09:55 +0100148 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 Szingcd3241a2021-06-30 22:41:11 +0000170 #Tell cmake the dependency (source) file is fake.
171 set_source_files_properties("${_nanopb_fake_file}" PROPERTIES SYMBOLIC "true")
Julian Halldf86fce2020-11-23 18:09:55 +0100172 #Create a custom target which depends on a "fake" file.
173 add_custom_target("${_nanopb_target}"
174 DEPENDS "${_nanopb_fake_file}")
Gyorgy Szing2247d242021-09-03 16:17:25 +0200175 #Add a custom command to the target to create output directory.
Julian Halldf86fce2020-11-23 18:09:55 +0100176 add_custom_command(OUTPUT "${_nanopb_fake_file}"
Ross Burton39a5bc72023-09-29 16:21:26 +0100177 COMMAND ${CMAKE_COMMAND} -E make_directory ${_OUT_DIR_BASE}/${_OUT_DIR_REL}
Julian Halldf86fce2020-11-23 18:09:55 +0100178 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
185 #Append a protobuf generator command to the nanopb_generate target.
Gyorgy Szingcd3241a2021-06-30 22:41:11 +0000186 add_custom_command(OUTPUT "${_OUT_C}" "${_OUT_H}"
Gyorgy Szing0358e082022-05-10 09:09:37 +0200187 COMMAND
Ross Burton39a5bc72023-09-29 16:21:26 +0100188 ${CMAKE_COMMAND} -E env PYTHONPATH=${NANOPB_INSTALL_DIR}/lib/python
Gyorgy Szing0358e082022-05-10 09:09:37 +0200189 ${Python3_EXECUTABLE} ${NANOPB_GENERATOR_PATH}
Julian Halldf86fce2020-11-23 18:09:55 +0100190 -I ${PARAMS_BASE_DIR}
191 -D ${_OUT_DIR_BASE}
192 ${_SRC_REL}
193 DEPENDS "${PARAMS_SRC}")
194
195 #Add generated file to the target
196 set_property(SOURCE "${_OUT_C}" PROPERTY GENERATED TRUE)
197 target_sources(${PARAMS_TGT} PRIVATE "${_OUT_C}")
198endfunction()
199
200#[===[.rst:
201.. cmake:command:: protobuf_generate_all
202
203 .. code-block:: cmake
204
205 protobuf_generate_all(TGT foo
206 NAMESPACE bar
207 BASE_DIR "proto/definitions")
208
209 Generates C code from all .proto files listed in the target
210 property PROTOBUF_FILES.
211
212 Inputs:
213
214 ``TGT``
215 Name of target to compile generated source files.
216
217 ``NAMESPACE``
218 Namespace to put generated files under. Specifies include path and allows
219 separating colliding protobuf files.
220
221 ``BASE_DIR``
Gyorgy Szing2247d242021-09-03 16:17:25 +0200222 Base directory. Generated files are located relative to this base.
Julian Halldf86fce2020-11-23 18:09:55 +0100223
224#]===]
225function(protobuf_generate_all)
226 set(_options )
227 set(_oneValueArgs TGT NAMESPACE BASE_DIR)
228 set(_multiValueArgs )
229
230 cmake_parse_arguments(PARAMS "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
231
232 #Verify mandatory parameters
233 if (NOT DEFINED PARAMS_TGT)
234 message(FATAL_ERROR "nanopb_generate_all(): mandatory parameter TGT missing.")
235 endif()
236 if (NOT DEFINED PARAMS_NAMESPACE)
237 message(FATAL_ERROR "nanopb_generate_all(): mandatory parameter NAMESPACE missing.")
238 endif()
239 if (NOT DEFINED PARAMS_BASE_DIR)
240 message(FATAL_ERROR "nanopb_generate_all(): mandatory parameter BASE_DIR missing.")
241 endif()
242
243 get_property(_protolist TARGET ${PARAMS_TGT} PROPERTY PROTOBUF_FILES)
244
245 #Build of each .proto file
246 foreach(_file IN LISTS _protolist)
247 protobuf_generate(
248 TGT ${PARAMS_TGT}
249 SRC "${_file}"
250 NAMESPACE ${PARAMS_NAMESPACE}
251 BASE_DIR ${PARAMS_BASE_DIR})
252 endforeach()
Gyorgy Szing96669942021-12-08 04:19:50 +0100253endfunction()