install.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. include(GNUInstallDirs)
  2. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/protobuf.pc.cmake
  3. ${CMAKE_CURRENT_BINARY_DIR}/protobuf.pc @ONLY)
  4. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/protobuf-lite.pc.cmake
  5. ${CMAKE_CURRENT_BINARY_DIR}/protobuf-lite.pc @ONLY)
  6. foreach(_library
  7. libprotobuf-lite
  8. libprotobuf
  9. libprotoc)
  10. set_property(TARGET ${_library}
  11. PROPERTY INTERFACE_INCLUDE_DIRECTORIES
  12. $<BUILD_INTERFACE:${protobuf_source_dir}/src>
  13. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
  14. install(TARGETS ${_library} EXPORT protobuf-targets
  15. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${_library}
  16. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${_library}
  17. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${_library})
  18. endforeach()
  19. install(TARGETS protoc EXPORT protobuf-targets
  20. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT protoc)
  21. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/protobuf.pc ${CMAKE_CURRENT_BINARY_DIR}/protobuf-lite.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  22. file(STRINGS extract_includes.bat.in _extract_strings
  23. REGEX "^copy")
  24. foreach(_extract_string ${_extract_strings})
  25. string(REGEX REPLACE "^.* .+ include\\\\(.+)$" "\\1"
  26. _header ${_extract_string})
  27. string(REPLACE "\\" "/" _header ${_header})
  28. get_filename_component(_extract_from "${protobuf_SOURCE_DIR}/../src/${_header}" ABSOLUTE)
  29. get_filename_component(_extract_name ${_header} NAME)
  30. get_filename_component(_extract_to "${CMAKE_INSTALL_INCLUDEDIR}/${_header}" PATH)
  31. if(EXISTS "${_extract_from}")
  32. install(FILES "${_extract_from}"
  33. DESTINATION "${_extract_to}"
  34. COMPONENT protobuf-headers
  35. RENAME "${_extract_name}")
  36. else()
  37. message(AUTHOR_WARNING "The file \"${_extract_from}\" is listed in "
  38. "\"${protobuf_SOURCE_DIR}/cmake/extract_includes.bat.in\" "
  39. "but there not exists. The file will not be installed.")
  40. endif()
  41. endforeach()
  42. # Internal function for parsing auto tools scripts
  43. function(_protobuf_auto_list FILE_NAME VARIABLE)
  44. file(STRINGS ${FILE_NAME} _strings)
  45. set(_list)
  46. foreach(_string ${_strings})
  47. set(_found)
  48. string(REGEX MATCH "^[ \t]*${VARIABLE}[ \t]*=[ \t]*" _found "${_string}")
  49. if(_found)
  50. string(LENGTH "${_found}" _length)
  51. string(SUBSTRING "${_string}" ${_length} -1 _draft_list)
  52. foreach(_item ${_draft_list})
  53. string(STRIP "${_item}" _item)
  54. list(APPEND _list "${_item}")
  55. endforeach()
  56. endif()
  57. endforeach()
  58. set(${VARIABLE} ${_list} PARENT_SCOPE)
  59. endfunction()
  60. # Install well-known type proto files
  61. _protobuf_auto_list("../src/Makefile.am" nobase_dist_proto_DATA)
  62. foreach(_file ${nobase_dist_proto_DATA})
  63. get_filename_component(_file_from "../src/${_file}" ABSOLUTE)
  64. get_filename_component(_file_name ${_file} NAME)
  65. get_filename_component(_file_path ${_file} PATH)
  66. if(EXISTS "${_file_from}")
  67. install(FILES "${_file_from}"
  68. DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${_file_path}"
  69. COMPONENT protobuf-protos
  70. RENAME "${_file_name}")
  71. else()
  72. message(AUTHOR_WARNING "The file \"${_file_from}\" is listed in "
  73. "\"${protobuf_SOURCE_DIR}/../src/Makefile.am\" as nobase_dist_proto_DATA "
  74. "but there not exists. The file will not be installed.")
  75. endif()
  76. endforeach()
  77. # Install configuration
  78. set(_cmakedir_desc "Directory relative to CMAKE_INSTALL to install the cmake configuration files")
  79. if(NOT MSVC)
  80. set(CMAKE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/protobuf" CACHE STRING "${_cmakedir_desc}")
  81. else()
  82. set(CMAKE_INSTALL_CMAKEDIR "cmake" CACHE STRING "${_cmakedir_desc}")
  83. endif()
  84. mark_as_advanced(CMAKE_INSTALL_CMAKEDIR)
  85. configure_file(protobuf-config.cmake.in
  86. ${CMAKE_INSTALL_CMAKEDIR}/protobuf-config.cmake @ONLY)
  87. configure_file(protobuf-config-version.cmake.in
  88. ${CMAKE_INSTALL_CMAKEDIR}/protobuf-config-version.cmake @ONLY)
  89. configure_file(protobuf-module.cmake.in
  90. ${CMAKE_INSTALL_CMAKEDIR}/protobuf-module.cmake @ONLY)
  91. configure_file(protobuf-options.cmake
  92. ${CMAKE_INSTALL_CMAKEDIR}/protobuf-options.cmake @ONLY)
  93. # Allows the build directory to be used as a find directory.
  94. export(TARGETS libprotobuf-lite libprotobuf libprotoc protoc
  95. NAMESPACE protobuf::
  96. FILE ${CMAKE_INSTALL_CMAKEDIR}/protobuf-targets.cmake
  97. )
  98. install(EXPORT protobuf-targets
  99. DESTINATION "${CMAKE_INSTALL_CMAKEDIR}"
  100. NAMESPACE protobuf::
  101. COMPONENT protobuf-export)
  102. install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_CMAKEDIR}/
  103. DESTINATION "${CMAKE_INSTALL_CMAKEDIR}"
  104. COMPONENT protobuf-export
  105. PATTERN protobuf-targets.cmake EXCLUDE
  106. )
  107. option(protobuf_INSTALL_EXAMPLES "Install the examples folder" OFF)
  108. if(protobuf_INSTALL_EXAMPLES)
  109. install(DIRECTORY ../examples/ DESTINATION examples
  110. COMPONENT protobuf-examples)
  111. endif()