protobuf-config.cmake.in 4.32 KB
Newer Older
1 2
# User options
include("${CMAKE_CURRENT_LIST_DIR}/protobuf-options.cmake")
3

4 5 6
# Depend packages
@_protobuf_FIND_ZLIB@

7
# Imported targets
8
include("${CMAKE_CURRENT_LIST_DIR}/protobuf-targets.cmake")
9

10 11
function(protobuf_generate)
  include(CMakeParseArguments)
12 13

  set(_options APPEND_PATH)
14
  set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR)
15 16 17
  if(COMMAND target_sources)
    list(APPEND _singleargs TARGET)
  endif()
18
  set(_multiargs PROTOS IMPORT_DIRS GENERATE_EXTENSIONS)
19

20
  cmake_parse_arguments(protobuf_generate "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}")
21

22
  if(NOT protobuf_generate_PROTOS AND NOT protobuf_generate_TARGET)
23 24 25 26 27 28 29 30 31 32 33 34 35 36
    message(SEND_ERROR "Error: protobuf_generate called without any targets or source files")
    return()
  endif()

  if(NOT protobuf_generate_OUT_VAR AND NOT protobuf_generate_TARGET)
    message(SEND_ERROR "Error: protobuf_generate called without a target or output variable")
    return()
  endif()

  if(NOT protobuf_generate_LANGUAGE)
    set(protobuf_generate_LANGUAGE cpp)
  endif()
  string(TOLOWER ${protobuf_generate_LANGUAGE} protobuf_generate_LANGUAGE)

37 38 39 40
  if(NOT protobuf_generate_PROTOC_OUT_DIR)
    set(protobuf_generate_PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
  endif()
  
41 42 43 44
  if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp)
    set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}:")
  endif()

45
  if(NOT protobuf_generate_GENERATE_EXTENSIONS)
46
    if(protobuf_generate_LANGUAGE STREQUAL cpp)
47
      set(protobuf_generate_GENERATE_EXTENSIONS .pb.h .pb.cc)
48
    elseif(protobuf_generate_LANGUAGE STREQUAL python)
49
      set(protobuf_generate_GENERATE_EXTENSIONS _pb2.py)
50 51 52 53 54 55
    else()
      message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS")
      return()
    endif()
  endif()

56 57 58 59 60 61 62 63 64 65 66 67 68 69
  if(protobuf_generate_TARGET)
    get_target_property(_source_list ${protobuf_generate_TARGET} SOURCES)
    foreach(_file ${_source_list})
      if(_file MATCHES "proto$")
        list(APPEND protobuf_generate_PROTOS ${_file})
      endif()
    endforeach()
  endif()

  if(NOT protobuf_generate_PROTOS)
    message(SEND_ERROR "Error: protobuf_generate could not find any .proto files")
    return()
  endif()

70 71
  if(protobuf_generate_APPEND_PATH)
    # Create an include path for each file specified
72
    foreach(_file ${protobuf_generate_PROTOS})
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
      get_filename_component(_abs_file ${_file} ABSOLUTE)
      get_filename_component(_abs_path ${_abs_file} PATH)
      list(FIND _protobuf_include_path ${_abs_path} _contains_already)
      if(${_contains_already} EQUAL -1)
          list(APPEND _protobuf_include_path -I ${_abs_path})
      endif()
    endforeach()
  else()
    set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  endif()

  foreach(DIR ${protobuf_generate_IMPORT_DIRS})
    get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
    list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
    if(${_contains_already} EQUAL -1)
        list(APPEND _protobuf_include_path -I ${ABS_PATH})
    endif()
  endforeach()

92
  set(_generated_srcs_all)
93 94 95 96
  foreach(_proto ${protobuf_generate_PROTOS})
    get_filename_component(_abs_file ${_proto} ABSOLUTE)
    get_filename_component(_basename ${_proto} NAME_WE)

97
    set(_generated_srcs)
98
    foreach(_ext ${protobuf_generate_GENERATE_EXTENSIONS})
99
      list(APPEND _generated_srcs "${protobuf_generate_PROTOC_OUT_DIR}/${_basename}${_ext}")
100
    endforeach()
101
    list(APPEND _generated_srcs_all ${_generated_srcs})
102 103 104 105

    add_custom_command(
      OUTPUT ${_generated_srcs}
      COMMAND  protobuf::protoc
106
      ARGS --${protobuf_generate_LANGUAGE}_out ${_dll_export_decl}${protobuf_generate_PROTOC_OUT_DIR} ${_protobuf_include_path} ${_abs_file}
107
      DEPENDS ${_abs_file} protobuf::protoc
108 109 110 111
      COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}"
      VERBATIM )
  endforeach()

112
  set_source_files_properties(${_generated_srcs_all} PROPERTIES GENERATED TRUE)
113
  if(protobuf_generate_OUT_VAR)
114
    set(${protobuf_generate_OUT_VAR} ${_generated_srcs_all} PARENT_SCOPE)
115 116
  endif()
  if(protobuf_generate_TARGET)
117
    target_sources(${protobuf_generate_TARGET} PRIVATE ${_generated_srcs_all})
118 119 120 121
  endif()

endfunction()

122
# CMake FindProtobuf module compatible file
123 124
if(protobuf_MODULE_COMPATIBLE)
  include("${CMAKE_CURRENT_LIST_DIR}/protobuf-module.cmake")
125
endif()