1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#!/bin/bash
# Copyright 2020, 2021, 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Run this script to regenerate the Bazel output in compile.org.
set -eu
# Bazel formats output differently when invoked from Emacs.
export INSIDE_EMACS='28.1,compile'
workspace="$(bazel info workspace)" || exit
testdata="${workspace:?}/testdata"
org="${testdata:?}/compile.org"
dir="$(mktemp -d)" || exit
trap 'rm -r -f -- "${dir}"' EXIT
# Canonicalize the directory so that the textual replacement below doesn’t
# choke on symbolic links.
dir="$(realpath -e -- "${dir:?}")" || exit
# Set up a basic workspace layout in the temporary directory.
emacs --quick --batch --visit="${org:?}" \
--eval="(let ((default-directory \"${dir:?}/\")) (org-babel-tangle))" || exit
execroot="$(cd "${dir:?}" && bazel --bazelrc=/dev/null info execution_root)"
# We explicitly compile with the current directory set to a subdirectory to
# ensure that workspace-relative filenames still work as expected. Invert the
# exit status of “bazel build” because we expect it to fail.
out="$(cd "${dir:?}/package" && ! bazel --bazelrc=/dev/null build //... 2>&1)"
# Replace the arbitrary temporary directory in the output with placeholders.
out="${out//${dir}/%ROOTDIR%}"
# The execution root is somewhere else, but since we only access source files,
# we can virtually merge it with the workspace root.
out="${out//${execroot}/%ROOTDIR%}"
# We can’t use ‘org-babel-detangle’ because there are no links in the tangled
# files.
awk -v out=bazel.out -v src=<(echo "${out:?}") \
-f "${testdata:?}/detangle.awk" \
-- "${org:?}" > "${dir:?}/org.tmp" || exit
cp -- "${dir:?}/org.tmp" "${org:?}" || exit
echo 'Successfully refreshed compile.org'
|