1#!/bin/bash
2# Copyright 2023 The gRPC Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# Generates dockerimage_current_versions.bzl file from the .current_version
17# files found throughout the repository.
18
19set -e
20
21cd $(dirname $0)/../..
22
23OUTPUT_FILE=tools/bazelify_tests/dockerimage_current_versions.bzl
24if [ "${CHECK_MODE}" != "" ]
25then
26  # generate into temporary file instead
27  ORIG_FILE="${OUTPUT_FILE}"
28  OUTPUT_FILE="$(mktemp)"
29fi
30
31# Generate license header and module docstring.
32cat >"${OUTPUT_FILE}" << EOF
33# Copyright 2023 The gRPC Authors
34#
35# Licensed under the Apache License, Version 2.0 (the "License");
36# you may not use this file except in compliance with the License.
37# You may obtain a copy of the License at
38#
39#     http://www.apache.org/licenses/LICENSE-2.0
40#
41# Unless required by applicable law or agreed to in writing, software
42# distributed under the License is distributed on an "AS IS" BASIS,
43# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44# See the License for the specific language governing permissions and
45# limitations under the License.
46
47"""
48This file is generated by generate_dockerimage_current_versions_bzl.sh
49It makes the info from testing docker image *.current_version files
50accessible to bazel builds.
51"""
52
53EOF
54
55echo "DOCKERIMAGE_CURRENT_VERSIONS = {" >>"${OUTPUT_FILE}"
56
57for current_version_file in $(find tools/ third_party/rake-compiler-dock -name '*.current_version' | LC_ALL=C sort)
58do
59  # Remove the docker image tag and only keep the SHA256 digest for the dockerimage.
60  # For some reason, bazel's "container-image" exec property doesn't accept image spec
61  # with both tag and SHA256 digest.
62  echo "    \"${current_version_file}\": \"docker://$(cat ${current_version_file} | sed 's/:[a-f0-9]*@sha256/@sha256/')\"," >>"${OUTPUT_FILE}"
63done
64
65echo "}" >>"${OUTPUT_FILE}"
66
67if [ "${CHECK_MODE}" != "" ]
68then
69  echo "Checking that ${ORIG_FILE} is up-to-date."
70  diff "${OUTPUT_FILE}" "${ORIG_FILE}" || CHECK_FAILED=true
71
72  if [ "${CHECK_FAILED}" != "" ]
73  then
74    echo "CHECK FAILED: Generated file ${ORIG_FILE} is out of date and needs to be regenerated."
75    exit 1
76  else
77    echo "${ORIG_FILE} is up-to-date."
78  fi
79else
80  echo "Generated ${OUTPUT_FILE}"
81fi
82