Skip to content

Commit

Permalink
Improved SHARED build from CMake project
Browse files Browse the repository at this point in the history
  • Loading branch information
podsvirov committed Oct 14, 2015
1 parent f397ede commit c3aa4c2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions appveyor.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,4 +26,4 @@ goto :EOF

:error
echo Failed!
EXIT /b %ERRORLEVEL%
EXIT /b %ERRORLEVEL%
48 changes: 26 additions & 22 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions cmake/libprotobuf-lite.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
9 changes: 7 additions & 2 deletions cmake/libprotobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
8 changes: 7 additions & 1 deletion cmake/libprotoc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c3aa4c2

Please sign in to comment.