1 # This is an example script for use with CMake projects for locating and configuring
4 # The following varialbes have to be set:
6 # NANOPB_SRC_ROOT_FOLDER - Path to nanopb source folder
8 # The following variables can be set and are optional:
11 # PROTOBUF_SRC_ROOT_FOLDER - When compiling with MSVC, if this cache variable is set
12 # the protobuf-default VS project build locations
13 # (vsprojects/Debug & vsprojects/Release) will be searched
14 # for libraries and binaries.
16 # NANOPB_IMPORT_DIRS - List of additional directories to be searched for
17 # imported .proto files.
19 # NANOPB_GENERATE_CPP_APPEND_PATH - By default -I will be passed to protoc
20 # for each directory where a proto file is referenced.
21 # Set to FALSE if you want to disable this behaviour.
23 # Defines the following variables:
25 # NANOPB_FOUND - Found the nanopb library (source&header files, generator tool, protoc compiler tool)
26 # NANOPB_INCLUDE_DIRS - Include directories for Google Protocol Buffers
28 # The following cache variables are also available to set or use:
29 # NANOPB_GENERATOR_EXECUTABLE - The nanopb generator
30 # PROTOBUF_PROTOC_EXECUTABLE - The protoc compiler
32 # ====================================================================
34 # NANOPB_GENERATE_CPP (public function)
35 # SRCS = Variable to define with autogenerated
37 # HDRS = Variable to define with autogenerated
41 # ====================================================================
44 # set(NANOPB_SRC_ROOT_FOLDER "/path/to/nanopb")
45 # set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${NANOPB_SRC_ROOT_FOLDER}/cmake)
46 # find_package( Nanopb REQUIRED )
47 # include_directories(${NANOPB_INCLUDE_DIRS})
49 # NANOPB_GENERATE_CPP(PROTO_SRCS PROTO_HDRS foo.proto)
51 # include_directories(${CMAKE_CURRENT_BINARY_DIR})
52 # add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
54 # ====================================================================
56 #=============================================================================
57 # Copyright 2009 Kitware, Inc.
58 # Copyright 2009-2011 Philip Lowman <philip@yhbt.com>
59 # Copyright 2008 Esben Mose Hansen, Ange Optimization ApS
61 # Redistribution and use in source and binary forms, with or without
62 # modification, are permitted provided that the following conditions
65 # * Redistributions of source code must retain the above copyright
66 # notice, this list of conditions and the following disclaimer.
68 # * Redistributions in binary form must reproduce the above copyright
69 # notice, this list of conditions and the following disclaimer in the
70 # documentation and/or other materials provided with the distribution.
72 # * Neither the names of Kitware, Inc., the Insight Software Consortium,
73 # nor the names of their contributors may be used to endorse or promote
74 # products derived from this software without specific prior written
77 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
79 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
80 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
81 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
89 #=============================================================================
92 # 2013.01.31 - Pavlo Ilin - used Modules/FindProtobuf.cmake from cmake 2.8.10 to
93 # write FindNanopb.cmake
95 #=============================================================================
98 function(NANOPB_GENERATE_CPP SRCS HDRS)
103 if(NANOPB_GENERATE_CPP_APPEND_PATH)
104 # Create an include path for each file specified
106 get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
107 get_filename_component(ABS_PATH ${ABS_FIL} PATH)
109 list(FIND _nanobp_include_path ${ABS_PATH} _contains_already)
110 if(${_contains_already} EQUAL -1)
111 list(APPEND _nanobp_include_path -I ${ABS_PATH})
115 set(_nanobp_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
118 if(DEFINED NANOPB_IMPORT_DIRS)
119 foreach(DIR ${NANOPB_IMPORT_DIRS})
120 get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
121 list(FIND _nanobp_include_path ${ABS_PATH} _contains_already)
122 if(${_contains_already} EQUAL -1)
123 list(APPEND _nanobp_include_path -I ${ABS_PATH})
130 get_filename_component(GENERATOR_PATH ${NANOPB_GENERATOR_EXECUTABLE} PATH)
133 get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
134 get_filename_component(FIL_WE ${FIL} NAME_WE)
136 list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.c")
137 list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
140 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb"
141 COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
142 ARGS -I${GENERATOR_PATH} -I${CMAKE_CURRENT_BINARY_DIR} ${_nanobp_include_path} -o${FIL_WE}.pb ${ABS_FIL}
144 COMMENT "Running C++ protocol buffer compiler on ${FIL}"
148 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.c"
149 "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
151 ARGS ${NANOPB_GENERATOR_EXECUTABLE} ${FIL_WE}.pb
152 DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb"
153 COMMENT "Running nanopb generator on ${FIL_WE}.pb"
157 set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
158 set(${SRCS} ${${SRCS}} ${NANOPB_SRCS} PARENT_SCOPE)
159 set(${HDRS} ${${HDRS}} ${NANOPB_HDRS} PARENT_SCOPE)
169 # By default have NANOPB_GENERATE_CPP macro pass -I to protoc
170 # for each directory where a proto file is referenced.
171 if(NOT DEFINED NANOPB_GENERATE_CPP_APPEND_PATH)
172 set(NANOPB_GENERATE_CPP_APPEND_PATH TRUE)
175 # Find the include directory
176 find_path(NANOPB_INCLUDE_DIRS
178 PATHS ${NANOPB_SRC_ROOT_FOLDER}
180 mark_as_advanced(NANOPB_INCLUDE_DIRS)
182 # Find nanopb source files
185 list(APPEND _nanopb_srcs pb_decode.c pb_encode.c pb_common.c)
186 list(APPEND _nanopb_hdrs pb_decode.h pb_encode.h pb_common.h pb.h)
188 foreach(FIL ${_nanopb_srcs})
189 find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_SRC_ROOT_FOLDER} ${NANOPB_INCLUDE_DIRS})
190 list(APPEND NANOPB_SRCS "${${FIL}__nano_pb_file}")
191 mark_as_advanced(${FIL}__nano_pb_file)
194 foreach(FIL ${_nanopb_hdrs})
195 find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_INCLUDE_DIRS})
196 mark_as_advanced(${FIL}__nano_pb_file)
197 list(APPEND NANOPB_HDRS "${${FIL}__nano_pb_file}")
200 # Find the protoc Executable
201 find_program(PROTOBUF_PROTOC_EXECUTABLE
203 DOC "The Google Protocol Buffers Compiler"
205 ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Release
206 ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Debug
208 mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE)
210 # Find nanopb generator
211 find_file(NANOPB_GENERATOR_EXECUTABLE
212 NAMES nanopb_generator.py
213 DOC "nanopb generator"
215 ${NANOPB_SRC_ROOT_FOLDER}/generator
217 mark_as_advanced(NANOPB_GENERATOR_EXECUTABLE)
219 include(FindPackageHandleStandardArgs)
220 FIND_PACKAGE_HANDLE_STANDARD_ARGS(NANOPB DEFAULT_MSG
222 NANOPB_SRCS NANOPB_HDRS
223 NANOPB_GENERATOR_EXECUTABLE
224 PROTOBUF_PROTOC_EXECUTABLE