blob: 6f9b7517f396bb0b6127331bf34bf63136bfe922 [file] [log] [blame]
Konstantin Podsvirov82983432015-07-31 23:36:00 +03001# Minimum CMake required
Adam Cozzette23e7cfd2022-03-04 13:13:19 -08002cmake_minimum_required(VERSION 3.5)
Feng Xiao4333edb2015-05-31 02:28:34 -07003
Konstantin Podsvirov71556292016-06-01 17:00:08 +03004if(protobuf_VERBOSE)
5 message(STATUS "Protocol Buffers Configuring...")
6endif()
Feng Xiao4333edb2015-05-31 02:28:34 -07007
Konstantin Podsvirov905f4642015-09-01 16:44:48 +03008# CMake policies
9cmake_policy(SET CMP0022 NEW)
Mizux7306f542018-05-22 21:52:07 +020010# On MacOS use @rpath/ for target's install name prefix path
11if (POLICY CMP0042)
12 cmake_policy(SET CMP0042 NEW)
13endif ()
14# Clear VERSION variables when no VERSION is given to project()
Corentin Le Molgat1ec9beb2018-01-29 15:31:06 +010015if(POLICY CMP0048)
16 cmake_policy(SET CMP0048 NEW)
17endif()
Saleem Abdulrasoolc47adad2021-07-31 16:07:04 -070018# MSVC runtime library flags are selected by an abstraction.
19if(POLICY CMP0091)
20 cmake_policy(SET CMP0091 NEW)
21endif()
Corentin Le Molgat1ec9beb2018-01-29 15:31:06 +010022
Konstantin Podsvirov71556292016-06-01 17:00:08 +030023# Project
24project(protobuf C CXX)
25
Arfrever Frehtes Taifersar Arahesisdef70d72022-03-08 00:00:00 +000026if(protobuf_DEPRECATED_CMAKE_SUBDIRECTORY_USAGE)
27 if(CMAKE_PROJECT_NAME STREQUAL "protobuf")
28 get_filename_component(CMAKE_SOURCE_DIR ${CMAKE_SOURCE_DIR} DIRECTORY)
29 endif()
30 get_filename_component(CMAKE_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
31 get_filename_component(PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR} DIRECTORY)
32 get_filename_component(protobuf_SOURCE_DIR ${protobuf_SOURCE_DIR} DIRECTORY)
33endif()
34
Adam Cozzette500cbd72022-08-11 18:22:15 -070035# Add c++14 flags
Ivan Shynkarenkaf80a8862018-05-02 02:51:08 +030036if (CYGWIN)
Adam Cozzette500cbd72022-08-11 18:22:15 -070037 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14")
Ivan Shynkarenkaf80a8862018-05-02 02:51:08 +030038else()
Adam Cozzette500cbd72022-08-11 18:22:15 -070039 set(CMAKE_CXX_STANDARD 14)
Ivan Shynkarenkaf80a8862018-05-02 02:51:08 +030040 set(CMAKE_CXX_STANDARD_REQUIRED ON)
41 set(CMAKE_CXX_EXTENSIONS OFF)
42endif()
Silver Chand3e8a542018-04-04 10:31:05 +080043
Chuck Atkins10c48c92020-02-11 10:22:38 -050044# The Intel compiler isn't able to deal with noinline member functions of
Peter Newmane2cc2de2020-08-10 19:08:25 +010045# template classes defined in headers. As such it spams the output with
Chuck Atkins10c48c92020-02-11 10:22:38 -050046# warning #2196: routine is both "inline" and "noinline"
47# This silences that warning.
48if (CMAKE_CXX_COMPILER_ID MATCHES Intel)
49 string(APPEND CMAKE_CXX_FLAGS " -diag-disable=2196")
50endif()
51
Konstantin Podsvirov82983432015-07-31 23:36:00 +030052# Options
JCooky55645ca2022-02-03 01:29:25 +090053option(protobuf_INSTALL "Install protobuf binaries and files" ON)
Michael WERLE976f85b2020-05-01 16:50:53 +090054if(WITH_PROTOC)
55 set(protobuf_PROTOC_EXE ${WITH_PROTOC} CACHE FILEPATH "Protocol Buffer Compiler executable" FORCE)
56endif()
Konstantin Podsvirov673d32e2015-09-15 15:30:02 +030057option(protobuf_BUILD_TESTS "Build tests" ON)
Josh Haberman42496922018-08-24 12:27:21 -070058option(protobuf_BUILD_CONFORMANCE "Build conformance tests" OFF)
Konstantin Podsvirov71556292016-06-01 17:00:08 +030059option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
Mike Kruskal3edec1f2022-07-25 20:36:47 -070060option(protobuf_BUILD_PROTOBUF_BINARIES "Build protobuf libraries and protoc compiler" ON)
Yangqing Jiacba18ef2017-11-13 15:15:39 -080061option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" ON)
Daniel Joosc8f76332020-10-10 00:54:39 +020062option(protobuf_BUILD_LIBPROTOC "Build libprotoc" OFF)
Florian Simonb9a036b2021-03-04 11:39:28 -050063option(protobuf_DISABLE_RTTI "Remove runtime type information in the binaries" OFF)
Mike Kruskal3edec1f2022-07-25 20:36:47 -070064option(protobuf_TEST_XML_OUTDIR "Output directory for XML logs from tests." "")
Konstantin Podsvirovc3aa4c22015-10-15 02:56:48 +030065if (BUILD_SHARED_LIBS)
66 set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON)
67else (BUILD_SHARED_LIBS)
68 set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF)
69endif (BUILD_SHARED_LIBS)
70option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT})
Walter Gray4150a912016-06-09 13:25:27 -070071include(CMakeDependentOption)
Rafi Kamalde756512019-11-20 18:03:29 -080072cmake_dependent_option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON
73 "NOT protobuf_BUILD_SHARED_LIBS" OFF)
Corentin Le Molgat8dd0f4e2018-01-29 15:16:40 +010074set(protobuf_WITH_ZLIB_DEFAULT ON)
Konstantin Podsvirov620bd742015-09-15 15:31:25 +030075option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
Konstantin Podsvirovf397ede2015-09-17 13:00:12 +030076set(protobuf_DEBUG_POSTFIX "d"
77 CACHE STRING "Default debug postfix")
Konstantin Podsvirov71556292016-06-01 17:00:08 +030078mark_as_advanced(protobuf_DEBUG_POSTFIX)
79# User options
Arfrever Frehtes Taifersar Arahesis14cab5b2022-03-08 00:00:00 +000080include(${protobuf_SOURCE_DIR}/cmake/protobuf-options.cmake)
Feng Xiao4333edb2015-05-31 02:28:34 -070081
Mike Kruskalbc1b3102022-08-16 16:31:35 -070082if (protobuf_BUILD_SHARED_LIBS)
83 # This is necessary for linking in Abseil.
84 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
85endif ()
86
Mike Kruskaled5c57a2022-08-10 22:51:29 -070087# Version metadata
88set(protobuf_VERSION_STRING "3.21.4")
89set(protobuf_DESCRIPTION "Protocol Buffers")
90set(protobuf_CONTACT "protobuf@googlegroups.com")
91
Michael WERLE976f85b2020-05-01 16:50:53 +090092# Overrides for option dependencies
93if (protobuf_BUILD_PROTOC_BINARIES OR protobuf_BUILD_TESTS)
94 set(protobuf_BUILD_LIBPROTOC ON)
Michael WERLE976f85b2020-05-01 16:50:53 +090095endif ()
Mike Kruskal3edec1f2022-07-25 20:36:47 -070096if (NOT protobuf_BUILD_PROTOBUF_BINARIES)
97 set(protobuf_INSTALL OFF)
98endif()
Konstantin Podsvirov743ec442015-08-01 02:01:42 +030099# Parse version tweaks
Paul Yang5b4ac532019-02-01 18:43:55 -0800100set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+)([-]rc[-]|\\.)?([0-9]*)$")
Konstantin Podsvirovdb014602015-08-31 15:20:18 +0300101string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\1"
Konstantin Podsvirov743ec442015-08-01 02:01:42 +0300102 protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}")
Konstantin Podsvirovdb014602015-08-31 15:20:18 +0300103string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\2"
Konstantin Podsvirov743ec442015-08-01 02:01:42 +0300104 protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}")
Konstantin Podsvirovdb014602015-08-31 15:20:18 +0300105string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\3"
Konstantin Podsvirov743ec442015-08-01 02:01:42 +0300106 protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}")
Paul Yang5b4ac532019-02-01 18:43:55 -0800107string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\5"
Walter Grayf1091ab2016-05-27 22:48:24 -0700108 protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}")
109
Paul Yang5b4ac532019-02-01 18:43:55 -0800110message(STATUS "${protobuf_VERSION_PRERELEASE}")
111
Konstantin Podsvirov743ec442015-08-01 02:01:42 +0300112# Package version
Konstantin Podsvirov82983432015-07-31 23:36:00 +0300113set(protobuf_VERSION
Konstantin Podsvirov743ec442015-08-01 02:01:42 +0300114 "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
Konstantin Podsvirov82983432015-07-31 23:36:00 +0300115
Walter Grayf1091ab2016-05-27 22:48:24 -0700116if(protobuf_VERSION_PRERELEASE)
Paul Yang5b4ac532019-02-01 18:43:55 -0800117 set(protobuf_VERSION "${protobuf_VERSION}.${protobuf_VERSION_PRERELEASE}")
118else()
119 set(protobuf_VERSION "${protobuf_VERSION}.0")
Walter Grayf1091ab2016-05-27 22:48:24 -0700120endif()
Paul Yang5b4ac532019-02-01 18:43:55 -0800121message(STATUS "${protobuf_VERSION}")
Walter Grayf1091ab2016-05-27 22:48:24 -0700122
Konstantin Podsvirovdb014602015-08-31 15:20:18 +0300123if(protobuf_VERBOSE)
124 message(STATUS "Configuration script parsing status [")
125 message(STATUS " Description : ${protobuf_DESCRIPTION}")
126 message(STATUS " Version : ${protobuf_VERSION} (${protobuf_VERSION_STRING})")
127 message(STATUS " Contact : ${protobuf_CONTACT}")
128 message(STATUS "]")
129endif()
130
Jisi Liu78d470c2015-06-12 15:49:21 -0700131add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD)
132
Florian Simon1add7a72021-02-25 15:11:40 -0500133if (protobuf_DISABLE_RTTI)
134 add_definitions(-DGOOGLE_PROTOBUF_NO_RTTI=1)
135endif()
136
Arfrever1d13b602022-03-02 02:06:14 +0100137file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map
138"{
139 global:
140 main;
141 local:
142 *;
143};")
144# CheckLinkerFlag module available in CMake >=3.18.
Arfrever1cec8032022-03-08 02:03:50 +0100145if(${CMAKE_VERSION} VERSION_GREATER 3.18 OR ${CMAKE_VERSION} VERSION_EQUAL 3.18)
Arfrever1d13b602022-03-02 02:06:14 +0100146 include(CheckLinkerFlag)
147 check_linker_flag(CXX -Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map protobuf_HAVE_LD_VERSION_SCRIPT)
148else()
149 include(CheckCXXSourceCompiles)
150 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
151 set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} -Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map)
152 check_cxx_source_compiles("
153 int main() {
154 return 0;
155 }
156 " protobuf_HAVE_LD_VERSION_SCRIPT)
157 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
158endif()
159file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map)
160
Feng Xiao4333edb2015-05-31 02:28:34 -0700161find_package(Threads REQUIRED)
Feng Xiao4333edb2015-05-31 02:28:34 -0700162
Mike Kruskalcac97652022-08-12 16:41:00 -0700163# We can install dependencies from submodules if we're running
164# CMake v3.13 or newer.
165if(CMAKE_VERSION VERSION_LESS 3.13)
166 set(_protobuf_INSTALL_SUPPORTED_FROM_MODULE OFF)
167else()
168 set(_protobuf_INSTALL_SUPPORTED_FROM_MODULE ON)
169endif()
170
Konstantin Podsviroveefd1fd2016-08-02 22:37:36 +0300171set(_protobuf_FIND_ZLIB)
Konstantin Podsvirov620bd742015-09-15 15:31:25 +0300172if (protobuf_WITH_ZLIB)
Feng Xiao4333edb2015-05-31 02:28:34 -0700173 find_package(ZLIB)
174 if (ZLIB_FOUND)
175 set(HAVE_ZLIB 1)
Konstantin Podsvirov20b882d2015-08-31 16:23:40 +0300176 # FindZLIB module define ZLIB_INCLUDE_DIRS variable
177 # Set ZLIB_INCLUDE_DIRECTORIES for compatible
178 set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS})
179 # Using imported target if exists
180 if (TARGET ZLIB::ZLIB)
181 set(ZLIB_LIBRARIES ZLIB::ZLIB)
Konstantin Podsviroveefd1fd2016-08-02 22:37:36 +0300182 set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()")
Konstantin Podsvirov20b882d2015-08-31 16:23:40 +0300183 endif (TARGET ZLIB::ZLIB)
Feng Xiao4333edb2015-05-31 02:28:34 -0700184 else (ZLIB_FOUND)
185 set(HAVE_ZLIB 0)
186 # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
187 # complain when we use them later.
188 set(ZLIB_INCLUDE_DIRECTORIES)
189 set(ZLIB_LIBRARIES)
190 endif (ZLIB_FOUND)
Konstantin Podsvirov620bd742015-09-15 15:31:25 +0300191endif (protobuf_WITH_ZLIB)
Feng Xiao4333edb2015-05-31 02:28:34 -0700192
Jisi Liu78d470c2015-06-12 15:49:21 -0700193if (HAVE_ZLIB)
194 add_definitions(-DHAVE_ZLIB)
195endif (HAVE_ZLIB)
196
boscosiu55ed1d42019-07-03 06:34:09 -0700197# We need to link with libatomic on systems that do not have builtin atomics, or
198# don't have builtin support for 8 byte atomics
199set(protobuf_LINK_LIBATOMIC false)
200if (NOT MSVC)
201 include(CheckCXXSourceCompiles)
Ben Bader56e7bdf2019-07-08 11:13:53 -0700202 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
Adam Cozzette500cbd72022-08-11 18:22:15 -0700203 set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} -std=c++14)
boscosiu55ed1d42019-07-03 06:34:09 -0700204 check_cxx_source_compiles("
205 #include <atomic>
206 int main() {
207 return std::atomic<int64_t>{};
208 }
209 " protobuf_HAVE_BUILTIN_ATOMICS)
210 if (NOT protobuf_HAVE_BUILTIN_ATOMICS)
211 set(protobuf_LINK_LIBATOMIC true)
212 endif (NOT protobuf_HAVE_BUILTIN_ATOMICS)
Ben Bader56e7bdf2019-07-08 11:13:53 -0700213 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
boscosiu55ed1d42019-07-03 06:34:09 -0700214endif (NOT MSVC)
215
Konstantin Podsvirovc3aa4c22015-10-15 02:56:48 +0300216if (protobuf_BUILD_SHARED_LIBS)
217 set(protobuf_SHARED_OR_STATIC "SHARED")
218else (protobuf_BUILD_SHARED_LIBS)
219 set(protobuf_SHARED_OR_STATIC "STATIC")
Saleem Abdulrasoolc47adad2021-07-31 16:07:04 -0700220 # The CMAKE_<LANG>_FLAGS(_<BUILD_TYPE>)? is meant to be user controlled.
221 # Prior to CMake 3.15, the MSVC runtime library was pushed into the same flags
222 # making programmatic control difficult. Prefer the functionality in newer
223 # CMake versions when available.
Arfrever1cec8032022-03-08 02:03:50 +0100224 if(${CMAKE_VERSION} VERSION_GREATER 3.15 OR ${CMAKE_VERSION} VERSION_EQUAL 3.15)
David Geldreichf1802892022-01-24 22:17:14 +0100225 if (protobuf_MSVC_STATIC_RUNTIME)
226 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>)
227 else()
228 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>DLL)
229 endif()
Saleem Abdulrasoolc47adad2021-07-31 16:07:04 -0700230 else()
231 # In case we are building static libraries, link also the runtime library statically
232 # so that MSVCR*.DLL is not required at runtime.
233 # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
234 # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
235 # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
236 if (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
237 foreach(flag_var
238 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
239 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
240 if(${flag_var} MATCHES "/MD")
241 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
242 endif(${flag_var} MATCHES "/MD")
243 endforeach(flag_var)
244 endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
245 endif()
Konstantin Podsvirovc3aa4c22015-10-15 02:56:48 +0300246endif (protobuf_BUILD_SHARED_LIBS)
Feng Xiao4333edb2015-05-31 02:28:34 -0700247
Feng Xiaodffd5422015-06-05 17:59:09 -0700248if (MSVC)
Jorge López Fueyo1ae531d2021-06-04 20:13:56 +0200249 if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
250 # Build with multiple processes
Alexander Neumann30f273c2022-07-26 19:59:28 +0200251 add_compile_options(/MP)
Jorge López Fueyo1ae531d2021-06-04 20:13:56 +0200252 endif()
Shigeo Hashimoto25180ac2021-10-15 01:45:40 +0900253 # Set source file and execution character sets to UTF-8
Alexander Neumann30f273c2022-07-26 19:59:28 +0200254 add_compile_options(/utf-8)
Tim Ebringera23669c2017-10-21 09:40:32 -0400255 # MSVC warning suppressions
Alexander Neumann30f273c2022-07-26 19:59:28 +0200256 add_compile_options(
Tim Ebringera23669c2017-10-21 09:40:32 -0400257 /wd4065 # switch statement contains 'default' but no 'case' labels
Tim Ebringera23669c2017-10-21 09:40:32 -0400258 /wd4244 # 'conversion' conversion from 'type1' to 'type2', possible loss of data
259 /wd4251 # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
260 /wd4267 # 'var' : conversion from 'size_t' to 'type', possible loss of data
261 /wd4305 # 'identifier' : truncation from 'type1' to 'type2'
262 /wd4307 # 'operator' : integral constant overflow
263 /wd4309 # 'conversion' : truncation of constant value
264 /wd4334 # 'operator' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)
265 /wd4355 # 'this' : used in base member initializer list
266 /wd4506 # no definition for inline function 'function'
267 /wd4800 # 'type' : forcing value to bool 'true' or 'false' (performance warning)
268 /wd4996 # The compiler encountered a deprecated declaration.
269 )
Jisi Liu3b6df062016-04-01 15:02:45 -0700270 # Allow big object
Alexander Neumann30f273c2022-07-26 19:59:28 +0200271 add_compile_options(/bigobj)
Feng Xiaodffd5422015-06-05 17:59:09 -0700272 string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
273 string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
Jozef Izso34152012018-04-27 23:44:38 +0200274 string(REPLACE "." "," protobuf_RC_FILEVERSION "${protobuf_VERSION}")
Ivan Shynkarenkaf80a8862018-05-02 02:51:08 +0300275
Tim Ebringer2a728402017-10-06 14:26:43 -0400276 # Suppress linker warnings about files with no symbols defined.
Alexander Neumann30f273c2022-07-26 19:59:28 +0200277 string(APPEND CMAKE_STATIC_LINKER_FLAGS " /ignore:4221")
Jozef Izso34152012018-04-27 23:44:38 +0200278
Alexander Neumann30f273c2022-07-26 19:59:28 +0200279 # use English language (0x409) in resource compiler
280 string(APPEND CMAKE_RC_FLAGS " -l0x409")
Jozef Izso34152012018-04-27 23:44:38 +0200281
David L. Jonesc4ddd842022-04-28 15:17:51 -0700282 # Generate the version.rc file used elsewhere.
Arfrever Frehtes Taifersar Arahesis14cab5b2022-03-08 00:00:00 +0000283 configure_file(${protobuf_SOURCE_DIR}/cmake/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
David L. Jonesc4ddd842022-04-28 15:17:51 -0700284 set(protobuf_version_rc_file ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
285
286 # Add the "lib" prefix for generated .lib outputs.
287 set(LIB_PREFIX lib)
288else (MSVC)
289 # No version.rc file.
290 set(protobuf_version_rc_file)
291
292 # When building with "make", "lib" prefix will be added automatically by
293 # the build tool.
294 set(LIB_PREFIX)
Feng Xiaodffd5422015-06-05 17:59:09 -0700295endif (MSVC)
Feng Xiao4333edb2015-05-31 02:28:34 -0700296
Feng Xiao4333edb2015-05-31 02:28:34 -0700297include_directories(
298 ${ZLIB_INCLUDE_DIRECTORIES}
299 ${protobuf_BINARY_DIR}
Arfrever Frehtes Taifersar Arahesis42686622022-03-08 00:00:00 +0000300 ${protobuf_SOURCE_DIR}/src)
Feng Xiao4333edb2015-05-31 02:28:34 -0700301
Wei-Yin Chen (陳威尹)a7eaf362016-09-01 17:12:49 -0700302if (protobuf_UNICODE)
Wei-Yin Chen (陳威尹)588a8032016-08-19 15:25:54 -0700303 add_definitions(-DUNICODE -D_UNICODE)
Wei-Yin Chen (陳威尹)a7eaf362016-09-01 17:12:49 -0700304endif (protobuf_UNICODE)
Wei-Yin Chen (陳威尹)588a8032016-08-19 15:25:54 -0700305
Mike Kruskalcac97652022-08-12 16:41:00 -0700306set(protobuf_ABSL_PROVIDER "module" CACHE STRING "Provider of absl library")
307set_property(CACHE protobuf_ABSL_PROVIDER PROPERTY STRINGS "module" "package")
308
309include(${protobuf_SOURCE_DIR}/cmake/abseil-cpp.cmake)
310
Mike Kruskal3edec1f2022-07-25 20:36:47 -0700311if (protobuf_BUILD_PROTOBUF_BINARIES)
312 include(${protobuf_SOURCE_DIR}/cmake/libprotobuf-lite.cmake)
313 if (NOT DEFINED protobuf_LIB_PROTOBUF_LITE)
314 set(protobuf_LIB_PROTOBUF_LITE libprotobuf-lite)
315 endif ()
316 include(${protobuf_SOURCE_DIR}/cmake/libprotobuf.cmake)
317 if (NOT DEFINED protobuf_LIB_PROTOBUF)
318 set(protobuf_LIB_PROTOBUF libprotobuf)
319 endif ()
320 if (protobuf_BUILD_LIBPROTOC)
321 include(${protobuf_SOURCE_DIR}/cmake/libprotoc.cmake)
322 if (NOT DEFINED protobuf_LIB_PROTOC)
323 set(protobuf_LIB_PROTOC libprotoc)
324 endif ()
325 endif ()
326 if (protobuf_BUILD_PROTOC_BINARIES)
327 include(${protobuf_SOURCE_DIR}/cmake/protoc.cmake)
328 if (NOT DEFINED protobuf_PROTOC_EXE)
329 set(protobuf_PROTOC_EXE protoc)
330 endif ()
331 endif ()
332else ()
Mike Kruskal69c1fa52022-08-09 08:20:51 -0700333 find_package(Protobuf NO_MODULE)
334 if (Protobuf_FOUND)
Mike Kruskal3edec1f2022-07-25 20:36:47 -0700335 set(protobuf_PROTOC_EXE protobuf::protoc)
336 set(protobuf_LIB_PROTOC protobuf::libprotoc)
337 set(protobuf_LIB_PROTOBUF protobuf::libprotobuf)
338 set(protobuf_LIB_PROTOBUF_LITE protobuf::libprotobuf-lite)
339 message(STATUS "CMake installation of Protobuf found.")
340 endif ()
341endif ()
Konstantin Podsvirov82983432015-07-31 23:36:00 +0300342
Mike Kruskal3edec1f2022-07-25 20:36:47 -0700343# Ensure we have a protoc executable and protobuf libraries if we need one
Michael WERLE976f85b2020-05-01 16:50:53 +0900344if (protobuf_BUILD_TESTS OR protobuf_BUILD_CONFORMANCE OR protobuf_BUILD_EXAMPLES)
345 if (NOT DEFINED protobuf_PROTOC_EXE)
Mike Kruskal3edec1f2022-07-25 20:36:47 -0700346 find_program(protobuf_PROTOC_EXE protoc REQUIRED)
347 message(STATUS "Found system ${protobuf_PROTOC_EXE}.")
Michael WERLE976f85b2020-05-01 16:50:53 +0900348 endif ()
349 if(protobuf_VERBOSE)
350 message(STATUS "Using protoc : ${protobuf_PROTOC_EXE}")
Mike Kruskal3edec1f2022-07-25 20:36:47 -0700351 message(STATUS "Using libprotobuf : ${protobuf_LIB_PROTOBUF}")
352 message(STATUS "Using libprotobuf-lite : ${protobuf_LIB_PROTOBUF_LITE}")
353 message(STATUS "Using libprotoc : ${protobuf_LIB_PROTOC}")
Michael WERLE976f85b2020-05-01 16:50:53 +0900354 endif(protobuf_VERBOSE)
355endif ()
356
Konstantin Podsvirov673d32e2015-09-15 15:30:02 +0300357if (protobuf_BUILD_TESTS)
Florin Crișan18c951e2022-02-08 03:38:38 +0200358 enable_testing()
Arfrever Frehtes Taifersar Arahesis14cab5b2022-03-08 00:00:00 +0000359 include(${protobuf_SOURCE_DIR}/cmake/tests.cmake)
Konstantin Podsvirov673d32e2015-09-15 15:30:02 +0300360endif (protobuf_BUILD_TESTS)
Konstantin Podsvirov82983432015-07-31 23:36:00 +0300361
Josh Haberman42496922018-08-24 12:27:21 -0700362if (protobuf_BUILD_CONFORMANCE)
Arfrever Frehtes Taifersar Arahesis14cab5b2022-03-08 00:00:00 +0000363 include(${protobuf_SOURCE_DIR}/cmake/conformance.cmake)
Josh Haberman42496922018-08-24 12:27:21 -0700364endif (protobuf_BUILD_CONFORMANCE)
365
JCooky55645ca2022-02-03 01:29:25 +0900366if (protobuf_INSTALL)
Arfrever Frehtes Taifersar Arahesis14cab5b2022-03-08 00:00:00 +0000367 include(${protobuf_SOURCE_DIR}/cmake/install.cmake)
JCooky55645ca2022-02-03 01:29:25 +0900368endif (protobuf_INSTALL)
Konstantin Podsvirov71556292016-06-01 17:00:08 +0300369
370if (protobuf_BUILD_EXAMPLES)
Arfrever Frehtes Taifersar Arahesis14cab5b2022-03-08 00:00:00 +0000371 include(${protobuf_SOURCE_DIR}/cmake/examples.cmake)
Konstantin Podsvirov71556292016-06-01 17:00:08 +0300372endif (protobuf_BUILD_EXAMPLES)
373
374if(protobuf_VERBOSE)
Michael WERLE976f85b2020-05-01 16:50:53 +0900375 message(STATUS "Protocol Buffers Configuring done")
376endif(protobuf_VERBOSE)