blob: 05a79a09449aca33cf6bb09c9f071bf6cade6603 [file] [log] [blame]
Lann Martind8f0c0c2019-03-08 11:00:32 -07001#!/bin/bash -e
Sean Abrahamfdabbe72019-03-13 09:03:37 -06002#
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 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
Sean McAllister91734cf2021-05-20 15:52:47 -060013regenerate_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
21allow_breaking=0
22regen_golden=0
23while [[ $# -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
37done
38
39readonly script_dir="$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")"
40
41# By default we'll use the symlink to src/config.
42config_dir=extern/
43cros_config_subdir=""
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -070044
Sean Abrahambcf2e702021-02-04 10:36:06 -070045if [[ "$(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
50fi
51
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -070052cd "${script_dir}"
Sean McAllister91734cf2021-05-20 15:52:47 -060053source "./setup_cipd.sh"
Lann Martind8f0c0c2019-03-08 11:00:32 -070054
Sean McAllister91734cf2021-05-20 15:52:47 -060055if [[ $regen_golden -eq 1 ]]; then
56 echo "Forcing regenerating of golden proto descriptors."
57 regenerate_golden
58 exit 0
59fi
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.
63if [ ! -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
72fi
73
74echo "protoc version: $(protoc --version)"
75echo "buf version: $(buf --version 2>&1)"
76
77#### protobuffer checks
78mapfile -t proto_files < <(find src -type f -name '*.proto')
79mapfile -t proto_paths < \
80 <(find src -type f -name '*.proto' -exec echo '--path {} ' \;)
81
82echo
83echo "== Checking for breaking protobuffer changes"
84if ! buf breaking --against ${golden_file}; then
85 if [[ $allow_breaking -eq 0 ]]; then
86 (
87 echo
88 cat <<-EOF
89One or more breaking changes detected. If these are intentional, re-run
90with --allow-breaking to allow the changes.
91EOF
92 ) >&2
93 exit 1
94 else
95 cat <<EOF >&2
96One or more breaking changes, but --allow-breaking specified, continuing.
97EOF
98 fi
99fi
100
101echo "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 McAllistere97529c2021-06-01 09:34:28 -0600104buf build --exclude-imports --exclude-source-info \
105 -o -#format=json ${proto_paths[@]} \
Sean McAllister91734cf2021-05-20 15:52:47 -0600106 | jq -S > ${golden_file}
107
108# Check if golden file changed and offer to submit it for the user.
109if ! git diff --quiet "${golden_file}"; then
110 echo
Sean McAllisterbf743292021-06-16 14:59:06 -0600111 echo "Please commit ${golden_file} with your change"
Sean McAllister91734cf2021-05-20 15:52:47 -0600112else
113 echo "Clean diff on ${golden_file}, nothing else to do." >&2
114fi
115
116echo
117echo "== Linting protobuffers"
118
119# We want to split --path from the filenames so supress warning about quotes.
120# shellcheck disable=2068
121if ! buf lint ${proto_paths[@]}; then
122 echo "One or more files need cleanup" >&2
123 exit
124else
125 echo "Files are clean"
126fi
127
128echo
129echo "== Generating go bindings..."
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -0700130# Clean up existing go bindings.
131find go -name '*.pb.go' -exec rm '{}' \;
Lann Martind8f0c0c2019-03-08 11:00:32 -0700132# Go files need to be processed individually until this is fixed:
133# https://github.com/golang/protobuf/issues/39
Sean McAllister91734cf2021-05-20 15:52:47 -0600134for file in "${proto_files[@]}"; do
135 protoc -Isrc -I"${config_dir}/${cros_config_subdir}" \
136 --go_out=go/ --go_opt=paths=source_relative \
137 --go-grpc_out=go/ --go-grpc_opt=paths=source_relative "${file}";
138done
Vadim Bendebury7dc1e3a2020-11-13 11:08:27 -0800139
140chromite_root="$(readlink -f "$(dirname "$0")/../..")"
141chromite_api_compiler="${chromite_root}/api/compile_build_api_proto"
LaMont Jonesfea7df02021-05-18 10:55:45 -0600142if [[ ${USER} = chrome-bot ]]; then
143 echo "Not running chromite compiler"
144elif [[ -x "${chromite_api_compiler}" ]]; then
Vadim Bendebury7dc1e3a2020-11-13 11:08:27 -0800145 echo "Running chromite compiler"
146 "${chromite_api_compiler}"
147 echo "Don't forget to upload changes generated in ${chromite_root}, if any"
148fi