diff options
Diffstat (limited to 'Modules/FindProtobuf.cmake')
-rw-r--r-- | Modules/FindProtobuf.cmake | 129 |
1 files changed, 124 insertions, 5 deletions
diff --git a/Modules/FindProtobuf.cmake b/Modules/FindProtobuf.cmake index 3ffd5a7..33262f3 100644 --- a/Modules/FindProtobuf.cmake +++ b/Modules/FindProtobuf.cmake @@ -20,6 +20,9 @@ # imported .proto files. # ``Protobuf_DEBUG`` # Show debug messages. +# ``Protobuf_USE_STATIC_LIBS`` +# Set to ON to force the use of the static libraries. +# Default is OFF. # # Defines the following variables: # @@ -37,6 +40,15 @@ # ``Protobuf_LITE_LIBRARIES`` # The protobuf-lite libraries # +# The following :prop_tgt:`IMPORTED` targets are also defined: +# +# ``protobuf::libprotobuf`` +# The protobuf library. +# ``protobuf::libprotobuf-lite`` +# The protobuf lite library. +# ``protobuf::libprotoc`` +# The protoc library. +# # The following cache variables are also available to set or use: # # ``Protobuf_LIBRARY`` @@ -64,6 +76,7 @@ # include_directories(${Protobuf_INCLUDE_DIRS}) # include_directories(${CMAKE_CURRENT_BINARY_DIR}) # protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS foo.proto) +# protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS EXPORT_MACRO DLL_EXPORT foo.proto) # protobuf_generate_python(PROTO_PY foo.proto) # add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS}) # target_link_libraries(bar ${Protobuf_LIBRARIES}) @@ -77,12 +90,15 @@ # # Add custom commands to process ``.proto`` files to C++:: # -# protobuf_generate_cpp (<SRCS> <HDRS> [<ARGN>...]) +# protobuf_generate_cpp (<SRCS> <HDRS> [EXPORT_MACRO <MACRO>] [<ARGN>...]) # # ``SRCS`` # Variable to define with autogenerated source files # ``HDRS`` # Variable to define with autogenerated header files +# ``EXPORT_MACRO`` +# is a macro which should expand to ``__declspec(dllexport)`` or +# ``__declspec(dllimport)`` depending on what is being compiled. # ``ARGN`` # ``.proto`` files # @@ -98,14 +114,21 @@ # ``.proto`` filess function(PROTOBUF_GENERATE_CPP SRCS HDRS) - if(NOT ARGN) + cmake_parse_arguments(protobuf "" "EXPORT_MACRO" "" ${ARGN}) + + set(PROTO_FILES "${protobuf_UNPARSED_ARGUMENTS}") + if(NOT PROTO_FILES) message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files") return() endif() + if(protobuf_EXPORT_MACRO) + set(DLL_EXPORT_DECL "dllexport_decl=${protobuf_EXPORT_MACRO}:") + endif() + if(PROTOBUF_GENERATE_CPP_APPEND_PATH) # Create an include path for each file specified - foreach(FIL ${ARGN}) + foreach(FIL ${PROTO_FILES}) get_filename_component(ABS_FIL ${FIL} ABSOLUTE) get_filename_component(ABS_PATH ${ABS_FIL} PATH) list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) @@ -133,7 +156,7 @@ function(PROTOBUF_GENERATE_CPP SRCS HDRS) set(${SRCS}) set(${HDRS}) - foreach(FIL ${ARGN}) + foreach(FIL ${PROTO_FILES}) get_filename_component(ABS_FIL ${FIL} ABSOLUTE) get_filename_component(FIL_WE ${FIL} NAME_WE) if(NOT PROTOBUF_GENERATE_CPP_APPEND_PATH) @@ -150,7 +173,7 @@ function(PROTOBUF_GENERATE_CPP SRCS HDRS) OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc" "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h" COMMAND ${Protobuf_PROTOC_EXECUTABLE} - ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL} + ARGS "--cpp_out=${DLL_EXPORT_DECL}${CMAKE_CURRENT_BINARY_DIR}" ${_protobuf_include_path} ${ABS_FIL} DEPENDS ${ABS_FIL} ${Protobuf_PROTOC_EXECUTABLE} COMMENT "Running C++ protocol buffer compiler on ${FIL}" VERBATIM ) @@ -218,6 +241,14 @@ function(PROTOBUF_GENERATE_PYTHON SRCS) set(${SRCS} ${${SRCS}} PARENT_SCOPE) endfunction() + +if(Protobuf_DEBUG) + # Output some of their choices + message(STATUS "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE} ] " + "Protobuf_USE_STATIC_LIBS = ${Protobuf_USE_STATIC_LIBS}") +endif() + + # Backwards compatibility # Define camel case versions of input variables foreach(UPPER @@ -245,6 +276,17 @@ if(CMAKE_SIZEOF_VOID_P EQUAL 8) set(_PROTOBUF_ARCH_DIR x64/) endif() + +# Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES +if( Protobuf_USE_STATIC_LIBS ) + set( _protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES}) + if(WIN32) + set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES}) + else() + set(CMAKE_FIND_LIBRARY_SUFFIXES .a ) + endif() +endif() + include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake) # Internal function: search for normal library as well as a debug one @@ -387,6 +429,78 @@ if(Protobuf_INCLUDE_DIR) message(WARNING "Protobuf compiler version ${_PROTOBUF_PROTOC_EXECUTABLE_VERSION}" " doesn't match library version ${Protobuf_VERSION}") endif() + + if(Protobuf_LIBRARY) + if(NOT TARGET protobuf::libprotobuf) + add_library(protobuf::libprotobuf UNKNOWN IMPORTED) + set_target_properties(protobuf::libprotobuf PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}") + if(EXISTS "${Protobuf_LIBRARY}") + set_target_properties(protobuf::libprotobuf PROPERTIES + IMPORTED_LOCATION "${Protobuf_LIBRARY}") + endif() + if(EXISTS "${Protobuf_LIBRARY_RELEASE}") + set_property(TARGET protobuf::libprotobuf APPEND PROPERTY + IMPORTED_CONFIGURATIONS RELEASE) + set_target_properties(protobuf::libprotobuf PROPERTIES + IMPORTED_LOCATION_RELEASE "${Protobuf_LIBRARY_RELEASE}") + endif() + if(EXISTS "${Protobuf_LIBRARY_DEBUG}") + set_property(TARGET protobuf::libprotobuf APPEND PROPERTY + IMPORTED_CONFIGURATIONS DEBUG) + set_target_properties(protobuf::libprotobuf PROPERTIES + IMPORTED_LOCATION_DEBUG "${Protobuf_LIBRARY_DEBUG}") + endif() + endif() + endif() + + if(Protobuf_LITE_LIBRARY) + if(NOT TARGET protobuf::libprotobuf-lite) + add_library(protobuf::libprotobuf-lite UNKNOWN IMPORTED) + set_target_properties(protobuf::libprotobuf-lite PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}") + if(EXISTS "${Protobuf_LITE_LIBRARY}") + set_target_properties(protobuf::libprotobuf-lite PROPERTIES + IMPORTED_LOCATION "${Protobuf_LITE_LIBRARY}") + endif() + if(EXISTS "${Protobuf_LITE_LIBRARY_RELEASE}") + set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY + IMPORTED_CONFIGURATIONS RELEASE) + set_target_properties(protobuf::libprotobuf-lite PROPERTIES + IMPORTED_LOCATION_RELEASE "${Protobuf_LITE_LIBRARY_RELEASE}") + endif() + if(EXISTS "${Protobuf_LITE_LIBRARY_DEBUG}") + set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY + IMPORTED_CONFIGURATIONS DEBUG) + set_target_properties(protobuf::libprotobuf-lite PROPERTIES + IMPORTED_LOCATION_DEBUG "${Protobuf_LITE_LIBRARY_DEBUG}") + endif() + endif() + endif() + + if(Protobuf_PROTOC_LIBRARY) + if(NOT TARGET protobuf::libprotoc) + add_library(protobuf::libprotoc UNKNOWN IMPORTED) + set_target_properties(protobuf::libprotoc PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${Protobuf_INCLUDE_DIR}") + if(EXISTS "${Protobuf_PROTOC_LIBRARY}") + set_target_properties(protobuf::libprotoc PROPERTIES + IMPORTED_LOCATION "${Protobuf_PROTOC_LIBRARY}") + endif() + if(EXISTS "${Protobuf_PROTOC_LIBRARY_RELEASE}") + set_property(TARGET protobuf::libprotoc APPEND PROPERTY + IMPORTED_CONFIGURATIONS RELEASE) + set_target_properties(protobuf::libprotoc PROPERTIES + IMPORTED_LOCATION_RELEASE "${Protobuf_PROTOC_LIBRARY_RELEASE}") + endif() + if(EXISTS "${Protobuf_PROTOC_LIBRARY_DEBUG}") + set_property(TARGET protobuf::libprotoc APPEND PROPERTY + IMPORTED_CONFIGURATIONS DEBUG) + set_target_properties(protobuf::libprotoc PROPERTIES + IMPORTED_LOCATION_DEBUG "${Protobuf_PROTOC_LIBRARY_DEBUG}") + endif() + endif() + endif() endif() include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) @@ -399,6 +513,11 @@ if(Protobuf_FOUND) set(Protobuf_INCLUDE_DIRS ${Protobuf_INCLUDE_DIR}) endif() +# Restore the original find library ordering +if( Protobuf_USE_STATIC_LIBS ) + set(CMAKE_FIND_LIBRARY_SUFFIXES ${_protobuf_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES}) +endif() + # Backwards compatibility # Define upper case versions of output variables foreach(Camel |