blob: 82036cb6878c58deaccbb06f2b12b365388e1161 [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001include(GNUInstallDirs)
2
Austin Schuh40c16522018-10-28 20:27:54 -07003configure_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})
Brian Silverman9c614bc2016-02-15 20:20:02 -050014 set_property(TARGET ${_library}
15 PROPERTY INTERFACE_INCLUDE_DIRECTORIES
Austin Schuh40c16522018-10-28 20:27:54 -070016 $<BUILD_INTERFACE:${protobuf_source_dir}/src>
Brian Silverman9c614bc2016-02-15 20:20:02 -050017 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
18 install(TARGETS ${_library} EXPORT protobuf-targets
19 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${_library}
20 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${_library}
21 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${_library})
22endforeach()
23
Austin Schuh40c16522018-10-28 20:27:54 -070024if (protobuf_BUILD_PROTOC_BINARIES)
25 install(TARGETS protoc EXPORT protobuf-targets
26 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT protoc)
27endif (protobuf_BUILD_PROTOC_BINARIES)
Brian Silverman9c614bc2016-02-15 20:20:02 -050028
Austin Schuh40c16522018-10-28 20:27:54 -070029install(FILES ${CMAKE_CURRENT_BINARY_DIR}/protobuf.pc ${CMAKE_CURRENT_BINARY_DIR}/protobuf-lite.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
30
31file(STRINGS extract_includes.bat.in _extract_strings
32 REGEX "^copy")
33foreach(_extract_string ${_extract_strings})
34 string(REGEX REPLACE "^.* .+ include\\\\(.+)$" "\\1"
35 _header ${_extract_string})
36 string(REPLACE "\\" "/" _header ${_header})
37 get_filename_component(_extract_from "${protobuf_SOURCE_DIR}/../src/${_header}" ABSOLUTE)
38 get_filename_component(_extract_name ${_header} NAME)
39 get_filename_component(_extract_to "${CMAKE_INSTALL_INCLUDEDIR}/${_header}" PATH)
40 if(EXISTS "${_extract_from}")
41 install(FILES "${_extract_from}"
42 DESTINATION "${_extract_to}"
43 COMPONENT protobuf-headers
44 RENAME "${_extract_name}")
45 else()
46 message(AUTHOR_WARNING "The file \"${_extract_from}\" is listed in "
47 "\"${protobuf_SOURCE_DIR}/cmake/extract_includes.bat.in\" "
48 "but there not exists. The file will not be installed.")
49 endif()
50endforeach()
Brian Silverman9c614bc2016-02-15 20:20:02 -050051
52# Internal function for parsing auto tools scripts
53function(_protobuf_auto_list FILE_NAME VARIABLE)
54 file(STRINGS ${FILE_NAME} _strings)
55 set(_list)
56 foreach(_string ${_strings})
57 set(_found)
58 string(REGEX MATCH "^[ \t]*${VARIABLE}[ \t]*=[ \t]*" _found "${_string}")
59 if(_found)
60 string(LENGTH "${_found}" _length)
61 string(SUBSTRING "${_string}" ${_length} -1 _draft_list)
62 foreach(_item ${_draft_list})
63 string(STRIP "${_item}" _item)
64 list(APPEND _list "${_item}")
65 endforeach()
66 endif()
67 endforeach()
68 set(${VARIABLE} ${_list} PARENT_SCOPE)
69endfunction()
70
71# Install well-known type proto files
72_protobuf_auto_list("../src/Makefile.am" nobase_dist_proto_DATA)
73foreach(_file ${nobase_dist_proto_DATA})
74 get_filename_component(_file_from "../src/${_file}" ABSOLUTE)
75 get_filename_component(_file_name ${_file} NAME)
76 get_filename_component(_file_path ${_file} PATH)
77 if(EXISTS "${_file_from}")
78 install(FILES "${_file_from}"
79 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${_file_path}"
80 COMPONENT protobuf-protos
81 RENAME "${_file_name}")
82 else()
83 message(AUTHOR_WARNING "The file \"${_file_from}\" is listed in "
84 "\"${protobuf_SOURCE_DIR}/../src/Makefile.am\" as nobase_dist_proto_DATA "
85 "but there not exists. The file will not be installed.")
86 endif()
87endforeach()
88
Austin Schuh40c16522018-10-28 20:27:54 -070089# Install configuration
90set(_cmakedir_desc "Directory relative to CMAKE_INSTALL to install the cmake configuration files")
91if(NOT MSVC)
92 set(CMAKE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/protobuf" CACHE STRING "${_cmakedir_desc}")
93else()
94 set(CMAKE_INSTALL_CMAKEDIR "cmake" CACHE STRING "${_cmakedir_desc}")
95endif()
96mark_as_advanced(CMAKE_INSTALL_CMAKEDIR)
Brian Silverman9c614bc2016-02-15 20:20:02 -050097
98configure_file(protobuf-config.cmake.in
Austin Schuh40c16522018-10-28 20:27:54 -070099 ${CMAKE_INSTALL_CMAKEDIR}/protobuf-config.cmake @ONLY)
Brian Silverman9c614bc2016-02-15 20:20:02 -0500100configure_file(protobuf-config-version.cmake.in
Austin Schuh40c16522018-10-28 20:27:54 -0700101 ${CMAKE_INSTALL_CMAKEDIR}/protobuf-config-version.cmake @ONLY)
Brian Silverman9c614bc2016-02-15 20:20:02 -0500102configure_file(protobuf-module.cmake.in
Austin Schuh40c16522018-10-28 20:27:54 -0700103 ${CMAKE_INSTALL_CMAKEDIR}/protobuf-module.cmake @ONLY)
104configure_file(protobuf-options.cmake
105 ${CMAKE_INSTALL_CMAKEDIR}/protobuf-options.cmake @ONLY)
Brian Silverman9c614bc2016-02-15 20:20:02 -0500106
Austin Schuh40c16522018-10-28 20:27:54 -0700107# Allows the build directory to be used as a find directory.
108
109if (protobuf_BUILD_PROTOC_BINARIES)
110 export(TARGETS libprotobuf-lite libprotobuf libprotoc protoc
111 NAMESPACE protobuf::
112 FILE ${CMAKE_INSTALL_CMAKEDIR}/protobuf-targets.cmake
113 )
114else (protobuf_BUILD_PROTOC_BINARIES)
115 export(TARGETS libprotobuf-lite libprotobuf
116 NAMESPACE protobuf::
117 FILE ${CMAKE_INSTALL_CMAKEDIR}/protobuf-targets.cmake
118 )
119endif (protobuf_BUILD_PROTOC_BINARIES)
120
121install(EXPORT protobuf-targets
122 DESTINATION "${CMAKE_INSTALL_CMAKEDIR}"
123 NAMESPACE protobuf::
Brian Silverman9c614bc2016-02-15 20:20:02 -0500124 COMPONENT protobuf-export)
Austin Schuh40c16522018-10-28 20:27:54 -0700125
126install(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
132option(protobuf_INSTALL_EXAMPLES "Install the examples folder" OFF)
133if(protobuf_INSTALL_EXAMPLES)
134 install(DIRECTORY ../examples/ DESTINATION examples
135 COMPONENT protobuf-examples)
136endif()