cmake: Build generator files in build directory
[apps/agl-service-can-low-level.git] / extra / FindNanopb.cmake
1 # This is an example script for use with CMake projects for locating and configuring
2 # the nanopb library.
3 #
4 # The following varialbes have to be set:
5 #
6 #   NANOPB_SRC_ROOT_FOLDER  - Path to nanopb source folder
7 #
8 # The following variables can be set and are optional:
9 #
10 #
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.
15 #
16 #   NANOPB_IMPORT_DIRS       - List of additional directories to be searched for
17 #                              imported .proto files.
18 #
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.
22 #
23 # Defines the following variables:
24 #
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
27 #
28 # The following cache variables are also available to set or use:
29 #   PROTOBUF_PROTOC_EXECUTABLE - The protoc compiler
30 #   NANOPB_GENERATOR_SOURCE_DIR - The nanopb generator source
31 #
32 #  ====================================================================
33 #
34 # NANOPB_GENERATE_CPP (public function)
35 #   SRCS = Variable to define with autogenerated
36 #          source files
37 #   HDRS = Variable to define with autogenerated
38 #          header files
39 #   ARGN = proto files
40 #
41 #  ====================================================================
42 #  Example:
43 #
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})
48 #
49 #   NANOPB_GENERATE_CPP(PROTO_SRCS PROTO_HDRS foo.proto)
50 #
51 #   include_directories(${CMAKE_CURRENT_BINARY_DIR})
52 #   add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
53 #
54 #  ====================================================================
55
56 #=============================================================================
57 # Copyright 2009 Kitware, Inc.
58 # Copyright 2009-2011 Philip Lowman <philip@yhbt.com>
59 # Copyright 2008 Esben Mose Hansen, Ange Optimization ApS
60 #
61 # Redistribution and use in source and binary forms, with or without
62 # modification, are permitted provided that the following conditions
63 # are met:
64 #
65 # * Redistributions of source code must retain the above copyright
66 #   notice, this list of conditions and the following disclaimer.
67 #
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.
71 #
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
75 #   permission.
76 #
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.
88 #
89 #=============================================================================
90 #
91 # Changes
92 # 2013.01.31 - Pavlo Ilin - used Modules/FindProtobuf.cmake from cmake 2.8.10 to
93 #                           write FindNanopb.cmake
94 #
95 #=============================================================================
96
97
98 function(NANOPB_GENERATE_CPP SRCS HDRS)
99   if(NOT ARGN)
100     return()
101   endif()
102
103   if(NANOPB_GENERATE_CPP_APPEND_PATH)
104     # Create an include path for each file specified
105     foreach(FIL ${ARGN})
106       get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
107       get_filename_component(ABS_PATH ${ABS_FIL} PATH)
108
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})
112       endif()
113     endforeach()
114   else()
115     set(_nanobp_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
116   endif()
117
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})
124       endif()
125     endforeach()
126   endif()
127
128   set(${SRCS})
129   set(${HDRS})
130
131   set(GENERATOR_PATH ${CMAKE_BINARY_DIR}/nanopb/generator)
132
133   set(NANOPB_GENERATOR_EXECUTABLE ${GENERATOR_PATH}/nanopb_generator.py)
134
135   set(GENERATOR_CORE_DIR ${GENERATOR_PATH}/proto)
136   set(GENERATOR_CORE_SRC
137       ${GENERATOR_CORE_DIR}/nanopb.proto
138       ${GENERATOR_CORE_DIR}/plugin.proto)
139
140   # Treat the source diretory as immutable.
141   #
142   # Copy the generator directory to the build directory before
143   # compiling python and proto files.  Fixes issues when using the
144   # same build directory with different python/protobuf versions
145   # as the binary build directory is discarded across builds.
146   #
147   add_custom_command(
148       OUTPUT ${NANOPB_GENERATOR_EXECUTABLE} ${GENERATOR_CORE_SRC}
149       COMMAND ${CMAKE_COMMAND} -E copy_directory
150       ARGS ${NANOPB_GENERATOR_SOURCE_DIR} ${GENERATOR_PATH}
151       VERBATIM)
152
153   set(GENERATOR_CORE_PYTHON_SRC)
154   foreach(FIL ${GENERATOR_CORE_SRC})
155       get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
156       get_filename_component(FIL_WE ${FIL} NAME_WE)
157
158       set(output "${GENERATOR_CORE_DIR}/${FIL_WE}_pb2.py")
159       set(GENERATOR_CORE_PYTHON_SRC ${GENERATOR_CORE_PYTHON_SRC} ${output})
160       add_custom_command(
161         OUTPUT ${output}
162         COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
163         ARGS -I${GENERATOR_PATH}/proto
164           --python_out=${GENERATOR_CORE_DIR} ${ABS_FIL}
165         DEPENDS ${ABS_FIL}
166         VERBATIM)
167   endforeach()
168
169   foreach(FIL ${ARGN})
170     get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
171     get_filename_component(FIL_WE ${FIL} NAME_WE)
172     get_filename_component(FIL_DIR ${FIL} PATH)
173     set(NANOPB_OPTIONS_FILE ${FIL_DIR}/${FIL_WE}.options)
174     set(NANOPB_OPTIONS)
175     if(EXISTS ${NANOPB_OPTIONS_FILE})
176         set(NANOPB_OPTIONS -f ${NANOPB_OPTIONS_FILE})
177     endif()
178
179     list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.c")
180     list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
181
182     add_custom_command(
183       OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb"
184       COMMAND  ${PROTOBUF_PROTOC_EXECUTABLE}
185       ARGS -I${GENERATOR_PATH} -I${GENERATOR_CORE_DIR}
186         -I${CMAKE_CURRENT_BINARY_DIR} ${_nanobp_include_path}
187         -o${FIL_WE}.pb ${ABS_FIL}
188       DEPENDS ${ABS_FIL} ${GENERATOR_CORE_PYTHON_SRC}
189       COMMENT "Running C++ protocol buffer compiler on ${FIL}"
190       VERBATIM )
191
192     add_custom_command(
193       OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.c"
194              "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
195       COMMAND ${PYTHON_EXECUTABLE}
196       ARGS ${NANOPB_GENERATOR_EXECUTABLE} ${FIL_WE}.pb ${NANOPB_OPTIONS}
197       DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb"
198       COMMENT "Running nanopb generator on ${FIL_WE}.pb"
199       VERBATIM )
200   endforeach()
201
202   set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
203   set(${SRCS} ${${SRCS}} ${NANOPB_SRCS} PARENT_SCOPE)
204   set(${HDRS} ${${HDRS}} ${NANOPB_HDRS} PARENT_SCOPE)
205
206 endfunction()
207
208
209
210 #
211 # Main.
212 #
213
214 # By default have NANOPB_GENERATE_CPP macro pass -I to protoc
215 # for each directory where a proto file is referenced.
216 if(NOT DEFINED NANOPB_GENERATE_CPP_APPEND_PATH)
217   set(NANOPB_GENERATE_CPP_APPEND_PATH TRUE)
218 endif()
219
220 # Find the include directory
221 find_path(NANOPB_INCLUDE_DIRS
222     pb.h
223     PATHS ${NANOPB_SRC_ROOT_FOLDER}
224 )
225 mark_as_advanced(NANOPB_INCLUDE_DIRS)
226
227 # Find nanopb source files
228 set(NANOPB_SRCS)
229 set(NANOPB_HDRS)
230 list(APPEND _nanopb_srcs pb_decode.c pb_encode.c pb_common.c)
231 list(APPEND _nanopb_hdrs pb_decode.h pb_encode.h pb_common.h pb.h)
232
233 foreach(FIL ${_nanopb_srcs})
234   find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_SRC_ROOT_FOLDER} ${NANOPB_INCLUDE_DIRS})
235   list(APPEND NANOPB_SRCS "${${FIL}__nano_pb_file}")
236   mark_as_advanced(${FIL}__nano_pb_file)
237 endforeach()
238
239 foreach(FIL ${_nanopb_hdrs})
240   find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_INCLUDE_DIRS})
241   mark_as_advanced(${FIL}__nano_pb_file)
242   list(APPEND NANOPB_HDRS "${${FIL}__nano_pb_file}")
243 endforeach()
244
245 # Find the protoc Executable
246 find_program(PROTOBUF_PROTOC_EXECUTABLE
247     NAMES protoc
248     DOC "The Google Protocol Buffers Compiler"
249     PATHS
250     ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Release
251     ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Debug
252 )
253 mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE)
254
255 # Find nanopb generator source dir
256 find_path(NANOPB_GENERATOR_SOURCE_DIR
257     NAMES nanopb_generator.py
258     DOC "nanopb generator source"
259     PATHS
260     ${NANOPB_SRC_ROOT_FOLDER}/generator
261 )
262 mark_as_advanced(NANOPB_GENERATOR_SOURCE_DIR)
263
264 find_package(PythonInterp REQUIRED)
265
266 include(FindPackageHandleStandardArgs)
267 FIND_PACKAGE_HANDLE_STANDARD_ARGS(NANOPB DEFAULT_MSG
268   NANOPB_INCLUDE_DIRS
269   NANOPB_SRCS NANOPB_HDRS
270   NANOPB_GENERATOR_SOURCE_DIR
271   PROTOBUF_PROTOC_EXECUTABLE
272   )