1 # This is an example script for use with CMake projects for locating and configuring
4 # The following variables can be set and are optional:
7 # PROTOBUF_SRC_ROOT_FOLDER - When compiling with MSVC, if this cache variable is set
8 # the protobuf-default VS project build locations
9 # (vsprojects/Debug & vsprojects/Release) will be searched
10 # for libraries and binaries.
12 # NANOPB_IMPORT_DIRS - List of additional directories to be searched for
13 # imported .proto files.
15 # NANOPB_GENERATE_CPP_APPEND_PATH - By default -I will be passed to protoc
16 # for each directory where a proto file is referenced.
17 # Set to FALSE if you want to disable this behaviour.
19 # Defines the following variables:
21 # NANOPB_FOUND - Found the nanopb library (source&header files, generator tool, protoc compiler tool)
22 # NANOPB_INCLUDE_DIRS - Include directories for Google Protocol Buffers
24 # The following cache variables are also available to set or use:
25 # PROTOBUF_PROTOC_EXECUTABLE - The protoc compiler
26 # NANOPB_GENERATOR_SOURCE_DIR - The nanopb generator source
28 # ====================================================================
30 # NANOPB_GENERATE_CPP (public function)
31 # SRCS = Variable to define with autogenerated
33 # HDRS = Variable to define with autogenerated
37 # ====================================================================
40 # set(NANOPB_SRC_ROOT_FOLDER "/path/to/nanopb")
41 # set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${NANOPB_SRC_ROOT_FOLDER}/cmake)
42 # find_package( Nanopb REQUIRED )
43 # include_directories(${NANOPB_INCLUDE_DIRS})
45 # NANOPB_GENERATE_CPP(PROTO_SRCS PROTO_HDRS foo.proto)
47 # include_directories(${CMAKE_CURRENT_BINARY_DIR})
48 # add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
50 # ====================================================================
52 #=============================================================================
53 # Copyright 2009 Kitware, Inc.
54 # Copyright 2009-2011 Philip Lowman <philip@yhbt.com>
55 # Copyright 2008 Esben Mose Hansen, Ange Optimization ApS
57 # Redistribution and use in source and binary forms, with or without
58 # modification, are permitted provided that the following conditions
61 # * Redistributions of source code must retain the above copyright
62 # notice, this list of conditions and the following disclaimer.
64 # * Redistributions in binary form must reproduce the above copyright
65 # notice, this list of conditions and the following disclaimer in the
66 # documentation and/or other materials provided with the distribution.
68 # * Neither the names of Kitware, Inc., the Insight Software Consortium,
69 # nor the names of their contributors may be used to endorse or promote
70 # products derived from this software without specific prior written
73 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
74 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
75 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
76 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
77 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
78 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
79 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
80 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
81 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
82 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
83 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
85 #=============================================================================
88 # 2013.01.31 - Pavlo Ilin - used Modules/FindProtobuf.cmake from cmake 2.8.10 to
89 # write FindNanopb.cmake
91 #=============================================================================
94 function(NANOPB_GENERATE_CPP SRCS HDRS)
99 if(NANOPB_GENERATE_CPP_APPEND_PATH)
100 # Create an include path for each file specified
102 get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
103 get_filename_component(ABS_PATH ${ABS_FIL} PATH)
105 list(FIND _nanobp_include_path ${ABS_PATH} _contains_already)
106 if(${_contains_already} EQUAL -1)
107 list(APPEND _nanobp_include_path -I ${ABS_PATH})
111 set(_nanobp_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
114 if(DEFINED NANOPB_IMPORT_DIRS)
115 foreach(DIR ${NANOPB_IMPORT_DIRS})
116 get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
117 list(FIND _nanobp_include_path ${ABS_PATH} _contains_already)
118 if(${_contains_already} EQUAL -1)
119 list(APPEND _nanobp_include_path -I ${ABS_PATH})
127 set(GENERATOR_PATH ${CMAKE_BINARY_DIR}/nanopb/generator)
129 set(NANOPB_GENERATOR_EXECUTABLE ${GENERATOR_PATH}/nanopb_generator.py)
131 set(GENERATOR_CORE_DIR ${GENERATOR_PATH}/proto)
132 set(GENERATOR_CORE_SRC
133 ${GENERATOR_CORE_DIR}/nanopb.proto
134 ${GENERATOR_CORE_DIR}/plugin.proto)
136 # Treat the source diretory as immutable.
138 # Copy the generator directory to the build directory before
139 # compiling python and proto files. Fixes issues when using the
140 # same build directory with different python/protobuf versions
141 # as the binary build directory is discarded across builds.
144 OUTPUT ${NANOPB_GENERATOR_EXECUTABLE} ${GENERATOR_CORE_SRC}
145 COMMAND ${CMAKE_COMMAND} -E copy_directory
146 ARGS ${NANOPB_GENERATOR_SOURCE_DIR} ${GENERATOR_PATH}
149 set(GENERATOR_CORE_PYTHON_SRC)
150 foreach(FIL ${GENERATOR_CORE_SRC})
151 get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
152 get_filename_component(FIL_WE ${FIL} NAME_WE)
154 set(output "${GENERATOR_CORE_DIR}/${FIL_WE}_pb2.py")
155 set(GENERATOR_CORE_PYTHON_SRC ${GENERATOR_CORE_PYTHON_SRC} ${output})
158 COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
159 ARGS -I${GENERATOR_PATH}/proto
160 --python_out=${GENERATOR_CORE_DIR} ${ABS_FIL}
166 get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
167 get_filename_component(FIL_WE ${FIL} NAME_WE)
168 get_filename_component(FIL_DIR ${FIL} PATH)
169 set(NANOPB_OPTIONS_FILE ${FIL_DIR}/${FIL_WE}.options)
171 if(EXISTS ${NANOPB_OPTIONS_FILE})
172 set(NANOPB_OPTIONS -f ${NANOPB_OPTIONS_FILE})
175 list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.c")
176 list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
179 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb"
180 COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
181 ARGS -I${GENERATOR_PATH} -I${GENERATOR_CORE_DIR}
182 -I${CMAKE_CURRENT_BINARY_DIR} ${_nanobp_include_path}
183 -o${FIL_WE}.pb ${ABS_FIL}
184 DEPENDS ${ABS_FIL} ${GENERATOR_CORE_PYTHON_SRC}
185 COMMENT "Running C++ protocol buffer compiler on ${FIL}"
189 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.c"
190 "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
191 COMMAND ${PYTHON_EXECUTABLE}
192 ARGS ${NANOPB_GENERATOR_EXECUTABLE} ${FIL_WE}.pb ${NANOPB_OPTIONS}
193 DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb"
194 COMMENT "Running nanopb generator on ${FIL_WE}.pb"
198 set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
199 set(${SRCS} ${${SRCS}} ${NANOPB_SRCS} PARENT_SCOPE)
200 set(${HDRS} ${${HDRS}} ${NANOPB_HDRS} PARENT_SCOPE)
210 # By default have NANOPB_GENERATE_CPP macro pass -I to protoc
211 # for each directory where a proto file is referenced.
212 if(NOT DEFINED NANOPB_GENERATE_CPP_APPEND_PATH)
213 set(NANOPB_GENERATE_CPP_APPEND_PATH TRUE)
216 # Make a really good guess regarding location of NANOPB_SRC_ROOT_FOLDER
217 if(NOT DEFINED NANOPB_SRC_ROOT_FOLDER)
218 get_filename_component(NANOPB_SRC_ROOT_FOLDER
219 ${CMAKE_CURRENT_LIST_DIR}/.. ABSOLUTE)
222 # Find the include directory
223 find_path(NANOPB_INCLUDE_DIRS
225 PATHS ${NANOPB_SRC_ROOT_FOLDER}
227 mark_as_advanced(NANOPB_INCLUDE_DIRS)
229 # Find nanopb source files
232 list(APPEND _nanopb_srcs pb_decode.c pb_encode.c pb_common.c)
233 list(APPEND _nanopb_hdrs pb_decode.h pb_encode.h pb_common.h pb.h)
235 foreach(FIL ${_nanopb_srcs})
236 find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_SRC_ROOT_FOLDER} ${NANOPB_INCLUDE_DIRS})
237 list(APPEND NANOPB_SRCS "${${FIL}__nano_pb_file}")
238 mark_as_advanced(${FIL}__nano_pb_file)
241 foreach(FIL ${_nanopb_hdrs})
242 find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_INCLUDE_DIRS})
243 mark_as_advanced(${FIL}__nano_pb_file)
244 list(APPEND NANOPB_HDRS "${${FIL}__nano_pb_file}")
247 # Find the protoc Executable
248 find_program(PROTOBUF_PROTOC_EXECUTABLE
250 DOC "The Google Protocol Buffers Compiler"
252 ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Release
253 ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Debug
255 mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE)
257 # Find nanopb generator source dir
258 find_path(NANOPB_GENERATOR_SOURCE_DIR
259 NAMES nanopb_generator.py
260 DOC "nanopb generator source"
262 ${NANOPB_SRC_ROOT_FOLDER}/generator
264 mark_as_advanced(NANOPB_GENERATOR_SOURCE_DIR)
266 find_package(PythonInterp REQUIRED)
268 include(FindPackageHandleStandardArgs)
269 FIND_PACKAGE_HANDLE_STANDARD_ARGS(NANOPB DEFAULT_MSG
271 NANOPB_SRCS NANOPB_HDRS
272 NANOPB_GENERATOR_SOURCE_DIR
273 PROTOBUF_PROTOC_EXECUTABLE