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