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