1#!/bin/bash -e 2# Copyright 2023 The ChromiumOS Authors 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 9if [[ -z "$1" ]] ; then 10 echo "ERROR: $0 argument required: destination folder for descpb.bin." 11 exit 1 12fi 13 14set -eu 15 16PROTO_REPO="https://chromium.googlesource.com/chromiumos/infra/proto" 17CROS_CONFIG_REPO="https://chromium.googlesource.com/chromiumos/config" 18 19CIPD_PROTOC_VERSION='v3.17.0' 20CIPD_PROTOC_GEN_GO_VERSION='v1.3.2' 21 22script_dir="$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")" 23readonly script_dir 24 25readonly target="$1/descpb.bin" 26 27 28work_dir=$(mktemp --tmpdir -d genprotodescXXXXXX) 29readonly work_dir 30trap 'rm -rf ${work_dir}' EXIT 31echo "Using temporary directory ${work_dir}" 32 33if [[ -n ${CHROMIUMOS_PROTO_DIR+x} ]]; then 34 echo "CHROMIUMOS_PROTO_DIR is set: " \ 35 "Copying sources from ${CHROMIUMOS_PROTO_DIR}/" 36 cp -r "${CHROMIUMOS_PROTO_DIR}/" "${work_dir}/proto" 37else 38 echo "Creating a shallow clone of ${PROTO_REPO}" 39 git clone -q --depth=1 --shallow-submodules "${PROTO_REPO}" \ 40 "${work_dir}/proto" 41fi 42readonly proto_subdir="proto/src" 43 44if [[ -n ${CHROMIUMOS_CONFIG_DIR+x} ]]; then 45 echo "CHROMIUMOS_CONFIG_DIR is set: " \ 46 "Copying sources from ${CHROMIUMOS_CONFIG_DIR}/" 47 cp -r "${CHROMIUMOS_CONFIG_DIR}/" "${work_dir}/config" 48else 49 echo "Creating a shallow clone of ${CROS_CONFIG_REPO}" 50 git clone -q --depth=1 --shallow-submodules "${CROS_CONFIG_REPO}" \ 51 "${work_dir}/config" 52fi 53readonly cros_config_subdir="config/proto" 54 55echo "Grabbing protoc from CIPD" 56cipd_root="${script_dir}/.cipd_bin" 57cipd ensure \ 58 -log-level warning \ 59 -root "${cipd_root}" \ 60 -ensure-file - \ 61 <<ENSURE_FILE 62infra/tools/protoc/\${platform} protobuf_version:${CIPD_PROTOC_VERSION} 63chromiumos/infra/tools/protoc-gen-go version:${CIPD_PROTOC_GEN_GO_VERSION} 64ENSURE_FILE 65 66export PATH="${cipd_root}:${PATH}" 67 68mapfile -t all_protos < <( 69 cd "${work_dir}" && 70 find \ 71 "${proto_subdir}" \ 72 "${cros_config_subdir}" \ 73 -name "*.proto" | sort 74) 75readonly all_protos 76( 77 cd "${work_dir}" && 78 export LC_ALL=C # for stable sorting order 79 protoc \ 80 -I"${proto_subdir}" \ 81 -I"${cros_config_subdir}" \ 82 --descriptor_set_out=descpb.bin "${all_protos[@]}" 83) 84 85echo "Copying generated descriptors" 86cp "${work_dir}/descpb.bin" "${target}" 87