install.cmake 6.0 KB

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