• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <philip@yhbt.com>
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  set(NANOPB_GENERATOR_PLUGIN ${GENERATOR_PATH}/protoc-gen-nanopb)
153
154  set(GENERATOR_CORE_DIR ${GENERATOR_PATH}/proto)
155  set(GENERATOR_CORE_SRC
156      ${GENERATOR_CORE_DIR}/nanopb.proto
157      ${GENERATOR_CORE_DIR}/plugin.proto)
158
159  # Treat the source diretory as immutable.
160  #
161  # Copy the generator directory to the build directory before
162  # compiling python and proto files.  Fixes issues when using the
163  # same build directory with different python/protobuf versions
164  # as the binary build directory is discarded across builds.
165  #
166  add_custom_command(
167      OUTPUT ${NANOPB_GENERATOR_EXECUTABLE} ${GENERATOR_CORE_SRC}
168      COMMAND ${CMAKE_COMMAND} -E copy_directory
169      ARGS ${NANOPB_GENERATOR_SOURCE_DIR} ${GENERATOR_PATH}
170      VERBATIM)
171
172  set(GENERATOR_CORE_PYTHON_SRC)
173  foreach(FIL ${GENERATOR_CORE_SRC})
174      get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
175      get_filename_component(FIL_WE ${FIL} NAME_WE)
176
177      set(output "${GENERATOR_CORE_DIR}/${FIL_WE}_pb2.py")
178      set(GENERATOR_CORE_PYTHON_SRC ${GENERATOR_CORE_PYTHON_SRC} ${output})
179      add_custom_command(
180        OUTPUT ${output}
181        COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
182        ARGS -I${GENERATOR_PATH}/proto
183          --python_out=${GENERATOR_CORE_DIR} ${ABS_FIL}
184        DEPENDS ${ABS_FIL}
185        VERBATIM)
186  endforeach()
187
188  if(NANOPB_GENERATE_CPP_RELPATH)
189      get_filename_component(ABS_ROOT ${NANOPB_GENERATE_CPP_RELPATH} ABSOLUTE)
190  endif()
191  foreach(FIL ${NANOPB_GENERATE_CPP_UNPARSED_ARGUMENTS})
192    get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
193    get_filename_component(FIL_WE ${FIL} NAME_WE)
194    get_filename_component(FIL_DIR ${FIL} PATH)
195    set(FIL_PATH_REL)
196    if(ABS_ROOT)
197      # Check that the file is under the given "RELPATH"
198      string(FIND ${ABS_FIL} ${ABS_ROOT} LOC)
199      if (${LOC} EQUAL 0)
200        string(REPLACE "${ABS_ROOT}/" "" FIL_REL ${ABS_FIL})
201        get_filename_component(FIL_PATH_REL ${FIL_REL} PATH)
202        file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL})
203      endif()
204    endif()
205    if(NOT FIL_PATH_REL)
206      set(FIL_PATH_REL ".")
207    endif()
208
209    list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.c")
210    list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.h")
211
212    set(NANOPB_PLUGIN_OPTIONS)
213    set(NANOPB_OPTIONS_DIRS)
214
215    # If there an options file in the same working directory, set it as a dependency
216    set(NANOPB_OPTIONS_FILE ${FIL_DIR}/${FIL_WE}.options)
217    if(EXISTS ${NANOPB_OPTIONS_FILE})
218        # Get directory as lookups for dependency options fail if an options
219        # file is used. The options is still set as a dependency of the
220        # generated source and header.
221        get_filename_component(options_dir ${NANOPB_OPTIONS_FILE} DIRECTORY)
222        list(APPEND NANOPB_OPTIONS_DIRS ${options_dir})
223    else()
224        set(NANOPB_OPTIONS_FILE)
225    endif()
226
227    # If the dependencies are options files, we need to pass the directories
228    # as arguments to nanopb
229    foreach(depends_file ${NANOPB_DEPENDS})
230        get_filename_component(ext ${depends_file} EXT)
231        if(ext STREQUAL ".options")
232            get_filename_component(depends_dir ${depends_file} DIRECTORY)
233            list(APPEND NANOPB_OPTIONS_DIRS ${depends_dir})
234        endif()
235    endforeach()
236
237    if(NANOPB_OPTIONS_DIRS)
238        list(REMOVE_DUPLICATES NANOPB_OPTIONS_DIRS)
239    endif()
240
241    foreach(options_path ${NANOPB_OPTIONS_DIRS})
242        set(NANOPB_PLUGIN_OPTIONS "${NANOPB_PLUGIN_OPTIONS} -I${options_path}")
243    endforeach()
244
245    if(NANOPB_OPTIONS)
246        set(NANOPB_PLUGIN_OPTIONS "${NANOPB_PLUGIN_OPTIONS} ${NANOPB_OPTIONS}")
247    endif()
248
249    add_custom_command(
250      OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.c"
251             "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.h"
252      COMMAND  ${PROTOBUF_PROTOC_EXECUTABLE}
253      ARGS -I${GENERATOR_PATH} -I${GENERATOR_CORE_DIR}
254           -I${CMAKE_CURRENT_BINARY_DIR} ${_nanopb_include_path}
255           --plugin=protoc-gen-nanopb=${NANOPB_GENERATOR_PLUGIN}
256           "--nanopb_out=${NANOPB_PLUGIN_OPTIONS}:${CMAKE_CURRENT_BINARY_DIR}" ${ABS_FIL}
257      DEPENDS ${ABS_FIL} ${GENERATOR_CORE_PYTHON_SRC}
258           ${NANOPB_OPTIONS_FILE} ${NANOPB_DEPENDS}
259      COMMENT "Running C++ protocol buffer compiler using nanopb plugin on ${FIL}"
260      VERBATIM )
261
262  endforeach()
263
264  set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
265  set(${SRCS} ${${SRCS}} ${NANOPB_SRCS} PARENT_SCOPE)
266  set(${HDRS} ${${HDRS}} ${NANOPB_HDRS} PARENT_SCOPE)
267
268endfunction()
269
270
271
272#
273# Main.
274#
275
276# By default have NANOPB_GENERATE_CPP macro pass -I to protoc
277# for each directory where a proto file is referenced.
278if(NOT DEFINED NANOPB_GENERATE_CPP_APPEND_PATH)
279  set(NANOPB_GENERATE_CPP_APPEND_PATH TRUE)
280endif()
281
282# Make a really good guess regarding location of NANOPB_SRC_ROOT_FOLDER
283if(NOT DEFINED NANOPB_SRC_ROOT_FOLDER)
284  get_filename_component(NANOPB_SRC_ROOT_FOLDER
285                         ${CMAKE_CURRENT_LIST_DIR}/.. ABSOLUTE)
286endif()
287
288# Find the include directory
289find_path(NANOPB_INCLUDE_DIRS
290    pb.h
291    PATHS ${NANOPB_SRC_ROOT_FOLDER}
292)
293mark_as_advanced(NANOPB_INCLUDE_DIRS)
294
295# Find nanopb source files
296set(NANOPB_SRCS)
297set(NANOPB_HDRS)
298list(APPEND _nanopb_srcs pb_decode.c pb_encode.c pb_common.c)
299list(APPEND _nanopb_hdrs pb_decode.h pb_encode.h pb_common.h pb.h)
300
301foreach(FIL ${_nanopb_srcs})
302  find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_SRC_ROOT_FOLDER} ${NANOPB_INCLUDE_DIRS})
303  list(APPEND NANOPB_SRCS "${${FIL}__nano_pb_file}")
304  mark_as_advanced(${FIL}__nano_pb_file)
305endforeach()
306
307foreach(FIL ${_nanopb_hdrs})
308  find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_INCLUDE_DIRS})
309  mark_as_advanced(${FIL}__nano_pb_file)
310  list(APPEND NANOPB_HDRS "${${FIL}__nano_pb_file}")
311endforeach()
312
313# Find the protoc Executable
314find_program(PROTOBUF_PROTOC_EXECUTABLE
315    NAMES protoc
316    DOC "The Google Protocol Buffers Compiler"
317    PATHS
318    ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Release
319    ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Debug
320)
321mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE)
322
323# Find nanopb generator source dir
324find_path(NANOPB_GENERATOR_SOURCE_DIR
325    NAMES nanopb_generator.py
326    DOC "nanopb generator source"
327    PATHS
328    ${NANOPB_SRC_ROOT_FOLDER}/generator
329)
330mark_as_advanced(NANOPB_GENERATOR_SOURCE_DIR)
331
332find_package(PythonInterp REQUIRED)
333
334include(FindPackageHandleStandardArgs)
335FIND_PACKAGE_HANDLE_STANDARD_ARGS(NANOPB DEFAULT_MSG
336  NANOPB_INCLUDE_DIRS
337  NANOPB_SRCS NANOPB_HDRS
338  NANOPB_GENERATOR_SOURCE_DIR
339  PROTOBUF_PROTOC_EXECUTABLE
340  )
341