Lann Martin | d8f0c0c | 2019-03-08 11:00:32 -0700 | [diff] [blame] | 1 | #!/bin/bash -e |
Sean Abraham | fdabbe7 | 2019-03-13 09:03:37 -0600 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2019 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 protos in this repo to produce generated proto code. |
Lann Martin | d8f0c0c | 2019-03-08 11:00:32 -0700 | [diff] [blame] | 8 | |
Prathmesh Prabhu | 6ade1ae | 2020-03-09 12:46:39 -0700 | [diff] [blame] | 9 | CROS_CONFIG_REPO="https://chromium.googlesource.com/chromiumos/config" |
| 10 | |
Sean McAllister | 91734cf | 2021-05-20 15:52:47 -0600 | [diff] [blame] | 11 | readonly golden_file="gen/golden_descriptors.json" |
Prathmesh Prabhu | 6ade1ae | 2020-03-09 12:46:39 -0700 | [diff] [blame] | 12 | |
Sean McAllister | 91734cf | 2021-05-20 15:52:47 -0600 | [diff] [blame] | 13 | regenerate_golden() { |
| 14 | # We want to split --path from the filenames so silence warning. |
| 15 | # shellcheck disable=2068 |
| 16 | buf build --exclude-imports -o -#format=json ${proto_paths[@]} \ |
| 17 | | jq -S > ${golden_file} |
| 18 | } |
| 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 | readonly script_dir="$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")" |
| 40 | |
| 41 | # By default we'll use the symlink to src/config. |
| 42 | config_dir=extern/ |
| 43 | cros_config_subdir="" |
Prathmesh Prabhu | 6ade1ae | 2020-03-09 12:46:39 -0700 | [diff] [blame] | 44 | |
Sean Abraham | bcf2e70 | 2021-02-04 10:36:06 -0700 | [diff] [blame] | 45 | if [[ "$(uname -s)" != "Linux" || "$(uname -m)" != "x86_64" ]]; then |
| 46 | echo "Error: currently generate.sh can only run on Linux x86_64 systems." |
| 47 | echo "This is because we use checked-in binaries for protoc-gen-go(-grpc)?." |
| 48 | echo "This will change soon though. See https://crbug.com/1174238" |
| 49 | exit 1 |
| 50 | fi |
| 51 | |
Prathmesh Prabhu | 6ade1ae | 2020-03-09 12:46:39 -0700 | [diff] [blame] | 52 | cd "${script_dir}" |
Sean McAllister | 91734cf | 2021-05-20 15:52:47 -0600 | [diff] [blame] | 53 | source "./setup_cipd.sh" |
Lann Martin | d8f0c0c | 2019-03-08 11:00:32 -0700 | [diff] [blame] | 54 | |
Sean McAllister | 91734cf | 2021-05-20 15:52:47 -0600 | [diff] [blame] | 55 | if [[ $regen_golden -eq 1 ]]; then |
| 56 | echo "Forcing regenerating of golden proto descriptors." |
| 57 | regenerate_golden |
| 58 | exit 0 |
| 59 | fi |
| 60 | |
| 61 | # If we don't have src/config checked out too, then check out our own copy and |
| 62 | # stash it in the .generate directory. |
| 63 | if [ ! -e "extern/chromiumos" ]; then |
| 64 | config_dir=./.generate |
| 65 | cros_config_subdir="config/proto" |
| 66 | |
| 67 | echo "Creating a shallow clone of ${CROS_CONFIG_REPO}" |
| 68 | git clone -q --depth=1 --shallow-submodules "${CROS_CONFIG_REPO}" \ |
| 69 | ".generate/config" |
| 70 | |
| 71 | trap "rm -rf .generate/*" EXIT |
| 72 | fi |
| 73 | |
| 74 | echo "protoc version: $(protoc --version)" |
| 75 | echo "buf version: $(buf --version 2>&1)" |
| 76 | |
| 77 | #### protobuffer checks |
| 78 | mapfile -t proto_files < <(find src -type f -name '*.proto') |
| 79 | mapfile -t proto_paths < \ |
| 80 | <(find src -type f -name '*.proto' -exec echo '--path {} ' \;) |
| 81 | |
| 82 | echo |
| 83 | echo "== Checking for breaking protobuffer changes" |
| 84 | if ! buf breaking --against ${golden_file}; then |
| 85 | if [[ $allow_breaking -eq 0 ]]; then |
| 86 | ( |
| 87 | echo |
| 88 | cat <<-EOF |
| 89 | One or more breaking changes detected. If these are intentional, re-run |
| 90 | with --allow-breaking to allow the changes. |
| 91 | EOF |
| 92 | ) >&2 |
| 93 | exit 1 |
| 94 | else |
| 95 | cat <<EOF >&2 |
| 96 | One or more breaking changes, but --allow-breaking specified, continuing. |
| 97 | EOF |
| 98 | fi |
| 99 | fi |
| 100 | |
| 101 | echo "No breaking changes, regenerating '${golden_file}'" |
| 102 | # We want to split --path from the filenames so supress warning about quotes. |
| 103 | # shellcheck disable=2068 |
Sean McAllister | e97529c | 2021-06-01 09:34:28 -0600 | [diff] [blame^] | 104 | buf build --exclude-imports --exclude-source-info \ |
| 105 | -o -#format=json ${proto_paths[@]} \ |
Sean McAllister | 91734cf | 2021-05-20 15:52:47 -0600 | [diff] [blame] | 106 | | jq -S > ${golden_file} |
| 107 | |
| 108 | # Check if golden file changed and offer to submit it for the user. |
| 109 | if ! git diff --quiet "${golden_file}"; then |
| 110 | echo |
| 111 | read -p "${golden_file} changed, amend last commit to add it? " -n 1 -r |
| 112 | echo |
| 113 | if [[ $REPLY =~ ^[Yy]$ ]]; then |
| 114 | git add "${golden_file}" |
| 115 | git commit --amend --no-edit |
| 116 | else |
| 117 | echo "Please commit ${golden_file} manually" >&2 |
| 118 | fi |
| 119 | else |
| 120 | echo "Clean diff on ${golden_file}, nothing else to do." >&2 |
| 121 | fi |
| 122 | |
| 123 | echo |
| 124 | echo "== Linting protobuffers" |
| 125 | |
| 126 | # We want to split --path from the filenames so supress warning about quotes. |
| 127 | # shellcheck disable=2068 |
| 128 | if ! buf lint ${proto_paths[@]}; then |
| 129 | echo "One or more files need cleanup" >&2 |
| 130 | exit |
| 131 | else |
| 132 | echo "Files are clean" |
| 133 | fi |
| 134 | |
| 135 | echo |
| 136 | echo "== Generating go bindings..." |
Prathmesh Prabhu | 6ade1ae | 2020-03-09 12:46:39 -0700 | [diff] [blame] | 137 | # Clean up existing go bindings. |
| 138 | find go -name '*.pb.go' -exec rm '{}' \; |
Lann Martin | d8f0c0c | 2019-03-08 11:00:32 -0700 | [diff] [blame] | 139 | # Go files need to be processed individually until this is fixed: |
| 140 | # https://github.com/golang/protobuf/issues/39 |
Sean McAllister | 91734cf | 2021-05-20 15:52:47 -0600 | [diff] [blame] | 141 | for file in "${proto_files[@]}"; do |
| 142 | protoc -Isrc -I"${config_dir}/${cros_config_subdir}" \ |
| 143 | --go_out=go/ --go_opt=paths=source_relative \ |
| 144 | --go-grpc_out=go/ --go-grpc_opt=paths=source_relative "${file}"; |
| 145 | done |
Vadim Bendebury | 7dc1e3a | 2020-11-13 11:08:27 -0800 | [diff] [blame] | 146 | |
| 147 | chromite_root="$(readlink -f "$(dirname "$0")/../..")" |
| 148 | chromite_api_compiler="${chromite_root}/api/compile_build_api_proto" |
LaMont Jones | fea7df0 | 2021-05-18 10:55:45 -0600 | [diff] [blame] | 149 | if [[ ${USER} = chrome-bot ]]; then |
| 150 | echo "Not running chromite compiler" |
| 151 | elif [[ -x "${chromite_api_compiler}" ]]; then |
Vadim Bendebury | 7dc1e3a | 2020-11-13 11:08:27 -0800 | [diff] [blame] | 152 | echo "Running chromite compiler" |
| 153 | "${chromite_api_compiler}" |
| 154 | echo "Don't forget to upload changes generated in ${chromite_root}, if any" |
| 155 | fi |