|
| 1 | +# Copyright 2008 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions are |
| 5 | +# met: |
| 6 | +# |
| 7 | +# * Redistributions of source code must retain the above copyright |
| 8 | +# notice, this list of conditions and the following disclaimer. |
| 9 | +# * Redistributions in binary form must reproduce the above |
| 10 | +# copyright notice, this list of conditions and the following disclaimer |
| 11 | +# in the documentation and/or other materials provided with the |
| 12 | +# distribution. |
| 13 | +# * Neither the name of Google Inc. nor the names of its |
| 14 | +# contributors may be used to endorse or promote products derived from |
| 15 | +# this software without specific prior written permission. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 | +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 | +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 | +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +# |
| 29 | +# Code generated by the Protocol Buffer compiler is owned by the owner |
| 30 | +# of the input file used when generating it. This code is not |
| 31 | +# standalone and requires a support library to be linked with it. This |
| 32 | +# support library is itself covered by the above license. |
| 33 | + |
| 34 | +# --- BEGIN NOTICE --- |
| 35 | +# Source: https://github.com/protocolbuffers/protobuf/blob/v3.21.12/cmake/ |
| 36 | +# Modifications: |
| 37 | +# - Add license text above from repository |
| 38 | +# - Add `-I ${protobuf_INCLUDE_DIR}` at line 158 |
| 39 | +# --- END NOTICE --- |
| 40 | + |
| 41 | +# User options |
| 42 | +include("${CMAKE_CURRENT_LIST_DIR}/protobuf-options.cmake") |
| 43 | + |
| 44 | +# Depend packages |
| 45 | +# BEGIN CONAN PATCH |
| 46 | +#_protobuf_FIND_ZLIB@ |
| 47 | +# END CONAN PATCH |
| 48 | + |
| 49 | +# Imported targets |
| 50 | +if(NOT TARGET protobuf::protoc) |
| 51 | + if(CMAKE_CROSSCOMPILING) |
| 52 | + find_program(PROTOC_PROGRAM protoc PATHS ENV PATH NO_DEFAULT_PATH) |
| 53 | + endif() |
| 54 | + if(NOT PROTOC_PROGRAM) |
| 55 | + set(PROTOC_PROGRAM "${CMAKE_CURRENT_LIST_DIR}/../../../bin/protoc") |
| 56 | + endif() |
| 57 | + get_filename_component(PROTOC_PROGRAM "${PROTOC_PROGRAM}" ABSOLUTE) |
| 58 | + set(Protobuf_PROTOC_EXECUTABLE ${PROTOC_PROGRAM} CACHE FILEPATH "The protoc compiler") |
| 59 | + add_executable(protobuf::protoc IMPORTED) |
| 60 | + set_property(TARGET protobuf::protoc PROPERTY IMPORTED_LOCATION ${Protobuf_PROTOC_EXECUTABLE}) |
| 61 | +endif() |
| 62 | + |
| 63 | + |
| 64 | +function(protobuf_generate) |
| 65 | + include(CMakeParseArguments) |
| 66 | + |
| 67 | + set(_options APPEND_PATH) |
| 68 | + set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR PLUGIN PLUGIN_OPTIONS) |
| 69 | + if(COMMAND target_sources) |
| 70 | + list(APPEND _singleargs TARGET) |
| 71 | + endif() |
| 72 | + set(_multiargs PROTOS IMPORT_DIRS GENERATE_EXTENSIONS PROTOC_OPTIONS) |
| 73 | + |
| 74 | + cmake_parse_arguments(protobuf_generate "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}") |
| 75 | + |
| 76 | + if(NOT protobuf_generate_PROTOS AND NOT protobuf_generate_TARGET) |
| 77 | + message(SEND_ERROR "Error: protobuf_generate called without any targets or source files") |
| 78 | + return() |
| 79 | + endif() |
| 80 | + |
| 81 | + if(NOT protobuf_generate_OUT_VAR AND NOT protobuf_generate_TARGET) |
| 82 | + message(SEND_ERROR "Error: protobuf_generate called without a target or output variable") |
| 83 | + return() |
| 84 | + endif() |
| 85 | + |
| 86 | + if(NOT protobuf_generate_LANGUAGE) |
| 87 | + set(protobuf_generate_LANGUAGE cpp) |
| 88 | + endif() |
| 89 | + string(TOLOWER ${protobuf_generate_LANGUAGE} protobuf_generate_LANGUAGE) |
| 90 | + |
| 91 | + if(NOT protobuf_generate_PROTOC_OUT_DIR) |
| 92 | + set(protobuf_generate_PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}) |
| 93 | + endif() |
| 94 | + |
| 95 | + if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp) |
| 96 | + set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}") |
| 97 | + endif() |
| 98 | + |
| 99 | + foreach(_option ${_dll_export_decl} ${protobuf_generate_PLUGIN_OPTIONS}) |
| 100 | + # append comma - not using CMake lists and string replacement as users |
| 101 | + # might have semicolons in options |
| 102 | + if(_plugin_options) |
| 103 | + set( _plugin_options "${_plugin_options},") |
| 104 | + endif() |
| 105 | + set(_plugin_options "${_plugin_options}${_option}") |
| 106 | + endforeach() |
| 107 | + |
| 108 | + if(protobuf_generate_PLUGIN) |
| 109 | + set(_plugin "--plugin=${protobuf_generate_PLUGIN}") |
| 110 | + endif() |
| 111 | + |
| 112 | + if(NOT protobuf_generate_GENERATE_EXTENSIONS) |
| 113 | + if(protobuf_generate_LANGUAGE STREQUAL cpp) |
| 114 | + set(protobuf_generate_GENERATE_EXTENSIONS .pb.h .pb.cc) |
| 115 | + elseif(protobuf_generate_LANGUAGE STREQUAL python) |
| 116 | + set(protobuf_generate_GENERATE_EXTENSIONS _pb2.py) |
| 117 | + else() |
| 118 | + message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS") |
| 119 | + return() |
| 120 | + endif() |
| 121 | + endif() |
| 122 | + |
| 123 | + if(protobuf_generate_TARGET) |
| 124 | + get_target_property(_source_list ${protobuf_generate_TARGET} SOURCES) |
| 125 | + foreach(_file ${_source_list}) |
| 126 | + if(_file MATCHES "proto$") |
| 127 | + list(APPEND protobuf_generate_PROTOS ${_file}) |
| 128 | + endif() |
| 129 | + endforeach() |
| 130 | + endif() |
| 131 | + |
| 132 | + if(NOT protobuf_generate_PROTOS) |
| 133 | + message(SEND_ERROR "Error: protobuf_generate could not find any .proto files") |
| 134 | + return() |
| 135 | + endif() |
| 136 | + |
| 137 | + if(protobuf_generate_APPEND_PATH) |
| 138 | + # Create an include path for each file specified |
| 139 | + foreach(_file ${protobuf_generate_PROTOS}) |
| 140 | + get_filename_component(_abs_file ${_file} ABSOLUTE) |
| 141 | + get_filename_component(_abs_dir ${_abs_file} DIRECTORY) |
| 142 | + list(FIND _protobuf_include_path ${_abs_dir} _contains_already) |
| 143 | + if(${_contains_already} EQUAL -1) |
| 144 | + list(APPEND _protobuf_include_path -I ${_abs_dir}) |
| 145 | + endif() |
| 146 | + endforeach() |
| 147 | + endif() |
| 148 | + |
| 149 | + foreach(DIR ${protobuf_generate_IMPORT_DIRS}) |
| 150 | + get_filename_component(ABS_PATH ${DIR} ABSOLUTE) |
| 151 | + list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) |
| 152 | + if(${_contains_already} EQUAL -1) |
| 153 | + list(APPEND _protobuf_include_path -I ${ABS_PATH}) |
| 154 | + endif() |
| 155 | + endforeach() |
| 156 | + |
| 157 | + if(NOT _protobuf_include_path) |
| 158 | + set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR} -I ${protobuf_INCLUDE_DIR}) |
| 159 | + endif() |
| 160 | + |
| 161 | + set(_generated_srcs_all) |
| 162 | + foreach(_proto ${protobuf_generate_PROTOS}) |
| 163 | + get_filename_component(_abs_file ${_proto} ABSOLUTE) |
| 164 | + get_filename_component(_abs_dir ${_abs_file} DIRECTORY) |
| 165 | + |
| 166 | + get_filename_component(_file_full_name ${_proto} NAME) |
| 167 | + string(FIND "${_file_full_name}" "." _file_last_ext_pos REVERSE) |
| 168 | + string(SUBSTRING "${_file_full_name}" 0 ${_file_last_ext_pos} _basename) |
| 169 | + |
| 170 | + set(_suitable_include_found FALSE) |
| 171 | + foreach(DIR ${_protobuf_include_path}) |
| 172 | + if(NOT DIR STREQUAL "-I") |
| 173 | + file(RELATIVE_PATH _rel_dir ${DIR} ${_abs_dir}) |
| 174 | + string(FIND "${_rel_dir}" "../" _is_in_parent_folder) |
| 175 | + if (NOT ${_is_in_parent_folder} EQUAL 0) |
| 176 | + set(_suitable_include_found TRUE) |
| 177 | + break() |
| 178 | + endif() |
| 179 | + endif() |
| 180 | + endforeach() |
| 181 | + |
| 182 | + if(NOT _suitable_include_found) |
| 183 | + message(SEND_ERROR "Error: protobuf_generate could not find any correct proto include directory.") |
| 184 | + return() |
| 185 | + endif() |
| 186 | + |
| 187 | + set(_generated_srcs) |
| 188 | + foreach(_ext ${protobuf_generate_GENERATE_EXTENSIONS}) |
| 189 | + list(APPEND _generated_srcs "${protobuf_generate_PROTOC_OUT_DIR}/${_rel_dir}/${_basename}${_ext}") |
| 190 | + endforeach() |
| 191 | + list(APPEND _generated_srcs_all ${_generated_srcs}) |
| 192 | + |
| 193 | + set(_comment "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}") |
| 194 | + if(protobuf_generate_PROTOC_OPTIONS) |
| 195 | + set(_comment "${_comment}, protoc-options: ${protobuf_generate_PROTOC_OPTIONS}") |
| 196 | + endif() |
| 197 | + if(_plugin_options) |
| 198 | + set(_comment "${_comment}, plugin-options: ${_plugin_options}") |
| 199 | + endif() |
| 200 | + |
| 201 | + add_custom_command( |
| 202 | + OUTPUT ${_generated_srcs} |
| 203 | + COMMAND protobuf::protoc |
| 204 | + ARGS ${protobuf_generate_PROTOC_OPTIONS} --${protobuf_generate_LANGUAGE}_out ${_plugin_options}:${protobuf_generate_PROTOC_OUT_DIR} ${_plugin} ${_protobuf_include_path} ${_abs_file} |
| 205 | + DEPENDS ${_abs_file} protobuf::protoc |
| 206 | + COMMENT ${_comment} |
| 207 | + VERBATIM ) |
| 208 | + endforeach() |
| 209 | + |
| 210 | + set_source_files_properties(${_generated_srcs_all} PROPERTIES GENERATED TRUE) |
| 211 | + if(protobuf_generate_OUT_VAR) |
| 212 | + set(${protobuf_generate_OUT_VAR} ${_generated_srcs_all} PARENT_SCOPE) |
| 213 | + endif() |
| 214 | + if(protobuf_generate_TARGET) |
| 215 | + target_sources(${protobuf_generate_TARGET} PRIVATE ${_generated_srcs_all}) |
| 216 | + endif() |
| 217 | + |
| 218 | +endfunction() |
| 219 | + |
| 220 | +# CMake FindProtobuf module compatible file |
| 221 | +if(protobuf_MODULE_COMPATIBLE) |
| 222 | + include("${CMAKE_CURRENT_LIST_DIR}/protobuf-module.cmake") |
| 223 | +endif() |
0 commit comments