FindNanopb.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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(MSVC)
  125. set(CUSTOM_COMMAND_PREFIX call)
  126. endif()
  127. if(NANOPB_GENERATE_CPP_APPEND_PATH)
  128. # Create an include path for each file specified
  129. foreach(FIL ${NANOPB_GENERATE_CPP_UNPARSED_ARGUMENTS})
  130. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  131. get_filename_component(ABS_PATH ${ABS_FIL} PATH)
  132. list(APPEND _nanopb_include_path "-I${ABS_PATH}")
  133. endforeach()
  134. else()
  135. set(_nanopb_include_path "-I${CMAKE_CURRENT_SOURCE_DIR}")
  136. endif()
  137. if(NANOPB_GENERATE_CPP_RELPATH)
  138. list(APPEND _nanopb_include_path "-I${NANOPB_GENERATE_CPP_RELPATH}")
  139. endif()
  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. list(REMOVE_DUPLICATES _nanopb_include_path)
  147. set(GENERATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/nanopb/generator)
  148. set(NANOPB_GENERATOR_EXECUTABLE ${GENERATOR_PATH}/nanopb_generator.py)
  149. if (CMAKE_HOST_WIN32)
  150. set(NANOPB_GENERATOR_PLUGIN ${GENERATOR_PATH}/protoc-gen-nanopb.bat)
  151. else()
  152. set(NANOPB_GENERATOR_PLUGIN ${GENERATOR_PATH}/protoc-gen-nanopb)
  153. endif()
  154. set(GENERATOR_CORE_DIR ${GENERATOR_PATH}/proto)
  155. set(GENERATOR_CORE_SRC
  156. ${GENERATOR_CORE_DIR}/nanopb.proto)
  157. # Treat the source directory as immutable.
  158. #
  159. # Copy the generator directory to the build directory before
  160. # compiling python and proto files. Fixes issues when using the
  161. # same build directory with different python/protobuf versions
  162. # as the binary build directory is discarded across builds.
  163. #
  164. add_custom_command(
  165. OUTPUT ${NANOPB_GENERATOR_EXECUTABLE} ${GENERATOR_CORE_SRC}
  166. COMMAND ${CMAKE_COMMAND} -E copy_directory
  167. ARGS ${NANOPB_GENERATOR_SOURCE_DIR} ${GENERATOR_PATH}
  168. VERBATIM)
  169. set(GENERATOR_CORE_PYTHON_SRC)
  170. foreach(FIL ${GENERATOR_CORE_SRC})
  171. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  172. get_filename_component(FIL_WE ${FIL} NAME_WE)
  173. set(output "${GENERATOR_CORE_DIR}/${FIL_WE}_pb2.py")
  174. set(GENERATOR_CORE_PYTHON_SRC ${GENERATOR_CORE_PYTHON_SRC} ${output})
  175. add_custom_command(
  176. OUTPUT ${output}
  177. COMMAND ${CUSTOM_COMMAND_PREFIX} ${PROTOBUF_PROTOC_EXECUTABLE}
  178. ARGS -I${GENERATOR_PATH}/proto
  179. --python_out=${GENERATOR_CORE_DIR} ${ABS_FIL}
  180. DEPENDS ${ABS_FIL}
  181. VERBATIM)
  182. endforeach()
  183. if(NANOPB_GENERATE_CPP_RELPATH)
  184. get_filename_component(ABS_ROOT ${NANOPB_GENERATE_CPP_RELPATH} ABSOLUTE)
  185. endif()
  186. foreach(FIL ${NANOPB_GENERATE_CPP_UNPARSED_ARGUMENTS})
  187. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  188. get_filename_component(FIL_WE ${FIL} NAME_WE)
  189. get_filename_component(FIL_DIR ${FIL} PATH)
  190. set(FIL_PATH_REL)
  191. if(ABS_ROOT)
  192. # Check that the file is under the given "RELPATH"
  193. string(FIND ${ABS_FIL} ${ABS_ROOT} LOC)
  194. if (${LOC} EQUAL 0)
  195. string(REPLACE "${ABS_ROOT}/" "" FIL_REL ${ABS_FIL})
  196. get_filename_component(FIL_PATH_REL ${FIL_REL} PATH)
  197. file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL})
  198. endif()
  199. endif()
  200. if(NOT FIL_PATH_REL)
  201. set(FIL_PATH_REL ".")
  202. endif()
  203. list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.c")
  204. list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.h")
  205. set(NANOPB_PLUGIN_OPTIONS)
  206. set(NANOPB_OPTIONS_DIRS)
  207. # If there an options file in the same working directory, set it as a dependency
  208. get_filename_component(ABS_OPT_FIL ${FIL_DIR}/${FIL_WE}.options ABSOLUTE)
  209. if(EXISTS ${ABS_OPT_FIL})
  210. # Get directory as lookups for dependency options fail if an options
  211. # file is used. The options is still set as a dependency of the
  212. # generated source and header.
  213. get_filename_component(options_dir ${ABS_OPT_FIL} DIRECTORY)
  214. list(APPEND NANOPB_OPTIONS_DIRS ${options_dir})
  215. else()
  216. set(ABS_OPT_FIL)
  217. endif()
  218. # If the dependencies are options files, we need to pass the directories
  219. # as arguments to nanopb
  220. foreach(depends_file ${NANOPB_DEPENDS})
  221. get_filename_component(ext ${depends_file} EXT)
  222. if(ext STREQUAL ".options")
  223. get_filename_component(depends_dir ${depends_file} DIRECTORY)
  224. list(APPEND NANOPB_OPTIONS_DIRS ${depends_dir})
  225. endif()
  226. endforeach()
  227. if(NANOPB_OPTIONS_DIRS)
  228. list(REMOVE_DUPLICATES NANOPB_OPTIONS_DIRS)
  229. endif()
  230. foreach(options_path ${NANOPB_OPTIONS_DIRS})
  231. set(NANOPB_PLUGIN_OPTIONS "${NANOPB_PLUGIN_OPTIONS} -I${options_path}")
  232. endforeach()
  233. if(NANOPB_OPTIONS)
  234. set(NANOPB_PLUGIN_OPTIONS "${NANOPB_PLUGIN_OPTIONS} ${NANOPB_OPTIONS}")
  235. endif()
  236. # based on the version of protoc it might be necessary to add "/${FIL_PATH_REL}" currently dealt with in #516
  237. set(NANOPB_OUT "${CMAKE_CURRENT_BINARY_DIR}")
  238. # We need to pass the path to the option files to the nanopb plugin. There are two ways to do it.
  239. # - An older hacky one using ':' as option separator in protoc args preventing the ':' to be used in path.
  240. # - Or a newer one, using --nanopb_opt which requires a version of protoc >= 3.6
  241. # So we will determine which version of protoc we have available and choose accordingly.
  242. execute_process(COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --version OUTPUT_VARIABLE PROTOC_VERSION_STRING OUTPUT_STRIP_TRAILING_WHITESPACE)
  243. string(REGEX MATCH "[(0-9)].*.[(0-9)].*.[(0-9)].*$" PROTOC_VERSION "${PROTOC_VERSION_STRING}")
  244. if(PROTOC_VERSION AND PROTOC_VERSION VERSION_LESS "3.6.0")
  245. #try to use the older way
  246. string(REGEX MATCH ":" HAS_COLON_IN_PATH ${NANOPB_PLUGIN_OPTIONS} ${NANOPB_OUT})
  247. if(HAS_COLON_IN_PATH)
  248. 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.")
  249. endif()
  250. set(NANOPB_OPT_STRING "--nanopb_out=${NANOPB_PLUGIN_OPTIONS}:${NANOPB_OUT}")
  251. else()
  252. set(NANOPB_OPT_STRING "--nanopb_opt=${NANOPB_PLUGIN_OPTIONS}" "--nanopb_out=${NANOPB_OUT}")
  253. endif()
  254. add_custom_command(
  255. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.c"
  256. "${CMAKE_CURRENT_BINARY_DIR}/${FIL_PATH_REL}/${FIL_WE}.pb.h"
  257. COMMAND ${CUSTOM_COMMAND_PREFIX} ${PROTOBUF_PROTOC_EXECUTABLE}
  258. ARGS -I${GENERATOR_PATH} -I${GENERATOR_CORE_DIR}
  259. -I${CMAKE_CURRENT_BINARY_DIR} ${_nanopb_include_path}
  260. --plugin=protoc-gen-nanopb=${NANOPB_GENERATOR_PLUGIN}
  261. ${NANOPB_OPT_STRING}
  262. ${ABS_FIL}
  263. DEPENDS ${ABS_FIL} ${GENERATOR_CORE_PYTHON_SRC}
  264. ${ABS_OPT_FIL} ${NANOPB_DEPENDS}
  265. COMMENT "Running C++ protocol buffer compiler using nanopb plugin on ${FIL}"
  266. VERBATIM )
  267. endforeach()
  268. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
  269. set(${SRCS} ${${SRCS}} ${NANOPB_SRCS} PARENT_SCOPE)
  270. set(${HDRS} ${${HDRS}} ${NANOPB_HDRS} PARENT_SCOPE)
  271. if(MSVC)
  272. unset(CUSTOM_COMMAND_PREFIX)
  273. endif()
  274. endfunction()
  275. #
  276. # Main.
  277. #
  278. # By default have NANOPB_GENERATE_CPP macro pass -I to protoc
  279. # for each directory where a proto file is referenced.
  280. if(NOT DEFINED NANOPB_GENERATE_CPP_APPEND_PATH)
  281. set(NANOPB_GENERATE_CPP_APPEND_PATH TRUE)
  282. endif()
  283. # Make a really good guess regarding location of NANOPB_SRC_ROOT_FOLDER
  284. if(NOT DEFINED NANOPB_SRC_ROOT_FOLDER)
  285. get_filename_component(NANOPB_SRC_ROOT_FOLDER
  286. ${CMAKE_CURRENT_LIST_DIR}/.. ABSOLUTE)
  287. endif()
  288. # Find the include directory
  289. find_path(NANOPB_INCLUDE_DIRS
  290. pb.h
  291. PATHS ${NANOPB_SRC_ROOT_FOLDER}
  292. NO_CMAKE_FIND_ROOT_PATH
  293. )
  294. mark_as_advanced(NANOPB_INCLUDE_DIRS)
  295. # Find nanopb source files
  296. set(NANOPB_SRCS)
  297. set(NANOPB_HDRS)
  298. list(APPEND _nanopb_srcs pb_decode.c pb_encode.c pb_common.c)
  299. list(APPEND _nanopb_hdrs pb_decode.h pb_encode.h pb_common.h pb.h)
  300. foreach(FIL ${_nanopb_srcs})
  301. find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_SRC_ROOT_FOLDER} ${NANOPB_INCLUDE_DIRS} NO_CMAKE_FIND_ROOT_PATH)
  302. list(APPEND NANOPB_SRCS "${${FIL}__nano_pb_file}")
  303. mark_as_advanced(${FIL}__nano_pb_file)
  304. endforeach()
  305. foreach(FIL ${_nanopb_hdrs})
  306. find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_INCLUDE_DIRS} NO_CMAKE_FIND_ROOT_PATH)
  307. mark_as_advanced(${FIL}__nano_pb_file)
  308. list(APPEND NANOPB_HDRS "${${FIL}__nano_pb_file}")
  309. endforeach()
  310. # Find the protoc Executable
  311. find_program(PROTOBUF_PROTOC_EXECUTABLE
  312. NAMES protoc
  313. DOC "The Google Protocol Buffers Compiler"
  314. PATHS
  315. ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Release
  316. ${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Debug
  317. ${NANOPB_SRC_ROOT_FOLDER}/generator-bin
  318. ${NANOPB_SRC_ROOT_FOLDER}/generator
  319. )
  320. mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE)
  321. # Find nanopb generator source dir
  322. find_path(NANOPB_GENERATOR_SOURCE_DIR
  323. NAMES nanopb_generator.py
  324. DOC "nanopb generator source"
  325. PATHS
  326. ${NANOPB_SRC_ROOT_FOLDER}/generator
  327. NO_CMAKE_FIND_ROOT_PATH
  328. )
  329. mark_as_advanced(NANOPB_GENERATOR_SOURCE_DIR)
  330. include(FindPackageHandleStandardArgs)
  331. find_package_handle_standard_args(Nanopb DEFAULT_MSG
  332. NANOPB_INCLUDE_DIRS
  333. NANOPB_SRCS NANOPB_HDRS
  334. NANOPB_GENERATOR_SOURCE_DIR
  335. PROTOBUF_PROTOC_EXECUTABLE
  336. )