|
| 1 | +# Copyright 2022 Google LLC. All rights reserved. |
| 2 | +# |
| 3 | +# Use of this source code is governed by a BSD-style |
| 4 | +# license that can be found in the LICENSE file or at |
| 5 | +# https://developers.google.com/open-source/licenses/bsd |
| 6 | + |
| 7 | +# Build generated python sources for the protobuf library. |
| 8 | +set(protobuf_py_proto_path ${CMAKE_CURRENT_SOURCE_DIR}/source/src) |
| 9 | +set(protobuf_py_library_path ${CMAKE_CURRENT_SOURCE_DIR}/source/python) |
| 10 | +set(protobuf_py_output_path ${CMAKE_CURRENT_BINARY_DIR}/py) |
| 11 | + |
| 12 | +# Copy the library without the generated sources. |
| 13 | +add_custom_command( |
| 14 | + DEPENDS |
| 15 | + ${protobuf_py_proto_path} |
| 16 | + OUTPUT |
| 17 | + ${protobuf_py_output_path}/google |
| 18 | + COMMAND |
| 19 | + ${CMAKE_COMMAND} -E copy_directory |
| 20 | + ${CMAKE_CURRENT_SOURCE_DIR}/source/python/google |
| 21 | + ${protobuf_py_output_path}/google) |
| 22 | + |
| 23 | +# This list was taken from packager/third_party/protobuf/source/python/setup.py |
| 24 | +set(protobuf_py_filenames |
| 25 | + google/protobuf/descriptor |
| 26 | + google/protobuf/compiler/plugin |
| 27 | + google/protobuf/any |
| 28 | + google/protobuf/api |
| 29 | + google/protobuf/duration |
| 30 | + google/protobuf/empty |
| 31 | + google/protobuf/field_mask |
| 32 | + google/protobuf/source_context |
| 33 | + google/protobuf/struct |
| 34 | + google/protobuf/timestamp |
| 35 | + google/protobuf/type |
| 36 | + google/protobuf/wrappers) |
| 37 | + |
| 38 | +# For each proto in the list, generate a _pb2.py file to add to the library. |
| 39 | +set(protobuf_py_outputs "") |
| 40 | +foreach (filename ${protobuf_py_filenames}) |
| 41 | + set(proto_path "${protobuf_py_proto_path}/${filename}.proto") |
| 42 | + set(output_path "${protobuf_py_output_path}/${filename}_pb2.py") |
| 43 | + |
| 44 | + list(APPEND protobuf_py_outputs "${output_path}") |
| 45 | + |
| 46 | + add_custom_command( |
| 47 | + DEPENDS |
| 48 | + protoc |
| 49 | + ${proto_path} |
| 50 | + ${protobuf_py_output_path}/google |
| 51 | + OUTPUT |
| 52 | + ${output_path} |
| 53 | + COMMAND |
| 54 | + protoc |
| 55 | + ARGS |
| 56 | + -I${protobuf_py_proto_path} |
| 57 | + --python_out=${protobuf_py_output_path} |
| 58 | + ${proto_path}) |
| 59 | +endforeach () |
| 60 | + |
| 61 | +# The entire python protobuf library (repo source and generated) to the output |
| 62 | +# folder. |
| 63 | +add_custom_target(protobuf_py ALL DEPENDS ${protobuf_py_outputs}) |
0 commit comments