Browse Source

Improved SHARED build from CMake project

Konstantin Podsvirov 10 years ago
parent
commit
c3aa4c2675
6 changed files with 50 additions and 29 deletions
  1. 1 0
      .gitignore
  2. 2 2
      appveyor.bat
  3. 26 22
      cmake/CMakeLists.txt
  4. 7 2
      cmake/libprotobuf-lite.cmake
  5. 7 2
      cmake/libprotobuf.cmake
  6. 7 1
      cmake/libprotoc.cmake

+ 1 - 0
.gitignore

@@ -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.

+ 2 - 2
appveyor.bat

@@ -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%

+ 26 - 22
cmake/CMakeLists.txt

@@ -10,7 +10,12 @@ cmake_policy(SET CMP0022 NEW)
 # Options
 # Options
 option(protobuf_VERBOSE "Enable for verbose output" OFF)
 option(protobuf_VERBOSE "Enable for verbose output" OFF)
 option(protobuf_BUILD_TESTS "Build tests" ON)
 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)
 option(protobuf_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON)
 if (MSVC)
 if (MSVC)
   set(protobuf_WITH_ZLIB_DEFAULT OFF)
   set(protobuf_WITH_ZLIB_DEFAULT OFF)
@@ -89,29 +94,28 @@ if (HAVE_ZLIB)
   add_definitions(-DHAVE_ZLIB)
   add_definitions(-DHAVE_ZLIB)
 endif (HAVE_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)
 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)

+ 7 - 2
cmake/libprotobuf-lite.cmake

@@ -24,10 +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}")
     DEBUG_POSTFIX "${protobuf_DEBUG_POSTFIX}")

+ 7 - 2
cmake/libprotobuf.cmake

@@ -53,10 +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}")
     DEBUG_POSTFIX "${protobuf_DEBUG_POSTFIX}")

+ 7 - 1
cmake/libprotoc.cmake

@@ -89,8 +89,14 @@ 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