blob: df85d5601f2ad5a178538b3011a0aea1358a7f2e [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
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -070011readonly script_dir="$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")"
Allen Li4f211142020-02-21 17:31:10 -080012source "${script_dir}/setup_cipd.sh"
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -070013
14readonly work_dir=$(mktemp --tmpdir -d genprotoXXXXXX)
15trap "rm -rf ${work_dir}" EXIT
16echo "Using temporary directory ${work_dir}"
17
Sean Abrahambcf2e702021-02-04 10:36:06 -070018if [[ "$(uname -s)" != "Linux" || "$(uname -m)" != "x86_64" ]]; then
19 echo "Error: currently generate.sh can only run on Linux x86_64 systems."
20 echo "This is because we use checked-in binaries for protoc-gen-go(-grpc)?."
21 echo "This will change soon though. See https://crbug.com/1174238"
22 exit 1
23fi
24
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -070025if [[ -n ${CHROMIUMOS_CONFIG_DIR+x} ]]; then
26 echo "CHROMIUMOS_CONFIG_DIR is set: " \
27 "Copying sources from ${CHROMIUMOS_CONFIG_DIR}/"
28 cp -r "${CHROMIUMOS_CONFIG_DIR}/" "${work_dir}/config"
29else
30 echo "Creating a shallow clone of ${CROS_CONFIG_REPO}"
31 git clone -q --depth=1 --shallow-submodules "${CROS_CONFIG_REPO}" \
32 "${work_dir}/config"
33fi
34readonly cros_config_subdir="config/proto"
Lann Martind8f0c0c2019-03-08 11:00:32 -070035
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -070036cd "${script_dir}"
Lann Martind8f0c0c2019-03-08 11:00:32 -070037
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -070038echo "Generating go bindings..."
39# Clean up existing go bindings.
40find go -name '*.pb.go' -exec rm '{}' \;
Lann Martind8f0c0c2019-03-08 11:00:32 -070041# Go files need to be processed individually until this is fixed:
42# https://github.com/golang/protobuf/issues/39
43find src -name '*.proto' -exec \
Prathmesh Prabhu6ade1ae2020-03-09 12:46:39 -070044 protoc -Isrc -I"${work_dir}/${cros_config_subdir}" \
Sean Abraham17db9e82021-02-03 14:05:53 -070045 --go_out=go/ --go_opt=paths=source_relative \
46 --go-grpc_out=go/ --go-grpc_opt=paths=source_relative '{}' \;
Vadim Bendebury7dc1e3a2020-11-13 11:08:27 -080047
48chromite_root="$(readlink -f "$(dirname "$0")/../..")"
49chromite_api_compiler="${chromite_root}/api/compile_build_api_proto"
LaMont Jonesfea7df02021-05-18 10:55:45 -060050if [[ ${USER} = chrome-bot ]]; then
51 echo "Not running chromite compiler"
52elif [[ -x "${chromite_api_compiler}" ]]; then
Vadim Bendebury7dc1e3a2020-11-13 11:08:27 -080053 echo "Running chromite compiler"
54 "${chromite_api_compiler}"
55 echo "Don't forget to upload changes generated in ${chromite_root}, if any"
56fi