Fix path in FindNanopb.cmake.
[apps/agl-service-can-low-level.git] / cmake / FindNanopb.cmake
1 # Locate and configure the nanopb library.
2 #
3 # The following varialbes have to be set:
4 #
5 #   NANOPB_SRC_ROOT_FOLDER  - Path to nanopb source folder
6 #
7 # The following variables can be set and are optional:
8 #
9 #
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.
14 #
15 #   NANOPB_IMPORT_DIRS       - List of additional directories to be searched for
16 #                              imported .proto files.
17 #
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.
21 #
22 # Defines the following variables:
23 #
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
26 #
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
30 #
31 #  ====================================================================
32 #
33 # NANOPB_GENERATE_CPP (public function)
34 #   SRCS = Variable to define with autogenerated
35 #          source files
36 #   HDRS = Variable to define with autogenerated
37 #          header files
38 #   ARGN = proto files
39 #
40 #  ====================================================================
41 #  Example:
42 #
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})
47 #
48 #   NANOPB_GENERATE_CPP(PROTO_SRCS PROTO_HDRS foo.proto)
49 #
50 #   include_directories(${CMAKE_CURRENT_BINARY_DIR})
51 #   add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
52 #
53 #  ====================================================================
54
55 #=============================================================================
56 # Copyright 2009 Kitware, Inc.
57 # Copyright 2009-2011 Philip Lowman <philip@yhbt.com>
58 # Copyright 2008 Esben Mose Hansen, Ange Optimization ApS
59 #
60 # Redistribution and use in source and binary forms, with or without
61 # modification, are permitted provided that the following conditions
62 # are met:
63 #
64 # * Redistributions of source code must retain the above copyright
65 #   notice, this list of conditions and the following disclaimer.
66 #
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.
70 #
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
74 #   permission.
75 #
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.
87 #
88 #=============================================================================
89 #
90 # Changes
91 # 2013.01.31 - Pavlo Ilin - used Modules/FindProtobuf.cmake from cmake 2.8.10 to
92 #                           write FindNanopb.cmake
93 #
94 #=============================================================================
95
96
97 function(NANOPB_GENERATE_CPP SRCS HDRS)
98   if(NOT ARGN)
99     return()
100   endif()
101
102   if(NANOPB_GENERATE_CPP_APPEND_PATH)
103     # Create an include path for each file specified
104     foreach(FIL ${ARGN})
105       get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
106       get_filename_component(ABS_PATH ${ABS_FIL} PATH)
107
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})
111       endif()
112     endforeach()
113   else()
114     set(_nanobp_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
115   endif()
116
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})
123       endif()
124     endforeach()
125   endif()
126
127   set(${SRCS})
128   set(${HDRS})
129   get_filename_component(GENERATOR_PATH ${NANOPB_GENERATOR_EXECUTABLE} PATH)
130
131   foreach(FIL ${ARGN})
132     get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
133     get_filename_component(FIL_WE ${FIL} NAME_WE)
134
135     list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.c")
136     list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
137
138     add_custom_command(
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}
142       DEPENDS ${ABS_FIL}
143       COMMENT "Running C++ protocol buffer compiler on ${FIL}"
144       VERBATIM )
145
146     add_custom_command(
147       OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.c"
148              "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
149       COMMAND python
150       ARGS ${NANOPB_GENERATOR_EXECUTABLE} ${FIL_WE}.pb
151       DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb"
152       COMMENT "Running nanopb generator on ${FIL_WE}.pb"
153       VERBATIM )
154   endforeach()
155
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)
159
160 endfunction()
161
162
163
164 #
165 # Main.
166 #
167
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)
172 endif()
173
174 # Find the include directory
175 find_path(NANOPB_INCLUDE_DIRS
176     pb.h
177     PATHS ${NANOPB_SRC_ROOT_FOLDER}
178 )
179 mark_as_advanced(NANOPB_INCLUDE_DIRS)
180
181 # Find nanopb source files
182 set(NANOPB_SRCS)
183 set(NANOPB_HDRS)
184 list(APPEND _nanopb_srcs pb_decode.c pb_encode.c)
185 list(APPEND _nanopb_hdrs pb_decode.h pb_encode.h pb.h)
186
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)
191 endforeach()
192
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}")
197 endforeach()
198
199 # Find the protoc Executable
200 find_program(PROTOBUF_PROTOC_EXECUTABLE
201     NAMES protoc
202     DOC "The Google Protocol Buffers Compiler"
203     PATHS
204     ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Release
205     ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Debug
206 )
207 mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE)
208
209 # Find nanopb generator
210 find_file(NANOPB_GENERATOR_EXECUTABLE
211     NAMES nanopb_generator.py
212     DOC "nanopb generator"
213     PATHS
214     ${NANOPB_SRC_ROOT_FOLDER}/generator
215 )
216 mark_as_advanced(NANOPB_GENERATOR_EXECUTABLE)
217
218 include(FindPackageHandleStandardArgs)
219 FIND_PACKAGE_HANDLE_STANDARD_ARGS(NANOPB DEFAULT_MSG
220   NANOPB_INCLUDE_DIRS
221   NANOPB_SRCS NANOPB_HDRS
222   NANOPB_GENERATOR_EXECUTABLE
223   PROTOBUF_PROTOC_EXECUTABLE
224   )