blob: 5aa6bd0cd6a4570be802d2b1f300f3666f2f6a3a [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}
17mydir="$(dirname -- "${BASH_SOURCE[0]}")"
18test -d "$mydir" || die 'cannot find own directory'
19cd -P -- "$mydir" || die 'cannot chdir to own directory'
20
Sean McAllister91734cf2021-05-20 15:52:47 -060021regenerate_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
29allow_breaking=0
30regen_golden=0
31while [[ $# -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
45done
46
47readonly script_dir="$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")"
48
49# By default we'll use the symlink to src/config.
50config_dir=extern/
51cros_config_subdir=""
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -070052
Sean Abrahambcf2e702021-02-04 10:36:06 -070053if [[ "$(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
58fi
59
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -070060cd "${script_dir}"
Sean McAllister91734cf2021-05-20 15:52:47 -060061source "./setup_cipd.sh"
Lann Martind8f0c0c2019-03-08 11:00:32 -070062
Sean McAllister91734cf2021-05-20 15:52:47 -060063if [[ $regen_golden -eq 1 ]]; then
64 echo "Forcing regenerating of golden proto descriptors."
65 regenerate_golden
66 exit 0
67fi
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.
71if [ ! -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
80fi
81
82echo "protoc version: $(protoc --version)"
83echo "buf version: $(buf --version 2>&1)"
84
85#### protobuffer checks
86mapfile -t proto_files < <(find src -type f -name '*.proto')
87mapfile -t proto_paths < \
88 <(find src -type f -name '*.proto' -exec echo '--path {} ' \;)
89
90echo
91echo "== Checking for breaking protobuffer changes"
92if ! buf breaking --against ${golden_file}; then
93 if [[ $allow_breaking -eq 0 ]]; then
94 (
95 echo
96 cat <<-EOF
97One or more breaking changes detected. If these are intentional, re-run
98with --allow-breaking to allow the changes.
99EOF
100 ) >&2
101 exit 1
102 else
103 cat <<EOF >&2
104One or more breaking changes, but --allow-breaking specified, continuing.
105EOF
106 fi
107fi
108
109echo "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 McAllistere97529c2021-06-01 09:34:28 -0600112buf build --exclude-imports --exclude-source-info \
113 -o -#format=json ${proto_paths[@]} \
Sean McAllister91734cf2021-05-20 15:52:47 -0600114 | jq -S > ${golden_file}
115
116# Check if golden file changed and offer to submit it for the user.
117if ! git diff --quiet "${golden_file}"; then
118 echo
Sean McAllisterbf743292021-06-16 14:59:06 -0600119 echo "Please commit ${golden_file} with your change"
Sean McAllister91734cf2021-05-20 15:52:47 -0600120else
121 echo "Clean diff on ${golden_file}, nothing else to do." >&2
122fi
123
124echo
125echo "== Linting protobuffers"
126
127# We want to split --path from the filenames so supress warning about quotes.
128# shellcheck disable=2068
129if ! buf lint ${proto_paths[@]}; then
130 echo "One or more files need cleanup" >&2
131 exit
132else
133 echo "Files are clean"
134fi
135
136echo
137echo "== Generating go bindings..."
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -0700138# Clean up existing go bindings.
139find go -name '*.pb.go' -exec rm '{}' \;
Lann Martind8f0c0c2019-03-08 11:00:32 -0700140# Go files need to be processed individually until this is fixed:
141# https://github.com/golang/protobuf/issues/39
Sean McAllister91734cf2021-05-20 15:52:47 -0600142for 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}";
146done
Greg NISBETfddeb872023-02-09 17:05:22 -0800147echo "== Formatting everything..."
148test -d "./go" || die 'go dir does not exist'
149gofmt -s -w "./go" || die 'failed to format source directory'
Vadim Bendebury7dc1e3a2020-11-13 11:08:27 -0800150
151chromite_root="$(readlink -f "$(dirname "$0")/../..")"
152chromite_api_compiler="${chromite_root}/api/compile_build_api_proto"
LaMont Jonesfea7df02021-05-18 10:55:45 -0600153if [[ ${USER} = chrome-bot ]]; then
154 echo "Not running chromite compiler"
155elif [[ -x "${chromite_api_compiler}" ]]; then
Vadim Bendebury7dc1e3a2020-11-13 11:08:27 -0800156 echo "Running chromite compiler"
157 "${chromite_api_compiler}"
158 echo "Don't forget to upload changes generated in ${chromite_root}, if any"
159fi