blob: 5dd624588c55b79488b768413d8175cc7da76c6d [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 McAllister8704ebd2021-06-23 13:38:27 -060012readonly golden_file="gen/golden_descriptors.json"
13
14regenerate_golden() {
15 # We want to split --path from the filenames so silence warning.
16 # shellcheck disable=2068
17 buf build --exclude-imports -o -#format=json ${proto_paths[@]} \
18 | jq -S > "${golden_file}"
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
39script_dir="$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")"
40readonly script_dir
41
Prathmesh Prabhu42207542020-04-20 23:13:23 -070042cd "${script_dir}"
C Shapiroe8a8f802021-06-30 15:21:46 -050043./generate_grpc_py_bindings.sh
Sean McAllister8704ebd2021-06-23 13:38:27 -060044source "./setup_cipd.sh" # uses script_dir
45
46if [[ "${regen_golden}" -eq 1 ]]; then
47 echo "Forcing regenerating of golden proto descriptors."
48 regenerate_golden
49 exit 0
50fi
51
52echo "protoc version: $(protoc --version)"
53echo "buf version: $(buf --version 2>&1)"
54
55#### protobuffer checks
56mapfile -t proto_files < <(find proto/ -type f -name '*.proto')
57mapfile -t proto_paths < \
58 <(find proto/ -type f -name '*.proto' -exec echo '--path {} ' \;)
59
60echo
61echo "== Checking for breaking protobuffer changes"
62if ! buf breaking --against "${golden_file}"; then
63 if [[ "${allow_breaking}" -eq 0 ]]; then
64 (
65 echo
66 cat <<-EOF
67One or more breaking changes detected. If these are intentional, re-run
68with --allow-breaking to allow the changes.
69EOF
70 ) >&2
71 exit 1
72 else
73 cat <<EOF >&2
74One or more breaking changes, but --allow-breaking specified, continuing.
75EOF
76 fi
77fi
78
79echo "No breaking changes, regenerating '${golden_file}'"
80# We want to split --path from the filenames so suppress warning about quotes.
81# shellcheck disable=2068
82buf build --exclude-imports --exclude-source-info \
83 -o -#format=json ${proto_paths[@]} \
84 | jq -S > "${golden_file}"
85
86# Check if golden file changed and offer to submit it for the user.
87if ! git diff --quiet "${golden_file}"; then
88 echo
89 echo "Please commit ${golden_file} with your change"
90else
91 echo "Clean diff on ${golden_file}, nothing else to do." >&2
92fi
93
94echo
95echo "== Linting protobuffers"
96
97# We want to split --path from the filenames so suppress warning about quotes.
98# shellcheck disable=2068
99if ! buf lint ${proto_paths[@]}; then
100 echo "One or more files need cleanup" >&2
101 exit
102else
103 echo "Files are clean"
104fi
105
106echo
107echo "== Generating Python bindings"
David Burgerc33d1eb2020-01-21 14:24:50 -0700108
David Burger78d3e622020-05-07 19:18:25 -0600109# Remove files from prior python protocol buffer code generation in
110# case any .proto files have been removed.
111find python/ -type f -name '*_pb2.py' -delete
112find python/chromiumos -mindepth 1 -type d -not -name __pycache__ \
113 -exec rm -f '{}/__init__.py' \;
114
Alex Zamorzaev6f41e3d2020-06-08 19:46:01 +0000115PATH="${CIPD_ROOT}" protoc -Iproto \
David Burger7f5a8fb2020-06-01 10:17:32 -0600116 --descriptor_set_out=util/bindings/descpb.bin \
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
Alex Zamorzaev6f41e3d2020-06-08 19:46:01 +0000144cp -rf "${GO_TEMP_DIR}"/chromiumos/config/* go/
C Shapiroca31c6d2021-05-05 09:29:19 -0500145cp "${GO_TEMP_DIR}"/chromiumos/*.go go/
C Shapiro0eba6392021-05-05 11:57:19 -0500146cp "${GO_TEMP_DIR}"/chromiumos/longrunning/*.go go/longrunning
Sean McAllisterb8a979f2021-06-01 10:27:00 -0600147cp -rf "${GO_TEMP_DIR}"/chromiumos/build/* go/build/
Andrew Lamb7a582872021-03-29 15:09:24 -0600148cp -rf "${GO_TEMP_DIR}"/chromiumos/test/* go/test