brandjon | de1babe | 2020-11-17 14:19:23 -0800 | [diff] [blame] | 1 | #!/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 | |
| 23 | set -euo pipefail |
| 24 | |
| 25 | origdir="$(pwd)" |
| 26 | |
| 27 | # the "zip" command to use; system "zip" if empty |
| 28 | if [ -z "$1" ]; then |
| 29 | zip="zip" |
| 30 | else |
| 31 | zip=$origdir/$1; |
| 32 | fi; shift |
| 33 | output=$origdir/$1; shift # zip file to write |
| 34 | builtins_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 | |
| 38 | TMPDIR="$(mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX")" |
| 39 | trap "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. |
| 43 | mkdir -p "$TMPDIR/staging" |
| 44 | for 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" |
| 49 | done |
| 50 | |
| 51 | cd "$TMPDIR" |
| 52 | |
| 53 | # Strip the dir prefix leading up to the builtins root. |
| 54 | mv "staging/$builtins_root" builtins_bzl |
brandjon | de1babe | 2020-11-17 14:19:23 -0800 | [diff] [blame] | 55 | |
| 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. |
| 60 | find builtins_bzl -type f -print0 | xargs -0 touch -t 198001010000.00 |
| 61 | find builtins_bzl -type f | sort | "$zip" -qDX0r@ "$output" builtins_bzl |