blob: 5317d809ea1d4bba942f4a7ce803ec0cdfb33555 [file] [log] [blame]
brandjonde1babe2020-11-17 14:19:23 -08001#!/bin/bash
2
3# Copyright 2020 The Bazel Authors. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Packages the builtins bzl files as a zip, while preserving their directory
18# structure and expanding any symlinks.
19#
20# Usage:
21# zip_builtins.sh zip output builtins_root files...
22
23set -euo pipefail
24
25origdir="$(pwd)"
26
27# the "zip" command to use; system "zip" if empty
28if [ -z "$1" ]; then
29 zip="zip"
30else
31 zip=$origdir/$1;
32fi; shift
33output=$origdir/$1; shift # zip file to write
34builtins_root=$1; shift # root-relative path to builtins_bzl source dir
35# "$@" contains the paths of the files to archive. They must all be under the
36# source root (no generated files as inputs).
37
38TMPDIR="$(mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX")"
39trap "rm -rf $TMPDIR" EXIT
40
41# Do the cp in origdir so $@ resolves. We'd like to use cp --parents, but macOS
42# doesn't have that flag, so break it out into a loop.
43mkdir -p "$TMPDIR/staging"
44for src in "$@"; do
45 dst="$TMPDIR/staging/$src"
46 mkdir -p $(dirname "$dst")
47 # Make certain to expand any symlinked files (-L).
48 cp -L "$src" "$dst"
49done
50
51cd "$TMPDIR"
52
53# Strip the dir prefix leading up to the builtins root.
54mv "staging/$builtins_root" builtins_bzl
brandjonde1babe2020-11-17 14:19:23 -080055
56# The zip step must take place while cwd is tmpdir, so the paths in the zipfile
57# are relative to tmpdir.
58#
59# For determinism, sort the files and zero out their timestamps before zipping.
60find builtins_bzl -type f -print0 | xargs -0 touch -t 198001010000.00
61find builtins_bzl -type f | sort | "$zip" -qDX0r@ "$output" builtins_bzl