From c3aa4c2675f993f3e6601547f03d76fd93183628 Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Thu, 15 Oct 2015 02:56:48 +0300 Subject: 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 a2cb75fd..46487128 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 356a13f8..9a46b928 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 cf891bf5..9de6341a 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 e767e8f8..036b0517 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 bf5f5e25..a6ee8f2e 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 497861ff..7424ff02 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 -- cgit v1.2.3