1 # Locate and configure the nanopb library.
3 # The following varialbes have to be set:
5 # NANOPB_SRC_ROOT_FOLDER - Path to nanopb source folder
7 # The following variables can be set and are optional:
10 # PROTOBUF_SRC_ROOT_FOLDER - When compiling with MSVC, if this cache variable is set
11 # the protobuf-default VS project build locations
12 # (vsprojects/Debug & vsprojects/Release) will be searched
13 # for libraries and binaries.
15 # NANOPB_IMPORT_DIRS - List of additional directories to be searched for
16 # imported .proto files.
18 # NANOPB_GENERATE_CPP_APPEND_PATH - By default -I will be passed to protoc
19 # for each directory where a proto file is referenced.
20 # Set to FALSE if you want to disable this behaviour.
22 # Defines the following variables:
24 # NANOPB_FOUND - Found the nanopb library (source&header files, generator tool, protoc compiler tool)
25 # NANOPB_INCLUDE_DIRS - Include directories for Google Protocol Buffers
27 # The following cache variables are also available to set or use:
28 # NANOPB_GENERATOR_EXECUTABLE - The nanopb generator
29 # PROTOBUF_PROTOC_EXECUTABLE - The protoc compiler
31 # ====================================================================
33 # NANOPB_GENERATE_CPP (public function)
34 # SRCS = Variable to define with autogenerated
36 # HDRS = Variable to define with autogenerated
40 # ====================================================================
43 # set(NANOPB_SRC_ROOT_FOLDER "/path/to/nanopb")
44 # set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${NANOPB_SRC_ROOT_FOLDER}/cmake)
45 # find_package( Nanopb REQUIRED )
46 # include_directories(${NANOPB_INCLUDE_DIRS})
48 # NANOPB_GENERATE_CPP(PROTO_SRCS PROTO_HDRS foo.proto)
50 # include_directories(${CMAKE_CURRENT_BINARY_DIR})
51 # add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
53 # ====================================================================
55 #=============================================================================
56 # Copyright 2009 Kitware, Inc.
57 # Copyright 2009-2011 Philip Lowman <philip@yhbt.com>
58 # Copyright 2008 Esben Mose Hansen, Ange Optimization ApS
60 # Redistribution and use in source and binary forms, with or without
61 # modification, are permitted provided that the following conditions
64 # * Redistributions of source code must retain the above copyright
65 # notice, this list of conditions and the following disclaimer.
67 # * Redistributions in binary form must reproduce the above copyright
68 # notice, this list of conditions and the following disclaimer in the
69 # documentation and/or other materials provided with the distribution.
71 # * Neither the names of Kitware, Inc., the Insight Software Consortium,
72 # nor the names of their contributors may be used to endorse or promote
73 # products derived from this software without specific prior written
76 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
77 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
78 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
79 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
80 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
81 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
82 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
83 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
84 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
85 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
86 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88 #=============================================================================
91 # 2013.01.31 - Pavlo Ilin - used Modules/FindProtobuf.cmake from cmake 2.8.10 to
92 # write FindNanopb.cmake
94 #=============================================================================
97 function(NANOPB_GENERATE_CPP SRCS HDRS)
102 if(NANOPB_GENERATE_CPP_APPEND_PATH)
103 # Create an include path for each file specified
105 get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
106 get_filename_component(ABS_PATH ${ABS_FIL} PATH)
108 list(FIND _nanobp_include_path ${ABS_PATH} _contains_already)
109 if(${_contains_already} EQUAL -1)
110 list(APPEND _nanobp_include_path -I ${ABS_PATH})
114 set(_nanobp_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
117 if(DEFINED NANOPB_IMPORT_DIRS)
118 foreach(DIR ${NANOPB_IMPORT_DIRS})
119 get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
120 list(FIND _nanobp_include_path ${ABS_PATH} _contains_already)
121 if(${_contains_already} EQUAL -1)
122 list(APPEND _nanobp_include_path -I ${ABS_PATH})
129 get_filename_component(GENERATOR_PATH ${NANOPB_GENERATOR_EXECUTABLE} PATH)
132 get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
133 get_filename_component(FIL_WE ${FIL} NAME_WE)
135 list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.c")
136 list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
139 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb"
140 COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
141 ARGS -I${GENERATOR_PATH} -I${CMAKE_CURRENT_BINARY_DIR} ${_nanobp_include_path} -o${FIL_WE}.pb ${ABS_FIL}
143 COMMENT "Running C++ protocol buffer compiler on ${FIL}"
147 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.c"
148 "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
150 ARGS ${NANOPB_GENERATOR_EXECUTABLE} ${FIL_WE}.pb
152 COMMENT "Running nanopb generator on ${FIL_WE}.pb"
156 set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
157 set(${SRCS} ${${SRCS}} ${NANOPB_SRCS} PARENT_SCOPE)
158 set(${HDRS} ${${HDRS}} ${NANOPB_HDRS} PARENT_SCOPE)
168 # By default have NANOPB_GENERATE_CPP macro pass -I to protoc
169 # for each directory where a proto file is referenced.
170 if(NOT DEFINED NANOPB_GENERATE_CPP_APPEND_PATH)
171 set(NANOPB_GENERATE_CPP_APPEND_PATH TRUE)
174 # Find the include directory
175 find_path(NANOPB_INCLUDE_DIRS
177 PATHS ${NANOPB_SRC_ROOT_FOLDER}
179 mark_as_advanced(NANOPB_INCLUDE_DIRS)
181 # Find nanopb source files
184 list(APPEND _nanopb_srcs pb_decode.c pb_encode.c)
185 list(APPEND _nanopb_hdrs pb_decode.h pb_encode.h pb.h)
187 foreach(FIL ${_nanopb_srcs})
188 find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_SRC_ROOT_FOLDER} ${NANOPB_INCLUDE_DIRS})
189 list(APPEND NANOPB_SRCS "${${FIL}__nano_pb_file}")
190 mark_as_advanced(${FIL}__nano_pb_file)
193 foreach(FIL ${_nanopb_hdrs})
194 find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_INCLUDE_DIRS})
195 mark_as_advanced(${FIL}__nano_pb_file)
196 list(APPEND NANOPB_HDRS "${${FIL}__nano_pb_file}")
199 # Find the protoc Executable
200 find_program(PROTOBUF_PROTOC_EXECUTABLE
202 DOC "The Google Protocol Buffers Compiler"
204 ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Release
205 ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Debug
207 mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE)
209 # Find nanopb generator
210 find_file(NANOPB_GENERATOR_EXECUTABLE
211 NAMES nanopb_generator.py
212 DOC "nanopb generator"
214 ${NANOPB_SRC_ROOT_FOLDER}/generator
216 mark_as_advanced(NANOPB_GENERATOR_EXECUTABLE)
218 include(FindPackageHandleStandardArgs)
219 FIND_PACKAGE_HANDLE_STANDARD_ARGS(NANOPB DEFAULT_MSG
221 NANOPB_SRCS NANOPB_HDRS
222 NANOPB_GENERATOR_EXECUTABLE
223 PROTOBUF_PROTOC_EXECUTABLE