blob: 6b05a6d60fe06a1669362f0236e34a651f753945 [file] [log] [blame]
Jacob Kopczynskic6817852020-06-09 14:17:55 -07001#!/bin/bash -e
2# Copyright 2020 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Obtain the most recent proto descriptors from chromiumos/infra/proto protos.
7# This is needed to work with these protos from *.star code.
8
9set -eu
10
11CROS_CONFIG_REPO="https://chromium.googlesource.com/chromiumos/config"
12
13CIPD_PROTOC_VERSION='v3.6.1'
14CIPD_PROTOC_GEN_GO_VERSION='v1.3.2'
15
16readonly script_dir="$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")"
17readonly target="${script_dir}/proto/descpb.bin"
18
19readonly work_dir=$(mktemp --tmpdir -d genprotodescXXXXXX)
20trap "rm -rf ${work_dir}" EXIT
21echo "Using temporary directory ${work_dir}"
22
23if [[ -n ${CHROMIUMOS_CONFIG_DIR+x} ]]; then
24 echo "CHROMIUMOS_CONFIG_DIR is set: " \
25 "Copying sources from ${CHROMIUMOS_CONFIG_DIR}/"
26 cp -r "${CHROMIUMOS_CONFIG_DIR}/" "${work_dir}/config"
27else
28 echo "Creating a shallow clone of ${CROS_CONFIG_REPO}"
29 git clone -q --depth=1 --shallow-submodules "${CROS_CONFIG_REPO}" \
30 "${work_dir}/config"
31fi
32readonly cros_config_subdir="config/proto"
33
34echo "Grabbing protoc from CIPD"
35cipd_root=.cipd_bin
36cipd ensure \
37 -log-level warning \
38 -root "${cipd_root}" \
39 -ensure-file - \
40 <<ENSURE_FILE
41infra/tools/protoc/\${platform} protobuf_version:${CIPD_PROTOC_VERSION}
42chromiumos/infra/tools/protoc-gen-go version:${CIPD_PROTOC_GEN_GO_VERSION}
43ENSURE_FILE
44
45# Need full path here for cipd as there is a cd to a different directory.
46export PATH="${script_dir}/${cipd_root}:${PATH}"
47
48echo "Generating protobuf descriptors"
49readonly all_protos=$(
50 cd "${work_dir}" &&
51 find "${cros_config_subdir}" -name "*.proto" | sort
52)
53(
54 cd "${work_dir}" &&
55 export LC_ALL=C # for stable sorting order
56 protoc -I"${cros_config_subdir}" \
57 --descriptor_set_out=descpb.bin ${all_protos}
58)
59
60echo "Copying generated descriptors"
61cp "${work_dir}/descpb.bin" "${target}"