blob: e9a6fa8c20f0f57e153784c866e4ef408aceeb09 [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.
Ivo Listd10013d2020-11-25 14:41:16 -080023# --platform Optional: The name of the platform where the zip was built. (If empty the zip should be platform independent).
iirina93e80732019-04-16 02:07:40 -070024
25set -euo pipefail
26
27# --- begin runfiles.bash initialization ---
28if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
29 if [[ -f "$0.runfiles_manifest" ]]; then
30 export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
31 elif [[ -f "$0.runfiles/MANIFEST" ]]; then
32 export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
33 elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
34 export RUNFILES_DIR="$0.runfiles"
35 fi
36fi
37if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
38 source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
39elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
40 source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
41 "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
42else
43 echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
44 exit 1
45fi
46# --- end runfiles.bash initialization ---
47
iirina1df16352019-05-09 05:48:58 -070048case "$(uname -s | tr [:upper:] [:lower:])" in
49msys*|mingw*|cygwin*)
50 declare -r is_windows=true
51 ;;
52*)
53 declare -r is_windows=false
54 ;;
55esac
56
57if "$is_windows"; then
58 export MSYS_NO_PATHCONV=1
59 export MSYS2_ARG_CONV_EXCL="*"
60fi
61
iirina93e80732019-04-16 02:07:40 -070062# Parsing the flags.
63while [[ -n "$@" ]]; do
64 arg="$1"; shift
65 val="$1"; shift
66 case "$arg" in
67 "--java_tools_zip") java_tools_zip_name="$val" ;;
68 "--gcs_java_tools_dir") gcs_java_tools_dir="$val" ;;
iirina93e80732019-04-16 02:07:40 -070069 "--platform") platform="$val" ;;
iirinae277b0c2019-04-16 05:25:01 -070070 "--commit_hash") commit_hash="$val" ;;
71 "--timestamp") timestamp="$val" ;;
72 "--bazel_version") bazel_version="$val" ;;
iirina93e80732019-04-16 02:07:40 -070073 *) echo "Flag $arg is not recognized." && exit 1 ;;
74 esac
75done
76
77java_tools_zip=$(rlocation io_bazel/${java_tools_zip_name})
78
iirinae277b0c2019-04-16 05:25:01 -070079# Create a temp directory and a writable temp zip file to add a README.md file to
80# the initial zip.
Googlerb0172be2020-06-09 05:05:08 -070081tmp_dir=$(mktemp -d -t 'tmp_bazel_zip_files_XXXXXX')
iirinae277b0c2019-04-16 05:25:01 -070082trap "rm -fr $tmp_dir" EXIT
83tmp_zip="$tmp_dir/archive.zip"
84
85# Copy the initial zip to the temp zip and make it writable to be able to add
86# the README.md file.
87cp $java_tools_zip $tmp_zip
88chmod +w $tmp_zip
89
Noa Resare9004fc72022-04-08 04:31:39 -070090target_basename=$(basename $java_tools_zip)
91
iirinae277b0c2019-04-16 05:25:01 -070092# 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}
Noa Resare9004fc72022-04-08 04:31:39 -070096using bazel version ${bazel_version}${platform:+" on platform ${platform}"}.
iirinae277b0c2019-04-16 05:25:01 -070097To build from source the same zip run the commands:
98
99$ git clone https://github.com/bazelbuild/bazel.git
100$ git checkout ${commit_hash}
Noa Resare9004fc72022-04-08 04:31:39 -0700101$ bazel build //src:${target_basename}
iirinae277b0c2019-04-16 05:25:01 -0700102EOF
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"
Ivo Listd10013d2020-11-25 14:41:16 -0800108if "$is_windows"; then
iirina93e80732019-04-16 02:07:40 -0700109 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" \
Noa Resare9004fc72022-04-08 04:31:39 -0700122 "gs://bazel-mirror/bazel_java_tools/${gcs_java_tools_dir}/${commit_hash}/java/java_tools${platform:+"_${platform}"}-${timestamp}.zip"