Merge pull request #783 from podsvirov/topic-cmake-project

Improved CMake project
pull/890/head
Feng Xiao 9 years ago
commit 5314ad1b21
  1. 1
      .gitignore
  2. 4
      appveyor.bat
  3. 110
      cmake/CMakeLists.txt
  4. 12
      cmake/libprotobuf-lite.cmake
  5. 12
      cmake/libprotobuf.cmake
  6. 11
      cmake/libprotoc.cmake
  7. 15
      cmake/tests.cmake
  8. 8
      src/google/protobuf/compiler/command_line_interface_unittest.cc

1
.gitignore vendored

@ -81,6 +81,7 @@ javanano/target
# Windows native output. # Windows native output.
cmake/build cmake/build
build_msvc
# NuGet packages: we want the repository configuration, but not the # NuGet packages: we want the repository configuration, but not the
# packages themselves. # packages themselves.

@ -10,7 +10,7 @@ goto :error
echo Building C++ echo Building C++
mkdir build_msvc mkdir build_msvc
cd build_msvc cd build_msvc
cmake -G "%generator%" -DBUILD_SHARED_LIBS=%BUILD_DLL% ../cmake cmake -G "%generator%" -Dprotobuf_BUILD_SHARED_LIBS=%BUILD_DLL% ../cmake
msbuild protobuf.sln /p:Platform=%vcplatform% /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" || goto error msbuild protobuf.sln /p:Platform=%vcplatform% /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" || goto error
cd %configuration% cd %configuration%
tests.exe || goto error tests.exe || goto error
@ -26,4 +26,4 @@ goto :EOF
:error :error
echo Failed! echo Failed!
EXIT /b %ERRORLEVEL% EXIT /b %ERRORLEVEL%

@ -4,39 +4,65 @@ cmake_minimum_required(VERSION 2.8)
# Project # Project
project(protobuf C CXX) project(protobuf C CXX)
# CMake policies
cmake_policy(SET CMP0022 NEW)
# Options # Options
option(BUILD_TESTING "Build tests" ON) option(protobuf_VERBOSE "Enable for verbose output" OFF)
option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF) option(protobuf_BUILD_TESTS "Build tests" ON)
if (BUILD_SHARED_LIBS)
set(protobuf_BUILD_SHARED_LIBS_DEFAULT ON)
else (BUILD_SHARED_LIBS)
set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF)
endif (BUILD_SHARED_LIBS)
option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" ${protobuf_BUILD_SHARED_LIBS_DEFAULT})
option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON)
if (MSVC) if (MSVC)
option(ZLIB "Build with zlib support" OFF) set(protobuf_WITH_ZLIB_DEFAULT OFF)
else (MSVC)
set(protobuf_WITH_ZLIB_DEFAULT ON)
endif (MSVC) endif (MSVC)
option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT})
set(protobuf_DEBUG_POSTFIX "d"
CACHE STRING "Default debug postfix")
# Path to main configure script # Path to main configure script
set(protobuf_CONFIGURE_SCRIPT "../configure.ac") set(protobuf_CONFIGURE_SCRIPT "../configure.ac")
# Parse version from configure script # Parse configure script
file(STRINGS "${protobuf_CONFIGURE_SCRIPT}" protobuf_VERSION_LINE set(protobuf_AC_INIT_REGEX
LIMIT_COUNT 1 "^AC_INIT\\(\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\],\\[([^]]+)\\]\\)$")
REGEX "^AC_INIT") file(STRINGS "${protobuf_CONFIGURE_SCRIPT}" protobuf_AC_INIT_LINE
# Replace special characters LIMIT_COUNT 1 REGEX "^AC_INIT")
string(REPLACE "(" "_" protobuf_VERSION_LINE ${protobuf_VERSION_LINE}) # Description
string(REPLACE ")" "_" protobuf_VERSION_LINE ${protobuf_VERSION_LINE}) string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\1"
string(REPLACE "[" "_" protobuf_VERSION_LINE ${protobuf_VERSION_LINE}) protobuf_DESCRIPTION "${protobuf_AC_INIT_LINE}")
string(REPLACE "]" "_" protobuf_VERSION_LINE ${protobuf_VERSION_LINE}) # Version
# Parse version string string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\2"
string(REGEX REPLACE "^AC_INIT__Protocol Buffers_,_([^_]+).*$" "\\1" protobuf_VERSION_STRING "${protobuf_AC_INIT_LINE}")
protobuf_VERSION_STRING "${protobuf_VERSION_LINE}") # Contact
string(REGEX REPLACE "${protobuf_AC_INIT_REGEX}" "\\3"
protobuf_CONTACT "${protobuf_AC_INIT_LINE}")
# Parse version tweaks # Parse version tweaks
string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+).*$" "\\1" set(protobuf_VERSION_REGEX "^([0-9]+)\\.([0-9]+)\\.([0-9]+).*$")
string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\1"
protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}") protobuf_VERSION_MAJOR "${protobuf_VERSION_STRING}")
string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+).*$" "\\2" string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\2"
protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}") protobuf_VERSION_MINOR "${protobuf_VERSION_STRING}")
string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+).*$" "\\3" string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\3"
protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}") protobuf_VERSION_PATCH "${protobuf_VERSION_STRING}")
# Package version # Package version
set(protobuf_VERSION set(protobuf_VERSION
"${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}") "${protobuf_VERSION_MAJOR}.${protobuf_VERSION_MINOR}.${protobuf_VERSION_PATCH}")
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()
add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD) add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
@ -44,18 +70,17 @@ if (CMAKE_USE_PTHREADS_INIT)
add_definitions(-DHAVE_PTHREAD) add_definitions(-DHAVE_PTHREAD)
endif (CMAKE_USE_PTHREADS_INIT) endif (CMAKE_USE_PTHREADS_INIT)
if (MSVC) if (protobuf_WITH_ZLIB)
if (ZLIB)
set(HAVE_ZLIB 1)
find_path(ZLIB_INCLUDE_DIRECTORIES zlib.h ${protobuf_SOURCE_DIR})
find_library(ZLIB_LIBRARIES zdll ${protobuf_SOURCE_DIR})
else (ZLIB)
set(HAVE_ZLIB 0)
endif (ZLIB)
else (MSVC)
find_package(ZLIB) find_package(ZLIB)
if (ZLIB_FOUND) if (ZLIB_FOUND)
set(HAVE_ZLIB 1) set(HAVE_ZLIB 1)
# 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)
else (ZLIB_FOUND) else (ZLIB_FOUND)
set(HAVE_ZLIB 0) set(HAVE_ZLIB 0)
# Explicitly set these to empty (override NOT_FOUND) so cmake doesn't # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't
@ -63,21 +88,22 @@ else (MSVC)
set(ZLIB_INCLUDE_DIRECTORIES) set(ZLIB_INCLUDE_DIRECTORIES)
set(ZLIB_LIBRARIES) set(ZLIB_LIBRARIES)
endif (ZLIB_FOUND) endif (ZLIB_FOUND)
endif (MSVC) endif (protobuf_WITH_ZLIB)
if (HAVE_ZLIB) if (HAVE_ZLIB)
add_definitions(-DHAVE_ZLIB) add_definitions(-DHAVE_ZLIB)
endif (HAVE_ZLIB) endif (HAVE_ZLIB)
if (MSVC) if (protobuf_BUILD_SHARED_LIBS)
if (BUILD_SHARED_LIBS) set(protobuf_SHARED_OR_STATIC "SHARED")
add_definitions(-DPROTOBUF_USE_DLLS) else (protobuf_BUILD_SHARED_LIBS)
else (BUILD_SHARED_LIBS) set(protobuf_SHARED_OR_STATIC "STATIC")
# In case we are building static libraries, link also the runtime library statically # In case we are building static libraries, link also the runtime library statically
# so that MSVCR*.DLL is not required at runtime. # so that MSVCR*.DLL is not required at runtime.
# https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
# This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd # 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 # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
if (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
foreach(flag_var foreach(flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
@ -85,11 +111,11 @@ if (MSVC)
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif(${flag_var} MATCHES "/MD") endif(${flag_var} MATCHES "/MD")
endforeach(flag_var) endforeach(flag_var)
endif (BUILD_SHARED_LIBS) endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
add_definitions(/wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146 /wd4305) endif (protobuf_BUILD_SHARED_LIBS)
endif (MSVC)
if (MSVC) if (MSVC)
add_definitions(/wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146 /wd4305)
string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR}) string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR})
string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR}) string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR})
configure_file(extract_includes.bat.in extract_includes.bat) configure_file(extract_includes.bat.in extract_includes.bat)
@ -116,8 +142,8 @@ include(libprotobuf.cmake)
include(libprotoc.cmake) include(libprotoc.cmake)
include(protoc.cmake) include(protoc.cmake)
if (BUILD_TESTING) if (protobuf_BUILD_TESTS)
include(tests.cmake) include(tests.cmake)
endif (BUILD_TESTING) endif (protobuf_BUILD_TESTS)
include(install.cmake) include(install.cmake)

@ -24,9 +24,15 @@ set(libprotobuf_lite_files
${protobuf_source_dir}/src/google/protobuf/wire_format_lite.cc ${protobuf_source_dir}/src/google/protobuf/wire_format_lite.cc
) )
add_library(libprotobuf-lite ${libprotobuf_lite_files}) add_library(libprotobuf-lite ${protobuf_SHARED_OR_STATIC}
${libprotobuf_lite_files})
target_link_libraries(libprotobuf-lite ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(libprotobuf-lite ${CMAKE_THREAD_LIBS_INIT})
target_include_directories(libprotobuf-lite PUBLIC ${protobuf_source_dir}/src) target_include_directories(libprotobuf-lite PUBLIC ${protobuf_source_dir}/src)
if(MSVC AND protobuf_BUILD_SHARED_LIBS)
target_compile_definitions(libprotobuf-lite
PUBLIC PROTOBUF_USE_DLLS
PRIVATE LIBPROTOBUF_EXPORTS)
endif()
set_target_properties(libprotobuf-lite PROPERTIES set_target_properties(libprotobuf-lite PROPERTIES
COMPILE_DEFINITIONS LIBPROTOBUF_EXPORTS OUTPUT_NAME ${LIB_PREFIX}protobuf-lite
OUTPUT_NAME ${LIB_PREFIX}protobuf-lite) DEBUG_POSTFIX "${protobuf_DEBUG_POSTFIX}")

@ -53,9 +53,15 @@ set(libprotobuf_files
${protobuf_source_dir}/src/google/protobuf/wrappers.pb.cc ${protobuf_source_dir}/src/google/protobuf/wrappers.pb.cc
) )
add_library(libprotobuf ${libprotobuf_lite_files} ${libprotobuf_files}) add_library(libprotobuf ${protobuf_SHARED_OR_STATIC}
${libprotobuf_lite_files} ${libprotobuf_files})
target_link_libraries(libprotobuf ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES}) target_link_libraries(libprotobuf ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES})
target_include_directories(libprotobuf PUBLIC ${protobuf_source_dir}/src) target_include_directories(libprotobuf PUBLIC ${protobuf_source_dir}/src)
if(MSVC AND protobuf_BUILD_SHARED_LIBS)
target_compile_definitions(libprotobuf
PUBLIC PROTOBUF_USE_DLLS
PRIVATE LIBPROTOBUF_EXPORTS)
endif()
set_target_properties(libprotobuf PROPERTIES set_target_properties(libprotobuf PROPERTIES
COMPILE_DEFINITIONS LIBPROTOBUF_EXPORTS OUTPUT_NAME ${LIB_PREFIX}protobuf
OUTPUT_NAME ${LIB_PREFIX}protobuf) DEBUG_POSTFIX "${protobuf_DEBUG_POSTFIX}")

@ -90,8 +90,15 @@ set(libprotoc_files
${protobuf_source_dir}/src/google/protobuf/compiler/zip_writer.cc ${protobuf_source_dir}/src/google/protobuf/compiler/zip_writer.cc
) )
add_library(libprotoc ${libprotoc_files}) add_library(libprotoc ${protobuf_SHARED_OR_STATIC}
${libprotoc_files})
target_link_libraries(libprotoc libprotobuf) target_link_libraries(libprotoc libprotobuf)
if(MSVC AND protobuf_BUILD_SHARED_LIBS)
target_compile_definitions(libprotoc
PUBLIC PROTOBUF_USE_DLLS
PRIVATE LIBPROTOC_EXPORTS)
endif()
set_target_properties(libprotoc PROPERTIES set_target_properties(libprotoc PROPERTIES
COMPILE_DEFINITIONS LIBPROTOC_EXPORTS COMPILE_DEFINITIONS LIBPROTOC_EXPORTS
OUTPUT_NAME ${LIB_PREFIX}protoc) OUTPUT_NAME ${LIB_PREFIX}protoc
DEBUG_POSTFIX "${protobuf_DEBUG_POSTFIX}")

@ -2,6 +2,9 @@ if (NOT EXISTS "${PROJECT_SOURCE_DIR}/../gmock/CMakeLists.txt")
message(FATAL_ERROR "Cannot find gmock directory.") message(FATAL_ERROR "Cannot find gmock directory.")
endif() endif()
option(protobuf_ABSOLUTE_TEST_PLUGIN_PATH
"Using absolute test_plugin path in tests" ON)
include_directories( include_directories(
${protobuf_source_dir}/gmock ${protobuf_source_dir}/gmock
${protobuf_source_dir}/gmock/gtest ${protobuf_source_dir}/gmock/gtest
@ -96,6 +99,8 @@ set(common_test_files
${protobuf_source_dir}/src/google/protobuf/test_util.cc ${protobuf_source_dir}/src/google/protobuf/test_util.cc
${protobuf_source_dir}/src/google/protobuf/testing/file.cc ${protobuf_source_dir}/src/google/protobuf/testing/file.cc
${protobuf_source_dir}/src/google/protobuf/testing/googletest.cc ${protobuf_source_dir}/src/google/protobuf/testing/googletest.cc
${protobuf_source_dir}/src/google/protobuf/compiler/mock_code_generator.cc
${protobuf_source_dir}/src/google/protobuf/util/internal/type_info_test_helper.cc
) )
set(common_lite_test_files set(common_lite_test_files
@ -116,7 +121,6 @@ set(tests_files
${protobuf_source_dir}/src/google/protobuf/compiler/importer_unittest.cc ${protobuf_source_dir}/src/google/protobuf/compiler/importer_unittest.cc
${protobuf_source_dir}/src/google/protobuf/compiler/java/java_doc_comment_unittest.cc ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_doc_comment_unittest.cc
${protobuf_source_dir}/src/google/protobuf/compiler/java/java_plugin_unittest.cc ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_plugin_unittest.cc
${protobuf_source_dir}/src/google/protobuf/compiler/mock_code_generator.cc
${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_helpers_unittest.cc ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_helpers_unittest.cc
${protobuf_source_dir}/src/google/protobuf/compiler/parser_unittest.cc ${protobuf_source_dir}/src/google/protobuf/compiler/parser_unittest.cc
${protobuf_source_dir}/src/google/protobuf/compiler/python/python_plugin_unittest.cc ${protobuf_source_dir}/src/google/protobuf/compiler/python/python_plugin_unittest.cc
@ -162,7 +166,6 @@ set(tests_files
${protobuf_source_dir}/src/google/protobuf/util/internal/json_stream_parser_test.cc ${protobuf_source_dir}/src/google/protobuf/util/internal/json_stream_parser_test.cc
${protobuf_source_dir}/src/google/protobuf/util/internal/protostream_objectsource_test.cc ${protobuf_source_dir}/src/google/protobuf/util/internal/protostream_objectsource_test.cc
${protobuf_source_dir}/src/google/protobuf/util/internal/protostream_objectwriter_test.cc ${protobuf_source_dir}/src/google/protobuf/util/internal/protostream_objectwriter_test.cc
${protobuf_source_dir}/src/google/protobuf/util/internal/type_info_test_helper.cc
${protobuf_source_dir}/src/google/protobuf/util/json_util_test.cc ${protobuf_source_dir}/src/google/protobuf/util/json_util_test.cc
${protobuf_source_dir}/src/google/protobuf/util/time_util_test.cc ${protobuf_source_dir}/src/google/protobuf/util/time_util_test.cc
${protobuf_source_dir}/src/google/protobuf/util/type_resolver_util_test.cc ${protobuf_source_dir}/src/google/protobuf/util/type_resolver_util_test.cc
@ -170,6 +173,10 @@ set(tests_files
${protobuf_source_dir}/src/google/protobuf/wire_format_unittest.cc ${protobuf_source_dir}/src/google/protobuf/wire_format_unittest.cc
) )
if(protobuf_ABSOLUTE_TEST_PLUGIN_PATH)
add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$<TARGET_FILE:test_plugin>")
endif()
add_executable(tests ${tests_files} ${common_test_files} ${tests_proto_files} ${lite_test_proto_files}) add_executable(tests ${tests_files} ${common_test_files} ${tests_proto_files} ${lite_test_proto_files})
target_link_libraries(tests libprotoc libprotobuf gmock_main) target_link_libraries(tests libprotoc libprotobuf gmock_main)
@ -194,3 +201,7 @@ set(lite_arena_test_files
) )
add_executable(lite-arena-test ${lite_arena_test_files} ${common_lite_test_files} ${lite_test_proto_files}) add_executable(lite-arena-test ${lite_arena_test_files} ${common_lite_test_files} ${lite_test_proto_files})
target_link_libraries(lite-arena-test libprotobuf-lite gmock_main) target_link_libraries(lite-arena-test libprotobuf-lite gmock_main)
add_custom_target(check
COMMAND tests
WORKING_DIRECTORY ${protobuf_source_dir})

@ -294,6 +294,10 @@ void CommandLineInterfaceTest::Run(const string& command) {
if (!disallow_plugins_) { if (!disallow_plugins_) {
cli_.AllowPlugins("prefix-"); cli_.AllowPlugins("prefix-");
#ifndef GOOGLE_THIRD_PARTY_PROTOBUF #ifndef GOOGLE_THIRD_PARTY_PROTOBUF
string plugin_path;
#ifdef GOOGLE_PROTOBUF_TEST_PLUGIN_PATH
plugin_path = GOOGLE_PROTOBUF_TEST_PLUGIN_PATH;
#else
const char* possible_paths[] = { const char* possible_paths[] = {
// When building with shared libraries, libtool hides the real executable // When building with shared libraries, libtool hides the real executable
// in .libs and puts a fake wrapper in the current directory. // in .libs and puts a fake wrapper in the current directory.
@ -311,15 +315,13 @@ void CommandLineInterfaceTest::Run(const string& command) {
"test_plugin.exe", // Other Win32 (MSVC) "test_plugin.exe", // Other Win32 (MSVC)
"test_plugin", // Unix "test_plugin", // Unix
}; };
string plugin_path;
for (int i = 0; i < GOOGLE_ARRAYSIZE(possible_paths); i++) { for (int i = 0; i < GOOGLE_ARRAYSIZE(possible_paths); i++) {
if (access(possible_paths[i], F_OK) == 0) { if (access(possible_paths[i], F_OK) == 0) {
plugin_path = possible_paths[i]; plugin_path = possible_paths[i];
break; break;
} }
} }
#endif
if (plugin_path.empty()) { if (plugin_path.empty()) {
#else #else

Loading…
Cancel
Save