blob: a47a8e906250865e895cdefb73589cb0b704fabb [file] [log] [blame]
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +00001#!/bin/bash
2
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00003# Copyright 2015 The Bazel Authors. All rights reserved.
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +00004#
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# Script for building bazel from scratch without bazel
18
Yun Pengd2998a32020-04-24 01:32:40 -070019if [ -d derived/jars ]; then
Yun Pengfcc7d4a2020-05-25 07:47:04 -070020 ADDITIONAL_JARS=derived/jars
Yun Pengd2998a32020-04-24 01:32:40 -070021else
Yun Pengd83fc582020-09-03 02:03:22 -070022 DISTRIBUTION=${BAZEL_DISTRIBUTION:-debian}
Yun Pengfcc7d4a2020-05-25 07:47:04 -070023 ADDITIONAL_JARS="$(grep -o '".*\.jar"' tools/distributions/${DISTRIBUTION}/${DISTRIBUTION}_java.BUILD | sed 's/"//g' | sed 's|^|/usr/share/java/|g')"
Yun Pengd2998a32020-04-24 01:32:40 -070024fi
Yun Peng89cfb702020-05-25 06:55:22 -070025
26# Parse third_party/googleapis/BUILD.bazel to find the proto files we need to compile from googleapis
27GOOGLE_API_PROTOS="$(grep -o '".*\.proto"' third_party/googleapis/BUILD.bazel | sed 's/"//g' | sed 's|^|third_party/googleapis/|g')"
Dmitry Ivankov645093c2020-10-21 04:55:17 -070028PROTO_FILES=$(find third_party/remoteapis ${GOOGLE_API_PROTOS} third_party/pprof src/main/protobuf src/main/java/com/google/devtools/build/lib/buildeventstream/proto src/main/java/com/google/devtools/build/skyframe src/main/java/com/google/devtools/build/lib/skyframe/proto src/main/java/com/google/devtools/build/lib/bazel/debug src/main/java/com/google/devtools/build/lib/starlarkdebug/proto src/main/java/com/google/devtools/build/lib/packages/metrics/package_metrics.proto -name "*.proto")
Benjamin Peterson676d6302020-10-16 06:45:10 -070029LIBRARY_JARS=$(find $ADDITIONAL_JARS third_party -name '*.jar' | grep -Fv JavaBuilder | grep -Fv third_party/guava/guava | grep -ve 'third_party/grpc/grpc.*jar' | tr "\n" " ")
Lauri Peltonen2df3d392021-02-26 04:37:48 -080030GRPC_JAVA_VERSION=1.33.1
Androbinef381e52017-12-14 07:24:27 -080031GRPC_LIBRARY_JARS=$(find third_party/grpc -name '*.jar' | grep -e ".*${GRPC_JAVA_VERSION}.*jar" | tr "\n" " ")
Benjamin Peterson676d6302020-10-16 06:45:10 -070032GUAVA_VERSION=29.0
Androbinef381e52017-12-14 07:24:27 -080033GUAVA_JARS=$(find third_party/guava -name '*.jar' | grep -e ".*${GUAVA_VERSION}.*jar" | tr "\n" " ")
Damien Martin-Guillerezd72bc572017-03-07 16:40:41 +000034LIBRARY_JARS="${LIBRARY_JARS} ${GRPC_LIBRARY_JARS} ${GUAVA_JARS}"
Nathan Harmata8a475902016-11-23 19:08:12 +000035
pcloudyc20900e2020-04-29 12:01:56 -070036DIRS=$(echo src/{java_tools/singlejar/java/com/google/devtools/build/zip,main/java} tools/java/runfiles ${OUTPUT_DIR}/src)
Yun Peng83249e52020-05-25 08:54:24 -070037# Exclude source files that are not needed for Bazel itself, which avoids dependencies like truth.
michajlo4e11dc12020-09-11 06:16:01 -070038EXCLUDE_FILES="src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/testing/* src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetCodecTestUtils.java"
cparsonsf7854a902018-11-07 12:48:52 -080039# Exclude whole directories under the bazel src tree that bazel itself
40# doesn't depend on.
Yun Peng83249e52020-05-25 08:54:24 -070041EXCLUDE_DIRS="src/main/java/com/google/devtools/build/skydoc src/main/java/com/google/devtools/build/docgen tools/java/runfiles/testing src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils src/main/java/com/google/devtools/common/options/testing"
cparsonsf7854a902018-11-07 12:48:52 -080042for d in $EXCLUDE_DIRS ; do
43 for f in $(find $d -type f) ; do
44 EXCLUDE_FILES+=" $f"
45 done
46done
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000047
Alexander Chungfc071422017-02-07 12:48:26 +000048mkdir -p "${OUTPUT_DIR}/classes"
49mkdir -p "${OUTPUT_DIR}/src"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000050
51# May be passed in from outside.
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000052ZIPOPTS="$ZIPOPTS"
53
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000054unset JAVA_TOOL_OPTIONS
55unset _JAVA_OPTIONS
56
57LDFLAGS=${LDFLAGS:-""}
58
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000059MSYS_DLLS=""
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000060
Harsh Vardhanfe04cfd2017-03-21 15:27:12 +000061function get_minor_java_version() {
62 get_java_version
hlopko35664742017-05-29 17:03:07 +020063 java_minor_version=$(echo $JAVA_VERSION | sed 's/[^.][^.]*\.//' | sed 's/\..*$//')
64 javac_minor_version=$(echo $JAVAC_VERSION | sed 's/[^.][^.]*\.//' | sed 's/\..*$//')
Harsh Vardhanfe04cfd2017-03-21 15:27:12 +000065}
66
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000067# Check that javac -version returns a upper version than $JAVA_VERSION.
Harsh Vardhanfe04cfd2017-03-21 15:27:12 +000068get_minor_java_version
69[ ${java_minor_version} -le ${javac_minor_version} ] || \
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +000070 fail "JDK version (${JAVAC_VERSION}) is lower than ${JAVA_VERSION}, please set \$JAVA_HOME."
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000071
72JAR="${JAVA_HOME}/bin/jar"
73
Laszlo Csomor7523e2c2019-07-05 05:21:09 -070074# Ensures unzip won't create paths longer than 259 chars (MAX_PATH) on Windows.
75function check_unzip_wont_create_long_paths() {
76 output_path="$1"
77 jars="$2"
78 if [[ "${PLATFORM}" == "windows" ]]; then
79 log "Checking if helper classes can be extracted..."
80 max_path=$((259-${#output_path}))
81 # Do not quote $jars, we rely on it being split on spaces.
82 for f in $jars ; do
83 # Get the zip entries. Match lines with a date: they have the paths.
84 entries="$(unzip -l "$f" | grep '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}' | awk '{print $4}')"
85 for e in $entries; do
86 if [[ ${#e} -gt $max_path ]]; then
87 fail "Cannot unzip \"$f\" because the output path is too long: extracting file \"$e\" under \"$output_path\" would create a path longer than 259 characters. To fix this, set a shorter TMP value and try again. Example: export TMP=/c/tmp/bzl"
88 fi
89 done
90 done
91 fi
92}
93
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000094# Compiles java classes.
95function java_compilation() {
96 local name=$1
97 local directories=$2
Lukacs Berkie21e5922016-04-12 12:22:20 +000098 local excludes=$3
99 local library_jars=$4
100 local output=$5
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000101
Laszlo Csomor54029932016-12-19 15:43:31 +0000102 local classpath=${library_jars// /$PATHSEP}${PATHSEP}$5
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000103 local sourcepath=${directories// /$PATHSEP}
104
105 tempdir
106 local tmp="${NEW_TMPDIR}"
107 local paramfile="${tmp}/param"
Lukacs Berkie21e5922016-04-12 12:22:20 +0000108 local filelist="${tmp}/filelist"
109 local excludefile="${tmp}/excludefile"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000110 touch $paramfile
111
112 mkdir -p "${output}/classes"
113
114 # Compile .java files (incl. generated ones) using javac
115 log "Compiling $name code..."
Lukacs Berkie21e5922016-04-12 12:22:20 +0000116 find ${directories} -name "*.java" | sort > "$filelist"
117 # Quotes around $excludes intentionally omitted in the for statement so that
118 # it's split on spaces
119 (for i in $excludes; do
120 echo $i
121 done) | sort > "$excludefile"
122
123 comm -23 "$filelist" "$excludefile" > "$paramfile"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000124
125 if [ ! -z "$BAZEL_DEBUG_JAVA_COMPILATION" ]; then
126 echo "directories=${directories}" >&2
127 echo "classpath=${classpath}" >&2
128 echo "sourcepath=${sourcepath}" >&2
129 echo "libraries=${library_jars}" >&2
130 echo "output=${output}/classes" >&2
131 echo "List of compiled files:" >&2
132 cat "$paramfile" >&2
133 fi
134
Laszlo Csomor7523e2c2019-07-05 05:21:09 -0700135 check_unzip_wont_create_long_paths "${output}/classes" "$library_jars"
136
Googler09222fa2018-06-14 08:06:32 -0700137 # Use BAZEL_JAVAC_OPTS to pass additional arguments to javac, e.g.,
138 # export BAZEL_JAVAC_OPTS="-J-Xmx2g -J-Xms200m"
139 # Useful if your system chooses too small of a max heap for javac.
140 # We intentionally rely on shell word splitting to allow multiple
141 # additional arguments to be passed to javac.
Julio Merino5909d9d2016-02-25 21:44:31 +0000142 run "${JAVAC}" -classpath "${classpath}" -sourcepath "${sourcepath}" \
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000143 -d "${output}/classes" -source "$JAVA_VERSION" -target "$JAVA_VERSION" \
Googler09222fa2018-06-14 08:06:32 -0700144 -encoding UTF-8 ${BAZEL_JAVAC_OPTS} "@${paramfile}"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000145
146 log "Extracting helper classes for $name..."
147 for f in ${library_jars} ; do
Julio Merino5909d9d2016-02-25 21:44:31 +0000148 run unzip -qn ${f} -d "${output}/classes"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000149 done
150}
151
152# Create the deploy JAR
153function create_deploy_jar() {
154 local name=$1
155 local mainClass=$2
156 local output=$3
157 shift 3
158 local packages=""
159 for i in $output/classes/*; do
160 local package=$(basename $i)
161 if [[ "$package" != "META-INF" ]]; then
162 packages="$packages -C $output/classes $package"
163 fi
164 done
165
166 log "Creating $name.jar..."
167 echo "Main-Class: $mainClass" > $output/MANIFEST.MF
Julio Merino5909d9d2016-02-25 21:44:31 +0000168 run "$JAR" cmf $output/MANIFEST.MF $output/$name.jar $packages "$@"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000169}
170
Klaus Aehlig967a0a32016-12-02 15:08:25 +0000171HOW_TO_BOOTSTRAP='
172
173--------------------------------------------------------------------------------
174NOTE: This failure is likely occuring if you are trying to bootstrap bazel from
175a developer checkout. Those checkouts do not include the generated output of
176the protoc compiler (as we prefer not to version generated files).
177
178* To build a developer version of bazel, do
179
180 bazel build //src:bazel
181
182* To bootstrap your first bazel binary, please download a dist archive from our
183 release page at https://github.com/bazelbuild/bazel/releases and run
184 compile.sh on the unpacked archive.
185
186The full install instructions to install a release version of bazel can be found
dzc5596d3b2017-06-07 21:51:52 -0400187at https://docs.bazel.build/install-compile-source.html
Klaus Aehlig967a0a32016-12-02 15:08:25 +0000188For a rationale, why the bootstrap process is organized in this way, see
189https://bazel.build/designs/2016/10/11/distribution-artifact.html
190--------------------------------------------------------------------------------
191
192'
193
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000194if [ -z "${BAZEL_SKIP_JAVA_COMPILATION}" ]; then
Klaus Aehligd700dff52016-11-23 16:53:22 +0000195
196 if [ -d derived/src/java ]
197 then
198 log "Using pre-generated java proto files"
199 mkdir -p "${OUTPUT_DIR}/src"
200 cp -r derived/src/java/* "${OUTPUT_DIR}/src"
201 else
202
203 [ -n "${PROTOC}" ] \
Klaus Aehlig967a0a32016-12-02 15:08:25 +0000204 || fail "Must specify PROTOC if not bootstrapping from the distribution artifact${HOW_TO_BOOTSTRAP}"
Klaus Aehligd700dff52016-11-23 16:53:22 +0000205
206 [ -n "${GRPC_JAVA_PLUGIN}" ] \
Klaus Aehlig967a0a32016-12-02 15:08:25 +0000207 || fail "Must specify GRPC_JAVA_PLUGIN if not bootstrapping from the distribution artifact${HOW_TO_BOOTSTRAP}"
Klaus Aehligd700dff52016-11-23 16:53:22 +0000208
209 [[ -x "${PROTOC-}" ]] \
210 || fail "Protobuf compiler not found in ${PROTOC-}"
211
212 [[ -x "${GRPC_JAVA_PLUGIN-}" ]] \
213 || fail "gRPC Java plugin not found in ${GRPC_JAVA_PLUGIN-}"
214
215 log "Compiling Java stubs for protocol buffers..."
216 for f in $PROTO_FILES ; do
Olivier Chafik98be14f2017-11-29 07:34:49 -0800217 run "${PROTOC}" \
218 -I. \
219 -Isrc/main/protobuf/ \
Klaus Aehligd700dff52016-11-23 16:53:22 +0000220 -Isrc/main/java/com/google/devtools/build/lib/buildeventstream/proto/ \
Googler94bf0042020-03-05 09:02:03 -0800221 -Isrc/main/java/com/google/devtools/build/lib/skyframe/proto/ \
janakr65970e52020-02-26 08:25:00 -0800222 -Isrc/main/java/com/google/devtools/build/skyframe/ \
Yun Pengd2998a32020-04-24 01:32:40 -0700223 -Isrc/main/java/com/google/devtools/build/lib/bazel/debug/ \
laurentlb6e87c952020-04-28 11:44:05 -0700224 -Isrc/main/java/com/google/devtools/build/lib/starlarkdebug/proto/ \
Yun Pengd2998a32020-04-24 01:32:40 -0700225 -Ithird_party/remoteapis/ \
226 -Ithird_party/googleapis/ \
227 -Ithird_party/pprof/ \
Klaus Aehligd700dff52016-11-23 16:53:22 +0000228 --java_out=${OUTPUT_DIR}/src \
229 --plugin=protoc-gen-grpc="${GRPC_JAVA_PLUGIN-}" \
230 --grpc_out=${OUTPUT_DIR}/src "$f"
231 done
Yun Peng89cfb702020-05-25 06:55:22 -0700232
233 # Recreate the derived directory
234 mkdir -p derived/src/java/build
235 mkdir -p derived/src/java/com
236 link_children ${OUTPUT_DIR}/src build derived/src/java
237 link_children ${OUTPUT_DIR}/src com derived/src/java
Klaus Aehligd700dff52016-11-23 16:53:22 +0000238 fi
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000239
Lukacs Berkie21e5922016-04-12 12:22:20 +0000240 java_compilation "Bazel Java" "$DIRS" "$EXCLUDE_FILES" "$LIBRARY_JARS" "${OUTPUT_DIR}"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000241
242 # help files: all non java and BUILD files in src/main/java.
243 for i in $(find src/main/java -type f -a \! -name '*.java' -a \! -name 'BUILD' | sed 's|src/main/java/||'); do
244 mkdir -p $(dirname ${OUTPUT_DIR}/classes/$i)
245 cp src/main/java/$i ${OUTPUT_DIR}/classes/$i
246 done
247
John Cater32c5add2018-03-19 08:51:46 -0700248 # Create the bazel_tools repository.
249 BAZEL_TOOLS_REPO=${OUTPUT_DIR}/embedded_tools
250 mkdir -p ${BAZEL_TOOLS_REPO}
251 cat <<EOF >${BAZEL_TOOLS_REPO}/WORKSPACE
252workspace(name = 'bazel_tools')
253EOF
Laszlo Csomorc84c40d2019-04-30 07:17:00 -0700254
255 mkdir -p "${BAZEL_TOOLS_REPO}/src/conditions"
256 link_file "${PWD}/src/conditions/BUILD.tools" \
257 "${BAZEL_TOOLS_REPO}/src/conditions/BUILD"
258 link_children "${PWD}" src/conditions "${BAZEL_TOOLS_REPO}"
259 link_children "${PWD}" src "${BAZEL_TOOLS_REPO}"
260
John Cater32c5add2018-03-19 08:51:46 -0700261 link_dir ${PWD}/third_party ${BAZEL_TOOLS_REPO}/third_party
Laszlo Csomor3eec3fe2018-09-03 06:40:28 -0700262
263 # Create @bazel_tools//tools/cpp/runfiles
264 mkdir -p ${BAZEL_TOOLS_REPO}/tools/cpp/runfiles
265 link_file "${PWD}/tools/cpp/runfiles/runfiles_src.h" \
266 "${BAZEL_TOOLS_REPO}/tools/cpp/runfiles/runfiles.h"
267 # Transform //tools/cpp/runfiles:runfiles_src.cc to
268 # @bazel_tools//tools/cpp/runfiles:runfiles.cc
269 # Keep this transformation logic in sync with the
270 # //tools/cpp/runfiles:srcs_for_embedded_tools genrule.
271 sed 's|^#include.*/runfiles_src.h.*|#include \"tools/cpp/runfiles/runfiles.h\"|' \
272 "${PWD}/tools/cpp/runfiles/runfiles_src.cc" > \
273 "${BAZEL_TOOLS_REPO}/tools/cpp/runfiles/runfiles.cc"
274 link_file "${PWD}/tools/cpp/runfiles/BUILD.tools" \
275 "${BAZEL_TOOLS_REPO}/tools/cpp/runfiles/BUILD"
John Caterd93ebe12018-09-27 06:59:58 -0700276
277 # Create @bazel_tools//tools/sh
278 mkdir -p ${BAZEL_TOOLS_REPO}/tools/sh
279 link_file "${PWD}/tools/sh/sh_configure.bzl" "${BAZEL_TOOLS_REPO}/tools/sh/sh_configure.bzl"
280 link_file "${PWD}/tools/sh/sh_toolchain.bzl" "${BAZEL_TOOLS_REPO}/tools/sh/sh_toolchain.bzl"
281 link_file "${PWD}/tools/sh/BUILD.tools" "${BAZEL_TOOLS_REPO}/tools/sh/BUILD"
282
Ivo List504a5b72020-12-03 04:00:13 -0800283 # Create @bazel_tools//tools/jdk
284 mkdir -p ${BAZEL_TOOLS_REPO}/tools/jdk
285 link_file "${PWD}/tools/jdk/BUILD.tools" "${BAZEL_TOOLS_REPO}/tools/jdk/BUILD"
286 link_children "${PWD}" tools/jdk "${BAZEL_TOOLS_REPO}"
287
Laszlo Csomor4d05f382018-11-22 07:53:57 -0800288 # Create @bazel_tools//tools/java/runfiles
289 mkdir -p ${BAZEL_TOOLS_REPO}/tools/java/runfiles
290 link_file "${PWD}/tools/java/runfiles/Runfiles.java" "${BAZEL_TOOLS_REPO}/tools/java/runfiles/Runfiles.java"
291 link_file "${PWD}/tools/java/runfiles/Util.java" "${BAZEL_TOOLS_REPO}/tools/java/runfiles/Util.java"
292 link_file "${PWD}/tools/java/runfiles/BUILD.tools" "${BAZEL_TOOLS_REPO}/tools/java/runfiles/BUILD"
293
brandjonc9f78b52019-03-05 12:20:12 -0800294 # Create @bazel_tools/tools/python/BUILD
295 mkdir -p ${BAZEL_TOOLS_REPO}/tools/python
296 link_file "${PWD}/tools/python/BUILD.tools" "${BAZEL_TOOLS_REPO}/tools/python/BUILD"
297
John Cater1e005022020-07-20 07:50:49 -0700298 # Create @bazel_tools/tools/android/BUILD
299 mkdir -p ${BAZEL_TOOLS_REPO}/tools/android
300 link_file "${PWD}/tools/android/BUILD.tools" "${BAZEL_TOOLS_REPO}/tools/android/BUILD"
301 link_children "${PWD}" tools/android "${BAZEL_TOOLS_REPO}"
302
Laszlo Csomor3eec3fe2018-09-03 06:40:28 -0700303 # Create the rest of @bazel_tools//tools/...
304 link_children "${PWD}" tools/cpp "${BAZEL_TOOLS_REPO}"
hlopkobea9d252019-11-15 07:30:16 -0800305 mv -f ${BAZEL_TOOLS_REPO}/tools/cpp/BUILD.tools ${BAZEL_TOOLS_REPO}/tools/cpp/BUILD
brandjonc9f78b52019-03-05 12:20:12 -0800306 link_children "${PWD}" tools/python "${BAZEL_TOOLS_REPO}"
Laszlo Csomor3eec3fe2018-09-03 06:40:28 -0700307 link_children "${PWD}" tools "${BAZEL_TOOLS_REPO}"
John Cater32c5add2018-03-19 08:51:46 -0700308
309 # Set up @bazel_tools//platforms properly
310 mkdir -p ${BAZEL_TOOLS_REPO}/platforms
philwoe67c9612019-05-06 04:28:48 -0700311 cp tools/platforms/BUILD.tools ${BAZEL_TOOLS_REPO}/platforms/BUILD
John Cater32c5add2018-03-19 08:51:46 -0700312
Damien Martin-Guillerez12c68aa2016-01-15 13:18:11 +0000313 # Overwrite tools.WORKSPACE, this is only for the bootstrap binary
Klaus Aehlig286cfdd2016-10-17 16:47:55 +0000314 chmod u+w "${OUTPUT_DIR}/classes/com/google/devtools/build/lib/bazel/rules/tools.WORKSPACE"
Damien Martin-Guillerezbe5b2eb2016-03-02 16:08:07 +0000315 cat <<EOF >${OUTPUT_DIR}/classes/com/google/devtools/build/lib/bazel/rules/tools.WORKSPACE
John Cater32c5add2018-03-19 08:51:46 -0700316local_repository(name = 'bazel_tools', path = '${BAZEL_TOOLS_REPO}')
Damien Martin-Guillerezbe5b2eb2016-03-02 16:08:07 +0000317bind(name = "cc_toolchain", actual = "@bazel_tools//tools/cpp:default-toolchain")
John Cater00f8d652019-08-01 14:21:54 -0700318local_config_platform(name = 'local_config_platform')
Damien Martin-Guillerezbe5b2eb2016-03-02 16:08:07 +0000319EOF
Damien Martin-Guillerez12c68aa2016-01-15 13:18:11 +0000320
michajlo7131b2c2018-04-09 09:25:50 -0700321 create_deploy_jar "libblaze" "com.google.devtools.build.lib.bazel.Bazel" \
Han-Wen Nienhuysa4b61822015-11-09 13:55:44 +0000322 ${OUTPUT_DIR}
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000323fi
324
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000325log "Creating Bazel install base..."
326ARCHIVE_DIR=${OUTPUT_DIR}/archive
Googler3a5b7102019-11-07 13:53:48 -0800327mkdir -p ${ARCHIVE_DIR}
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000328
hlopkoaaf64572019-06-14 02:33:56 -0700329# Prepare @platforms local repository
Googler3a5b7102019-11-07 13:53:48 -0800330link_dir ${PWD}/platforms ${ARCHIVE_DIR}/platforms
hlopkoaaf64572019-06-14 02:33:56 -0700331
lberki73b08082018-10-05 10:00:57 -0700332# Dummy build-runfiles (we can't compile C++ yet, so we can't have the real one)
333if [ "${PLATFORM}" = "windows" ]; then
334 # We don't rely on runfiles trees on Windows
Googler3a5b7102019-11-07 13:53:48 -0800335 cat <<'EOF' >${ARCHIVE_DIR}/build-runfiles${EXE_EXT}
lberki73b08082018-10-05 10:00:57 -0700336#!/bin/sh
337mkdir -p $2
338cp $1 $2/MANIFEST
339EOF
340else
Googler3a5b7102019-11-07 13:53:48 -0800341 cat <<'EOF' >${ARCHIVE_DIR}/build-runfiles${EXE_EXT}
Klaus Aehlig6992e682016-08-05 12:27:21 +0000342#!/bin/sh
lberki988626e2018-10-05 07:14:50 -0700343# This is bash implementation of build-runfiles: reads space-separated paths
344# from each line in the file in $1, then creates a symlink under $2 for the
345# first element of the pair that points to the second element of the pair.
346#
347# bash is a terrible tool for this job, but in this case, that's the only one
348# we have (we could hand-compile a little .jar file like we hand-compile the
349# bootstrap version of Bazel, but we'd still need a shell wrapper around it, so
350# it's not clear whether that would be a win over a few lines of Lovecraftian
351# code)
352MANIFEST="$1"
353TREE="$2"
354
355rm -fr "$TREE"
356mkdir -p "$TREE"
357
358# Read the lines in $MANIFEST. the usual "for VAR in $(cat FILE)" idiom won't do
359# because the lines in FILE contain spaces.
360while read LINE; do
361 # Split each line into two parts on the first space
362 SYMLINK_PATH="${LINE%% *}"
363 TARGET_PATH="${LINE#* }"
364 ABSOLUTE_SYMLINK_PATH="$TREE/$SYMLINK_PATH"
365 mkdir -p "$(dirname $ABSOLUTE_SYMLINK_PATH)"
366 ln -s "$TARGET_PATH" "$ABSOLUTE_SYMLINK_PATH"
367done < "$MANIFEST"
368
369cp "$MANIFEST" "$TREE/MANIFEST"
Damien Martin-Guillerezdceee0e2016-01-18 11:57:26 +0000370EOF
lberki73b08082018-10-05 10:00:57 -0700371fi
372
Googler3a5b7102019-11-07 13:53:48 -0800373chmod 0755 ${ARCHIVE_DIR}/build-runfiles${EXE_EXT}
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000374
Laszlo Csomord40c78a2016-12-20 10:22:17 +0000375function build_jni() {
376 local -r output_dir=$1
377
Laszlo Csomord0d7ef02017-04-26 10:48:00 +0200378 if [ "${PLATFORM}" = "windows" ]; then
Laszlo Csomord40c78a2016-12-20 10:22:17 +0000379 # We need JNI on Windows because some filesystem operations are not (and
380 # cannot be) implemented in native Java.
381 log "Building Windows JNI library..."
382
383 local -r jni_lib_name="windows_jni.dll"
384 local -r output="${output_dir}/${jni_lib_name}"
385 local -r tmp_output="${NEW_TMPDIR}/jni/${jni_lib_name}"
386 mkdir -p "$(dirname "$tmp_output")"
387 mkdir -p "$(dirname "$output")"
388
389 # Keep this `find` command in sync with the `srcs` of
Laszlo Csomor94091092017-07-04 05:45:40 -0400390 # //src/main/native/windows:windows_jni
Laszlo Csomor17770e52017-06-29 17:43:05 +0200391 local srcs=$(find src/main/native/windows -name '*.cc' -o -name '*.h')
Laszlo Csomord40c78a2016-12-20 10:22:17 +0000392 [ -n "$srcs" ] || fail "Could not find sources for Windows JNI library"
393
394 # do not quote $srcs because we need to expand it to multiple args
Laszlo Csomor94091092017-07-04 05:45:40 -0400395 src/main/native/windows/build_windows_jni.sh "$tmp_output" ${srcs}
Laszlo Csomord40c78a2016-12-20 10:22:17 +0000396
397 cp "$tmp_output" "$output"
398 chmod 0555 "$output"
399
jmmv50adefb2020-10-02 09:25:34 -0700400 JNI_FLAGS="-Djava.library.path=${output_dir}"
Laszlo Csomord0d7ef02017-04-26 10:48:00 +0200401 else
jmmv50adefb2020-10-02 09:25:34 -0700402 # We don't need JNI on other platforms. The Java NIO file system fallback is
403 # sufficient.
404 true
Laszlo Csomord0d7ef02017-04-26 10:48:00 +0200405 fi
Laszlo Csomord40c78a2016-12-20 10:22:17 +0000406}
407
Googler3a5b7102019-11-07 13:53:48 -0800408build_jni "${ARCHIVE_DIR}"
Laszlo Csomord40c78a2016-12-20 10:22:17 +0000409
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000410cp $OUTPUT_DIR/libblaze.jar ${ARCHIVE_DIR}
Chris Parsons381850e2016-08-31 17:04:17 +0000411
412# TODO(b/28965185): Remove when xcode-locator is no longer required in embedded_binaries.
413log "Compiling xcode-locator..."
414if [[ $PLATFORM == "darwin" ]]; then
Googler3a5b7102019-11-07 13:53:48 -0800415 run /usr/bin/xcrun --sdk macosx clang -mmacosx-version-min=10.9 -fobjc-arc -framework CoreServices -framework Foundation -o ${ARCHIVE_DIR}/xcode-locator tools/osx/xcode_locator.m
Chris Parsons381850e2016-08-31 17:04:17 +0000416else
Googler3a5b7102019-11-07 13:53:48 -0800417 cp tools/osx/xcode_locator_stub.sh ${ARCHIVE_DIR}/xcode-locator
Chris Parsons381850e2016-08-31 17:04:17 +0000418fi
Chris Parsonsf4888182016-01-08 00:42:14 +0000419
Laszlo Csomord0d7ef02017-04-26 10:48:00 +0200420function get_cwd() {
421 local result=${PWD}
422 [ "$PLATFORM" = "windows" ] && result="$(cygpath -m "$result")"
423 echo "$result"
424}
425
Laszlo Csomor9f7180f2016-09-27 13:07:43 +0000426function run_bazel_jar() {
427 local command=$1
428 shift
Yun Peng8efd2822017-03-09 13:15:41 +0000429 local client_env=()
Damien Martin-Guillerez755669f2017-05-08 12:27:38 -0400430 # Propagate all environment variables to bootstrapped Bazel.
hlopko200479a2017-05-09 07:10:35 -0400431 # See https://stackoverflow.com/questions/41898503/loop-over-environment-variables-in-posix-sh
Laszlo Csomor0a936d12017-05-31 09:47:43 +0200432 local env_vars="$(awk 'END { for (name in ENVIRON) { if(name != "_" && name ~ /^[A-Za-z0-9_]*$/) print name; } }' </dev/null)"
Nicolas Lopezbcbdb872017-03-28 08:08:50 +0000433 for varname in $env_vars; do
Dmitry Lomov31fab292017-03-07 18:33:08 +0000434 eval value=\$$varname
pcloudy8c21b0b2018-09-19 07:19:09 -0700435 if [ "${PLATFORM}" = "windows" ] && echo "$varname" | grep -q -i "^\(path\|tmp\|temp\|tempdir\|systemroot\|systemdrive\)$" ; then
Laszlo Csomor0a936d12017-05-31 09:47:43 +0200436 varname="$(echo "$varname" | tr [:lower:] [:upper:])"
437 fi
Yun Peng8efd2822017-03-09 13:15:41 +0000438 if [ "${value}" ]; then
439 client_env=("${client_env[@]}" --client_env="${varname}=${value}")
Dmitry Lomov31fab292017-03-07 18:33:08 +0000440 fi
441 done
442
Dmitry Lomovf9f2e102016-01-29 14:26:46 +0000443 "${JAVA_HOME}/bin/java" \
Philipp Wollermann74cb8c72016-07-15 14:01:37 +0000444 -XX:+HeapDumpOnOutOfMemoryError -Xverify:none -Dfile.encoding=ISO-8859-1 \
Carmi Grushko4068a1d2016-06-22 18:43:13 +0000445 -XX:HeapDumpPath=${OUTPUT_DIR} \
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000446 -Djava.util.logging.config.file=${OUTPUT_DIR}/javalog.properties \
Laszlo Csomord40c78a2016-12-20 10:22:17 +0000447 ${JNI_FLAGS} \
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000448 -jar ${ARCHIVE_DIR}/libblaze.jar \
449 --batch \
450 --install_base=${ARCHIVE_DIR} \
451 --output_base=${OUTPUT_DIR}/out \
mschaller09924272020-04-07 20:50:28 -0700452 --failure_detail_out=${OUTPUT_DIR}/failure_detail.rawproto \
Klaus Aehligc2499c42018-02-27 05:47:21 -0800453 --output_user_root=${OUTPUT_DIR}/user_root \
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000454 --install_md5= \
cushon849df362018-05-14 01:51:45 -0700455 --default_system_javabase="${JAVA_HOME}" \
Laszlo Csomord0d7ef02017-04-26 10:48:00 +0200456 --workspace_directory="$(get_cwd)" \
Kristina Chodorow4873e562016-06-20 14:17:50 +0000457 --nofatal_event_bus_exceptions \
Klaus Aehlig276a8cd2016-07-11 12:06:07 +0000458 ${BAZEL_DIR_STARTUP_OPTIONS} \
Laszlo Csomor50f6bba2016-09-26 14:10:25 +0000459 ${BAZEL_BOOTSTRAP_STARTUP_OPTIONS:-} \
Laszlo Csomor9f7180f2016-09-27 13:07:43 +0000460 $command \
Damien Martin-Guillerez51a204a2016-02-08 14:59:59 +0000461 --ignore_unsupported_sandboxing \
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000462 --startup_time=329 --extract_data_time=523 \
Klaus Aehlig7038d3f2016-03-16 15:41:22 +0000463 --rc_source=/dev/null --isatty=1 \
Philipp Wollermann4c558982017-07-27 18:01:12 +0200464 --build_python_zip \
Yun Peng8efd2822017-03-09 13:15:41 +0000465 "${client_env[@]}" \
Laszlo Csomord0d7ef02017-04-26 10:48:00 +0200466 --client_cwd="$(get_cwd)" \
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000467 "${@}"
468}