blob: 11b5a1e9c6ab2451eb4c8aeff991ece23494a354 [file] [log] [blame]
iirina93e80732019-04-16 02:07:40 -07001#!/bin/bash
2
3# Copyright 2019 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# A script to upload a given java_tools zip on GCS. Used by the java_tools_binaries
18# Buildkite pipeline. It is not recommended to run this script manually.
19#
20# Mandatory flags:
21# --java_tools_zip The workspace-relative path of a java_tools zip.
22# --gcs_java_tools_dir The directory under bazel_java_tools on GCS where the zip is uploaded.
23# --java_version The version of the javac the given zip embeds.
24# --platform The name of the platform where the zip was built.
25
26set -euo pipefail
27
28# --- begin runfiles.bash initialization ---
29if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
30 if [[ -f "$0.runfiles_manifest" ]]; then
31 export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
32 elif [[ -f "$0.runfiles/MANIFEST" ]]; then
33 export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
34 elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
35 export RUNFILES_DIR="$0.runfiles"
36 fi
37fi
38if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
39 source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
40elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
41 source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
42 "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
43else
44 echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
45 exit 1
46fi
47# --- end runfiles.bash initialization ---
48
iirina1df16352019-05-09 05:48:58 -070049case "$(uname -s | tr [:upper:] [:lower:])" in
50msys*|mingw*|cygwin*)
51 declare -r is_windows=true
52 ;;
53*)
54 declare -r is_windows=false
55 ;;
56esac
57
58if "$is_windows"; then
59 export MSYS_NO_PATHCONV=1
60 export MSYS2_ARG_CONV_EXCL="*"
61fi
62
iirina93e80732019-04-16 02:07:40 -070063# Parsing the flags.
64while [[ -n "$@" ]]; do
65 arg="$1"; shift
66 val="$1"; shift
67 case "$arg" in
68 "--java_tools_zip") java_tools_zip_name="$val" ;;
69 "--gcs_java_tools_dir") gcs_java_tools_dir="$val" ;;
70 "--java_version") java_version="$val" ;;
71 "--platform") platform="$val" ;;
iirinae277b0c2019-04-16 05:25:01 -070072 "--commit_hash") commit_hash="$val" ;;
73 "--timestamp") timestamp="$val" ;;
74 "--bazel_version") bazel_version="$val" ;;
iirina93e80732019-04-16 02:07:40 -070075 *) echo "Flag $arg is not recognized." && exit 1 ;;
76 esac
77done
78
79java_tools_zip=$(rlocation io_bazel/${java_tools_zip_name})
80
iirinae277b0c2019-04-16 05:25:01 -070081# Create a temp directory and a writable temp zip file to add a README.md file to
82# the initial zip.
83tmp_dir=$(mktemp -d -t 'tmp_bazel_zip_files_XXXXX')
84trap "rm -fr $tmp_dir" EXIT
85tmp_zip="$tmp_dir/archive.zip"
86
87# Copy the initial zip to the temp zip and make it writable to be able to add
88# the README.md file.
89cp $java_tools_zip $tmp_zip
90chmod +w $tmp_zip
91
92# Create the README.md file and add the re-build java tools instructions.
93readme_file="README.md"
94cat >${readme_file} <<EOF
95This Java tools version was built from the bazel repository at commit hash ${commit_hash}
96using bazel version ${bazel_version}.
97To build from source the same zip run the commands:
98
99$ git clone https://github.com/bazelbuild/bazel.git
100$ git checkout ${commit_hash}
101$ bazel build //src:java_tools_java${java_version}.zip
102EOF
103
104# Add the README.md file to the temp zip.
105zip -rv "${tmp_zip}" "${readme_file}"
106
iirina93e80732019-04-16 02:07:40 -0700107gsutil_cmd="gsutil"
108if [[ "$platform" == "windows" ]]; then
109 gsutil_cmd="gsutil.cmd"
110fi
111
iirina1df16352019-05-09 05:48:58 -0700112
113if "$is_windows"; then
114 zip_url=$(cygpath -m ${tmp_zip})
115else
116 # Non-Windows needs "file:///foo/bar".
117 zip_url=${tmp_zip}
118fi
119
iirinae277b0c2019-04-16 05:25:01 -0700120# Upload the zip that contains the README.md to GCS.
iirina1df16352019-05-09 05:48:58 -0700121"$gsutil_cmd" cp "$zip_url" \
iirinadead8522019-04-17 00:41:54 -0700122 "gs://bazel-mirror/bazel_java_tools/${gcs_java_tools_dir}/${commit_hash}/java${java_version}/java_tools_javac${java_version}_${platform}-${timestamp}.zip"