blob: 548c726d337786ea7d4adb68a0226130e93ff6dd [file] [log] [blame]
David Burgerc33d1eb2020-01-21 14:24:50 -07001#!/bin/bash -e
2#
3# Copyright 2020 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 configuration protos to produce generated proto code.
8
David Burger1b845362020-02-13 14:43:37 -07009# Allows the recursive glob for proto files below to work.
10shopt -s globstar
11
Sean McAllisterc8c43062021-09-10 10:47:22 -060012readonly desc_file="generated/descriptors.json"
13readonly gen_owners="generated/OWNERS"
Sean McAllister8704ebd2021-06-23 13:38:27 -060014
15regenerate_golden() {
16 # We want to split --path from the filenames so silence warning.
17 # shellcheck disable=2068
18 buf build --exclude-imports -o -#format=json ${proto_paths[@]} \
Sean McAllister80e02ff2021-08-28 12:47:06 -060019 | jq -S > "${desc_file}"
Sean McAllister8704ebd2021-06-23 13:38:27 -060020}
21
22allow_breaking=0
23regen_golden=0
24while [[ $# -gt 0 ]]; do
25 case $1 in
26 --allow-breaking)
27 allow_breaking=1
28 shift
29 ;;
30 --force-regen-golden)
31 regen_golden=1
32 shift
33 ;;
34 *)
35 break
36 ;;
37 esac
38done
39
40script_dir="$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")"
41readonly script_dir
42
Prathmesh Prabhu42207542020-04-20 23:13:23 -070043cd "${script_dir}"
C Shapiroe8a8f802021-06-30 15:21:46 -050044./generate_grpc_py_bindings.sh
Sean McAllister8704ebd2021-06-23 13:38:27 -060045source "./setup_cipd.sh" # uses script_dir
46
47if [[ "${regen_golden}" -eq 1 ]]; then
48 echo "Forcing regenerating of golden proto descriptors."
49 regenerate_golden
50 exit 0
51fi
52
53echo "protoc version: $(protoc --version)"
54echo "buf version: $(buf --version 2>&1)"
55
56#### protobuffer checks
57mapfile -t proto_files < <(find proto/ -type f -name '*.proto')
58mapfile -t proto_paths < \
59 <(find proto/ -type f -name '*.proto' -exec echo '--path {} ' \;)
60
61echo
62echo "== Checking for breaking protobuffer changes"
Sean McAllister80e02ff2021-08-28 12:47:06 -060063if ! buf breaking --against "${desc_file}"; then
Sean McAllister8704ebd2021-06-23 13:38:27 -060064 if [[ "${allow_breaking}" -eq 0 ]]; then
65 (
66 echo
67 cat <<-EOF
68One or more breaking changes detected. If these are intentional, re-run
69with --allow-breaking to allow the changes.
70EOF
71 ) >&2
72 exit 1
73 else
74 cat <<EOF >&2
75One or more breaking changes, but --allow-breaking specified, continuing.
76EOF
77 fi
78fi
79
Sean McAllister80e02ff2021-08-28 12:47:06 -060080echo "No breaking changes, regenerating '${desc_file}'"
Sean McAllister8704ebd2021-06-23 13:38:27 -060081# We want to split --path from the filenames so suppress warning about quotes.
82# shellcheck disable=2068
83buf build --exclude-imports --exclude-source-info \
84 -o -#format=json ${proto_paths[@]} \
Sean McAllister80e02ff2021-08-28 12:47:06 -060085 | jq -S > "${desc_file}"
Sean McAllister8704ebd2021-06-23 13:38:27 -060086
Sean McAllister80e02ff2021-08-28 12:47:06 -060087# Check if golden file changed and inform use to commit it.
88if ! git diff --quiet "${desc_file}"; then
Sean McAllister8704ebd2021-06-23 13:38:27 -060089 echo
Sean McAllister80e02ff2021-08-28 12:47:06 -060090 echo "Please commit ${desc_file} with your change"
Sean McAllister8704ebd2021-06-23 13:38:27 -060091else
Sean McAllister80e02ff2021-08-28 12:47:06 -060092 echo "Clean diff on ${desc_file}, nothing else to do." >&2
Sean McAllister8704ebd2021-06-23 13:38:27 -060093fi
94
95echo
96echo "== Linting protobuffers"
97
98# We want to split --path from the filenames so suppress warning about quotes.
99# shellcheck disable=2068
100if ! buf lint ${proto_paths[@]}; then
101 echo "One or more files need cleanup" >&2
102 exit
103else
104 echo "Files are clean"
105fi
106
107echo
108echo "== Generating Python bindings"
David Burgerc33d1eb2020-01-21 14:24:50 -0700109
David Burger78d3e622020-05-07 19:18:25 -0600110# Remove files from prior python protocol buffer code generation in
111# case any .proto files have been removed.
112find python/ -type f -name '*_pb2.py' -delete
113find python/chromiumos -mindepth 1 -type d -not -name __pycache__ \
114 -exec rm -f '{}/__init__.py' \;
115
Alex Zamorzaev6f41e3d2020-06-08 19:46:01 +0000116PATH="${CIPD_ROOT}" protoc -Iproto \
Sean McAllister8704ebd2021-06-23 13:38:27 -0600117 --python_out=python "${proto_files[@]}"
David Riley996c5242020-05-06 11:31:25 -0700118find python/chromiumos -mindepth 1 -type d -not -name __pycache__ \
119 -exec touch '{}/__init__.py' \;
Andrew Lambbc029d32020-02-24 12:42:50 -0700120
Sean McAllister8704ebd2021-06-23 13:38:27 -0600121echo
122echo "== Generating Go bindings"
Prathmesh Prabhu72f8a002020-04-10 09:57:53 -0700123
124# Go bindings are already namespaced under go.chromium.org/chromiumos/config/go
125# We remove the "chromiumos/config" prefix from local path to avoid redundant
126# namespaceing.
David Burger78d3e622020-05-07 19:18:25 -0600127
128# Remove files from prior go protocol buffer code generation in
129# case any .proto files have been removed.
130find go/ -name '*pb.go' -delete
131
Sean McAllister8704ebd2021-06-23 13:38:27 -0600132GO_TEMP_DIR=$(mktemp -d)
133readonly GO_TEMP_DIR
134trap 'rm -rf ${GO_TEMP_DIR}' EXIT
135
Andrew Lambbc029d32020-02-24 12:42:50 -0700136# Go files need to be processed individually until this is fixed:
137# https://github.com/golang/protobuf/issues/39
Sean McAllister8704ebd2021-06-23 13:38:27 -0600138for proto in "${proto_files[@]}"; do
Alex Zamorzaev6f41e3d2020-06-08 19:46:01 +0000139 PATH="${CIPD_ROOT}" protoc -I"proto" \
140 --go_out=plugins=grpc,paths=source_relative:"${GO_TEMP_DIR}" \
Prathmesh Prabhu72f8a002020-04-10 09:57:53 -0700141 "${proto}"
C Shapirob8cd5f92020-03-10 09:47:10 -0500142done
C Shapiro0eba6392021-05-05 11:57:19 -0500143
C Shapirofc742162021-08-15 05:23:19 -0500144echo
145echo "== Generating OWNERS file for generated code paths"
146echo "##### AUTO-GENERATED FILE #####" > "${gen_owners}"
147echo "##### See generate.sh #####" >> "${gen_owners}"
148find proto/* -type f -name "OWNERS*" | xargs cat | grep -E ^include | sort | uniq >> "${gen_owners}"
149find proto/* -type f -name "OWNERS*" | xargs cat | grep -E ^[a-z0-9]+@.+\..+ | sort | uniq >> "${gen_owners}"
150
151if ! git diff --quiet "${gen_owners}"; then
152 echo
153 echo "An OWNERS file was updated in the src/config/proto directory."
154 echo "${gen_owners} was automatically updated based on this change."
155 echo "Please commit ${gen_owners} with your change."
156fi
157
Alex Zamorzaev6f41e3d2020-06-08 19:46:01 +0000158cp -rf "${GO_TEMP_DIR}"/chromiumos/config/* go/
C Shapiroca31c6d2021-05-05 09:29:19 -0500159cp "${GO_TEMP_DIR}"/chromiumos/*.go go/
C Shapiro0eba6392021-05-05 11:57:19 -0500160cp "${GO_TEMP_DIR}"/chromiumos/longrunning/*.go go/longrunning
Sean McAllisterb8a979f2021-06-01 10:27:00 -0600161cp -rf "${GO_TEMP_DIR}"/chromiumos/build/* go/build/
Andrew Lamb7a582872021-03-29 15:09:24 -0600162cp -rf "${GO_TEMP_DIR}"/chromiumos/test/* go/test
Sean McAllisterc8c43062021-09-10 10:47:22 -0600163
164echo
165echo "== Regenerating DutAttributes"
166starlark/dut_attributes/generate