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