install.cmake 5.6 KB

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