blob: cd8389854104cfc5a77bd62914c75f0acff7a6b9 [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 McAllister91734cf2021-05-20 15:52:47 -060011readonly golden_file="gen/golden_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
104buf build --exclude-imports -o -#format=json ${proto_paths[@]} \
105 | jq -S > ${golden_file}
106
107# Check if golden file changed and offer to submit it for the user.
108if ! git diff --quiet "${golden_file}"; then
109 echo
110 read -p "${golden_file} changed, amend last commit to add it? " -n 1 -r
111 echo
112 if [[ $REPLY =~ ^[Yy]$ ]]; then
113 git add "${golden_file}"
114 git commit --amend --no-edit
115 else
116 echo "Please commit ${golden_file} manually" >&2
117 fi
118else
119 echo "Clean diff on ${golden_file}, nothing else to do." >&2
120fi
121
122echo
123echo "== Linting protobuffers"
124
125# We want to split --path from the filenames so supress warning about quotes.
126# shellcheck disable=2068
127if ! buf lint ${proto_paths[@]}; then
128 echo "One or more files need cleanup" >&2
129 exit
130else
131 echo "Files are clean"
132fi
133
134echo
135echo "== Generating go bindings..."
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -0700136# Clean up existing go bindings.
137find go -name '*.pb.go' -exec rm '{}' \;
Lann Martind8f0c0c2019-03-08 11:00:32 -0700138# Go files need to be processed individually until this is fixed:
139# https://github.com/golang/protobuf/issues/39
Sean McAllister91734cf2021-05-20 15:52:47 -0600140for file in "${proto_files[@]}"; do
141 protoc -Isrc -I"${config_dir}/${cros_config_subdir}" \
142 --go_out=go/ --go_opt=paths=source_relative \
143 --go-grpc_out=go/ --go-grpc_opt=paths=source_relative "${file}";
144done
Vadim Bendebury7dc1e3a2020-11-13 11:08:27 -0800145
146chromite_root="$(readlink -f "$(dirname "$0")/../..")"
147chromite_api_compiler="${chromite_root}/api/compile_build_api_proto"
LaMont Jonesfea7df02021-05-18 10:55:45 -0600148if [[ ${USER} = chrome-bot ]]; then
149 echo "Not running chromite compiler"
150elif [[ -x "${chromite_api_compiler}" ]]; then
Vadim Bendebury7dc1e3a2020-11-13 11:08:27 -0800151 echo "Running chromite compiler"
152 "${chromite_api_compiler}"
153 echo "Don't forget to upload changes generated in ${chromite_root}, if any"
154fi