install.cmake 4.68 KB
Newer Older
1 2
include(GNUInstallDirs)

Axel Huebl's avatar
Axel Huebl committed
3 4 5 6 7
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/protobuf.pc.cmake
               ${CMAKE_CURRENT_BINARY_DIR}/protobuf.pc @ONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/protobuf-lite.pc.cmake
               ${CMAKE_CURRENT_BINARY_DIR}/protobuf-lite.pc @ONLY)

8 9 10 11 12 13
foreach(_library
  libprotobuf-lite
  libprotobuf
  libprotoc)
  set_property(TARGET ${_library}
    PROPERTY INTERFACE_INCLUDE_DIRECTORIES
14
    $<BUILD_INTERFACE:${protobuf_source_dir}/src>
15 16 17 18 19 20 21 22 23 24
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
  install(TARGETS ${_library} EXPORT protobuf-targets
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${_library}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${_library}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${_library})
endforeach()

install(TARGETS protoc EXPORT protobuf-targets
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT protoc)

Axel Huebl's avatar
Axel Huebl committed
25 26
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/protobuf.pc ${CMAKE_CURRENT_BINARY_DIR}/protobuf-lite.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")

Walter Gray's avatar
Walter Gray committed
27 28 29
file(STRINGS extract_includes.bat.in _extract_strings
  REGEX "^copy")
foreach(_extract_string ${_extract_strings})
30 31 32 33 34 35
  string(REGEX REPLACE "^.* .+ include\\\\(.+)$" "\\1"
    _header ${_extract_string})
  string(REPLACE "\\" "/" _header ${_header})
  get_filename_component(_extract_from "${protobuf_SOURCE_DIR}/../src/${_header}" ABSOLUTE)
  get_filename_component(_extract_name ${_header} NAME)
  get_filename_component(_extract_to "${CMAKE_INSTALL_INCLUDEDIR}/${_header}" PATH)
Walter Gray's avatar
Walter Gray committed
36 37 38 39 40 41 42 43 44 45 46
  if(EXISTS "${_extract_from}")
    install(FILES "${_extract_from}"
      DESTINATION "${_extract_to}"
      COMPONENT protobuf-headers
      RENAME "${_extract_name}")
  else()
    message(AUTHOR_WARNING "The file \"${_extract_from}\" is listed in "
      "\"${protobuf_SOURCE_DIR}/cmake/extract_includes.bat.in\" "
      "but there not exists. The file will not be installed.")
  endif()
endforeach()
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

# Internal function for parsing auto tools scripts
function(_protobuf_auto_list FILE_NAME VARIABLE)
  file(STRINGS ${FILE_NAME} _strings)
  set(_list)
  foreach(_string ${_strings})
    set(_found)
    string(REGEX MATCH "^[ \t]*${VARIABLE}[ \t]*=[ \t]*" _found "${_string}")
    if(_found)
      string(LENGTH "${_found}" _length)
      string(SUBSTRING "${_string}" ${_length} -1 _draft_list)
      foreach(_item ${_draft_list})
        string(STRIP "${_item}" _item)
        list(APPEND _list "${_item}")
      endforeach()
    endif()
  endforeach()
  set(${VARIABLE} ${_list} PARENT_SCOPE)
endfunction()

# Install well-known type proto files
_protobuf_auto_list("../src/Makefile.am" nobase_dist_proto_DATA)
foreach(_file ${nobase_dist_proto_DATA})
  get_filename_component(_file_from "../src/${_file}" ABSOLUTE)
  get_filename_component(_file_name ${_file} NAME)
  get_filename_component(_file_path ${_file} PATH)
  if(EXISTS "${_file_from}")
    install(FILES "${_file_from}"
      DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${_file_path}"
      COMPONENT protobuf-protos
      RENAME "${_file_name}")
  else()
    message(AUTHOR_WARNING "The file \"${_file_from}\" is listed in "
      "\"${protobuf_SOURCE_DIR}/../src/Makefile.am\" as nobase_dist_proto_DATA "
      "but there not exists. The file will not be installed.")
  endif()
endforeach()

85
# Install configuration
86 87
set(_cmakedir_desc "Directory relative to CMAKE_INSTALL to install the cmake configuration files")
if(NOT MSVC)
88
  set(CMAKE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/protobuf" CACHE STRING "${_cmakedir_desc}")
89 90 91
else()
  set(CMAKE_INSTALL_CMAKEDIR "cmake" CACHE STRING "${_cmakedir_desc}")
endif()
92
mark_as_advanced(CMAKE_INSTALL_CMAKEDIR)
93 94

configure_file(protobuf-config.cmake.in
95
  ${CMAKE_INSTALL_CMAKEDIR}/protobuf-config.cmake @ONLY)
96
configure_file(protobuf-config-version.cmake.in
97
  ${CMAKE_INSTALL_CMAKEDIR}/protobuf-config-version.cmake @ONLY)
98
configure_file(protobuf-module.cmake.in
99
  ${CMAKE_INSTALL_CMAKEDIR}/protobuf-module.cmake @ONLY)
100 101
configure_file(protobuf-options.cmake
  ${CMAKE_INSTALL_CMAKEDIR}/protobuf-options.cmake @ONLY)
102

103 104 105 106 107
# Allows the build directory to be used as a find directory.
export(TARGETS libprotobuf-lite libprotobuf libprotoc protoc
  NAMESPACE protobuf::
  FILE ${CMAKE_INSTALL_CMAKEDIR}/protobuf-targets.cmake
)
108

109
install(EXPORT protobuf-targets
110
  DESTINATION "${CMAKE_INSTALL_CMAKEDIR}"
111
  NAMESPACE protobuf::
112
  COMPONENT protobuf-export)
113

114
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_CMAKEDIR}/
115 116 117 118 119 120 121 122 123 124
  DESTINATION "${CMAKE_INSTALL_CMAKEDIR}"
  COMPONENT protobuf-export
  PATTERN protobuf-targets.cmake EXCLUDE
)

option(protobuf_INSTALL_EXAMPLES "Install the examples folder" OFF)
if(protobuf_INSTALL_EXAMPLES)
  install(DIRECTORY ../examples/ DESTINATION examples
    COMPONENT protobuf-examples)
endif()