2015-07-31 20:36:00 +00:00
|
|
|
# Minimum CMake required
|
2015-05-31 09:28:34 +00:00
|
|
|
cmake_minimum_required(VERSION 2.8)
|
|
|
|
|
2015-07-31 20:36:00 +00:00
|
|
|
# Project
|
2015-05-31 09:28:34 +00:00
|
|
|
project(protobuf C CXX)
|
|
|
|
|
2015-09-01 13:44:48 +00:00
|
|
|
# CMake policies
|
|
|
|
cmake_policy(SET CMP0022 NEW)
|
|
|
|
|
2015-07-31 20:36:00 +00:00
|
|
|
# Options
|
2015-08-31 12:20:18 +00:00
|
|
|
option(protobuf_VERBOSE "Enable for verbose output" OFF)
|
2015-09-15 12:30:02 +00:00
|
|
|
option(protobuf_BUILD_TESTS "Build tests" ON)
|
2015-09-15 12:36:20 +00:00
|
|
|
option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" OFF)
|
2015-09-01 12:00:00 +00:00
|
|
|
option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON)
|
2015-05-31 09:28:34 +00:00
|
|
|
if (MSVC)
|
2015-09-15 12:31:25 +00:00
|
|
|
set(protobuf_WITH_ZLIB_DEFAULT OFF)
|
2015-08-31 13:23:40 +00:00
|
|
|
else (MSVC)
|
2015-09-15 12:31:25 +00:00
|
|
|
set(protobuf_WITH_ZLIB_DEFAULT ON)
|
2015-05-31 09:28:34 +00:00
|
|
|
endif (MSVC)
|
2015-09-15 12:31:25 +00:00
|
|
|
option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
|
2015-05-31 09:28:34 +00:00
|
|
|
|
2015-07-31 23:01:42 +00:00
|
|
|
# Path to main configure script
|
|
|
|
set(protobuf_CONFIGURE_SCRIPT "../configure.ac")
|
2015-07-31 20:36:00 +00:00
|
|
|
|
2015-08-31 12:20:18 +00:00
|
|
|
# Parse configure script
|
|
|
|
set(protobuf_AC_INIT_REGEX
|
|
|
|
"^AC_INIT\\(\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\]\\)$")
|
|
|
|
file(STRINGS "${protobuf_CONFIGURE_SCRIPT}" protobuf_AC_INIT_LINE
|
|
|
|
LIMIT_COUNT 1 REGEX "^AC_INIT")
|
|
|
|
# Description
|
|
|
|
string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\1"
|
|
|
|
protobuf_DESCRIPTION "${protobuf_AC_INIT_LINE}")
|
|
|
|
# Version
|
|
|
|
string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\2"
|
|
|
|
protobuf_VERSION_STRING "${protobuf_AC_INIT_LINE}")
|
|
|
|
# Contact
|
|
|
|
string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\3"
|
|
|
|
protobuf_CONTACT "${protobuf_AC_INIT_LINE}")
|
2015-07-31 23:01:42 +00:00
|
|
|
# Parse version tweaks
|
2015-08-31 12:20:18 +00:00
|
|
|
set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+).*$")
|
|
|
|
string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\1"
|
2015-07-31 23:01:42 +00:00
|
|
|
protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}")
|
2015-08-31 12:20:18 +00:00
|
|
|
string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\2"
|
2015-07-31 23:01:42 +00:00
|
|
|
protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}")
|
2015-08-31 12:20:18 +00:00
|
|
|
string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\3"
|
2015-07-31 23:01:42 +00:00
|
|
|
protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}")
|
|
|
|
# Package version
|
2015-07-31 20:36:00 +00:00
|
|
|
set(protobuf_VERSION
|
2015-07-31 23:01:42 +00:00
|
|
|
"${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
|
2015-07-31 20:36:00 +00:00
|
|
|
|
2015-08-31 12:20:18 +00:00
|
|
|
if(protobuf_VERBOSE)
|
|
|
|
message(STATUS "Configuration script parsing status [")
|
|
|
|
message(STATUS " Description : ${protobuf_DESCRIPTION}")
|
|
|
|
message(STATUS " Version : ${protobuf_VERSION} (${protobuf_VERSION_STRING})")
|
|
|
|
message(STATUS " Contact : ${protobuf_CONTACT}")
|
|
|
|
message(STATUS "]")
|
|
|
|
endif()
|
|
|
|
|
2015-06-12 22:49:21 +00:00
|
|
|
add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD)
|
|
|
|
|
2015-05-31 09:28:34 +00:00
|
|
|
find_package(Threads REQUIRED)
|
|
|
|
if (CMAKE_USE_PTHREADS_INIT)
|
2015-06-12 22:49:21 +00:00
|
|
|
add_definitions(-DHAVE_PTHREAD)
|
2015-05-31 09:28:34 +00:00
|
|
|
endif (CMAKE_USE_PTHREADS_INIT)
|
|
|
|
|
2015-09-15 12:31:25 +00:00
|
|
|
if (protobuf_WITH_ZLIB)
|
2015-05-31 09:28:34 +00:00
|
|
|
find_package(ZLIB)
|
|
|
|
if (ZLIB_FOUND)
|
|
|
|
set(HAVE_ZLIB 1)
|
2015-08-31 13:23:40 +00:00
|
|
|
# FindZLIB module define ZLIB_INCLUDE_DIRS variable
|
|
|
|
# Set ZLIB_INCLUDE_DIRECTORIES for compatible
|
|
|
|
set(ZLIB_INCLUDE_DIRECTORIES ${ZLIB_INCLUDE_DIRECTORIES} ${ZLIB_INCLUDE_DIRS})
|
|
|
|
# Using imported target if exists
|
|
|
|
if (TARGET ZLIB::ZLIB)
|
|
|
|
set(ZLIB_LIBRARIES ZLIB::ZLIB)
|
|
|
|
endif (TARGET ZLIB::ZLIB)
|
2015-05-31 09:28:34 +00:00
|
|
|
else (ZLIB_FOUND)
|
|
|
|
set(HAVE_ZLIB 0)
|
|
|
|
# Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
|
|
|
|
# complain when we use them later.
|
|
|
|
set(ZLIB_INCLUDE_DIRECTORIES)
|
|
|
|
set(ZLIB_LIBRARIES)
|
|
|
|
endif (ZLIB_FOUND)
|
2015-09-15 12:31:25 +00:00
|
|
|
endif (protobuf_WITH_ZLIB)
|
2015-05-31 09:28:34 +00:00
|
|
|
|
2015-06-12 22:49:21 +00:00
|
|
|
if (HAVE_ZLIB)
|
|
|
|
add_definitions(-DHAVE_ZLIB)
|
|
|
|
endif (HAVE_ZLIB)
|
|
|
|
|
2015-05-31 09:28:34 +00:00
|
|
|
if (MSVC)
|
2015-09-15 12:36:20 +00:00
|
|
|
if (protobuf_BUILD_SHARED_LIBS)
|
2015-05-31 09:28:34 +00:00
|
|
|
add_definitions(-DPROTOBUF_USE_DLLS)
|
2015-09-15 12:36:20 +00:00
|
|
|
else (protobuf_BUILD_SHARED_LIBS)
|
2015-08-12 19:47:02 +00:00
|
|
|
# In case we are building static libraries, link also the runtime library statically
|
2015-08-31 13:23:40 +00:00
|
|
|
# so that MSVCR*.DLL is not required at runtime.
|
2015-08-12 19:47:02 +00:00
|
|
|
# https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
|
|
|
|
# This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
|
|
|
|
# http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
|
2015-09-01 12:00:00 +00:00
|
|
|
if (protobuf_MSVC_STATIC_RUNTIME)
|
|
|
|
foreach(flag_var
|
|
|
|
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
|
|
|
|
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
|
|
|
|
if(${flag_var} MATCHES "/MD")
|
|
|
|
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
|
|
|
|
endif(${flag_var} MATCHES "/MD")
|
|
|
|
endforeach(flag_var)
|
|
|
|
endif (protobuf_MSVC_STATIC_RUNTIME)
|
2015-09-15 12:36:20 +00:00
|
|
|
endif (protobuf_BUILD_SHARED_LIBS)
|
2015-08-12 19:47:02 +00:00
|
|
|
add_definitions(/wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146 /wd4305)
|
2015-05-31 09:28:34 +00:00
|
|
|
endif (MSVC)
|
|
|
|
|
2015-06-06 00:59:09 +00:00
|
|
|
if (MSVC)
|
|
|
|
string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
|
|
|
|
string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
|
|
|
|
configure_file(extract_includes.bat.in extract_includes.bat)
|
|
|
|
endif (MSVC)
|
2015-05-31 09:28:34 +00:00
|
|
|
|
|
|
|
get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH)
|
|
|
|
|
|
|
|
include_directories(
|
|
|
|
${ZLIB_INCLUDE_DIRECTORIES}
|
|
|
|
${protobuf_BINARY_DIR}
|
|
|
|
${protobuf_source_dir}/src)
|
|
|
|
|
|
|
|
if (MSVC)
|
|
|
|
# Add the "lib" prefix for generated .lib outputs.
|
|
|
|
set(LIB_PREFIX lib)
|
|
|
|
else (MSVC)
|
|
|
|
# When building with "make", "lib" prefix will be added automatically by
|
|
|
|
# the build tool.
|
|
|
|
set(LIB_PREFIX)
|
|
|
|
endif (MSVC)
|
|
|
|
|
|
|
|
include(libprotobuf-lite.cmake)
|
|
|
|
include(libprotobuf.cmake)
|
|
|
|
include(libprotoc.cmake)
|
2015-06-06 04:24:23 +00:00
|
|
|
include(protoc.cmake)
|
2015-07-31 20:36:00 +00:00
|
|
|
|
2015-09-15 12:30:02 +00:00
|
|
|
if (protobuf_BUILD_TESTS)
|
2015-06-06 04:24:23 +00:00
|
|
|
include(tests.cmake)
|
2015-09-15 12:30:02 +00:00
|
|
|
endif (protobuf_BUILD_TESTS)
|
2015-07-31 20:36:00 +00:00
|
|
|
|
|
|
|
include(install.cmake)
|