blob: 9b2ae93cf62c29d87038489b8849b9bd9e5fb1c6 [file] [log] [blame]
Feng Xiaoeee38b02015-08-22 18:25:48 -07001include(GNUInstallDirs)
2
Axel Huebl668712c2017-07-21 18:57:07 +02003configure_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
Yangqing Jiacba18ef2017-11-13 15:15:39 -08008set(_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})
Feng Xiaoeee38b02015-08-22 18:25:48 -070014 set_property(TARGET ${_library}
15 PROPERTY INTERFACE_INCLUDE_DIRECTORIES
Walter Gray23fef562016-06-06 10:59:58 -070016 $<BUILD_INTERFACE:${protobuf_source_dir}/src>
Feng Xiaoeee38b02015-08-22 18:25:48 -070017 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
Mizux7306f542018-05-22 21:52:07 +020018 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()
Feng Xiaoeee38b02015-08-22 18:25:48 -070025 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
Yangqing Jiacba18ef2017-11-13 15:15:39 -080031if (protobuf_BUILD_PROTOC_BINARIES)
32 install(TARGETS protoc EXPORT protobuf-targets
33 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT protoc)
Mizux7306f542018-05-22 21:52:07 +020034 if (UNIX AND NOT APPLE)
35 set_property(TARGET protoc
36 PROPERTY INSTALL_RPATH "$ORIGIN/../lib")
37 elseif (APPLE)
38 set_property(TARGET protoc
39 PROPERTY INSTALL_RPATH "@loader_path/../lib")
40 endif()
Yangqing Jiacba18ef2017-11-13 15:15:39 -080041endif (protobuf_BUILD_PROTOC_BINARIES)
Feng Xiaoeee38b02015-08-22 18:25:48 -070042
Axel Huebl668712c2017-07-21 18:57:07 +020043install(FILES ${CMAKE_CURRENT_BINARY_DIR}/protobuf.pc ${CMAKE_CURRENT_BINARY_DIR}/protobuf-lite.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
44
Walter Gray04265e42016-05-26 18:04:32 -070045file(STRINGS extract_includes.bat.in _extract_strings
46 REGEX "^copy")
47foreach(_extract_string ${_extract_strings})
Feng Xiaoc94555f2017-03-10 16:32:19 -080048 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)
Walter Gray04265e42016-05-26 18:04:32 -070054 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()
Feng Xiaoeee38b02015-08-22 18:25:48 -070065
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
Walter Gray23fef562016-06-06 10:59:58 -0700103# Install configuration
Walter Gray7d794582016-05-19 14:52:04 -0700104set(_cmakedir_desc "Directory relative to CMAKE_INSTALL to install the cmake configuration files")
105if(NOT MSVC)
Jeff Merver733ef982016-05-21 16:01:32 -0700106 set(CMAKE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/protobuf" CACHE STRING "${_cmakedir_desc}")
Walter Gray7d794582016-05-19 14:52:04 -0700107else()
108 set(CMAKE_INSTALL_CMAKEDIR "cmake" CACHE STRING "${_cmakedir_desc}")
109endif()
Konstantin Podsvirov71556292016-06-01 17:00:08 +0300110mark_as_advanced(CMAKE_INSTALL_CMAKEDIR)
Feng Xiaoeee38b02015-08-22 18:25:48 -0700111
Feng Xiaoeee38b02015-08-22 18:25:48 -0700112configure_file(protobuf-config.cmake.in
Konstantin Podsvirov71556292016-06-01 17:00:08 +0300113 ${CMAKE_INSTALL_CMAKEDIR}/protobuf-config.cmake @ONLY)
Feng Xiaoeee38b02015-08-22 18:25:48 -0700114configure_file(protobuf-config-version.cmake.in
Konstantin Podsvirov71556292016-06-01 17:00:08 +0300115 ${CMAKE_INSTALL_CMAKEDIR}/protobuf-config-version.cmake @ONLY)
Feng Xiaoeee38b02015-08-22 18:25:48 -0700116configure_file(protobuf-module.cmake.in
Konstantin Podsvirov71556292016-06-01 17:00:08 +0300117 ${CMAKE_INSTALL_CMAKEDIR}/protobuf-module.cmake @ONLY)
Walter Gray23fef562016-06-06 10:59:58 -0700118configure_file(protobuf-options.cmake
119 ${CMAKE_INSTALL_CMAKEDIR}/protobuf-options.cmake @ONLY)
Konstantin Podsvirov71556292016-06-01 17:00:08 +0300120
Walter Gray23fef562016-06-06 10:59:58 -0700121# Allows the build directory to be used as a find directory.
Yangqing Jiacba18ef2017-11-13 15:15:39 -0800122
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)
Feng Xiaoeee38b02015-08-22 18:25:48 -0700134
Walter Gray23fef562016-06-06 10:59:58 -0700135install(EXPORT protobuf-targets
Walter Gray7d794582016-05-19 14:52:04 -0700136 DESTINATION "${CMAKE_INSTALL_CMAKEDIR}"
Walter Gray23fef562016-06-06 10:59:58 -0700137 NAMESPACE protobuf::
Feng Xiaoeee38b02015-08-22 18:25:48 -0700138 COMPONENT protobuf-export)
Walter Gray23fef562016-06-06 10:59:58 -0700139
Adnan154e2782016-07-25 12:13:53 -0700140install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_CMAKEDIR}/
Walter Gray23fef562016-06-06 10:59:58 -0700141 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()