blob: eb70e7a96dea5c8f344c3a9ab66f0e30edfebcf5 [file] [log] [blame]
Lann Martind8f0c0c2019-03-08 11:00:32 -07001#!/bin/bash -e
Sean Abrahamfdabbe72019-03-13 09:03:37 -06002#
Greg NISBETfddeb872023-02-09 17:05:22 -08003# Copyright 2023 The ChromiumOS Authors. All rights reserved.
Sean Abrahamfdabbe72019-03-13 09:03:37 -06004# 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 Martind8f0c0c2019-03-08 11:00:32 -07008
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -07009CROS_CONFIG_REPO="https://chromium.googlesource.com/chromiumos/config"
10
Sean McAllister80943c92021-10-08 09:12:50 -060011readonly golden_file="gen/descriptors.json"
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -070012
Greg NISBETfddeb872023-02-09 17:05:22 -080013die() {
14 1>&2 printf '%s\n' "$@"
15 exit 1
16}
Greg NISBETfddeb872023-02-09 17:05:22 -080017
Sean McAllister91734cf2021-05-20 15:52:47 -060018regenerate_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
26allow_breaking=0
27regen_golden=0
28while [[ $# -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
42done
43
44readonly script_dir="$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")"
45
46# By default we'll use the symlink to src/config.
47config_dir=extern/
48cros_config_subdir=""
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -070049
Sean Abrahambcf2e702021-02-04 10:36:06 -070050if [[ "$(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
55fi
56
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -070057cd "${script_dir}"
Sean McAllister91734cf2021-05-20 15:52:47 -060058source "./setup_cipd.sh"
Lann Martind8f0c0c2019-03-08 11:00:32 -070059
Sean McAllister91734cf2021-05-20 15:52:47 -060060if [[ $regen_golden -eq 1 ]]; then
61 echo "Forcing regenerating of golden proto descriptors."
62 regenerate_golden
63 exit 0
64fi
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.
68if [ ! -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
77fi
78
79echo "protoc version: $(protoc --version)"
80echo "buf version: $(buf --version 2>&1)"
81
82#### protobuffer checks
83mapfile -t proto_files < <(find src -type f -name '*.proto')
84mapfile -t proto_paths < \
85 <(find src -type f -name '*.proto' -exec echo '--path {} ' \;)
86
87echo
88echo "== Checking for breaking protobuffer changes"
89if ! buf breaking --against ${golden_file}; then
90 if [[ $allow_breaking -eq 0 ]]; then
91 (
92 echo
93 cat <<-EOF
94One or more breaking changes detected. If these are intentional, re-run
95with --allow-breaking to allow the changes.
96EOF
97 ) >&2
98 exit 1
99 else
100 cat <<EOF >&2
101One or more breaking changes, but --allow-breaking specified, continuing.
102EOF
103 fi
104fi
105
106echo "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 McAllistere97529c2021-06-01 09:34:28 -0600109buf build --exclude-imports --exclude-source-info \
110 -o -#format=json ${proto_paths[@]} \
Sean McAllister91734cf2021-05-20 15:52:47 -0600111 | jq -S > ${golden_file}
112
113# Check if golden file changed and offer to submit it for the user.
114if ! git diff --quiet "${golden_file}"; then
115 echo
Sean McAllisterbf743292021-06-16 14:59:06 -0600116 echo "Please commit ${golden_file} with your change"
Sean McAllister91734cf2021-05-20 15:52:47 -0600117else
118 echo "Clean diff on ${golden_file}, nothing else to do." >&2
119fi
120
121echo
122echo "== Linting protobuffers"
123
124# We want to split --path from the filenames so supress warning about quotes.
125# shellcheck disable=2068
126if ! buf lint ${proto_paths[@]}; then
127 echo "One or more files need cleanup" >&2
Elib06a1fe2023-04-26 20:31:57 +0000128 exit 1 # failing lint should block presubmit as it prevents pb file gen
Sean McAllister91734cf2021-05-20 15:52:47 -0600129else
130 echo "Files are clean"
131fi
132
133echo
134echo "== Generating go bindings..."
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -0700135# Clean up existing go bindings.
136find go -name '*.pb.go' -exec rm '{}' \;
Lann Martind8f0c0c2019-03-08 11:00:32 -0700137# Go files need to be processed individually until this is fixed:
138# https://github.com/golang/protobuf/issues/39
Sean McAllister91734cf2021-05-20 15:52:47 -0600139for 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}";
143done
Greg NISBETfddeb872023-02-09 17:05:22 -0800144echo "== Formatting everything..."
145test -d "./go" || die 'go dir does not exist'
146gofmt -s -w "./go" || die 'failed to format source directory'
Vadim Bendebury7dc1e3a2020-11-13 11:08:27 -0800147
148chromite_root="$(readlink -f "$(dirname "$0")/../..")"
149chromite_api_compiler="${chromite_root}/api/compile_build_api_proto"
LaMont Jonesfea7df02021-05-18 10:55:45 -0600150if [[ ${USER} = chrome-bot ]]; then
151 echo "Not running chromite compiler"
152elif [[ -x "${chromite_api_compiler}" ]]; then
Vadim Bendebury7dc1e3a2020-11-13 11:08:27 -0800153 echo "Running chromite compiler"
154 "${chromite_api_compiler}"
155 echo "Don't forget to upload changes generated in ${chromite_root}, if any"
156fi