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