Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 1 | # User options |
| 2 | include("${CMAKE_CURRENT_LIST_DIR}/protobuf-options.cmake") |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 4 | # Depend packages |
| 5 | @_protobuf_FIND_ZLIB@ |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 6 | |
| 7 | # Imported targets |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 8 | include("${CMAKE_CURRENT_LIST_DIR}/protobuf-targets.cmake") |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 9 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 10 | function(protobuf_generate) |
| 11 | include(CMakeParseArguments) |
| 12 | |
| 13 | set(_options APPEND_PATH) |
| 14 | set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO) |
| 15 | if(COMMAND target_sources) |
| 16 | list(APPEND _singleargs TARGET) |
| 17 | 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 | message(SEND_ERROR "Error: protobuf_generate called without any targets or source files") |
| 24 | return() |
| 25 | endif() |
| 26 | |
| 27 | if(NOT protobuf_generate_OUT_VAR AND NOT protobuf_generate_TARGET) |
| 28 | message(SEND_ERROR "Error: protobuf_generate called without a target or output variable") |
| 29 | return() |
| 30 | endif() |
| 31 | |
| 32 | if(NOT protobuf_generate_LANGUAGE) |
| 33 | set(protobuf_generate_LANGUAGE cpp) |
| 34 | endif() |
| 35 | string(TOLOWER ${protobuf_generate_LANGUAGE} protobuf_generate_LANGUAGE) |
| 36 | |
| 37 | if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp) |
| 38 | set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}:") |
| 39 | endif() |
| 40 | |
| 41 | if(NOT protobuf_GENERATE_EXTENSIONS) |
| 42 | if(protobuf_generate_LANGUAGE STREQUAL cpp) |
| 43 | set(protobuf_GENERATE_EXTENSIONS .pb.h .pb.cc) |
| 44 | elseif(protobuf_generate_LANGUAGE STREQUAL python) |
| 45 | set(protobuf_GENERATE_EXTENSIONS _pb2.py) |
| 46 | else() |
| 47 | message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS") |
| 48 | return() |
| 49 | endif() |
| 50 | endif() |
| 51 | |
| 52 | if(protobuf_generate_TARGET) |
| 53 | get_target_property(_source_list ${protobuf_generate_TARGET} SOURCES) |
| 54 | foreach(_file ${_source_list}) |
| 55 | if(_file MATCHES "proto$") |
| 56 | list(APPEND protobuf_generate_PROTOS ${_file}) |
| 57 | endif() |
| 58 | endforeach() |
| 59 | endif() |
| 60 | |
| 61 | if(NOT protobuf_generate_PROTOS) |
| 62 | message(SEND_ERROR "Error: protobuf_generate could not find any .proto files") |
| 63 | return() |
| 64 | endif() |
| 65 | |
| 66 | if(protobuf_generate_APPEND_PATH) |
| 67 | # Create an include path for each file specified |
| 68 | foreach(_file ${protobuf_generate_PROTOS}) |
| 69 | get_filename_component(_abs_file ${_file} ABSOLUTE) |
| 70 | get_filename_component(_abs_path ${_abs_file} PATH) |
| 71 | list(FIND _protobuf_include_path ${_abs_path} _contains_already) |
| 72 | if(${_contains_already} EQUAL -1) |
| 73 | list(APPEND _protobuf_include_path -I ${_abs_path}) |
| 74 | endif() |
| 75 | endforeach() |
| 76 | else() |
| 77 | set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR}) |
| 78 | endif() |
| 79 | |
| 80 | foreach(DIR ${protobuf_generate_IMPORT_DIRS}) |
| 81 | get_filename_component(ABS_PATH ${DIR} ABSOLUTE) |
| 82 | list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) |
| 83 | if(${_contains_already} EQUAL -1) |
| 84 | list(APPEND _protobuf_include_path -I ${ABS_PATH}) |
| 85 | endif() |
| 86 | endforeach() |
| 87 | |
| 88 | set(_generated_srcs_all) |
| 89 | foreach(_proto ${protobuf_generate_PROTOS}) |
| 90 | get_filename_component(_abs_file ${_proto} ABSOLUTE) |
| 91 | get_filename_component(_basename ${_proto} NAME_WE) |
| 92 | |
| 93 | set(_generated_srcs) |
| 94 | foreach(_ext ${protobuf_GENERATE_EXTENSIONS}) |
| 95 | list(APPEND _generated_srcs "${CMAKE_CURRENT_BINARY_DIR}/${_basename}${_ext}") |
| 96 | endforeach() |
| 97 | list(APPEND _generated_srcs_all ${_generated_srcs}) |
| 98 | |
| 99 | add_custom_command( |
| 100 | OUTPUT ${_generated_srcs} |
| 101 | COMMAND protobuf::protoc |
| 102 | ARGS --${protobuf_generate_LANGUAGE}_out ${_dll_export_decl}${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${_abs_file} |
| 103 | DEPENDS ${ABS_FIL} protobuf::protoc |
| 104 | COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}" |
| 105 | VERBATIM ) |
| 106 | endforeach() |
| 107 | |
| 108 | set_source_files_properties(${_generated_srcs_all} PROPERTIES GENERATED TRUE) |
| 109 | if(protobuf_generate_OUT_VAR) |
| 110 | set(${protobuf_generate_OUT_VAR} ${_generated_srcs_all} PARENT_SCOPE) |
| 111 | endif() |
| 112 | if(protobuf_generate_TARGET) |
| 113 | target_sources(${protobuf_generate_TARGET} PRIVATE ${_generated_srcs_all}) |
| 114 | endif() |
| 115 | |
| 116 | endfunction() |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 117 | |
| 118 | # CMake FindProtobuf module compatible file |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 119 | if(protobuf_MODULE_COMPATIBLE) |
| 120 | include("${CMAKE_CURRENT_LIST_DIR}/protobuf-module.cmake") |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 121 | endif() |