Home | History | Annotate | Download | only in cmake
      1 if(PROTOBUF_SRC_ROOT_FOLDER)
      2   message(AUTHOR_WARNING "Variable PROTOBUF_SRC_ROOT_FOLDER defined, but not"
      3     " used in CONFIG mode")
      4 endif()
      5 
      6 function(PROTOBUF_GENERATE_CPP SRCS HDRS)
      7   if(NOT ARGN)
      8     message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
      9     return()
     10   endif()
     11 
     12   if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
     13     # Create an include path for each file specified
     14     foreach(FIL ${ARGN})
     15       get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
     16       get_filename_component(ABS_PATH ${ABS_FIL} PATH)
     17       list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
     18       if(${_contains_already} EQUAL -1)
     19           list(APPEND _protobuf_include_path -I ${ABS_PATH})
     20       endif()
     21     endforeach()
     22   else()
     23     set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
     24   endif()
     25 
     26   # Add well-known type protos include path
     27   list(APPEND _protobuf_include_path
     28     -I "${_PROTOBUF_IMPORT_PREFIX}/@CMAKE_INSTALL_INCLUDEDIR@")
     29 
     30   if(DEFINED PROTOBUF_IMPORT_DIRS)
     31     foreach(DIR ${PROTOBUF_IMPORT_DIRS})
     32       get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
     33       list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
     34       if(${_contains_already} EQUAL -1)
     35           list(APPEND _protobuf_include_path -I ${ABS_PATH})
     36       endif()
     37     endforeach()
     38   endif()
     39 
     40   set(${SRCS})
     41   set(${HDRS})
     42   foreach(FIL ${ARGN})
     43     get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
     44     get_filename_component(FIL_WE ${FIL} NAME_WE)
     45 
     46     list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
     47     list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
     48 
     49     add_custom_command(
     50       OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
     51              "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
     52       COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
     53       ARGS --cpp_out  ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
     54       DEPENDS ${ABS_FIL} ${PROTOBUF_PROTOC_EXECUTABLE}
     55       COMMENT "Running C++ protocol buffer compiler on ${FIL}"
     56       VERBATIM)
     57   endforeach()
     58 
     59   set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
     60   set(${SRCS} ${${SRCS}} PARENT_SCOPE)
     61   set(${HDRS} ${${HDRS}} PARENT_SCOPE)
     62 endfunction()
     63 
     64 # Internal function: search for normal library as well as a debug one
     65 #    if the debug one is specified also include debug/optimized keywords
     66 #    in *_LIBRARIES variable
     67 function(_protobuf_find_libraries name filename)
     68    get_target_property(${name}_LIBRARY lib${filename}
     69      IMPORTED_LOCATION_RELEASE)
     70    set(${name}_LIBRARY "${${name}_LIBRARY}" PARENT_SCOPE)
     71    get_target_property(${name}_LIBRARY_DEBUG lib${filename}
     72      IMPORTED_LOCATION_DEBUG)
     73    set(${name}_LIBRARY_DEBUG "${${name}_LIBRARY_DEBUG}" PARENT_SCOPE)
     74 
     75    if(NOT ${name}_LIBRARY_DEBUG)
     76       # There is no debug library
     77       set(${name}_LIBRARY_DEBUG ${${name}_LIBRARY} PARENT_SCOPE)
     78       set(${name}_LIBRARIES     ${${name}_LIBRARY} PARENT_SCOPE)
     79    else()
     80       # There IS a debug library
     81       set(${name}_LIBRARIES
     82           optimized ${${name}_LIBRARY}
     83           debug     ${${name}_LIBRARY_DEBUG}
     84           PARENT_SCOPE
     85       )
     86    endif()
     87 endfunction()
     88 
     89 # Internal function: find threads library
     90 function(_protobuf_find_threads)
     91     set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
     92     find_package(Threads)
     93     if(Threads_FOUND)
     94         list(APPEND PROTOBUF_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
     95         set(PROTOBUF_LIBRARIES "${PROTOBUF_LIBRARIES}" PARENT_SCOPE)
     96     endif()
     97 endfunction()
     98 
     99 #
    100 # Main.
    101 #
    102 
    103 # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
    104 # for each directory where a proto file is referenced.
    105 if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
    106   set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
    107 endif()
    108 
    109 # The Protobuf library
    110 _protobuf_find_libraries(PROTOBUF protobuf)
    111 
    112 # The Protobuf Lite library
    113 _protobuf_find_libraries(PROTOBUF_LITE protobuf-lite)
    114 
    115 # The Protobuf Protoc Library
    116 _protobuf_find_libraries(PROTOBUF_PROTOC protoc)
    117 
    118 if(UNIX)
    119   _protobuf_find_threads()
    120 endif()
    121 
    122 # Set the include directory
    123 set(PROTOBUF_INCLUDE_DIR "${_PROTOBUF_IMPORT_PREFIX}/@CMAKE_INSTALL_INCLUDEDIR@")
    124 
    125 # Set the protoc Executable
    126 get_target_property(PROTOBUF_PROTOC_EXECUTABLE protoc
    127   IMPORTED_LOCATION_RELEASE)
    128 if(NOT PROTOBUF_PROTOC_EXECUTABLE)
    129   get_target_property(PROTOBUF_PROTOC_EXECUTABLE protoc
    130     IMPORTED_LOCATION_DEBUG)
    131 endif()
    132 
    133 include(FindPackageHandleStandardArgs)
    134 FIND_PACKAGE_HANDLE_STANDARD_ARGS(PROTOBUF DEFAULT_MSG
    135     PROTOBUF_LIBRARY PROTOBUF_INCLUDE_DIR)
    136 
    137 if(PROTOBUF_FOUND)
    138     set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIR})
    139 endif()
    140