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 | c8c4306 | 2021-09-10 10:47:22 -0600 | [diff] [blame] | 12 | readonly desc_file="generated/descriptors.json" |
| 13 | readonly gen_owners="generated/OWNERS" |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 14 | |
| 15 | regenerate_golden() { |
| 16 | # We want to split --path from the filenames so silence warning. |
| 17 | # shellcheck disable=2068 |
| 18 | buf build --exclude-imports -o -#format=json ${proto_paths[@]} \ |
Sean McAllister | 80e02ff | 2021-08-28 12:47:06 -0600 | [diff] [blame] | 19 | | jq -S > "${desc_file}" |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | allow_breaking=0 |
| 23 | regen_golden=0 |
| 24 | while [[ $# -gt 0 ]]; do |
| 25 | case $1 in |
| 26 | --allow-breaking) |
| 27 | allow_breaking=1 |
| 28 | shift |
| 29 | ;; |
| 30 | --force-regen-golden) |
| 31 | regen_golden=1 |
| 32 | shift |
| 33 | ;; |
| 34 | *) |
| 35 | break |
| 36 | ;; |
| 37 | esac |
| 38 | done |
| 39 | |
| 40 | script_dir="$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")" |
| 41 | readonly script_dir |
| 42 | |
Prathmesh Prabhu | 4220754 | 2020-04-20 23:13:23 -0700 | [diff] [blame] | 43 | cd "${script_dir}" |
C Shapiro | e8a8f80 | 2021-06-30 15:21:46 -0500 | [diff] [blame] | 44 | ./generate_grpc_py_bindings.sh |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 45 | source "./setup_cipd.sh" # uses script_dir |
| 46 | |
| 47 | if [[ "${regen_golden}" -eq 1 ]]; then |
| 48 | echo "Forcing regenerating of golden proto descriptors." |
| 49 | regenerate_golden |
| 50 | exit 0 |
| 51 | fi |
| 52 | |
| 53 | echo "protoc version: $(protoc --version)" |
| 54 | echo "buf version: $(buf --version 2>&1)" |
| 55 | |
| 56 | #### protobuffer checks |
| 57 | mapfile -t proto_files < <(find proto/ -type f -name '*.proto') |
| 58 | mapfile -t proto_paths < \ |
| 59 | <(find proto/ -type f -name '*.proto' -exec echo '--path {} ' \;) |
| 60 | |
| 61 | echo |
| 62 | echo "== Checking for breaking protobuffer changes" |
Sean McAllister | 80e02ff | 2021-08-28 12:47:06 -0600 | [diff] [blame] | 63 | if ! buf breaking --against "${desc_file}"; then |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 64 | if [[ "${allow_breaking}" -eq 0 ]]; then |
| 65 | ( |
| 66 | echo |
| 67 | cat <<-EOF |
| 68 | One or more breaking changes detected. If these are intentional, re-run |
| 69 | with --allow-breaking to allow the changes. |
| 70 | EOF |
| 71 | ) >&2 |
| 72 | exit 1 |
| 73 | else |
| 74 | cat <<EOF >&2 |
| 75 | One or more breaking changes, but --allow-breaking specified, continuing. |
| 76 | EOF |
| 77 | fi |
| 78 | fi |
| 79 | |
Sean McAllister | 80e02ff | 2021-08-28 12:47:06 -0600 | [diff] [blame] | 80 | echo "No breaking changes, regenerating '${desc_file}'" |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 81 | # We want to split --path from the filenames so suppress warning about quotes. |
| 82 | # shellcheck disable=2068 |
| 83 | buf build --exclude-imports --exclude-source-info \ |
| 84 | -o -#format=json ${proto_paths[@]} \ |
Sean McAllister | 80e02ff | 2021-08-28 12:47:06 -0600 | [diff] [blame] | 85 | | jq -S > "${desc_file}" |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 86 | |
Sean McAllister | 80e02ff | 2021-08-28 12:47:06 -0600 | [diff] [blame] | 87 | # Check if golden file changed and inform use to commit it. |
| 88 | if ! git diff --quiet "${desc_file}"; then |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 89 | echo |
Sean McAllister | 80e02ff | 2021-08-28 12:47:06 -0600 | [diff] [blame] | 90 | echo "Please commit ${desc_file} with your change" |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 91 | else |
Sean McAllister | 80e02ff | 2021-08-28 12:47:06 -0600 | [diff] [blame] | 92 | echo "Clean diff on ${desc_file}, nothing else to do." >&2 |
Sean McAllister | 8704ebd | 2021-06-23 13:38:27 -0600 | [diff] [blame] | 93 | fi |
| 94 | |
| 95 | echo |
| 96 | echo "== Linting protobuffers" |
| 97 | |
| 98 | # We want to split --path from the filenames so suppress warning about quotes. |
| 99 | # shellcheck disable=2068 |
| 100 | if ! buf lint ${proto_paths[@]}; then |
| 101 | echo "One or more files need cleanup" >&2 |
| 102 | exit |
| 103 | else |
| 104 | echo "Files are clean" |
| 105 | fi |
| 106 | |
| 107 | echo |
| 108 | echo "== Generating Python bindings" |
David Burger | c33d1eb | 2020-01-21 14:24:50 -0700 | [diff] [blame] | 109 | |
David Burger | 78d3e62 | 2020-05-07 19:18:25 -0600 | [diff] [blame] | 110 | # Remove files from prior python protocol buffer code generation in |
| 111 | # case any .proto files have been removed. |
| 112 | find python/ -type f -name '*_pb2.py' -delete |
| 113 | find python/chromiumos -mindepth 1 -type d -not -name __pycache__ \ |
| 114 | -exec rm -f '{}/__init__.py' \; |
| 115 | |
Alex Zamorzaev | 6f41e3d | 2020-06-08 19:46:01 +0000 | [diff] [blame] | 116 | PATH="${CIPD_ROOT}" protoc -Iproto \ |
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 | |
C Shapiro | fc74216 | 2021-08-15 05:23:19 -0500 | [diff] [blame] | 144 | echo |
| 145 | echo "== Generating OWNERS file for generated code paths" |
| 146 | echo "##### AUTO-GENERATED FILE #####" > "${gen_owners}" |
| 147 | echo "##### See generate.sh #####" >> "${gen_owners}" |
| 148 | find proto/* -type f -name "OWNERS*" | xargs cat | grep -E ^include | sort | uniq >> "${gen_owners}" |
| 149 | find proto/* -type f -name "OWNERS*" | xargs cat | grep -E ^[a-z0-9]+@.+\..+ | sort | uniq >> "${gen_owners}" |
| 150 | |
| 151 | if ! git diff --quiet "${gen_owners}"; then |
| 152 | echo |
| 153 | echo "An OWNERS file was updated in the src/config/proto directory." |
| 154 | echo "${gen_owners} was automatically updated based on this change." |
| 155 | echo "Please commit ${gen_owners} with your change." |
| 156 | fi |
| 157 | |
Alex Zamorzaev | 6f41e3d | 2020-06-08 19:46:01 +0000 | [diff] [blame] | 158 | cp -rf "${GO_TEMP_DIR}"/chromiumos/config/* go/ |
C Shapiro | ca31c6d | 2021-05-05 09:29:19 -0500 | [diff] [blame] | 159 | cp "${GO_TEMP_DIR}"/chromiumos/*.go go/ |
C Shapiro | 0eba639 | 2021-05-05 11:57:19 -0500 | [diff] [blame] | 160 | cp "${GO_TEMP_DIR}"/chromiumos/longrunning/*.go go/longrunning |
Sean McAllister | b8a979f | 2021-06-01 10:27:00 -0600 | [diff] [blame] | 161 | cp -rf "${GO_TEMP_DIR}"/chromiumos/build/* go/build/ |
Andrew Lamb | 7a58287 | 2021-03-29 15:09:24 -0600 | [diff] [blame] | 162 | cp -rf "${GO_TEMP_DIR}"/chromiumos/test/* go/test |
Sean McAllister | c8c4306 | 2021-09-10 10:47:22 -0600 | [diff] [blame] | 163 | |
| 164 | echo |
| 165 | echo "== Regenerating DutAttributes" |
| 166 | starlark/dut_attributes/generate |