1# This is an example script for use with CMake projects for locating and configuring 2# the nanopb library. 3# 4# The following variables can be set and are optional: 5# 6# 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. 11# 12# NANOPB_IMPORT_DIRS - List of additional directories to be searched for 13# imported .proto files. 14# 15# NANOPB_OPTIONS - List of options passed to nanopb. 16# 17# NANOPB_DEPENDS - List of files to be used as dependencies 18# for the generated source and header files. These 19# files are not directly passed as options to 20# nanopb but rather their directories. 21# 22# NANOPB_GENERATE_CPP_APPEND_PATH - By default -I will be passed to protoc 23# for each directory where a proto file is referenced. 24# Set to FALSE if you want to disable this behaviour. 25# 26# Defines the following variables: 27# 28# NANOPB_FOUND - Found the nanopb library (source&header files, generator tool, protoc compiler tool) 29# NANOPB_INCLUDE_DIRS - Include directories for Google Protocol Buffers 30# 31# The following cache variables are also available to set or use: 32# PROTOBUF_PROTOC_EXECUTABLE - The protoc compiler 33# NANOPB_GENERATOR_SOURCE_DIR - The nanopb generator source 34# 35# ==================================================================== 36# 37# NANOPB_GENERATE_CPP (public function) 38# NANOPB_GENERATE_CPP(SRCS HDRS [RELPATH <root-path-of-proto-files>] 39# <proto-files>...) 40# SRCS = Variable to define with autogenerated source files 41# HDRS = Variable to define with autogenerated header files 42# If you want to use relative paths in your import statements use the RELPATH 43# option. The argument to RELPATH should be the directory that all the 44# imports will be relative to. 45# When RELPATH is not specified then all proto files can be imported without 46# a path. 47# 48# 49# ==================================================================== 50# Example: 51# 52# set(NANOPB_SRC_ROOT_FOLDER "/path/to/nanopb") 53# set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${NANOPB_SRC_ROOT_FOLDER}/extra) 54# find_package( Nanopb REQUIRED ) 55# include_directories(${NANOPB_INCLUDE_DIRS}) 56# 57# NANOPB_GENERATE_CPP(PROTO_SRCS PROTO_HDRS foo.proto) 58# 59# include_directories(${CMAKE_CURRENT_BINARY_DIR}) 60# add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS}) 61# 62# Example with RELPATH: 63# Assume we have a layout like: 64# .../CMakeLists.txt 65# .../bar.cc 66# .../proto/ 67# .../proto/foo.proto (Which contains: import "sub/bar.proto"; ) 68# .../proto/sub/bar.proto 69# Everything would be the same as the previous example, but the call to 70# NANOPB_GENERATE_CPP would change to: 71# 72# NANOPB_GENERATE_CPP(PROTO_SRCS PROTO_HDRS RELPATH proto 73# proto/foo.proto proto/sub/bar.proto) 74# 75# ==================================================================== 76 77#============================================================================= 78# Copyright 2009 Kitware, Inc. 79# Copyright 2009-2011 Philip Lowman <[email protected]> 80# Copyright 2008 Esben Mose Hansen, Ange Optimization ApS 81# 82# Redistribution and use in source and binary forms, with or without 83# modification, are permitted provided that the following conditions 84# are met: 85# 86# * Redistributions of source code must retain the above copyright 87# notice, this list of conditions and the following disclaimer. 88# 89# * Redistributions in binary form must reproduce the above copyright 90# notice, this list of conditions and the following disclaimer in the 91# documentation and/or other materials provided with the distribution. 92# 93# * Neither the names of Kitware, Inc., the Insight Software Consortium, 94# nor the names of their contributors may be used to endorse or promote 95# products derived from this software without specific prior written 96# permission. 97# 98# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 99# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 100# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 101# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 102# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 103# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 104# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 105# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 106# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 107# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 108# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 109# 110#============================================================================= 111# 112# Changes 113# 2013.01.31 - Pavlo Ilin - used Modules/FindProtobuf.cmake from cmake 2.8.10 to 114# write FindNanopb.cmake 115# 116#============================================================================= 117 118 119function(NANOPB_GENERATE_CPP SRCS HDRS) 120 cmake_parse_arguments(NANOPB_GENERATE_CPP "" "RELPATH" "" ${ARGN}) 121 if(NOT NANOPB_GENERATE_CPP_UNPARSED_ARGUMENTS) 122 return() 123 endif() 124 125 if(NANOPB_GENERATE_CPP_APPEND_PATH) 126 # Create an include path for each file specified 127 foreach(FIL ${NANOPB_GENERATE_CPP_UNPARSED_ARGUMENTS}) 128 get_filename_component(ABS_FIL ${FIL} ABSOLUTE) 129 get_filename_component(ABS_PATH ${ABS_FIL} PATH) 130 list(APPEND _nanopb_include_path "-I${ABS_PATH}") 131 endforeach() 132 else() 133 set(_nanopb_include_path "-I${CMAKE_CURRENT_SOURCE_DIR}") 134 endif() 135 136 if(NANOPB_GENERATE_CPP_RELPATH) 137 list(APPEND _nanopb_include_path "-I${NANOPB_GENERATE_CPP_RELPATH}") 138 endif() 139 140 if(DEFINED NANOPB_IMPORT_DIRS) 141 foreach(DIR ${NANOPB_IMPORT_DIRS}) 142 get_filename_component(ABS_PATH ${DIR} ABSOLUTE) 143 list(APPEND _nanopb_include_path "-I${ABS_PATH}") 144 endforeach() 145 endif() 146 147 list(REMOVE_DUPLICATES _nanopb_include_path) 148 149 set(GENERATOR_PATH ${CMAKE_BINARY_DIR}/nanopb/generator) 150 151 set(NANOPB_GENERATOR_EXECUTABLE ${GENERATOR_PATH}/nanopb_generator.py) 152 if (WIN32) 153 set(NANOPB_GENERATOR_PLUGIN ${GENERATOR_PATH}/protoc-gen-nanopb.bat) 154 else() 155 set(NANOPB_GENERATOR_PLUGIN ${GENERATOR_PATH}/protoc-gen-nanopb) 156 endif() 157 158 set(GENERATOR_CORE_DIR ${GENERATOR_PATH}/proto) 159 set(GENERATOR_CORE_SRC 160 ${GENERATOR_CORE_DIR}/nanopb.proto 161 ${GENERATOR_CORE_DIR}/plugin.proto) 162 163 # Treat the source diretory as immutable. 164 # 165 # Copy the generator directory to the build directory before 166 # compiling python and proto files. Fixes issues when using the 167 # same build directory with different python/protobuf versions 168 # as the binary build directory is discarded across builds. 169 # 170 add_custom_command( 171 OUTPUT ${NANOPB_GENERATOR_EXECUTABLE} ${GENERATOR_CORE_SRC} 172 COMMAND ${CMAKE_COMMAND} -E copy_directory 173 ARGS ${NANOPB_GENERATOR_SOURCE_DIR} ${GENERATOR_PATH} 174 VERBATIM) 175 176 set(GENERATOR_CORE_PYTHON_SRC) 177 foreach(FIL ${GENERATOR_CORE_SRC}) 178 get_filename_component(ABS_FIL ${FIL} ABSOLUTE) 179 get_filename_component(FIL_WE ${FIL} NAME_WE) 180 181 set(output "${GENERATOR_CORE_DIR}/${FIL_WE}_pb2.py") 182 set(GENERATOR_CORE_PYTHON_SRC ${GENERATOR_CORE_PYTHON_SRC} ${output}) 183 add_custom_command( 184 OUTPUT ${output} 185 COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} 186 ARGS -I${GENERATOR_PATH}/proto 187 --python_out=${GENERATOR_CORE_DIR} ${ABS_FIL} 188 DEPENDS ${ABS_FIL} 189 VERBATIM) 190 endforeach() 191 192 if(NANOPB_GENERATE_CPP_RELPATH) 193 get_filename_component(ABS_ROOT ${NANOPB_GENERATE_CPP_RELPATH} ABSOLUTE) 194 endif() 195 foreach(FIL ${NANOPB_GENERATE_CPP_UNPARSED_ARGUMENTS}) 196 get_filename_component(ABS_FIL ${FIL} ABSOLUTE) 197 get_filename_component(FIL_WE ${FIL} NAME_WE) 198 get_filename_component(FIL_DIR ${FIL} PATH) 199 set(FIL_PATH_REL) 200 if(ABS_ROOT) 201 # Check that the file is under the given "RELPATH" 202 string(FIND ${ABS_FIL} ${ABS_ROOT} LOC) 203 if (${LOC} EQUAL 0) 204 string(REPLACE "${ABS_ROOT}/" "" FIL_REL ${ABS_FIL}) 205 get_filename_component(FIL_PATH_REL ${FIL_REL} PATH) 206 file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}) 207 endif() 208 endif() 209 if(NOT FIL_PATH_REL) 210 set(FIL_PATH_REL ".") 211 endif() 212 213 list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.c") 214 list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.h") 215 216 set(NANOPB_PLUGIN_OPTIONS) 217 set(NANOPB_OPTIONS_DIRS) 218 219 # If there an options file in the same working directory, set it as a dependency 220 set(NANOPB_OPTIONS_FILE ${FIL_DIR}/${FIL_WE}.options) 221 if(EXISTS ${NANOPB_OPTIONS_FILE}) 222 # Get directory as lookups for dependency options fail if an options 223 # file is used. The options is still set as a dependency of the 224 # generated source and header. 225 get_filename_component(options_dir ${NANOPB_OPTIONS_FILE} DIRECTORY) 226 list(APPEND NANOPB_OPTIONS_DIRS ${options_dir}) 227 else() 228 set(NANOPB_OPTIONS_FILE) 229 endif() 230 231 # If the dependencies are options files, we need to pass the directories 232 # as arguments to nanopb 233 foreach(depends_file ${NANOPB_DEPENDS}) 234 get_filename_component(ext ${depends_file} EXT) 235 if(ext STREQUAL ".options") 236 get_filename_component(depends_dir ${depends_file} DIRECTORY) 237 list(APPEND NANOPB_OPTIONS_DIRS ${depends_dir}) 238 endif() 239 endforeach() 240 241 if(NANOPB_OPTIONS_DIRS) 242 list(REMOVE_DUPLICATES NANOPB_OPTIONS_DIRS) 243 endif() 244 245 foreach(options_path ${NANOPB_OPTIONS_DIRS}) 246 set(NANOPB_PLUGIN_OPTIONS "${NANOPB_PLUGIN_OPTIONS} -I${options_path}") 247 endforeach() 248 249 if(NANOPB_OPTIONS) 250 set(NANOPB_PLUGIN_OPTIONS "${NANOPB_PLUGIN_OPTIONS} ${NANOPB_OPTIONS}") 251 endif() 252 253 add_custom_command( 254 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.c" 255 "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.h" 256 COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} 257 ARGS -I${GENERATOR_PATH} -I${GENERATOR_CORE_DIR} 258 -I${CMAKE_CURRENT_BINARY_DIR} ${_nanopb_include_path} 259 --plugin=protoc-gen-nanopb=${NANOPB_GENERATOR_PLUGIN} 260 "--nanopb_out=${NANOPB_PLUGIN_OPTIONS}:${CMAKE_CURRENT_BINARY_DIR}" ${ABS_FIL} 261 DEPENDS ${ABS_FIL} ${GENERATOR_CORE_PYTHON_SRC} 262 ${NANOPB_OPTIONS_FILE} ${NANOPB_DEPENDS} 263 COMMENT "Running C++ protocol buffer compiler using nanopb plugin on ${FIL}" 264 VERBATIM ) 265 266 endforeach() 267 268 set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE) 269 set(${SRCS} ${${SRCS}} ${NANOPB_SRCS} PARENT_SCOPE) 270 set(${HDRS} ${${HDRS}} ${NANOPB_HDRS} PARENT_SCOPE) 271 272endfunction() 273 274 275 276# 277# Main. 278# 279 280# By default have NANOPB_GENERATE_CPP macro pass -I to protoc 281# for each directory where a proto file is referenced. 282if(NOT DEFINED NANOPB_GENERATE_CPP_APPEND_PATH) 283 set(NANOPB_GENERATE_CPP_APPEND_PATH TRUE) 284endif() 285 286# Make a really good guess regarding location of NANOPB_SRC_ROOT_FOLDER 287if(NOT DEFINED NANOPB_SRC_ROOT_FOLDER) 288 get_filename_component(NANOPB_SRC_ROOT_FOLDER 289 ${CMAKE_CURRENT_LIST_DIR}/.. ABSOLUTE) 290endif() 291 292# Find the include directory 293find_path(NANOPB_INCLUDE_DIRS 294 pb.h 295 PATHS ${NANOPB_SRC_ROOT_FOLDER} 296) 297mark_as_advanced(NANOPB_INCLUDE_DIRS) 298 299# Find nanopb source files 300set(NANOPB_SRCS) 301set(NANOPB_HDRS) 302list(APPEND _nanopb_srcs pb_decode.c pb_encode.c pb_common.c) 303list(APPEND _nanopb_hdrs pb_decode.h pb_encode.h pb_common.h pb.h) 304 305foreach(FIL ${_nanopb_srcs}) 306 find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_SRC_ROOT_FOLDER} ${NANOPB_INCLUDE_DIRS}) 307 list(APPEND NANOPB_SRCS "${${FIL}__nano_pb_file}") 308 mark_as_advanced(${FIL}__nano_pb_file) 309endforeach() 310 311foreach(FIL ${_nanopb_hdrs}) 312 find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_INCLUDE_DIRS}) 313 mark_as_advanced(${FIL}__nano_pb_file) 314 list(APPEND NANOPB_HDRS "${${FIL}__nano_pb_file}") 315endforeach() 316 317# Find the protoc Executable 318find_program(PROTOBUF_PROTOC_EXECUTABLE 319 NAMES protoc 320 DOC "The Google Protocol Buffers Compiler" 321 PATHS 322 ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Release 323 ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Debug 324) 325mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE) 326 327# Find nanopb generator source dir 328find_path(NANOPB_GENERATOR_SOURCE_DIR 329 NAMES nanopb_generator.py 330 DOC "nanopb generator source" 331 PATHS 332 ${NANOPB_SRC_ROOT_FOLDER}/generator 333) 334mark_as_advanced(NANOPB_GENERATOR_SOURCE_DIR) 335 336find_package(PythonInterp REQUIRED) 337 338include(FindPackageHandleStandardArgs) 339FIND_PACKAGE_HANDLE_STANDARD_ARGS(NANOPB DEFAULT_MSG 340 NANOPB_INCLUDE_DIRS 341 NANOPB_SRCS NANOPB_HDRS 342 NANOPB_GENERATOR_SOURCE_DIR 343 PROTOBUF_PROTOC_EXECUTABLE 344 ) 345