David Burger | c33d1eb | 2020-01-21 14:24:50 -0700 | [diff] [blame] | 1 | #!/bin/bash -e |
| 2 | # |
| 3 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | # |
| 7 | # Runs protoc over the configuration protos to produce generated proto code. |
| 8 | |
David Burger | 1b84536 | 2020-02-13 14:43:37 -0700 | [diff] [blame] | 9 | # Allows the recursive glob for proto files below to work. |
| 10 | shopt -s globstar |
| 11 | |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 12 | readonly golden_file="gen/golden_descriptors.json" |
| 13 | |
| 14 | regenerate_golden() { |
| 15 | # We want to split --path from the filenames so silence warning. |
| 16 | # shellcheck disable=2068 |
| 17 | buf build --exclude-imports -o -#format=json ${proto_paths[@]} \ |
| 18 | | jq -S > "${golden_file}" |
| 19 | } |
| 20 | |
| 21 | allow_breaking=0 |
| 22 | regen_golden=0 |
| 23 | while [[ $# -gt 0 ]]; do |
| 24 | case $1 in |
| 25 | --allow-breaking) |
| 26 | allow_breaking=1 |
| 27 | shift |
| 28 | ;; |
| 29 | --force-regen-golden) |
| 30 | regen_golden=1 |
| 31 | shift |
| 32 | ;; |
| 33 | *) |
| 34 | break |
| 35 | ;; |
| 36 | esac |
| 37 | done |
| 38 | |
| 39 | script_dir="$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")" |
| 40 | readonly script_dir |
| 41 | |
Prathmesh Prabhu | 4220754 | 2020-04-20 23:13:23 -0700 | [diff] [blame] | 42 | cd "${script_dir}" |
C Shapiro | e8a8f80 | 2021-06-30 15:21:46 -0500 | [diff] [blame] | 43 | ./generate_grpc_py_bindings.sh |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 44 | source "./setup_cipd.sh" # uses script_dir |
| 45 | |
| 46 | if [[ "${regen_golden}" -eq 1 ]]; then |
| 47 | echo "Forcing regenerating of golden proto descriptors." |
| 48 | regenerate_golden |
| 49 | exit 0 |
| 50 | fi |
| 51 | |
| 52 | echo "protoc version: $(protoc --version)" |
| 53 | echo "buf version: $(buf --version 2>&1)" |
| 54 | |
| 55 | #### protobuffer checks |
| 56 | mapfile -t proto_files < <(find proto/ -type f -name '*.proto') |
| 57 | mapfile -t proto_paths < \ |
| 58 | <(find proto/ -type f -name '*.proto' -exec echo '--path {} ' \;) |
| 59 | |
| 60 | echo |
| 61 | echo "== Checking for breaking protobuffer changes" |
| 62 | if ! buf breaking --against "${golden_file}"; then |
| 63 | if [[ "${allow_breaking}" -eq 0 ]]; then |
| 64 | ( |
| 65 | echo |
| 66 | cat <<-EOF |
| 67 | One or more breaking changes detected. If these are intentional, re-run |
| 68 | with --allow-breaking to allow the changes. |
| 69 | EOF |
| 70 | ) >&2 |
| 71 | exit 1 |
| 72 | else |
| 73 | cat <<EOF >&2 |
| 74 | One or more breaking changes, but --allow-breaking specified, continuing. |
| 75 | EOF |
| 76 | fi |
| 77 | fi |
| 78 | |
| 79 | echo "No breaking changes, regenerating '${golden_file}'" |
| 80 | # We want to split --path from the filenames so suppress warning about quotes. |
| 81 | # shellcheck disable=2068 |
| 82 | buf build --exclude-imports --exclude-source-info \ |
| 83 | -o -#format=json ${proto_paths[@]} \ |
| 84 | | jq -S > "${golden_file}" |
| 85 | |
| 86 | # Check if golden file changed and offer to submit it for the user. |
| 87 | if ! git diff --quiet "${golden_file}"; then |
| 88 | echo |
| 89 | echo "Please commit ${golden_file} with your change" |
| 90 | else |
| 91 | echo "Clean diff on ${golden_file}, nothing else to do." >&2 |
| 92 | fi |
| 93 | |
| 94 | echo |
| 95 | echo "== Linting protobuffers" |
| 96 | |
| 97 | # We want to split --path from the filenames so suppress warning about quotes. |
| 98 | # shellcheck disable=2068 |
| 99 | if ! buf lint ${proto_paths[@]}; then |
| 100 | echo "One or more files need cleanup" >&2 |
| 101 | exit |
| 102 | else |
| 103 | echo "Files are clean" |
| 104 | fi |
| 105 | |
| 106 | echo |
| 107 | echo "== Generating Python bindings" |
David Burger | c33d1eb | 2020-01-21 14:24:50 -0700 | [diff] [blame] | 108 | |
David Burger | 78d3e62 | 2020-05-07 19:18:25 -0600 | [diff] [blame] | 109 | # Remove files from prior python protocol buffer code generation in |
| 110 | # case any .proto files have been removed. |
| 111 | find python/ -type f -name '*_pb2.py' -delete |
| 112 | find python/chromiumos -mindepth 1 -type d -not -name __pycache__ \ |
| 113 | -exec rm -f '{}/__init__.py' \; |
| 114 | |
Alex Zamorzaev | 6f41e3d | 2020-06-08 19:46:01 +0000 | [diff] [blame] | 115 | PATH="${CIPD_ROOT}" protoc -Iproto \ |
David Burger | 7f5a8fb | 2020-06-01 10:17:32 -0600 | [diff] [blame] | 116 | --descriptor_set_out=util/bindings/descpb.bin \ |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 117 | --python_out=python "${proto_files[@]}" |
David Riley | 996c524 | 2020-05-06 11:31:25 -0700 | [diff] [blame] | 118 | find python/chromiumos -mindepth 1 -type d -not -name __pycache__ \ |
| 119 | -exec touch '{}/__init__.py' \; |
Andrew Lamb | bc029d3 | 2020-02-24 12:42:50 -0700 | [diff] [blame] | 120 | |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 121 | echo |
| 122 | echo "== Generating Go bindings" |
Prathmesh Prabhu | 72f8a00 | 2020-04-10 09:57:53 -0700 | [diff] [blame] | 123 | |
| 124 | # Go bindings are already namespaced under go.chromium.org/chromiumos/config/go |
| 125 | # We remove the "chromiumos/config" prefix from local path to avoid redundant |
| 126 | # namespaceing. |
David Burger | 78d3e62 | 2020-05-07 19:18:25 -0600 | [diff] [blame] | 127 | |
| 128 | # Remove files from prior go protocol buffer code generation in |
| 129 | # case any .proto files have been removed. |
| 130 | find go/ -name '*pb.go' -delete |
| 131 | |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 132 | GO_TEMP_DIR=$(mktemp -d) |
| 133 | readonly GO_TEMP_DIR |
| 134 | trap 'rm -rf ${GO_TEMP_DIR}' EXIT |
| 135 | |
Andrew Lamb | bc029d3 | 2020-02-24 12:42:50 -0700 | [diff] [blame] | 136 | # Go files need to be processed individually until this is fixed: |
| 137 | # https://github.com/golang/protobuf/issues/39 |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 138 | for proto in "${proto_files[@]}"; do |
Alex Zamorzaev | 6f41e3d | 2020-06-08 19:46:01 +0000 | [diff] [blame] | 139 | PATH="${CIPD_ROOT}" protoc -I"proto" \ |
| 140 | --go_out=plugins=grpc,paths=source_relative:"${GO_TEMP_DIR}" \ |
Prathmesh Prabhu | 72f8a00 | 2020-04-10 09:57:53 -0700 | [diff] [blame] | 141 | "${proto}" |
C Shapiro | b8cd5f9 | 2020-03-10 09:47:10 -0500 | [diff] [blame] | 142 | done |
C Shapiro | 0eba639 | 2021-05-05 11:57:19 -0500 | [diff] [blame] | 143 | |
Alex Zamorzaev | 6f41e3d | 2020-06-08 19:46:01 +0000 | [diff] [blame] | 144 | cp -rf "${GO_TEMP_DIR}"/chromiumos/config/* go/ |
C Shapiro | ca31c6d | 2021-05-05 09:29:19 -0500 | [diff] [blame] | 145 | cp "${GO_TEMP_DIR}"/chromiumos/*.go go/ |
C Shapiro | 0eba639 | 2021-05-05 11:57:19 -0500 | [diff] [blame] | 146 | cp "${GO_TEMP_DIR}"/chromiumos/longrunning/*.go go/longrunning |
Sean McAllister | b8a979f | 2021-06-01 10:27:00 -0600 | [diff] [blame] | 147 | cp -rf "${GO_TEMP_DIR}"/chromiumos/build/* go/build/ |
Andrew Lamb | 7a58287 | 2021-03-29 15:09:24 -0600 | [diff] [blame] | 148 | cp -rf "${GO_TEMP_DIR}"/chromiumos/test/* go/test |