mirror of
https://github.com/protobuf-c/protobuf-c.git
synced 2024-12-26 21:04:23 +08:00
cmake: use static build by default, fix MSVC build
Matches protobuf default behavior: https://github.com/protocolbuffers/protobuf/tree/master/cmake#dlls-vs-static-linking Bump min cmake version to use Protobuf_USE_STATIC_LIBS: https://cmake.org/cmake/help/latest/module/FindProtobuf.html
This commit is contained in:
parent
18ede005af
commit
e79f7d3ac6
@ -4,12 +4,11 @@ SET(PACKAGE_VERSION 1.3.3)
|
|||||||
SET(PACKAGE_URL https://github.com/protobuf-c/protobuf-c)
|
SET(PACKAGE_URL https://github.com/protobuf-c/protobuf-c)
|
||||||
SET(PACKAGE_DESCRIPTION "Protocol Buffers implementation in C")
|
SET(PACKAGE_DESCRIPTION "Protocol Buffers implementation in C")
|
||||||
|
|
||||||
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
|
CMAKE_MINIMUM_REQUIRED(VERSION 3.10 FATAL_ERROR)
|
||||||
|
|
||||||
PROJECT(protobuf-c)
|
PROJECT(protobuf-c)
|
||||||
|
|
||||||
#options
|
#options
|
||||||
option(MSVC_STATIC_BUILD "MSVC_STATIC_BUILD" OFF)
|
|
||||||
option(BUILD_PROTO3 "BUILD_PROTO3" ON)
|
option(BUILD_PROTO3 "BUILD_PROTO3" ON)
|
||||||
option(BUILD_PROTOC "Build protoc-gen-c" ON)
|
option(BUILD_PROTOC "Build protoc-gen-c" ON)
|
||||||
if(CMAKE_BUILD_TYPE MATCHES Debug)
|
if(CMAKE_BUILD_TYPE MATCHES Debug)
|
||||||
@ -28,6 +27,10 @@ if (${WORDS_BIGENDIAN})
|
|||||||
ADD_DEFINITIONS(-DWORDS_BIGENDIAN)
|
ADD_DEFINITIONS(-DWORDS_BIGENDIAN)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
IF (MSVC AND BUILD_SHARED_LIBS)
|
||||||
|
ADD_DEFINITIONS(-DPROTOBUF_C_USE_SHARED_LIB)
|
||||||
|
ENDIF (MSVC AND BUILD_SHARED_LIBS)
|
||||||
|
|
||||||
if(MSVC)
|
if(MSVC)
|
||||||
# using Visual Studio C++
|
# using Visual Studio C++
|
||||||
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4267 /wd4244")
|
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4267 /wd4244")
|
||||||
@ -45,6 +48,9 @@ SET (PC_SOURCES
|
|||||||
|
|
||||||
ADD_LIBRARY(protobuf-c ${PC_SOURCES})
|
ADD_LIBRARY(protobuf-c ${PC_SOURCES})
|
||||||
set_target_properties(protobuf-c PROPERTIES COMPILE_PDB_NAME protobuf-c)
|
set_target_properties(protobuf-c PROPERTIES COMPILE_PDB_NAME protobuf-c)
|
||||||
|
IF (MSVC AND BUILD_SHARED_LIBS)
|
||||||
|
TARGET_COMPILE_DEFINITIONS(protobuf-c PRIVATE -DPROTOBUF_C_EXPORT)
|
||||||
|
ENDIF (MSVC AND BUILD_SHARED_LIBS)
|
||||||
|
|
||||||
INCLUDE_DIRECTORIES(${MAIN_DIR})
|
INCLUDE_DIRECTORIES(${MAIN_DIR})
|
||||||
INCLUDE_DIRECTORIES(${MAIN_DIR}/protobuf-c)
|
INCLUDE_DIRECTORIES(${MAIN_DIR}/protobuf-c)
|
||||||
@ -52,6 +58,10 @@ INCLUDE_DIRECTORIES(${MAIN_DIR}/protobuf-c)
|
|||||||
IF(BUILD_PROTOC)
|
IF(BUILD_PROTOC)
|
||||||
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR}) # for generated files
|
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR}) # for generated files
|
||||||
|
|
||||||
|
if (MSVC AND NOT BUILD_SHARED_LIBS)
|
||||||
|
SET(Protobuf_USE_STATIC_LIBS ON)
|
||||||
|
endif (MSVC AND NOT BUILD_SHARED_LIBS)
|
||||||
|
|
||||||
FIND_PACKAGE(Protobuf REQUIRED)
|
FIND_PACKAGE(Protobuf REQUIRED)
|
||||||
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
|
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
|
||||||
|
|
||||||
@ -60,7 +70,7 @@ if (BUILD_PROTO3)
|
|||||||
endif()
|
endif()
|
||||||
ENDIF()
|
ENDIF()
|
||||||
|
|
||||||
if (MSVC AND MSVC_STATIC_BUILD)
|
if (MSVC AND NOT BUILD_SHARED_LIBS)
|
||||||
# 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
|
||||||
@ -75,7 +85,7 @@ if (MSVC AND MSVC_STATIC_BUILD)
|
|||||||
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 (MSVC AND MSVC_STATIC_BUILD)
|
endif (MSVC AND NOT BUILD_SHARED_LIBS)
|
||||||
|
|
||||||
IF(BUILD_PROTOC)
|
IF(BUILD_PROTOC)
|
||||||
SET(CMAKE_CXX_STANDARD 11)
|
SET(CMAKE_CXX_STANDARD 11)
|
||||||
@ -86,6 +96,13 @@ ADD_EXECUTABLE(protoc-gen-c ${PROTOC_GEN_C_SRC})
|
|||||||
|
|
||||||
TARGET_LINK_LIBRARIES(protoc-gen-c ${PROTOBUF_PROTOC_LIBRARY} ${PROTOBUF_LIBRARY})
|
TARGET_LINK_LIBRARIES(protoc-gen-c ${PROTOBUF_PROTOC_LIBRARY} ${PROTOBUF_LIBRARY})
|
||||||
|
|
||||||
|
IF (MSVC AND BUILD_SHARED_LIBS)
|
||||||
|
TARGET_COMPILE_DEFINITIONS(protoc-gen-c PRIVATE -DPROTOBUF_USE_DLLS)
|
||||||
|
GET_FILENAME_COMPONENT(PROTOBUF_DLL_DIR ${PROTOBUF_PROTOC_EXECUTABLE} DIRECTORY)
|
||||||
|
FILE(GLOB PROTOBUF_DLLS ${PROTOBUF_DLL_DIR}/*.dll)
|
||||||
|
FILE(COPY ${PROTOBUF_DLLS} DESTINATION ${CMAKE_BINARY_DIR})
|
||||||
|
ENDIF (MSVC AND BUILD_SHARED_LIBS)
|
||||||
|
|
||||||
IF(CMAKE_HOST_UNIX)
|
IF(CMAKE_HOST_UNIX)
|
||||||
ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD
|
ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD
|
||||||
COMMAND ln -sf protoc-gen-c protoc-c
|
COMMAND ln -sf protoc-gen-c protoc-c
|
||||||
@ -117,6 +134,9 @@ GENERATE_TEST_SOURCES(${TEST_DIR}/test-full.proto t/test-full.pb-c.c t/test-full
|
|||||||
|
|
||||||
ADD_EXECUTABLE(cxx-generate-packed-data ${TEST_DIR}/generated-code2/cxx-generate-packed-data.cc t/test-full.pb.h t/test-full.pb.cc)
|
ADD_EXECUTABLE(cxx-generate-packed-data ${TEST_DIR}/generated-code2/cxx-generate-packed-data.cc t/test-full.pb.h t/test-full.pb.cc)
|
||||||
TARGET_LINK_LIBRARIES(cxx-generate-packed-data ${PROTOBUF_LIBRARY})
|
TARGET_LINK_LIBRARIES(cxx-generate-packed-data ${PROTOBUF_LIBRARY})
|
||||||
|
IF (MSVC AND BUILD_SHARED_LIBS)
|
||||||
|
TARGET_COMPILE_DEFINITIONS(cxx-generate-packed-data PRIVATE -DPROTOBUF_USE_DLLS)
|
||||||
|
ENDIF (MSVC AND BUILD_SHARED_LIBS)
|
||||||
|
|
||||||
FILE(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/t/generated-code2)
|
FILE(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/t/generated-code2)
|
||||||
ADD_CUSTOM_COMMAND(OUTPUT t/generated-code2/test-full-cxx-output.inc
|
ADD_CUSTOM_COMMAND(OUTPUT t/generated-code2/test-full-cxx-output.inc
|
||||||
@ -161,7 +181,7 @@ ENDIF()
|
|||||||
INSTALL(TARGETS protoc-gen-c RUNTIME DESTINATION bin)
|
INSTALL(TARGETS protoc-gen-c RUNTIME DESTINATION bin)
|
||||||
ENDIF() # BUILD_PROTOC
|
ENDIF() # BUILD_PROTOC
|
||||||
|
|
||||||
INSTALL(TARGETS protobuf-c LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)
|
INSTALL(TARGETS protobuf-c LIBRARY DESTINATION lib ARCHIVE DESTINATION lib RUNTIME DESTINATION bin)
|
||||||
INSTALL(FILES ${MAIN_DIR}/protobuf-c/protobuf-c.h DESTINATION include/protobuf-c)
|
INSTALL(FILES ${MAIN_DIR}/protobuf-c/protobuf-c.h DESTINATION include/protobuf-c)
|
||||||
INSTALL(FILES ${MAIN_DIR}/protobuf-c/protobuf-c.h DESTINATION include)
|
INSTALL(FILES ${MAIN_DIR}/protobuf-c/protobuf-c.h DESTINATION include)
|
||||||
INSTALL(FILES ${CMAKE_BINARY_DIR}/protobuf-c.pdb DESTINATION lib OPTIONAL)
|
INSTALL(FILES ${CMAKE_BINARY_DIR}/protobuf-c.pdb DESTINATION lib OPTIONAL)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user