aboutsummaryrefslogtreecommitdiff
path: root/external/nanopb/nanopb.cmake
blob: eda00e4a1828d7a070cd2a415bef31c53333069a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#-------------------------------------------------------------------------------
# Copyright (c) 2020, Arm Limited and Contributors. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
#-------------------------------------------------------------------------------

#[===[.rst:
NonoPB integration for cmake
----------------------------

This module will:
	- download nanopb if not available locally
	- build the runtime static library and the generator
	- import the static library to the build
	- define a function to provide access to the generator

Note: the python module created by the generator build will be installed under
Python_SITELIB ("Third-party platform independent installation directory.")
This means the build may alter the state of your system. Please use virtualenv.

Note: see requirements.txt for dependnecies which need to be installed before
running this module.

#]===]

#### Get the dependency

set(NANOPB_URL "https://github.com/nanopb/nanopb.git" CACHE STRING "nanopb repository URL")
set(NANOPB_REFSPEC "nanopb-0.4.2" CACHE STRING "nanopb git refspec")
set(NANOPB_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/nanopb_install" CACHE PATH "nanopb installation directory")
set(NANOPB_PACKAGE_PATH "${NANOPB_INSTALL_PATH}/libnanopb/cmake" CACHE PATH "nanopb CMake package directory")

include(FetchContent)

# Checking git
find_program(GIT_COMMAND "git")
if (NOT GIT_COMMAND)
	message(FATAL_ERROR "Please install git")
endif()

# Fetching nanopb
FetchContent_Declare(
	nanopb
	GIT_REPOSITORY ${NANOPB_URL}
	GIT_TAG ${NANOPB_REFSPEC}
	GIT_SHALLOW TRUE
	#See the .patch file for details on why it is needed.
	PATCH_COMMAND git stash
		COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/fix-pyhon-name.patch
)

# FetchContent_GetProperties exports nanopb_SOURCE_DIR and nanopb_BINARY_DIR variables
FetchContent_GetProperties(nanopb)
if(NOT nanopb_POPULATED)
	message(STATUS "Fetching nanopb")
	FetchContent_Populate(nanopb)
endif()

#### Build the runtime and the generator.
if( NOT CMAKE_CROSSCOMPILING)
	execute_process(COMMAND
		${CMAKE_COMMAND}
				-DBUILD_SHARED_LIBS=Off
				-DBUILD_STATIC_LIBS=On
				-Dnanopb_BUILD_RUNTIME=On
				-Dnanopb_BUILD_GENERATOR=On
				-Dnanopb_PROTOC_PATH=${nanopb_SOURCE_DIR}/generator/protoc
				-Dnanopb_MSVC_STATIC_RUNTIME=Off
				-DCMAKE_INSTALL_PREFIX=${NANOPB_INSTALL_PATH}
				-DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
				-GUnix\ Makefiles
				${nanopb_SOURCE_DIR}
			WORKING_DIRECTORY
				${nanopb_BINARY_DIR}
			RESULT_VARIABLE _exec_error
	)
else()
	execute_process(COMMAND
		${CMAKE_COMMAND}
				-DBUILD_SHARED_LIBS=Off
				-DBUILD_STATIC_LIBS=On
				-Dnanopb_BUILD_RUNTIME=On
				-Dnanopb_BUILD_GENERATOR=On
				-Dnanopb_PROTOC_PATH=${nanopb_SOURCE_DIR}/generator/protoc
				-Dnanopb_MSVC_STATIC_RUNTIME=Off
				-DCMAKE_INSTALL_PREFIX=${NANOPB_INSTALL_PATH}
				-DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
				-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
				-GUnix\ Makefiles
				${nanopb_SOURCE_DIR}
			WORKING_DIRECTORY
				${nanopb_BINARY_DIR}
			RESULT_VARIABLE _exec_error
	)
endif()

if (_exec_error)
	message(FATAL_ERROR "Configuration step of nanopb runtime failed with ${_exec_error}.")
endif()

execute_process(COMMAND
		${CMAKE_COMMAND} --build ${nanopb_BINARY_DIR} -- install -j8
		RESULT_VARIABLE _exec_error
	)
if (_exec_error)
	message(FATAL_ERROR "Build step of nanopb runtime failed with ${_exec_error}.")
endif()

#### Include Nanopb runtime in the build.
find_package(Nanopb
			PATHS "${NANOPB_INSTALL_PATH}"
			NO_DEFAULT_PATH
		)

#### Build access to the protobuf compiler
#TODO: verify protoc dependencies: python3-protobuf
find_package(Python3 COMPONENTS Interpreter)

if (NOT Python3_Interpreter_FOUND)
	message(FATAL_ERROR "Failed to find python3 interpreter.")
endif()

find_file(NANOPB_GENERATOR_PATH
			NAMES nanopb_generator.py
			PATHS ${nanopb_SOURCE_DIR}/generator
			DOC "nanopb protobuf compiler"
			NO_DEFAULT_PATH
		)

if (NOT NANOPB_GENERATOR_PATH)
	message(FATAL_ERROR "Nanopb generator was not found!")
endif()

#[===[.rst:
.. cmake:command:: protobuf_generate

  .. code-block:: cmake

	 protobuf_generate(SRC file.proto
					 TGT foo
					 NAMESPACE bar
					 BASE_DIR "proto/definitions")

  Run the ``nanopb_generator`` to compile a protobuf definition file into C source.
  Generated source file will be added to the source list of ``TGT``. Protobuf
  compilation will take part before TGT+NAMESPACE is built.

  Protobuf file names added to the same TGT must not collide.

  Inputs:

  ``SRC``
	Path to of the protobuf file to process. Either absoluto or relative to the
	callers location.

  ``TGT``
	Name of target to compile generated source files.

  ``NAMESPACE``
	Namespace to put generated files under. Specifies include path and allows
	separating colliding protobuf files.

  ``BASE_DIR``
	Base directory. Generated files are located reletive to this base.

#]===]
function(protobuf_generate)
	set(_options )
	set(_oneValueArgs SRC TGT NAMESPACE BASE_DIR)
	set(_multiValueArgs )

	cmake_parse_arguments(PARAMS "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})

	#Verify mandatory parameters
	if (NOT DEFINED PARAMS_SRC)
		message(FATAL_ERROR "nanopb_generate(): mandatory parameter SRC missing.")
	endif()
	if (NOT DEFINED PARAMS_TGT)
		message(FATAL_ERROR "nanopb_generate(): mandatory parameter TGT missing.")
	endif()
	if (NOT DEFINED PARAMS_NAMESPACE)
		message(FATAL_ERROR "nanopb_generate(): mandatory parameter NAMESPACE missing.")
	endif()
	if (NOT DEFINED PARAMS_BASE_DIR)
		message(FATAL_ERROR "nanopb_generate(): mandatory parameter BASE_DIR missing.")
	endif()

	#If SRC is not abolute make it relative to the callers location.
	if (NOT IS_ABSOLUTE ${PARAMS_SRC})
		set(PARAMS_SRC "${CMAKE_CURRENT_LIST_DIR}/${PARAMS_SRC}")
	endif()

	#Calculate the output directory
	set(_OUT_DIR_BASE ${CMAKE_BINARY_DIR}/src/${PARAMS_NAMESPACE})
	#Calculate output file names
	get_filename_component(_BASENAME ${PARAMS_SRC} NAME_WE)

	#Get relative path or SRC to BASE_DIR
	file(RELATIVE_PATH _SRC_REL ${PARAMS_BASE_DIR} ${PARAMS_SRC})
	get_filename_component(_OUT_DIR_REL ${_SRC_REL} DIRECTORY )

	#Calculate output file paths
	set(_OUT_C "${_OUT_DIR_BASE}/${_OUT_DIR_REL}/${_BASENAME}.pb.c")
	set(_OUT_H "${_OUT_DIR_BASE}/${_OUT_DIR_REL}/${_BASENAME}.pb.h")

	#some helper variables for the purpose of readability
	set(_nanopb_target "nanopb_generate_${PARAMS_TGT}_${PARAMS_NAMESPACE}")
	set(_nanopb_fake_file "nanopb_generate_ff_${PARAMS_TGT}")

	if (NOT TARGET "${_nanopb_target}")
		#Create a custom target which depends on a "fake" file.
		add_custom_target("${_nanopb_target}"
							DEPENDS "${_nanopb_fake_file}")
		#Tell cmake the dependency (source) file is fake.
		set_source_files_properties("${_nanopb_fake_file}" PROPERTIES SYMBOLIC "true")
		#Add a cutom command to the target to create output directory.
		add_custom_command(OUTPUT "${_nanopb_fake_file}"
			COMMAND ${CMAKE_COMMAND} -E make_directory ${_OUT_DIR_BASE}
			COMMENT "Generating source from protobuf definitions for target ${PARAMS_TGT}")
		#Ensure protobuf build happens before test target.
		add_dependencies(${PARAMS_TGT} ${_nanopb_target})
		#Add include path to protobuf output.
		target_include_directories(${PARAMS_TGT} PRIVATE ${_OUT_DIR_BASE})
	endif()

	#Append a protobuf generator command to the nanopb_generate target.
	add_custom_command(OUTPUT "${_nanopb_fake_file}" "${_OUT_C}" "${_OUT_H}"
					   APPEND
					   COMMAND ${Python3_EXECUTABLE} ${NANOPB_GENERATOR_PATH}
						  -I ${PARAMS_BASE_DIR}
						  -D ${_OUT_DIR_BASE}
						  ${_SRC_REL}
					   DEPENDS "${PARAMS_SRC}")

	#Add generated file to the target
	set_property(SOURCE "${_OUT_C}" PROPERTY GENERATED TRUE)
	target_sources(${PARAMS_TGT} PRIVATE "${_OUT_C}")
endfunction()

#[===[.rst:
.. cmake:command:: protobuf_generate_all

  .. code-block:: cmake

	 protobuf_generate_all(TGT foo
					 NAMESPACE bar
					 BASE_DIR "proto/definitions")

  Generates C code from all .proto files listed in the target
  property PROTOBUF_FILES.

  Inputs:

   ``TGT``
	Name of target to compile generated source files.

  ``NAMESPACE``
	Namespace to put generated files under. Specifies include path and allows
	separating colliding protobuf files.

  ``BASE_DIR``
	Base directory. Generated files are located reletive to this base.

#]===]
function(protobuf_generate_all)
	set(_options )
	set(_oneValueArgs TGT NAMESPACE BASE_DIR)
	set(_multiValueArgs )

	cmake_parse_arguments(PARAMS "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})

	#Verify mandatory parameters
	if (NOT DEFINED PARAMS_TGT)
		message(FATAL_ERROR "nanopb_generate_all(): mandatory parameter TGT missing.")
	endif()
	if (NOT DEFINED PARAMS_NAMESPACE)
		message(FATAL_ERROR "nanopb_generate_all(): mandatory parameter NAMESPACE missing.")
	endif()
	if (NOT DEFINED PARAMS_BASE_DIR)
		message(FATAL_ERROR "nanopb_generate_all(): mandatory parameter BASE_DIR missing.")
	endif()

	get_property(_protolist TARGET ${PARAMS_TGT} PROPERTY PROTOBUF_FILES)

	#Build of each .proto file
	foreach(_file IN LISTS _protolist)
		protobuf_generate(
				TGT ${PARAMS_TGT}
				SRC "${_file}"
				NAMESPACE ${PARAMS_NAMESPACE}
				BASE_DIR ${PARAMS_BASE_DIR})
	endforeach()
endfunction()