blob: 15ae457aa2b35640e785759a19e888ce7e15066e [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001# Minimum CMake required
2cmake_minimum_required(VERSION 2.8)
3
4# Project
5project(protobuf C CXX)
6
7# CMake policies
8cmake_policy(SET CMP0022 NEW)
9
10# Options
11option(protobuf_VERBOSE "Enable for verbose output" OFF)
12option(protobuf_BUILD_TESTS "Build tests" ON)
13if (BUILD_SHARED_LIBS)
14 set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON)
15else (BUILD_SHARED_LIBS)
16 set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF)
17endif (BUILD_SHARED_LIBS)
18option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT})
19option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON)
20if (MSVC)
21 set(protobuf_WITH_ZLIB_DEFAULT OFF)
22else (MSVC)
23 set(protobuf_WITH_ZLIB_DEFAULT ON)
24endif (MSVC)
25option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
26set(protobuf_DEBUG_POSTFIX "d"
27 CACHE STRING "Default debug postfix")
28
29# Path to main configure script
30set(protobuf_CONFIGURE_SCRIPT "../configure.ac")
31
32# Parse configure script
33set(protobuf_AC_INIT_REGEX
34 "^AC_INIT\\(\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\]\\)$")
35file(STRINGS "${protobuf_CONFIGURE_SCRIPT}" protobuf_AC_INIT_LINE
36 LIMIT_COUNT 1 REGEX "^AC_INIT")
37# Description
38string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\1"
39 protobuf_DESCRIPTION "${protobuf_AC_INIT_LINE}")
40# Version
41string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\2"
42 protobuf_VERSION_STRING "${protobuf_AC_INIT_LINE}")
43# Contact
44string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\3"
45 protobuf_CONTACT "${protobuf_AC_INIT_LINE}")
46# Parse version tweaks
47set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+).*$")
48string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\1"
49 protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}")
50string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\2"
51 protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}")
52string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\3"
53 protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}")
54# Package version
55set(protobuf_VERSION
56 "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
57
58if(protobuf_VERBOSE)
59 message(STATUS "Configuration script parsing status [")
60 message(STATUS " Description : ${protobuf_DESCRIPTION}")
61 message(STATUS " Version : ${protobuf_VERSION} (${protobuf_VERSION_STRING})")
62 message(STATUS " Contact : ${protobuf_CONTACT}")
63 message(STATUS "]")
64endif()
65
66add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD)
67
68find_package(Threads REQUIRED)
69if (CMAKE_USE_PTHREADS_INIT)
70 add_definitions(-DHAVE_PTHREAD)
71endif (CMAKE_USE_PTHREADS_INIT)
72
73if (protobuf_WITH_ZLIB)
74 find_package(ZLIB)
75 if (ZLIB_FOUND)
76 set(HAVE_ZLIB 1)
77 # FindZLIB module define ZLIB_INCLUDE_DIRS variable
78 # Set ZLIB_INCLUDE_DIRECTORIES for compatible
79 set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS})
80 # Using imported target if exists
81 if (TARGET ZLIB::ZLIB)
82 set(ZLIB_LIBRARIES ZLIB::ZLIB)
83 endif (TARGET ZLIB::ZLIB)
84 else (ZLIB_FOUND)
85 set(HAVE_ZLIB 0)
86 # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
87 # complain when we use them later.
88 set(ZLIB_INCLUDE_DIRECTORIES)
89 set(ZLIB_LIBRARIES)
90 endif (ZLIB_FOUND)
91endif (protobuf_WITH_ZLIB)
92
93if (HAVE_ZLIB)
94 add_definitions(-DHAVE_ZLIB)
95endif (HAVE_ZLIB)
96
97if (protobuf_BUILD_SHARED_LIBS)
98 set(protobuf_SHARED_OR_STATIC "SHARED")
99else (protobuf_BUILD_SHARED_LIBS)
100 set(protobuf_SHARED_OR_STATIC "STATIC")
101 # In case we are building static libraries, link also the runtime library statically
102 # so that MSVCR*.DLL is not required at runtime.
103 # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
104 # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
105 # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
106 if (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
107 foreach(flag_var
108 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
109 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
110 if(${flag_var} MATCHES "/MD")
111 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
112 endif(${flag_var} MATCHES "/MD")
113 endforeach(flag_var)
114 endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
115endif (protobuf_BUILD_SHARED_LIBS)
116
117if (MSVC)
118 # Build with multiple processes
119 add_definitions(/MP)
120 add_definitions(/wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146 /wd4305)
121 string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
122 string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
123 configure_file(extract_includes.bat.in extract_includes.bat)
124endif (MSVC)
125
126get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH)
127
128include_directories(
129 ${ZLIB_INCLUDE_DIRECTORIES}
130 ${protobuf_BINARY_DIR}
131 ${protobuf_source_dir}/src)
132
133if (MSVC)
134 # Add the "lib" prefix for generated .lib outputs.
135 set(LIB_PREFIX lib)
136else (MSVC)
137 # When building with "make", "lib" prefix will be added automatically by
138 # the build tool.
139 set(LIB_PREFIX)
140endif (MSVC)
141
142include(libprotobuf-lite.cmake)
143include(libprotobuf.cmake)
144include(libprotoc.cmake)
145include(protoc.cmake)
146
147if (protobuf_BUILD_TESTS)
148 include(tests.cmake)
149endif (protobuf_BUILD_TESTS)
150
151include(install.cmake)