From db014600427ad3618625d20f4ad6b5a2e3b7bdd2 Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Mon, 31 Aug 2015 15:20:18 +0300 Subject: [PATCH 01/12] Improved configure.ac parsing --- cmake/CMakeLists.txt | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 33e069fe6..aa76a16a1 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -5,6 +5,7 @@ cmake_minimum_required(VERSION 2.8) project(protobuf C CXX) # Options +option(protobuf_VERBOSE "Enable for verbose output" OFF) option(BUILD_TESTING "Build tests" ON) option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF) if (MSVC) @@ -14,29 +15,40 @@ endif (MSVC) # Path to main configure script set(protobuf_CONFIGURE_SCRIPT "../configure.ac") -# Parse version from configure script -file(STRINGS "${protobuf_CONFIGURE_SCRIPT}" protobuf_VERSION_LINE - LIMIT_COUNT 1 - REGEX "^AC_INIT") -# Replace special characters -string(REPLACE "(" "_" protobuf_VERSION_LINE ${protobuf_VERSION_LINE}) -string(REPLACE ")" "_" protobuf_VERSION_LINE ${protobuf_VERSION_LINE}) -string(REPLACE "[" "_" protobuf_VERSION_LINE ${protobuf_VERSION_LINE}) -string(REPLACE "]" "_" protobuf_VERSION_LINE ${protobuf_VERSION_LINE}) -# Parse version string -string(REGEX REPLACE "^AC_INIT__Protocol Buffers_,_([^_]+).*$" "\\1" - protobuf_VERSION_STRING "${protobuf_VERSION_LINE}") +# 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}") # 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}") -string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+).*$" "\\2" +string(REGEX REPLACE "${protobuf_VERSION_REGEX}" "\\2" 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}") # Package version set(protobuf_VERSION "${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) find_package(Threads REQUIRED) From 20b882d4692c6ffd15e0f7d2795a952b2b182ebe Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Mon, 31 Aug 2015 16:23:40 +0300 Subject: [PATCH 02/12] Using find_package(ZLIB) with MSVC too --- cmake/CMakeLists.txt | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index aa76a16a1..31a222d19 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -9,8 +9,11 @@ option(protobuf_VERBOSE "Enable for verbose output" OFF) option(BUILD_TESTING "Build tests" ON) option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF) if (MSVC) - option(ZLIB "Build with zlib support" OFF) + set(ZLIB_DEFAULT OFF) +else (MSVC) + set(ZLIB_DEFAULT ON) endif (MSVC) +option(ZLIB "Build with zlib support" ${ZLIB_DEFAULT}) # Path to main configure script set(protobuf_CONFIGURE_SCRIPT "../configure.ac") @@ -56,18 +59,17 @@ if (CMAKE_USE_PTHREADS_INIT) add_definitions(-DHAVE_PTHREAD) endif (CMAKE_USE_PTHREADS_INIT) -if (MSVC) - 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) +if (ZLIB) find_package(ZLIB) if (ZLIB_FOUND) 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) set(HAVE_ZLIB 0) # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't @@ -75,7 +77,7 @@ else (MSVC) set(ZLIB_INCLUDE_DIRECTORIES) set(ZLIB_LIBRARIES) endif (ZLIB_FOUND) -endif (MSVC) +endif (ZLIB) if (HAVE_ZLIB) add_definitions(-DHAVE_ZLIB) @@ -86,7 +88,7 @@ if (MSVC) add_definitions(-DPROTOBUF_USE_DLLS) else (BUILD_SHARED_LIBS) # 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 # 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 From d79e0379f2a600fa22d007e3a74149c6c541a09a Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Tue, 1 Sep 2015 15:00:00 +0300 Subject: [PATCH 03/12] Option for switching static runtime link policy with MSVC --- cmake/CMakeLists.txt | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 31a222d19..a9acf7e6f 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -8,6 +8,7 @@ project(protobuf C CXX) option(protobuf_VERBOSE "Enable for verbose output" OFF) option(BUILD_TESTING "Build tests" ON) option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF) +option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON) if (MSVC) set(ZLIB_DEFAULT OFF) else (MSVC) @@ -92,13 +93,15 @@ if (MSVC) # 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 - 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) + 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) endif (BUILD_SHARED_LIBS) add_definitions(/wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146 /wd4305) endif (MSVC) From 905f46403582ca2c43e1ad249dd01fa97191c4d0 Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Tue, 1 Sep 2015 16:44:48 +0300 Subject: [PATCH 04/12] Using NEW behavior for CMP0022 in CMake project --- cmake/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index a9acf7e6f..6a4ec2f40 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -4,6 +4,9 @@ cmake_minimum_required(VERSION 2.8) # Project project(protobuf C CXX) +# CMake policies +cmake_policy(SET CMP0022 NEW) + # Options option(protobuf_VERBOSE "Enable for verbose output" OFF) option(BUILD_TESTING "Build tests" ON) From 2fa0439b0074c61d48fa6120d0734ae8c1e5debb Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Tue, 15 Sep 2015 15:01:05 +0300 Subject: [PATCH 05/12] Improved testing from CMake project --- cmake/tests.cmake | 16 +++++++++++++--- .../compiler/command_line_interface_unittest.cc | 4 ++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cmake/tests.cmake b/cmake/tests.cmake index 65feca21c..46f631a82 100644 --- a/cmake/tests.cmake +++ b/cmake/tests.cmake @@ -96,6 +96,8 @@ set(common_test_files ${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/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 @@ -116,7 +118,6 @@ set(tests_files ${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_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/parser_unittest.cc ${protobuf_source_dir}/src/google/protobuf/compiler/python/python_plugin_unittest.cc @@ -162,7 +163,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/protostream_objectsource_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/time_util_test.cc ${protobuf_source_dir}/src/google/protobuf/util/type_resolver_util_test.cc @@ -179,7 +179,6 @@ set(test_plugin_files ${protobuf_source_dir}/src/google/protobuf/testing/file.h ${protobuf_source_dir}/src/google/protobuf/compiler/test_plugin.cc ) - add_executable(test_plugin ${test_plugin_files}) target_link_libraries(test_plugin libprotoc libprotobuf gmock) @@ -194,3 +193,14 @@ set(lite_arena_test_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) + +enable_testing() +add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$") +add_library(libtests STATIC ${common_test_files} ${tests_proto_files} ${lite_test_proto_files}) +foreach(file ${tests_files}) + get_filename_component(name ${file} NAME_WE) + add_executable(${name} ${file}) + target_link_libraries(${name} libtests libprotoc libprotobuf gmock_main) + add_test(NAME ${name} COMMAND ${name} + WORKING_DIRECTORY ${protobuf_source_dir}) +endforeach() diff --git a/src/google/protobuf/compiler/command_line_interface_unittest.cc b/src/google/protobuf/compiler/command_line_interface_unittest.cc index 9560d0e0b..3935de51b 100644 --- a/src/google/protobuf/compiler/command_line_interface_unittest.cc +++ b/src/google/protobuf/compiler/command_line_interface_unittest.cc @@ -314,12 +314,16 @@ void CommandLineInterfaceTest::Run(const string& command) { string plugin_path; +#ifdef GOOGLE_PROTOBUF_TEST_PLUGIN_PATH + plugin_path = GOOGLE_PROTOBUF_TEST_PLUGIN_PATH; +#else for (int i = 0; i < GOOGLE_ARRAYSIZE(possible_paths); i++) { if (access(possible_paths[i], F_OK) == 0) { plugin_path = possible_paths[i]; break; } } +#endif if (plugin_path.empty()) { #else From 673d32e0930bd87536da71b3bf1d0c6c3d098833 Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Tue, 15 Sep 2015 15:30:02 +0300 Subject: [PATCH 06/12] Rename CMake option BUILD_TESTING to protobuf_BUILD_TESTS --- cmake/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 6a4ec2f40..50e60f34a 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -9,7 +9,7 @@ cmake_policy(SET CMP0022 NEW) # Options option(protobuf_VERBOSE "Enable for verbose output" OFF) -option(BUILD_TESTING "Build tests" ON) +option(protobuf_BUILD_TESTS "Build tests" ON) option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF) option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON) if (MSVC) @@ -136,8 +136,8 @@ include(libprotobuf.cmake) include(libprotoc.cmake) include(protoc.cmake) -if (BUILD_TESTING) +if (protobuf_BUILD_TESTS) include(tests.cmake) -endif (BUILD_TESTING) +endif (protobuf_BUILD_TESTS) include(install.cmake) From 620bd7418d5ae0ac275e04ab216449a64e2cdc9d Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Tue, 15 Sep 2015 15:31:25 +0300 Subject: [PATCH 07/12] Rename CMake option ZLIB to protobuf_WITH_ZLIB --- cmake/CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 50e60f34a..474bfd0f7 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -13,11 +13,11 @@ option(protobuf_BUILD_TESTS "Build tests" ON) option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF) option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON) if (MSVC) - set(ZLIB_DEFAULT OFF) + set(protobuf_WITH_ZLIB_DEFAULT OFF) else (MSVC) - set(ZLIB_DEFAULT ON) + set(protobuf_WITH_ZLIB_DEFAULT ON) endif (MSVC) -option(ZLIB "Build with zlib support" ${ZLIB_DEFAULT}) +option(protobuf_WITH_ZLIB "Build with zlib support" ${protobuf_WITH_ZLIB_DEFAULT}) # Path to main configure script set(protobuf_CONFIGURE_SCRIPT "../configure.ac") @@ -63,7 +63,7 @@ if (CMAKE_USE_PTHREADS_INIT) add_definitions(-DHAVE_PTHREAD) endif (CMAKE_USE_PTHREADS_INIT) -if (ZLIB) +if (protobuf_WITH_ZLIB) find_package(ZLIB) if (ZLIB_FOUND) set(HAVE_ZLIB 1) @@ -81,7 +81,7 @@ if (ZLIB) set(ZLIB_INCLUDE_DIRECTORIES) set(ZLIB_LIBRARIES) endif (ZLIB_FOUND) -endif (ZLIB) +endif (protobuf_WITH_ZLIB) if (HAVE_ZLIB) add_definitions(-DHAVE_ZLIB) From 0a22fe59b7538bee74daa3c18eddd9d768d3d01e Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Tue, 15 Sep 2015 15:36:20 +0300 Subject: [PATCH 08/12] Rename CMake option BUILD_SHARED_LIBS to protobuf_BUILD_SHARED_LIBS --- cmake/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 474bfd0f7..0129401f0 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -10,7 +10,7 @@ cmake_policy(SET CMP0022 NEW) # Options option(protobuf_VERBOSE "Enable for verbose output" OFF) option(protobuf_BUILD_TESTS "Build tests" ON) -option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF) +option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" OFF) option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON) if (MSVC) set(protobuf_WITH_ZLIB_DEFAULT OFF) @@ -88,9 +88,9 @@ if (HAVE_ZLIB) endif (HAVE_ZLIB) if (MSVC) - if (BUILD_SHARED_LIBS) + if (protobuf_BUILD_SHARED_LIBS) add_definitions(-DPROTOBUF_USE_DLLS) - else (BUILD_SHARED_LIBS) + else (protobuf_BUILD_SHARED_LIBS) # In case we are building static libraries, link also the runtime library statically # so that MSVCR*.DLL is not required at runtime. # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx @@ -105,7 +105,7 @@ if (MSVC) endif(${flag_var} MATCHES "/MD") endforeach(flag_var) endif (protobuf_MSVC_STATIC_RUNTIME) - endif (BUILD_SHARED_LIBS) + endif (protobuf_BUILD_SHARED_LIBS) add_definitions(/wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146 /wd4305) endif (MSVC) From d41601c249ff377e19d2bc76521cebe96e73a610 Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Wed, 16 Sep 2015 12:55:35 +0300 Subject: [PATCH 09/12] Improved testing from CMake project Build tests optimization Now lite-test added to CTest collection --- cmake/tests.cmake | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cmake/tests.cmake b/cmake/tests.cmake index 46f631a82..ffa10034d 100644 --- a/cmake/tests.cmake +++ b/cmake/tests.cmake @@ -170,8 +170,7 @@ set(tests_files ${protobuf_source_dir}/src/google/protobuf/wire_format_unittest.cc ) -add_executable(tests ${tests_files} ${common_test_files} ${tests_proto_files} ${lite_test_proto_files}) -target_link_libraries(tests libprotoc libprotobuf gmock_main) +enable_testing() set(test_plugin_files ${protobuf_source_dir}/src/google/protobuf/compiler/mock_code_generator.cc @@ -182,11 +181,15 @@ set(test_plugin_files add_executable(test_plugin ${test_plugin_files}) target_link_libraries(test_plugin libprotoc libprotobuf gmock) +add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$") + set(lite_test_files ${protobuf_source_dir}/src/google/protobuf/lite_unittest.cc ) add_executable(lite-test ${lite_test_files} ${common_lite_test_files} ${lite_test_proto_files}) target_link_libraries(lite-test libprotobuf-lite) +add_test(NAME lite-test COMMAND lite-test + WORKING_DIRECTORY ${protobuf_source_dir}) set(lite_arena_test_files ${protobuf_source_dir}/src/google/protobuf/lite_arena_unittest.cc @@ -194,9 +197,13 @@ set(lite_arena_test_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) -enable_testing() -add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$") add_library(libtests STATIC ${common_test_files} ${tests_proto_files} ${lite_test_proto_files}) + +# Native single tests +add_executable(tests ${tests_files}) +target_link_libraries(tests libtests libprotoc libprotobuf gmock_main) + +# Sparated tests for CTest foreach(file ${tests_files}) get_filename_component(name ${file} NAME_WE) add_executable(${name} ${file}) From e301946ca460b6ad971798a9b69ed66681498114 Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Thu, 17 Sep 2015 12:08:47 +0300 Subject: [PATCH 10/12] Simplify testing from CMake project to "check" target --- cmake/tests.cmake | 32 ++++++++----------- .../command_line_interface_unittest.cc | 10 +++--- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/cmake/tests.cmake b/cmake/tests.cmake index ffa10034d..06d98860b 100644 --- a/cmake/tests.cmake +++ b/cmake/tests.cmake @@ -2,6 +2,9 @@ if (NOT EXISTS "${PROJECT_SOURCE_DIR}/../gmock/CMakeLists.txt") message(FATAL_ERROR "Cannot find gmock directory.") endif() +option(protobuf_ABSOLUTE_TEST_PLUGIN_PATH + "Using absolute test_plugin path in tests" ON) + include_directories( ${protobuf_source_dir}/gmock ${protobuf_source_dir}/gmock/gtest @@ -170,7 +173,12 @@ set(tests_files ${protobuf_source_dir}/src/google/protobuf/wire_format_unittest.cc ) -enable_testing() +if(protobuf_ABSOLUTE_TEST_PLUGIN_PATH) + add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$") +endif() + +add_executable(tests ${tests_files} ${common_test_files} ${tests_proto_files} ${lite_test_proto_files}) +target_link_libraries(tests libprotoc libprotobuf gmock_main) set(test_plugin_files ${protobuf_source_dir}/src/google/protobuf/compiler/mock_code_generator.cc @@ -178,18 +186,15 @@ set(test_plugin_files ${protobuf_source_dir}/src/google/protobuf/testing/file.h ${protobuf_source_dir}/src/google/protobuf/compiler/test_plugin.cc ) + add_executable(test_plugin ${test_plugin_files}) target_link_libraries(test_plugin libprotoc libprotobuf gmock) -add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$") - set(lite_test_files ${protobuf_source_dir}/src/google/protobuf/lite_unittest.cc ) add_executable(lite-test ${lite_test_files} ${common_lite_test_files} ${lite_test_proto_files}) target_link_libraries(lite-test libprotobuf-lite) -add_test(NAME lite-test COMMAND lite-test - WORKING_DIRECTORY ${protobuf_source_dir}) set(lite_arena_test_files ${protobuf_source_dir}/src/google/protobuf/lite_arena_unittest.cc @@ -197,17 +202,6 @@ set(lite_arena_test_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) -add_library(libtests STATIC ${common_test_files} ${tests_proto_files} ${lite_test_proto_files}) - -# Native single tests -add_executable(tests ${tests_files}) -target_link_libraries(tests libtests libprotoc libprotobuf gmock_main) - -# Sparated tests for CTest -foreach(file ${tests_files}) - get_filename_component(name ${file} NAME_WE) - add_executable(${name} ${file}) - target_link_libraries(${name} libtests libprotoc libprotobuf gmock_main) - add_test(NAME ${name} COMMAND ${name} - WORKING_DIRECTORY ${protobuf_source_dir}) -endforeach() +add_custom_target(check + COMMAND tests + WORKING_DIRECTORY ${protobuf_source_dir}) diff --git a/src/google/protobuf/compiler/command_line_interface_unittest.cc b/src/google/protobuf/compiler/command_line_interface_unittest.cc index 3935de51b..18102f4dd 100644 --- a/src/google/protobuf/compiler/command_line_interface_unittest.cc +++ b/src/google/protobuf/compiler/command_line_interface_unittest.cc @@ -294,6 +294,10 @@ void CommandLineInterfaceTest::Run(const string& command) { if (!disallow_plugins_) { cli_.AllowPlugins("prefix-"); #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[] = { // When building with shared libraries, libtool hides the real executable // in .libs and puts a fake wrapper in the current directory. @@ -311,12 +315,6 @@ void CommandLineInterfaceTest::Run(const string& command) { "test_plugin.exe", // Other Win32 (MSVC) "test_plugin", // Unix }; - - string plugin_path; - -#ifdef GOOGLE_PROTOBUF_TEST_PLUGIN_PATH - plugin_path = GOOGLE_PROTOBUF_TEST_PLUGIN_PATH; -#else for (int i = 0; i < GOOGLE_ARRAYSIZE(possible_paths); i++) { if (access(possible_paths[i], F_OK) == 0) { plugin_path = possible_paths[i]; From f397ede7c6441a780c6dac7eb32a2459635baa9c Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Thu, 17 Sep 2015 13:00:12 +0300 Subject: [PATCH 11/12] Add CMake option protobuf_DEBUG_POSTFIX --- cmake/CMakeLists.txt | 2 ++ cmake/libprotobuf-lite.cmake | 3 ++- cmake/libprotobuf.cmake | 3 ++- cmake/libprotoc.cmake | 3 ++- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 0129401f0..cf891bf5e 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -18,6 +18,8 @@ else (MSVC) set(protobuf_WITH_ZLIB_DEFAULT ON) 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 set(protobuf_CONFIGURE_SCRIPT "../configure.ac") diff --git a/cmake/libprotobuf-lite.cmake b/cmake/libprotobuf-lite.cmake index 9fafc3167..e767e8f82 100644 --- a/cmake/libprotobuf-lite.cmake +++ b/cmake/libprotobuf-lite.cmake @@ -29,4 +29,5 @@ target_link_libraries(libprotobuf-lite ${CMAKE_THREAD_LIBS_INIT}) target_include_directories(libprotobuf-lite PUBLIC ${protobuf_source_dir}/src) 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}") diff --git a/cmake/libprotobuf.cmake b/cmake/libprotobuf.cmake index 64a9cae01..bf5f5e255 100644 --- a/cmake/libprotobuf.cmake +++ b/cmake/libprotobuf.cmake @@ -58,4 +58,5 @@ target_link_libraries(libprotobuf ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES}) target_include_directories(libprotobuf PUBLIC ${protobuf_source_dir}/src) set_target_properties(libprotobuf PROPERTIES COMPILE_DEFINITIONS LIBPROTOBUF_EXPORTS - OUTPUT_NAME ${LIB_PREFIX}protobuf) + OUTPUT_NAME ${LIB_PREFIX}protobuf + DEBUG_POSTFIX "${protobuf_DEBUG_POSTFIX}") diff --git a/cmake/libprotoc.cmake b/cmake/libprotoc.cmake index 7aa92ee19..497861ff3 100644 --- a/cmake/libprotoc.cmake +++ b/cmake/libprotoc.cmake @@ -93,4 +93,5 @@ add_library(libprotoc ${libprotoc_files}) target_link_libraries(libprotoc libprotobuf) set_target_properties(libprotoc PROPERTIES COMPILE_DEFINITIONS LIBPROTOC_EXPORTS - OUTPUT_NAME ${LIB_PREFIX}protoc) + OUTPUT_NAME ${LIB_PREFIX}protoc + DEBUG_POSTFIX "${protobuf_DEBUG_POSTFIX}") From c3aa4c2675f993f3e6601547f03d76fd93183628 Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Thu, 15 Oct 2015 02:56:48 +0300 Subject: [PATCH 12/12] Improved SHARED build from CMake project --- .gitignore | 1 + appveyor.bat | 4 +-- cmake/CMakeLists.txt | 48 +++++++++++++++++++----------------- cmake/libprotobuf-lite.cmake | 9 +++++-- cmake/libprotobuf.cmake | 9 +++++-- cmake/libprotoc.cmake | 8 +++++- 6 files changed, 50 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index a2cb75fdb..46487128c 100644 --- a/.gitignore +++ b/.gitignore @@ -81,6 +81,7 @@ javanano/target # Windows native output. cmake/build +build_msvc # NuGet packages: we want the repository configuration, but not the # packages themselves. diff --git a/appveyor.bat b/appveyor.bat index 356a13f86..9a46b9289 100644 --- a/appveyor.bat +++ b/appveyor.bat @@ -10,7 +10,7 @@ goto :error echo Building C++ mkdir 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 cd %configuration% tests.exe || goto error @@ -26,4 +26,4 @@ goto :EOF :error echo Failed! -EXIT /b %ERRORLEVEL% \ No newline at end of file +EXIT /b %ERRORLEVEL% diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index cf891bf5e..9de6341ac 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -10,7 +10,12 @@ cmake_policy(SET CMP0022 NEW) # Options option(protobuf_VERBOSE "Enable for verbose output" OFF) option(protobuf_BUILD_TESTS "Build tests" ON) -option(protobuf_BUILD_SHARED_LIBS "Build Shared Libraries" OFF) +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) set(protobuf_WITH_ZLIB_DEFAULT OFF) @@ -89,29 +94,28 @@ if (HAVE_ZLIB) add_definitions(-DHAVE_ZLIB) endif (HAVE_ZLIB) -if (MSVC) - if (protobuf_BUILD_SHARED_LIBS) - add_definitions(-DPROTOBUF_USE_DLLS) - else (protobuf_BUILD_SHARED_LIBS) - # In case we are building static libraries, link also the runtime library statically - # so that MSVCR*.DLL is not required at runtime. - # 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 - 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) - endif (protobuf_BUILD_SHARED_LIBS) - add_definitions(/wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146 /wd4305) -endif (MSVC) +if (protobuf_BUILD_SHARED_LIBS) + set(protobuf_SHARED_OR_STATIC "SHARED") +else (protobuf_BUILD_SHARED_LIBS) + set(protobuf_SHARED_OR_STATIC "STATIC") + # In case we are building static libraries, link also the runtime library statically + # so that MSVCR*.DLL is not required at runtime. + # 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 + if (MSVC AND 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 (MSVC AND protobuf_MSVC_STATIC_RUNTIME) +endif (protobuf_BUILD_SHARED_LIBS) 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_BINARY_WIN32_PATH ${protobuf_BINARY_DIR}) configure_file(extract_includes.bat.in extract_includes.bat) diff --git a/cmake/libprotobuf-lite.cmake b/cmake/libprotobuf-lite.cmake index e767e8f82..036b05170 100644 --- a/cmake/libprotobuf-lite.cmake +++ b/cmake/libprotobuf-lite.cmake @@ -24,10 +24,15 @@ set(libprotobuf_lite_files ${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_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 - COMPILE_DEFINITIONS LIBPROTOBUF_EXPORTS OUTPUT_NAME ${LIB_PREFIX}protobuf-lite DEBUG_POSTFIX "${protobuf_DEBUG_POSTFIX}") diff --git a/cmake/libprotobuf.cmake b/cmake/libprotobuf.cmake index bf5f5e255..a6ee8f2eb 100644 --- a/cmake/libprotobuf.cmake +++ b/cmake/libprotobuf.cmake @@ -53,10 +53,15 @@ set(libprotobuf_files ${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_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 - COMPILE_DEFINITIONS LIBPROTOBUF_EXPORTS OUTPUT_NAME ${LIB_PREFIX}protobuf DEBUG_POSTFIX "${protobuf_DEBUG_POSTFIX}") diff --git a/cmake/libprotoc.cmake b/cmake/libprotoc.cmake index 497861ff3..7424ff02f 100644 --- a/cmake/libprotoc.cmake +++ b/cmake/libprotoc.cmake @@ -89,8 +89,14 @@ set(libprotoc_files ${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) +if(MSVC AND protobuf_BUILD_SHARED_LIBS) + target_compile_definitions(libprotoc + PUBLIC PROTOBUF_USE_DLLS + PRIVATE LIBPROTOC_EXPORTS) +endif() set_target_properties(libprotoc PROPERTIES COMPILE_DEFINITIONS LIBPROTOC_EXPORTS OUTPUT_NAME ${LIB_PREFIX}protoc