Explicitly convert an enum to an int (#19537)
[google-protobuf.git] / CMakeLists.txt
blobd5b73ef9f0811b6e0a87b547f1350c3ce5dabe92
1 # Minimum CMake required. If available, accept the policy-controlled behavior up
2 # to 3.26.
3 cmake_minimum_required(VERSION 3.16...3.26)
5 # Revert to old behavior for MSVC debug symbols.
6 if(POLICY CMP0141)
7   cmake_policy(SET CMP0141 OLD)
8 endif()
10 if(protobuf_VERBOSE)
11   message(STATUS "Protocol Buffers Configuring...")
12 endif()
14 # Project
15 project(protobuf C CXX)
17 if(protobuf_DEPRECATED_CMAKE_SUBDIRECTORY_USAGE)
18   if(CMAKE_PROJECT_NAME STREQUAL "protobuf")
19     get_filename_component(CMAKE_SOURCE_DIR ${CMAKE_SOURCE_DIR} DIRECTORY)
20   endif()
21   get_filename_component(CMAKE_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
22   get_filename_component(PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR} DIRECTORY)
23   get_filename_component(protobuf_SOURCE_DIR ${protobuf_SOURCE_DIR} DIRECTORY)
24 endif()
26 # Options
27 option(protobuf_INSTALL "Install protobuf binaries and files" ON)
28 option(protobuf_BUILD_TESTS "Build tests" ON)
29 option(protobuf_BUILD_CONFORMANCE "Build conformance tests" OFF)
30 option(protobuf_BUILD_EXAMPLES "Build examples" OFF)
31 option(protobuf_BUILD_PROTOBUF_BINARIES "Build protobuf libraries and protoc compiler" ON)
32 option(protobuf_BUILD_PROTOC_BINARIES "Build libprotoc and protoc compiler" ON)
33 option(protobuf_BUILD_LIBPROTOC "Build libprotoc" OFF)
34 option(protobuf_BUILD_LIBUPB "Build libupb" ON)
35 option(protobuf_DISABLE_RTTI "Remove runtime type information in the binaries" OFF)
36 option(protobuf_TEST_XML_OUTDIR "Output directory for XML logs from tests." "")
37 option(protobuf_ALLOW_CCACHE "Adjust build flags to allow for ccache support." OFF)
38 option(protobuf_FORCE_FETCH_DEPENDENCIES "Force all dependencies to be downloaded from GitHub.  Local installations will be ignored." OFF)
39 option(protobuf_LOCAL_DEPENDENCIES_ONLY "Prevent downloading any dependencies from GitHub. If this option is set, the dependency must be available locally as an installed package." OFF)
41 # We support Unity (Jumbo) builds best-effort.
42 option(protobuf_USE_UNITY_BUILD "Enable Unity (Jumbo) build for" OFF)
43 if (BUILD_SHARED_LIBS)
44   set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON)
45 else (BUILD_SHARED_LIBS)
46   set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF)
47 endif (BUILD_SHARED_LIBS)
48 option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT})
49 include(CMakeDependentOption)
50 cmake_dependent_option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON
51   "NOT protobuf_BUILD_SHARED_LIBS" OFF)
52 set(protobuf_WITH_ZLIB_DEFAULT ON)
53 option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
54 set(protobuf_DEBUG_POSTFIX "d"
55   CACHE STRING "Default debug postfix")
56 mark_as_advanced(protobuf_DEBUG_POSTFIX)
58 if(WITH_PROTOC)
59   set(protobuf_PROTOC_EXE protoc)
60   set(protobuf_BUILD_PROTOC_BINARIES OFF)
61   add_executable(protoc IMPORTED GLOBAL)
62   add_executable(protobuf::protoc ALIAS protoc)
63   set_property(TARGET protoc PROPERTY IMPORTED_LOCATION ${WITH_PROTOC})
64 endif()
66 # User options
67 include(${protobuf_SOURCE_DIR}/cmake/protobuf-options.cmake)
69 if (protobuf_BUILD_SHARED_LIBS)
70   # This is necessary for linking in Abseil.
71   set(CMAKE_POSITION_INDEPENDENT_CODE ON)
73   # Build Abseil as shared libraries to avoid ODR violations.
74   set(BUILD_SHARED_LIBS ON)
76   # Output directory is correct by default for most build setups. However, when
77   # building Protobuf as a DLL, it is important to have the DLL in the same
78   # directory as the executable using it. Thus, we put all binaries in a single
79   # /bin directory.
80   if (MSVC)
81     set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
82     set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
83     set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
84     set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
85   endif ()
86 endif ()
88 # Version metadata
89 set(protobuf_VERSION_STRING "6.31.0")
90 set(protobuf_DESCRIPTION "Protocol Buffers")
91 set(protobuf_CONTACT "protobuf@googlegroups.com")
93 # Overrides for option dependencies
94 if (protobuf_BUILD_PROTOC_BINARIES OR protobuf_BUILD_TESTS)
95   set(protobuf_BUILD_LIBPROTOC ON)
96 endif ()
97 if (NOT protobuf_BUILD_PROTOBUF_BINARIES)
98   set(protobuf_INSTALL OFF)
99 endif()
100 # Parse version tweaks
101 set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+)([-]rc[-]|\\.)?([0-9]*)$")
102 string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\1"
103   protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}")
104 string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\2"
105   protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}")
106 string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\3"
107   protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}")
108 string(REGEX REPLACE     "${protobuf_VERSION_REGEX}" "\\5"
109   protobuf_VERSION_PRERELEASE "${protobuf_VERSION_STRING}")
111 if (protobuf_FORCE_FETCH_DEPENDENCIES AND protobuf_LOCAL_DEPENDENCIES_ONLY)
112   message(FATAL_ERROR "Conflicting options protobuf_FORCE_FETCH_DEPENDENCIES and protobuf_LOCAL_DEPENDENCIES_ONLY both set")
113 endif()
115 # Package version
116 set(protobuf_VERSION
117   "${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
119 if(protobuf_VERSION_PRERELEASE)
120   message(STATUS "${protobuf_VERSION_PRERELEASE}")
121   set(protobuf_VERSION "${protobuf_VERSION}.${protobuf_VERSION_PRERELEASE}")
122 else()
123   set(protobuf_VERSION "${protobuf_VERSION}.0")
124 endif()
125 message(STATUS "protobuf version: ${protobuf_VERSION}")
127 if(protobuf_VERBOSE)
128   message(STATUS "Configuration script parsing status [")
129   message(STATUS "  Description : ${protobuf_DESCRIPTION}")
130   message(STATUS "  Version     : ${protobuf_VERSION} (${protobuf_VERSION_STRING})")
131   message(STATUS "  Contact     : ${protobuf_CONTACT}")
132   message(STATUS "]")
133 endif()
135 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map
137   global:
138     main;
139   local:
140     *;
141 };")
142 # CheckLinkerFlag module available in CMake >=3.18.
143 if(${CMAKE_VERSION} VERSION_GREATER 3.18 OR ${CMAKE_VERSION} VERSION_EQUAL 3.18)
144   include(CheckLinkerFlag)
145   check_linker_flag(CXX -Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map protobuf_HAVE_LD_VERSION_SCRIPT)
146 else()
147   include(CheckCXXSourceCompiles)
148   set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
149   set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} -Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map)
150   check_cxx_source_compiles("
151     int main() {
152       return 0;
153     }
154   " protobuf_HAVE_LD_VERSION_SCRIPT)
155   set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
156 endif()
157 file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/cmaketest.map)
159 find_package(Threads REQUIRED)
161 set(_protobuf_FIND_ZLIB)
162 if (protobuf_WITH_ZLIB)
163   find_package(ZLIB)
164   if (ZLIB_FOUND)
165     set(HAVE_ZLIB 1)
166     # FindZLIB module define ZLIB_INCLUDE_DIRS variable
167     # Set ZLIB_INCLUDE_DIRECTORIES for compatible
168     set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS})
169     # Using imported target if exists
170     if (TARGET ZLIB::ZLIB)
171       set(ZLIB_LIBRARIES ZLIB::ZLIB)
172       set(_protobuf_FIND_ZLIB "if(NOT ZLIB_FOUND)\n  find_package(ZLIB)\nendif()")
173     endif (TARGET ZLIB::ZLIB)
174   else (ZLIB_FOUND)
175     set(HAVE_ZLIB 0)
176     # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
177     # complain when we use them later.
178     set(ZLIB_INCLUDE_DIRECTORIES)
179     set(ZLIB_LIBRARIES)
180   endif (ZLIB_FOUND)
181 endif (protobuf_WITH_ZLIB)
183 # We need to link with libatomic on systems that do not have builtin atomics, or
184 # don't have builtin support for 8 byte atomics
185 set(protobuf_LINK_LIBATOMIC false)
186 if (NOT MSVC)
187   include(CheckCXXSourceCompiles)
188   set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
189   set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} -std=c++17)
190   check_cxx_source_compiles("
191     #include <atomic>
192     int main() {
193       return static_cast<int>(std::atomic<int64_t>{});
194     }
195   " protobuf_HAVE_BUILTIN_ATOMICS)
196   if (NOT protobuf_HAVE_BUILTIN_ATOMICS)
197     set(protobuf_LINK_LIBATOMIC true)
198   endif (NOT protobuf_HAVE_BUILTIN_ATOMICS)
199   set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
200 endif (NOT MSVC)
202 if (protobuf_BUILD_SHARED_LIBS)
203   set(protobuf_SHARED_OR_STATIC "SHARED")
204 else (protobuf_BUILD_SHARED_LIBS)
205   set(protobuf_SHARED_OR_STATIC "STATIC")
206   set(ABSL_MSVC_STATIC_RUNTIME ON)
207   if (protobuf_MSVC_STATIC_RUNTIME)
208       set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>)
209   else()
210       set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>DLL)
211   endif()
212 endif (protobuf_BUILD_SHARED_LIBS)
214 # Export all symbols on Windows when building shared libraries
215 SET(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
217 if (MSVC)
218   string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
219   string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
220   string(REPLACE "." ","  protobuf_RC_FILEVERSION "${protobuf_VERSION}")
222   if (protobuf_ALLOW_CCACHE)
223     # In order to support ccache, we need to remove the /Zi option because it
224     # puts debug symbols into separate pdb files (which in incompatible with
225     # ccache).  This can be replaced with /Z7 to preserve debug symbols, which
226     # embeds debug symbols into the object files instead of creating a separate
227     # pdb file, which isn't currently supported by ccache.  However, this bloats
228     # the ccache size by about a factor of 2x, making it very expensive in CI.
229     # Instead, we strip debug symbols to reduce this overhead.
230     foreach(v
231         CMAKE_C_FLAGS_DEBUG
232         CMAKE_CXX_FLAGS_DEBUG
233         CMAKE_C_FLAGS_RELWITHDEBINFO
234         CMAKE_CXX_FLAGS_RELWITHDEBINFO
235         )
236       string(REGEX REPLACE "[-/]Z[iI7]" "/DEBUG:NONE" ${v} "${${v}}")
237     endforeach()
238   endif()
240   # Suppress linker warnings about files with no symbols defined.
241   string(APPEND CMAKE_STATIC_LINKER_FLAGS " /ignore:4221")
243   # use English language (0x409) in resource compiler
244   string(APPEND CMAKE_RC_FLAGS " -l0x409")
246   # Generate the version.rc file used elsewhere.
247   configure_file(${protobuf_SOURCE_DIR}/cmake/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
248   set(protobuf_version_rc_file ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
250   # Add the "lib" prefix for generated .lib outputs.
251   set(LIB_PREFIX lib)
252 else (MSVC)
253   # No version.rc file.
254   set(protobuf_version_rc_file)
256   # When building with "make", "lib" prefix will be added automatically by
257   # the build tool.
258   set(LIB_PREFIX)
259 endif (MSVC)
261 include_directories(
262   ${ZLIB_INCLUDE_DIRECTORIES}
263   ${protobuf_BINARY_DIR}
264   # Support #include-ing other top-level directories, i.e. upb_generator.
265   ${protobuf_SOURCE_DIR}
266   ${protobuf_BINARY_DIR}/src
267   ${protobuf_SOURCE_DIR}/src)
269 if (protobuf_BUILD_TESTS)
270   include(${protobuf_SOURCE_DIR}/cmake/gtest.cmake)
271 endif (protobuf_BUILD_TESTS)
273 include(${protobuf_SOURCE_DIR}/cmake/abseil-cpp.cmake)
275 if (protobuf_BUILD_PROTOBUF_BINARIES)
276   include(${protobuf_SOURCE_DIR}/cmake/utf8_range.cmake)
277   include(${protobuf_SOURCE_DIR}/cmake/libprotobuf-lite.cmake)
278   if (NOT DEFINED protobuf_LIB_PROTOBUF_LITE)
279     set(protobuf_LIB_PROTOBUF_LITE libprotobuf-lite)
280   endif ()
281   include(${protobuf_SOURCE_DIR}/cmake/libprotobuf.cmake)
282   if (NOT DEFINED protobuf_LIB_PROTOBUF)
283     set(protobuf_LIB_PROTOBUF libprotobuf)
284   endif ()
285   if (protobuf_BUILD_LIBPROTOC)
286     include(${protobuf_SOURCE_DIR}/cmake/libprotoc.cmake)
287     if (NOT DEFINED protobuf_LIB_PROTOC)
288       set(protobuf_LIB_PROTOC libprotoc)
289     endif ()
290   endif ()
291   if (protobuf_BUILD_LIBUPB)
292     include(${protobuf_SOURCE_DIR}/cmake/libupb.cmake)
293     if (NOT DEFINED protobuf_LIB_UPB)
294       set(protobuf_LIB_UPB libupb)
295     endif ()
296     include(${protobuf_SOURCE_DIR}/cmake/upb_generators.cmake)
297   endif ()
298   if (protobuf_BUILD_PROTOC_BINARIES)
299     include(${protobuf_SOURCE_DIR}/cmake/protoc.cmake)
300     if (NOT DEFINED protobuf_PROTOC_EXE)
301       set(protobuf_PROTOC_EXE protoc)
302     endif ()
303   endif ()
304 else ()
305   find_package(Protobuf NO_MODULE)
306   if (Protobuf_FOUND)
307     set(protobuf_PROTOC_EXE protobuf::protoc)
308     set(protobuf_LIB_PROTOC protobuf::libprotoc)
309     set(protobuf_LIB_PROTOBUF protobuf::libprotobuf)
310     set(protobuf_LIB_PROTOBUF_LITE protobuf::libprotobuf-lite)
311     set(protobuf_LIB_UPB protobuf::libupb)
312     message(STATUS "CMake installation of Protobuf found.")
313   endif ()
314 endif ()
316 # Ensure we have a protoc executable and protobuf libraries if we need one
317 if (protobuf_BUILD_TESTS OR protobuf_BUILD_CONFORMANCE OR protobuf_BUILD_EXAMPLES)
318   if (NOT DEFINED protobuf_PROTOC_EXE)
319     find_program(protobuf_PROTOC_EXE protoc REQUIRED)
320     message(STATUS "Found system ${protobuf_PROTOC_EXE}.")
321   endif ()
322   if(protobuf_VERBOSE)
323     message(STATUS "Using protoc : ${protobuf_PROTOC_EXE}")
324     message(STATUS "Using libprotobuf : ${protobuf_LIB_PROTOBUF}")
325     message(STATUS "Using libprotobuf-lite : ${protobuf_LIB_PROTOBUF_LITE}")
326     message(STATUS "Using libprotoc : ${protobuf_LIB_PROTOC}")
327     message(STATUS "Using libupb : ${protobuf_LIB_UPB}")
328   endif(protobuf_VERBOSE)
329 endif ()
331 if (protobuf_BUILD_TESTS)
332   enable_testing()
333   include(${protobuf_SOURCE_DIR}/cmake/tests.cmake)
334 endif (protobuf_BUILD_TESTS)
336 if (protobuf_BUILD_CONFORMANCE)
337   include(${protobuf_SOURCE_DIR}/cmake/conformance.cmake)
338 endif (protobuf_BUILD_CONFORMANCE)
340 if (protobuf_INSTALL)
341   include(${protobuf_SOURCE_DIR}/cmake/install.cmake)
342 endif (protobuf_INSTALL)
344 if (protobuf_BUILD_EXAMPLES)
345   include(${protobuf_SOURCE_DIR}/cmake/examples.cmake)
346 endif (protobuf_BUILD_EXAMPLES)
348 if(protobuf_VERBOSE)
349   message(STATUS "Protocol Buffers Configuring done")
350 endif(protobuf_VERBOSE)