CMakeLists.txt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. cmake_minimum_required(VERSION 2.8.12)
  2. project(nanopb C)
  3. set(nanopb_VERSION_STRING nanopb-0.4.6-dev)
  4. set(nanopb_SOVERSION 0)
  5. string(REPLACE "nanopb-" "" nanopb_VERSION ${nanopb_VERSION_STRING})
  6. option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
  7. option(BUILD_STATIC_LIBS "Build static libraries" ON)
  8. option(nanopb_BUILD_RUNTIME "Build the headers and libraries needed at runtime" ON)
  9. option(nanopb_BUILD_GENERATOR "Build the protoc plugin for code generation" ON)
  10. option(nanopb_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON)
  11. find_program(nanopb_PROTOC_PATH protoc)
  12. if(NOT EXISTS ${nanopb_PROTOC_PATH})
  13. message(FATAL_ERROR "protoc compiler not found")
  14. endif()
  15. if(NOT DEFINED CMAKE_DEBUG_POSTFIX)
  16. set(CMAKE_DEBUG_POSTFIX "d")
  17. endif()
  18. include(GNUInstallDirs)
  19. if(MSVC AND nanopb_MSVC_STATIC_RUNTIME)
  20. foreach(flag_var
  21. CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
  22. CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
  23. if(${flag_var} MATCHES "/MD")
  24. string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
  25. endif(${flag_var} MATCHES "/MD")
  26. endforeach(flag_var)
  27. endif()
  28. if(NOT DEFINED CMAKE_INSTALL_CMAKEDIR)
  29. set(CMAKE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/nanopb")
  30. endif()
  31. find_package(Python REQUIRED COMPONENTS Interpreter)
  32. execute_process(
  33. COMMAND ${Python_EXECUTABLE} -c
  34. "import os.path, sys, sysconfig; print(os.path.relpath(sysconfig.get_path('purelib'), start=sys.prefix))"
  35. OUTPUT_VARIABLE PYTHON_INSTDIR
  36. OUTPUT_STRIP_TRAILING_WHITESPACE
  37. )
  38. if(nanopb_BUILD_GENERATOR)
  39. set(generator_protos nanopb)
  40. foreach(generator_proto IN LISTS generator_protos)
  41. string(REGEX REPLACE "([^;]+)" "${PROJECT_SOURCE_DIR}/generator/proto/\\1.proto" generator_proto_file "${generator_proto}")
  42. string(REGEX REPLACE "([^;]+)" "\\1_pb2.py" generator_proto_py_file "${generator_proto}")
  43. add_custom_command(
  44. OUTPUT ${generator_proto_py_file}
  45. COMMAND ${nanopb_PROTOC_PATH} --python_out=${PROJECT_BINARY_DIR} -I${PROJECT_SOURCE_DIR}/generator/proto ${generator_proto_file}
  46. DEPENDS ${generator_proto_file}
  47. )
  48. add_custom_target("generate_${generator_proto_py_file}" ALL DEPENDS ${generator_proto_py_file})
  49. install(
  50. FILES ${PROJECT_BINARY_DIR}/${generator_proto_py_file}
  51. ${generator_proto_file}
  52. DESTINATION ${PYTHON_INSTDIR}/proto/
  53. )
  54. endforeach()
  55. endif()
  56. install(FILES generator/proto/_utils.py
  57. DESTINATION ${PYTHON_INSTDIR}/proto/)
  58. if(WIN32)
  59. install(
  60. PROGRAMS
  61. generator/nanopb_generator.py
  62. generator/protoc-gen-nanopb.bat
  63. DESTINATION ${CMAKE_INSTALL_BINDIR}
  64. )
  65. else()
  66. install(
  67. PROGRAMS
  68. generator/nanopb_generator.py
  69. generator/protoc-gen-nanopb
  70. DESTINATION ${CMAKE_INSTALL_BINDIR}
  71. )
  72. endif()
  73. if(nanopb_BUILD_RUNTIME)
  74. if(BUILD_SHARED_LIBS)
  75. add_library(protobuf-nanopb SHARED
  76. pb.h
  77. pb_common.h
  78. pb_common.c
  79. pb_encode.h
  80. pb_encode.c
  81. pb_decode.h
  82. pb_decode.c)
  83. set_target_properties(protobuf-nanopb PROPERTIES
  84. SOVERSION ${nanopb_SOVERSION})
  85. install(TARGETS protobuf-nanopb EXPORT nanopb-targets
  86. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  87. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  88. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
  89. target_include_directories(protobuf-nanopb INTERFACE
  90. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  91. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
  92. )
  93. endif()
  94. if(BUILD_STATIC_LIBS)
  95. add_library(protobuf-nanopb-static STATIC
  96. pb.h
  97. pb_common.h
  98. pb_common.c
  99. pb_encode.h
  100. pb_encode.c
  101. pb_decode.h
  102. pb_decode.c)
  103. set_target_properties(protobuf-nanopb-static PROPERTIES
  104. OUTPUT_NAME protobuf-nanopb)
  105. install(TARGETS protobuf-nanopb-static EXPORT nanopb-targets
  106. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
  107. target_include_directories(protobuf-nanopb-static INTERFACE
  108. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  109. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
  110. )
  111. endif()
  112. configure_file(extra/nanopb-config-version.cmake.in
  113. nanopb-config-version.cmake @ONLY)
  114. install(EXPORT nanopb-targets
  115. DESTINATION ${CMAKE_INSTALL_CMAKEDIR}
  116. NAMESPACE nanopb::)
  117. install(FILES extra/nanopb-config.cmake
  118. ${CMAKE_CURRENT_BINARY_DIR}/nanopb-config-version.cmake
  119. DESTINATION ${CMAKE_INSTALL_CMAKEDIR})
  120. install(FILES pb.h pb_common.h pb_encode.h pb_decode.h
  121. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  122. endif()