FindNanopb.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. # Copyright 2009 Kitware, Inc.
  81. # Copyright 2009-2011 Philip Lowman <philip@yhbt.com>
  82. # Copyright 2008 Esben Mose Hansen, Ange Optimization ApS
  83. #
  84. # Redistribution and use in source and binary forms, with or without
  85. # modification, are permitted provided that the following conditions
  86. # are met:
  87. #
  88. # * Redistributions of source code must retain the above copyright
  89. # notice, this list of conditions and the following disclaimer.
  90. #
  91. # * Redistributions in binary form must reproduce the above copyright
  92. # notice, this list of conditions and the following disclaimer in the
  93. # documentation and/or other materials provided with the distribution.
  94. #
  95. # * Neither the names of Kitware, Inc., the Insight Software Consortium,
  96. # nor the names of their contributors may be used to endorse or promote
  97. # products derived from this software without specific prior written
  98. # permission.
  99. #
  100. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  101. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  102. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  103. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  104. # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  105. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  106. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  107. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  108. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  109. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  110. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  111. #
  112. #=============================================================================
  113. #
  114. # Changes
  115. # 2013.01.31 - Pavlo Ilin - used Modules/FindProtobuf.cmake from cmake 2.8.10 to
  116. # write FindNanopb.cmake
  117. #
  118. #=============================================================================
  119. function(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. if(NANOPB_GENERATE_CPP_APPEND_PATH)
  125. # Create an include path for each file specified
  126. foreach(FIL ${NANOPB_GENERATE_CPP_UNPARSED_ARGUMENTS})
  127. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  128. get_filename_component(ABS_PATH ${ABS_FIL} PATH)
  129. list(APPEND _nanopb_include_path "-I${ABS_PATH}")
  130. endforeach()
  131. else()
  132. set(_nanopb_include_path "-I${CMAKE_CURRENT_SOURCE_DIR}")
  133. endif()
  134. if(NANOPB_GENERATE_CPP_RELPATH)
  135. list(APPEND _nanopb_include_path "-I${NANOPB_GENERATE_CPP_RELPATH}")
  136. endif()
  137. if(DEFINED NANOPB_IMPORT_DIRS)
  138. foreach(DIR ${NANOPB_IMPORT_DIRS})
  139. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  140. list(APPEND _nanopb_include_path "-I${ABS_PATH}")
  141. endforeach()
  142. endif()
  143. list(REMOVE_DUPLICATES _nanopb_include_path)
  144. set(GENERATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/nanopb/generator)
  145. set(NANOPB_GENERATOR_EXECUTABLE ${GENERATOR_PATH}/nanopb_generator.py)
  146. if (CMAKE_HOST_WIN32)
  147. set(NANOPB_GENERATOR_PLUGIN ${GENERATOR_PATH}/protoc-gen-nanopb.bat)
  148. else()
  149. set(NANOPB_GENERATOR_PLUGIN ${GENERATOR_PATH}/protoc-gen-nanopb)
  150. endif()
  151. set(GENERATOR_CORE_DIR ${GENERATOR_PATH}/proto)
  152. set(GENERATOR_CORE_SRC
  153. ${GENERATOR_CORE_DIR}/nanopb.proto)
  154. # Treat the source directory as immutable.
  155. #
  156. # Copy the generator directory to the build directory before
  157. # compiling python and proto files. Fixes issues when using the
  158. # same build directory with different python/protobuf versions
  159. # as the binary build directory is discarded across builds.
  160. #
  161. add_custom_command(
  162. OUTPUT ${NANOPB_GENERATOR_EXECUTABLE} ${GENERATOR_CORE_SRC}
  163. COMMAND ${CMAKE_COMMAND} -E copy_directory
  164. ARGS ${NANOPB_GENERATOR_SOURCE_DIR} ${GENERATOR_PATH}
  165. VERBATIM)
  166. set(GENERATOR_CORE_PYTHON_SRC)
  167. foreach(FIL ${GENERATOR_CORE_SRC})
  168. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  169. get_filename_component(FIL_WE ${FIL} NAME_WE)
  170. set(output "${GENERATOR_CORE_DIR}/${FIL_WE}_pb2.py")
  171. set(GENERATOR_CORE_PYTHON_SRC ${GENERATOR_CORE_PYTHON_SRC} ${output})
  172. add_custom_command(
  173. OUTPUT ${output}
  174. COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
  175. ARGS -I${GENERATOR_PATH}/proto
  176. --python_out=${GENERATOR_CORE_DIR} ${ABS_FIL}
  177. DEPENDS ${ABS_FIL}
  178. VERBATIM)
  179. endforeach()
  180. if(NANOPB_GENERATE_CPP_RELPATH)
  181. get_filename_component(ABS_ROOT ${NANOPB_GENERATE_CPP_RELPATH} ABSOLUTE)
  182. endif()
  183. foreach(FIL ${NANOPB_GENERATE_CPP_UNPARSED_ARGUMENTS})
  184. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  185. get_filename_component(FIL_WE ${FIL} NAME_WE)
  186. get_filename_component(FIL_DIR ${FIL} PATH)
  187. set(FIL_PATH_REL)
  188. if(ABS_ROOT)
  189. # Check that the file is under the given "RELPATH"
  190. string(FIND ${ABS_FIL} ${ABS_ROOT} LOC)
  191. if (${LOC} EQUAL 0)
  192. string(REPLACE "${ABS_ROOT}/" "" FIL_REL ${ABS_FIL})
  193. get_filename_component(FIL_PATH_REL ${FIL_REL} PATH)
  194. file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL})
  195. endif()
  196. endif()
  197. if(NOT FIL_PATH_REL)
  198. set(FIL_PATH_REL ".")
  199. endif()
  200. list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.c")
  201. list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.h")
  202. set(NANOPB_PLUGIN_OPTIONS)
  203. set(NANOPB_OPTIONS_DIRS)
  204. # If there an options file in the same working directory, set it as a dependency
  205. get_filename_component(ABS_OPT_FIL ${FIL_DIR}/${FIL_WE}.options ABSOLUTE)
  206. if(EXISTS ${ABS_OPT_FIL})
  207. # Get directory as lookups for dependency options fail if an options
  208. # file is used. The options is still set as a dependency of the
  209. # generated source and header.
  210. get_filename_component(options_dir ${ABS_OPT_FIL} DIRECTORY)
  211. list(APPEND NANOPB_OPTIONS_DIRS ${options_dir})
  212. else()
  213. set(ABS_OPT_FIL)
  214. endif()
  215. # If the dependencies are options files, we need to pass the directories
  216. # as arguments to nanopb
  217. foreach(depends_file ${NANOPB_DEPENDS})
  218. get_filename_component(ext ${depends_file} EXT)
  219. if(ext STREQUAL ".options")
  220. get_filename_component(depends_dir ${depends_file} DIRECTORY)
  221. list(APPEND NANOPB_OPTIONS_DIRS ${depends_dir})
  222. endif()
  223. endforeach()
  224. if(NANOPB_OPTIONS_DIRS)
  225. list(REMOVE_DUPLICATES NANOPB_OPTIONS_DIRS)
  226. endif()
  227. foreach(options_path ${NANOPB_OPTIONS_DIRS})
  228. set(NANOPB_PLUGIN_OPTIONS "${NANOPB_PLUGIN_OPTIONS} -I${options_path}")
  229. endforeach()
  230. if(NANOPB_OPTIONS)
  231. set(NANOPB_PLUGIN_OPTIONS "${NANOPB_PLUGIN_OPTIONS} ${NANOPB_OPTIONS}")
  232. endif()
  233. # based on the version of protoc it might be necessary to add "/${FIL_PATH_REL}" currently dealt with in #516
  234. set(NANOPB_OUT "${CMAKE_CURRENT_BINARY_DIR}")
  235. # We need to pass the path to the option files to the nanopb plugin. There are two ways to do it.
  236. # - An older hacky one using ':' as option separator in protoc args preventing the ':' to be used in path.
  237. # - Or a newer one, using --nanopb_opt which requires a version of protoc >= 3.6
  238. # So we will determine which version of protoc we have available and choose accordingly.
  239. execute_process(COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --version OUTPUT_VARIABLE PROTOC_VERSION_STRING OUTPUT_STRIP_TRAILING_WHITESPACE)
  240. string(REGEX MATCH "[(0-9)].*.[(0-9)].*.[(0-9)].*$" PROTOC_VERSION "${PROTOC_VERSION_STRING}")
  241. if(PROTOC_VERSION AND PROTOC_VERSION VERSION_LESS "3.6.0")
  242. #try to use the older way
  243. string(REGEX MATCH ":" HAS_COLON_IN_PATH ${NANOPB_PLUGIN_OPTIONS} ${NANOPB_OUT})
  244. if(HAS_COLON_IN_PATH)
  245. 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.")
  246. endif()
  247. set(NANOPB_OPT_STRING "--nanopb_out=${NANOPB_PLUGIN_OPTIONS}:${NANOPB_OUT}")
  248. else()
  249. set(NANOPB_OPT_STRING "--nanopb_opt=${NANOPB_PLUGIN_OPTIONS}" "--nanopb_out=${NANOPB_OUT}")
  250. endif()
  251. add_custom_command(
  252. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.c"
  253. "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.h"
  254. COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
  255. ARGS -I${GENERATOR_PATH} -I${GENERATOR_CORE_DIR}
  256. -I${CMAKE_CURRENT_BINARY_DIR} ${_nanopb_include_path}
  257. --plugin=protoc-gen-nanopb=${NANOPB_GENERATOR_PLUGIN}
  258. ${NANOPB_OPT_STRING}
  259. ${ABS_FIL}
  260. DEPENDS ${ABS_FIL} ${GENERATOR_CORE_PYTHON_SRC}
  261. ${ABS_OPT_FIL} ${NANOPB_DEPENDS}
  262. COMMENT "Running C++ protocol buffer compiler using nanopb plugin on ${FIL}"
  263. VERBATIM )
  264. endforeach()
  265. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
  266. set(${SRCS} ${${SRCS}} ${NANOPB_SRCS} PARENT_SCOPE)
  267. set(${HDRS} ${${HDRS}} ${NANOPB_HDRS} PARENT_SCOPE)
  268. endfunction()
  269. #
  270. # Main.
  271. #
  272. # By default have NANOPB_GENERATE_CPP macro pass -I to protoc
  273. # for each directory where a proto file is referenced.
  274. if(NOT DEFINED NANOPB_GENERATE_CPP_APPEND_PATH)
  275. set(NANOPB_GENERATE_CPP_APPEND_PATH TRUE)
  276. endif()
  277. # Make a really good guess regarding location of NANOPB_SRC_ROOT_FOLDER
  278. if(NOT DEFINED NANOPB_SRC_ROOT_FOLDER)
  279. get_filename_component(NANOPB_SRC_ROOT_FOLDER
  280. ${CMAKE_CURRENT_LIST_DIR}/.. ABSOLUTE)
  281. endif()
  282. # Find the include directory
  283. find_path(NANOPB_INCLUDE_DIRS
  284. pb.h
  285. PATHS ${NANOPB_SRC_ROOT_FOLDER}
  286. NO_CMAKE_FIND_ROOT_PATH
  287. )
  288. mark_as_advanced(NANOPB_INCLUDE_DIRS)
  289. # Find nanopb source files
  290. set(NANOPB_SRCS)
  291. set(NANOPB_HDRS)
  292. list(APPEND _nanopb_srcs pb_decode.c pb_encode.c pb_common.c)
  293. list(APPEND _nanopb_hdrs pb_decode.h pb_encode.h pb_common.h pb.h)
  294. foreach(FIL ${_nanopb_srcs})
  295. find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_SRC_ROOT_FOLDER} ${NANOPB_INCLUDE_DIRS} NO_CMAKE_FIND_ROOT_PATH)
  296. list(APPEND NANOPB_SRCS "${${FIL}__nano_pb_file}")
  297. mark_as_advanced(${FIL}__nano_pb_file)
  298. endforeach()
  299. foreach(FIL ${_nanopb_hdrs})
  300. find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_INCLUDE_DIRS} NO_CMAKE_FIND_ROOT_PATH)
  301. mark_as_advanced(${FIL}__nano_pb_file)
  302. list(APPEND NANOPB_HDRS "${${FIL}__nano_pb_file}")
  303. endforeach()
  304. # Find the protoc Executable
  305. find_program(PROTOBUF_PROTOC_EXECUTABLE
  306. NAMES protoc
  307. DOC "The Google Protocol Buffers Compiler"
  308. PATHS
  309. ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Release
  310. ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Debug
  311. ${NANOPB_SRC_ROOT_FOLDER}/generator-bin
  312. ${NANOPB_SRC_ROOT_FOLDER}/generator
  313. )
  314. mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE)
  315. # Find nanopb generator source dir
  316. find_path(NANOPB_GENERATOR_SOURCE_DIR
  317. NAMES nanopb_generator.py
  318. DOC "nanopb generator source"
  319. PATHS
  320. ${NANOPB_SRC_ROOT_FOLDER}/generator
  321. NO_CMAKE_FIND_ROOT_PATH
  322. )
  323. mark_as_advanced(NANOPB_GENERATOR_SOURCE_DIR)
  324. include(FindPackageHandleStandardArgs)
  325. find_package_handle_standard_args(Nanopb DEFAULT_MSG
  326. NANOPB_INCLUDE_DIRS
  327. NANOPB_SRCS NANOPB_HDRS
  328. NANOPB_GENERATOR_SOURCE_DIR
  329. PROTOBUF_PROTOC_EXECUTABLE
  330. )