install.cmake 6.0 KB

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