• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1include(GNUInstallDirs)
2
3foreach(_target IN LISTS protobuf_ABSL_USED_TARGETS)
4  # shared abseil on windows breaks the absl::foo -> absl_foo replacement logic -
5  # preempt this by a more specific replace (harmless if it doesn't apply); see GH-15883
6  string(REPLACE "absl::abseil_dll" "abseil_dll" _modified_target ${_target})
7  string(REPLACE :: _ _modified_target ${_modified_target})
8  list(APPEND _pc_targets ${_modified_target})
9endforeach()
10list(APPEND _pc_targets "utf8_range")
11
12set(_protobuf_PC_REQUIRES "")
13set(_sep "")
14foreach (_target IN LISTS _pc_targets)
15  string(CONCAT _protobuf_PC_REQUIRES "${_protobuf_PC_REQUIRES}" "${_sep}" "${_target}")
16  set(_sep " ")
17endforeach ()
18set(_protobuf_PC_CFLAGS)
19if (protobuf_BUILD_SHARED_LIBS)
20  set(_protobuf_PC_CFLAGS -DPROTOBUF_USE_DLLS)
21endif ()
22
23configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/protobuf.pc.cmake
24               ${CMAKE_CURRENT_BINARY_DIR}/protobuf.pc @ONLY)
25configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/protobuf-lite.pc.cmake
26               ${CMAKE_CURRENT_BINARY_DIR}/protobuf-lite.pc @ONLY)
27if (protobuf_BUILD_LIBUPB)
28  configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/upb.pc.cmake
29                ${CMAKE_CURRENT_BINARY_DIR}/upb.pc @ONLY)
30endif ()
31
32set(_protobuf_libraries libprotobuf-lite libprotobuf)
33if (protobuf_BUILD_LIBPROTOC)
34    list(APPEND _protobuf_libraries libprotoc)
35endif (protobuf_BUILD_LIBPROTOC)
36if (protobuf_BUILD_LIBUPB)
37  list(APPEND _protobuf_libraries libupb)
38endif ()
39
40foreach(_library ${_protobuf_libraries})
41  if (UNIX AND NOT APPLE)
42    set_property(TARGET ${_library}
43      PROPERTY INSTALL_RPATH "$ORIGIN")
44  elseif (APPLE)
45    set_property(TARGET ${_library}
46      PROPERTY INSTALL_RPATH "@loader_path")
47  endif()
48  install(TARGETS ${_library} EXPORT protobuf-targets
49    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${_library}
50    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${_library}
51    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${_library})
52endforeach()
53
54if (protobuf_BUILD_PROTOC_BINARIES)
55  set(_protobuf_binaries protoc)
56  install(TARGETS protoc EXPORT protobuf-targets
57    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT protoc
58    BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT protoc)
59  if (protobuf_BUILD_LIBUPB)
60    foreach (generator upb upbdefs upb_minitable)
61      list(APPEND _protobuf_binaries protoc-gen-${generator})
62      install(TARGETS protoc-gen-${generator} EXPORT protobuf-targets
63        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT upb-generators
64        BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT upb-generators)
65    endforeach ()
66  endif ()
67  foreach (binary IN LISTS _protobuf_binaries)
68    if (UNIX AND NOT APPLE)
69      set_property(TARGET ${binary}
70        PROPERTY INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}")
71    elseif (APPLE)
72      set_property(TARGET ${binary}
73        PROPERTY INSTALL_RPATH "@loader_path/../lib")
74    endif ()
75  endforeach ()
76endif (protobuf_BUILD_PROTOC_BINARIES)
77
78install(FILES ${CMAKE_CURRENT_BINARY_DIR}/protobuf.pc ${CMAKE_CURRENT_BINARY_DIR}/protobuf-lite.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
79if (protobuf_BUILD_LIBUPB)
80  install(FILES ${CMAKE_CURRENT_BINARY_DIR}/upb.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
81endif ()
82
83include(${protobuf_SOURCE_DIR}/src/file_lists.cmake)
84set(protobuf_HEADERS
85  ${libprotobuf_hdrs}
86  ${libprotoc_hdrs}
87  ${wkt_protos_files}
88  ${cpp_features_proto_proto_srcs}
89  ${descriptor_proto_proto_srcs}
90  ${plugin_proto_proto_srcs}
91  ${java_features_proto_proto_srcs}
92  ${go_features_proto_proto_srcs}
93)
94if (protobuf_BUILD_LIBUPB)
95  list(APPEND protobuf_HEADERS ${libupb_hdrs})
96  # Manually install the bootstrap headers
97  install(
98    FILES
99      ${protobuf_SOURCE_DIR}/upb/reflection/cmake/google/protobuf/descriptor.upb.h
100      ${protobuf_SOURCE_DIR}/upb/reflection/cmake/google/protobuf/descriptor.upb_minitable.h
101    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/google/protobuf
102    COMPONENT protobuf-headers
103  )
104endif ()
105set(protobuf_STRIP_PREFIXES
106  "/src"
107  "/java/core/src/main/resources"
108  "/go"
109  "/"
110)
111foreach(_header ${protobuf_HEADERS})
112  foreach(_strip_prefix ${protobuf_STRIP_PREFIXES})
113    string(FIND ${_header} "${protobuf_SOURCE_DIR}${_strip_prefix}" _find_src)
114    if(_find_src GREATER -1)
115      set(_from_dir "${protobuf_SOURCE_DIR}${_strip_prefix}")
116      break()
117    endif()
118  endforeach()
119  message(${_from_dir} "-" ${_header})
120
121  # Escape _from_dir for regex special characters in the directory name.
122  string(REGEX REPLACE "([$^.[|*+?()]|])" "\\\\\\1" _from_dir_regexp "${_from_dir}")
123  # On some platforms `_form_dir` ends up being just "protobuf", which can
124  # easily match multiple times in our paths.  We force it to only replace
125  # prefixes to avoid this case.
126  string(REGEX REPLACE "^${_from_dir_regexp}" "" _header ${_header})
127  get_filename_component(_extract_from "${_from_dir}/${_header}" ABSOLUTE)
128  get_filename_component(_extract_name ${_header} NAME)
129  get_filename_component(_extract_to "${CMAKE_INSTALL_INCLUDEDIR}/${_header}" DIRECTORY)
130  install(FILES "${_extract_from}"
131    DESTINATION "${_extract_to}"
132    COMPONENT protobuf-headers
133    RENAME "${_extract_name}")
134endforeach()
135
136# Install configuration
137set(_install_cmakedir_desc "Directory relative to CMAKE_INSTALL to install the cmake configuration files")
138set(_build_cmakedir_desc "Directory relative to CMAKE_CURRENT_BINARY_DIR for cmake configuration files")
139set(_exampledir_desc "Directory relative to CMAKE_INSTALL_DATA to install examples")
140set(_protobuf_subdir_desc "Subdirectory in which to install cmake configuration files")
141set(protobuf_CMAKE_SUBDIR "cmake/protobuf" CACHE STRING "${_protobuf_subdir_desc}")
142set(CMAKE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/${protobuf_CMAKE_SUBDIR}" CACHE STRING "${_install_cmakedir_desc}")
143set(CMAKE_INSTALL_EXAMPLEDIR "${CMAKE_INSTALL_DATADIR}/protobuf/examples" CACHE STRING "${_exampledir_desc}")
144set(CMAKE_BUILD_CMAKEDIR "${CMAKE_CURRENT_BINARY_DIR}/${protobuf_CMAKE_SUBDIR}" CACHE STRING "${_build_cmakedir_desc}")
145mark_as_advanced(protobuf_CMAKE_SUBDIR)
146mark_as_advanced(CMAKE_BUILD_CMAKEDIR)
147mark_as_advanced(CMAKE_INSTALL_CMAKEDIR)
148mark_as_advanced(CMAKE_INSTALL_EXAMPLEDIR)
149
150configure_file(${protobuf_SOURCE_DIR}/cmake/protobuf-config.cmake.in
151  ${CMAKE_BUILD_CMAKEDIR}/protobuf-config.cmake @ONLY)
152configure_file(${protobuf_SOURCE_DIR}/cmake/protobuf-config-version.cmake.in
153  ${CMAKE_BUILD_CMAKEDIR}/protobuf-config-version.cmake @ONLY)
154configure_file(${protobuf_SOURCE_DIR}/cmake/protobuf-module.cmake.in
155  ${CMAKE_BUILD_CMAKEDIR}/protobuf-module.cmake @ONLY)
156configure_file(${protobuf_SOURCE_DIR}/cmake/protobuf-options.cmake
157  ${CMAKE_BUILD_CMAKEDIR}/protobuf-options.cmake @ONLY)
158configure_file(${protobuf_SOURCE_DIR}/cmake/protobuf-generate.cmake
159  ${CMAKE_BUILD_CMAKEDIR}/protobuf-generate.cmake @ONLY)
160
161# Allows the build directory to be used as a find directory.
162
163install(EXPORT protobuf-targets
164  DESTINATION "${CMAKE_INSTALL_CMAKEDIR}"
165  NAMESPACE protobuf::
166  COMPONENT protobuf-export
167)
168
169install(DIRECTORY ${CMAKE_BUILD_CMAKEDIR}/
170  DESTINATION "${CMAKE_INSTALL_CMAKEDIR}"
171  COMPONENT protobuf-export
172  PATTERN protobuf-targets.cmake EXCLUDE
173)
174
175option(protobuf_INSTALL_EXAMPLES "Install the examples folder" OFF)
176if(protobuf_INSTALL_EXAMPLES)
177  install(DIRECTORY examples/
178    DESTINATION "${CMAKE_INSTALL_EXAMPLEDIR}"
179    COMPONENT protobuf-examples)
180endif()
181
182if (protobuf_INSTALL_TESTS)
183  install(TARGETS gmock EXPORT protobuf-targets
184    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
185    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
186    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
187endif()
188