blob: 04e32e6c873620b0d38c83885cb3ee5ee3c7465f [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
19PROTO_FILES=$(ls src/main/protobuf/*.proto)
dapengzhang03abcec12016-07-22 20:54:44 +000020LIBRARY_JARS=$(find third_party -name '*.jar' | grep -Fv /javac.jar | grep -Fv /javac7.jar | grep -Fv JavaBuilder | grep -ve third_party/grpc/grpc.*jar | tr "\n" " ")
21GRPC_JAVA_VERSION=0.15.0
22GRPC_LIBRARY_JARS=$(find third_party/grpc -name '*.jar' | grep -e .*${GRPC_JAVA_VERSION}.*jar | tr "\n" " ")
23LIBRARY_JARS="${LIBRARY_JARS} ${GRPC_LIBRARY_JARS}"
Damien Martin-Guillereze700e722016-06-30 15:06:39 +020024DIRS=$(echo src/{java_tools/singlejar/java/com/google/devtools/build/zip,main/java,tools/xcode-common/java/com/google/devtools/build/xcode/{common,util}} third_party/java/dd_plist/java ${OUTPUT_DIR}/src)
Lukacs Berki8b3b9182016-04-14 08:29:05 +000025EXCLUDE_FILES=src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000026
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000027mkdir -p ${OUTPUT_DIR}/classes
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000028mkdir -p ${OUTPUT_DIR}/src
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000029
30# May be passed in from outside.
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000031ZIPOPTS="$ZIPOPTS"
32
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000033unset JAVA_TOOL_OPTIONS
34unset _JAVA_OPTIONS
35
36LDFLAGS=${LDFLAGS:-""}
37
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000038MSYS_DLLS=""
39PATHSEP=":"
40
41case "${PLATFORM}" in
42linux)
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000043 # JAVA_HOME must point to a Java installation.
44 JAVA_HOME="${JAVA_HOME:-$(readlink -f $(which javac) | sed 's_/bin/javac__')}"
Philipp Wollermann19f34132015-06-12 08:39:25 +000045 if [ "${MACHINE_IS_64BIT}" = 'yes' ]; then
46 PROTOC=${PROTOC:-third_party/protobuf/protoc-linux-x86_64.exe}
dapengzhang03abcec12016-07-22 20:54:44 +000047 GRPC_JAVA_PLUGIN=${GRPC_JAVA_PLUGIN:-third_party/grpc/protoc-gen-grpc-java-0.15.0-linux-x86_64.exe}
Philipp Wollermann19f34132015-06-12 08:39:25 +000048 else
Zhong Wang8c288132015-08-12 15:06:08 +000049 if [ "${MACHINE_IS_ARM}" = 'yes' ]; then
50 PROTOC=${PROTOC:-third_party/protobuf/protoc-linux-arm32.exe}
51 else
52 PROTOC=${PROTOC:-third_party/protobuf/protoc-linux-x86_32.exe}
dapengzhang03abcec12016-07-22 20:54:44 +000053 GRPC_JAVA_PLUGIN=${GRPC_JAVA_PLUGIN:-third_party/grpc/protoc-gen-grpc-java-0.15.0-linux-x86_32.exe}
Zhong Wang8c288132015-08-12 15:06:08 +000054 fi
Philipp Wollermann19f34132015-06-12 08:39:25 +000055 fi
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000056 ;;
57
Doug Rabson709bc612015-08-25 14:12:00 +000058freebsd)
Doug Rabson709bc612015-08-25 14:12:00 +000059 # JAVA_HOME must point to a Java installation.
60 JAVA_HOME="${JAVA_HOME:-/usr/local/openjdk8}"
61 # Note: the linux protoc binary works on freebsd using linux emulation.
62 # We choose the 32-bit version for maximum compatiblity since 64-bit
63 # linux binaries are only supported in FreeBSD-11.
64 PROTOC=${PROTOC:-third_party/protobuf/protoc-linux-x86_32.exe}
dapengzhang03abcec12016-07-22 20:54:44 +000065 GRPC_JAVA_PLUGIN=${GRPC_JAVA_PLUGIN:-third_party/grpc/protoc-gen-grpc-java-0.15.0-linux-x86_32.exe}
Doug Rabson709bc612015-08-25 14:12:00 +000066 ;;
67
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000068darwin)
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000069 if [[ -z "$JAVA_HOME" ]]; then
70 JAVA_HOME="$(/usr/libexec/java_home -v ${JAVA_VERSION}+ 2> /dev/null)" \
71 || fail "Could not find JAVA_HOME, please ensure a JDK (version ${JAVA_VERSION}+) is installed."
72 fi
Philipp Wollermann19f34132015-06-12 08:39:25 +000073 if [ "${MACHINE_IS_64BIT}" = 'yes' ]; then
74 PROTOC=${PROTOC:-third_party/protobuf/protoc-osx-x86_64.exe}
dapengzhang03abcec12016-07-22 20:54:44 +000075 GRPC_JAVA_PLUGIN=${GRPC_JAVA_PLUGIN:-third_party/grpc/protoc-gen-grpc-java-0.15.0-osx-x86_64.exe}
Philipp Wollermann19f34132015-06-12 08:39:25 +000076 else
77 PROTOC=${PROTOC:-third_party/protobuf/protoc-osx-x86_32.exe}
78 fi
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000079 ;;
80
81msys*|mingw*)
82 # Use a simplified platform string.
83 PLATFORM="mingw"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000084 PATHSEP=";"
85 # Find the latest available version of the SDK.
86 JAVA_HOME="${JAVA_HOME:-$(ls -d /c/Program\ Files/Java/jdk* | sort | tail -n 1)}"
87 # We do not use the JNI library on Windows.
Googler8a0ce7b2015-07-22 09:10:08 +000088 if [ "${MACHINE_IS_64BIT}" = 'yes' ]; then
89 PROTOC=${PROTOC:-third_party/protobuf/protoc-windows-x86_64.exe}
dapengzhang03abcec12016-07-22 20:54:44 +000090 GRPC_JAVA_PLUGIN=${GRPC_JAVA_PLUGIN:-third_party/grpc/protoc-gen-grpc-java-0.15.0-windows-x86_64.exe}
Googler8a0ce7b2015-07-22 09:10:08 +000091 else
92 PROTOC=${PROTOC:-third_party/protobuf/protoc-windows-x86_32.exe}
dapengzhang03abcec12016-07-22 20:54:44 +000093 GRPC_JAVA_PLUGIN=${GRPC_JAVA_PLUGIN:-third_party/grpc/protoc-gen-grpc-java-0.15.0-windows-x86_32.exe}
Googler8a0ce7b2015-07-22 09:10:08 +000094 fi
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000095esac
96
97[[ -x "${PROTOC-}" ]] \
98 || fail "Protobuf compiler not found in ${PROTOC-}"
99
Alpha Lama1a79cb2016-05-15 19:13:52 +0000100[[ -x "${GRPC_JAVA_PLUGIN-}" ]] \
101 || fail "gRPC Java plugin not found in ${GRPC_JAVA_PLUGIN-}"
102
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000103# Check that javac -version returns a upper version than $JAVA_VERSION.
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000104get_java_version
105[ ${JAVA_VERSION#*.} -le ${JAVAC_VERSION#*.} ] || \
106 fail "JDK version (${JAVAC_VERSION}) is lower than ${JAVA_VERSION}, please set \$JAVA_HOME."
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000107
108JAR="${JAVA_HOME}/bin/jar"
109
110# Compiles java classes.
111function java_compilation() {
112 local name=$1
113 local directories=$2
Lukacs Berkie21e5922016-04-12 12:22:20 +0000114 local excludes=$3
115 local library_jars=$4
116 local output=$5
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000117
118 local classpath=${library_jars// /$PATHSEP}:$5
119 local sourcepath=${directories// /$PATHSEP}
120
121 tempdir
122 local tmp="${NEW_TMPDIR}"
123 local paramfile="${tmp}/param"
Lukacs Berkie21e5922016-04-12 12:22:20 +0000124 local filelist="${tmp}/filelist"
125 local excludefile="${tmp}/excludefile"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000126 touch $paramfile
127
128 mkdir -p "${output}/classes"
129
130 # Compile .java files (incl. generated ones) using javac
131 log "Compiling $name code..."
Lukacs Berkie21e5922016-04-12 12:22:20 +0000132 find ${directories} -name "*.java" | sort > "$filelist"
133 # Quotes around $excludes intentionally omitted in the for statement so that
134 # it's split on spaces
135 (for i in $excludes; do
136 echo $i
137 done) | sort > "$excludefile"
138
139 comm -23 "$filelist" "$excludefile" > "$paramfile"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000140
141 if [ ! -z "$BAZEL_DEBUG_JAVA_COMPILATION" ]; then
142 echo "directories=${directories}" >&2
143 echo "classpath=${classpath}" >&2
144 echo "sourcepath=${sourcepath}" >&2
145 echo "libraries=${library_jars}" >&2
146 echo "output=${output}/classes" >&2
147 echo "List of compiled files:" >&2
148 cat "$paramfile" >&2
149 fi
150
Julio Merino5909d9d2016-02-25 21:44:31 +0000151 run "${JAVAC}" -classpath "${classpath}" -sourcepath "${sourcepath}" \
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000152 -d "${output}/classes" -source "$JAVA_VERSION" -target "$JAVA_VERSION" \
Damien Martin-Guillerez45d18d42015-09-21 11:45:10 +0000153 -encoding UTF-8 "@${paramfile}"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000154
155 log "Extracting helper classes for $name..."
156 for f in ${library_jars} ; do
Julio Merino5909d9d2016-02-25 21:44:31 +0000157 run unzip -qn ${f} -d "${output}/classes"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000158 done
159}
160
161# Create the deploy JAR
162function create_deploy_jar() {
163 local name=$1
164 local mainClass=$2
165 local output=$3
166 shift 3
167 local packages=""
168 for i in $output/classes/*; do
169 local package=$(basename $i)
170 if [[ "$package" != "META-INF" ]]; then
171 packages="$packages -C $output/classes $package"
172 fi
173 done
174
175 log "Creating $name.jar..."
176 echo "Main-Class: $mainClass" > $output/MANIFEST.MF
Julio Merino5909d9d2016-02-25 21:44:31 +0000177 run "$JAR" cmf $output/MANIFEST.MF $output/$name.jar $packages "$@"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000178}
179
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000180if [ -z "${BAZEL_SKIP_JAVA_COMPILATION}" ]; then
181 log "Compiling Java stubs for protocol buffers..."
182 for f in $PROTO_FILES ; do
Alpha Lama1a79cb2016-05-15 19:13:52 +0000183 run "${PROTOC}" -Isrc/main/protobuf/ --java_out=${OUTPUT_DIR}/src \
184 --plugin=protoc-gen-grpc="${GRPC_JAVA_PLUGIN-}" \
185 --grpc_out=${OUTPUT_DIR}/src "$f"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000186 done
187
Lukacs Berkie21e5922016-04-12 12:22:20 +0000188 java_compilation "Bazel Java" "$DIRS" "$EXCLUDE_FILES" "$LIBRARY_JARS" "${OUTPUT_DIR}"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000189
190 # help files: all non java and BUILD files in src/main/java.
191 for i in $(find src/main/java -type f -a \! -name '*.java' -a \! -name 'BUILD' | sed 's|src/main/java/||'); do
192 mkdir -p $(dirname ${OUTPUT_DIR}/classes/$i)
193 cp src/main/java/$i ${OUTPUT_DIR}/classes/$i
194 done
195
Damien Martin-Guillerez12c68aa2016-01-15 13:18:11 +0000196 # Overwrite tools.WORKSPACE, this is only for the bootstrap binary
Damien Martin-Guillerezbe5b2eb2016-03-02 16:08:07 +0000197 cat <<EOF >${OUTPUT_DIR}/classes/com/google/devtools/build/lib/bazel/rules/tools.WORKSPACE
198local_repository(name = 'bazel_tools', path = __workspace_dir__)
199bind(name = "cc_toolchain", actual = "@bazel_tools//tools/cpp:default-toolchain")
200EOF
Damien Martin-Guillerez12c68aa2016-01-15 13:18:11 +0000201
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000202 create_deploy_jar "libblaze" "com.google.devtools.build.lib.bazel.BazelMain" \
Han-Wen Nienhuysa4b61822015-11-09 13:55:44 +0000203 ${OUTPUT_DIR}
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000204fi
205
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000206log "Creating Bazel install base..."
207ARCHIVE_DIR=${OUTPUT_DIR}/archive
208mkdir -p ${ARCHIVE_DIR}/_embedded_binaries
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000209
Damien Martin-Guillerezdceee0e2016-01-18 11:57:26 +0000210# Dummy build-runfiles
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000211cat <<'EOF' >${ARCHIVE_DIR}/_embedded_binaries/build-runfiles${EXE_EXT}
Damien Martin-Guillerez7a1d4132016-08-05 11:37:09 +0000212#!/bin/bash
Lukacs Berki25f35692016-08-04 10:38:36 +0000213mkdir -p $2
Damien Martin-Guillerezdceee0e2016-01-18 11:57:26 +0000214cp $1 $2/MANIFEST
215EOF
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000216chmod 0755 ${ARCHIVE_DIR}/_embedded_binaries/build-runfiles${EXE_EXT}
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000217
Damien Martin-Guillerez8c04e9e2016-01-18 12:53:39 +0000218log "Creating process-wrapper..."
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000219cat <<'EOF' >${ARCHIVE_DIR}/_embedded_binaries/process-wrapper${EXE_EXT}
Klaus Aehlig6dbcc2d2016-05-11 12:46:42 +0000220#!/bin/sh
Damien Martin-Guillerezec758db2016-01-18 12:14:28 +0000221# Dummy process wrapper, does not support timeout
222shift 2
Dmitry Lomov9bf3f6a2016-07-07 13:50:06 +0000223stdout="$1"
224stderr="$2"
225shift 2
226
227if [ "$stdout" = "-" ]
228then
229 if [ "$stderr" = "-" ]
230 then
231 "$@"
232 exit $?
233 else
234 "$@" 2>"$stderr"
235 exit $?
236 fi
237else
238 if [ "$stderr" = "-" ]
239 then
240 "$@" >"$stdout"
241 exit $?
242 else
243 "$@" 2>"$stderr" >"$stdout"
244 exit $?
245 fi
246fi
247
Klaus Aehligb2973412016-03-16 13:55:40 +0000248
249"$@"
Damien Martin-Guillerezec758db2016-01-18 12:14:28 +0000250exit $?
251EOF
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000252chmod 0755 ${ARCHIVE_DIR}/_embedded_binaries/process-wrapper${EXE_EXT}
Philipp Wollermann43c4a1a2015-08-25 12:52:57 +0000253
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000254cp src/main/tools/build_interface_so ${ARCHIVE_DIR}/_embedded_binaries/build_interface_so
255cp src/main/tools/jdk.BUILD ${ARCHIVE_DIR}/_embedded_binaries/jdk.BUILD
256cp $OUTPUT_DIR/libblaze.jar ${ARCHIVE_DIR}
Chris Parsons5eb86fe2016-07-21 15:24:55 +0000257cp tools/osx/xcode_locator_stub.sh ${ARCHIVE_DIR}/_embedded_binaries/xcode-locator
Chris Parsonsf4888182016-01-08 00:42:14 +0000258
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000259# bazel build using bootstrap version
260function bootstrap_build() {
Dmitry Lomovf9f2e102016-01-29 14:26:46 +0000261 "${JAVA_HOME}/bin/java" \
Philipp Wollermann74cb8c72016-07-15 14:01:37 +0000262 -XX:+HeapDumpOnOutOfMemoryError -Xverify:none -Dfile.encoding=ISO-8859-1 \
Carmi Grushko4068a1d2016-06-22 18:43:13 +0000263 -XX:HeapDumpPath=${OUTPUT_DIR} \
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000264 -Djava.util.logging.config.file=${OUTPUT_DIR}/javalog.properties \
Lukacs Berki28219722016-06-30 12:34:39 +0000265 -Dio.bazel.EnableJni=0 \
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000266 -jar ${ARCHIVE_DIR}/libblaze.jar \
267 --batch \
268 --install_base=${ARCHIVE_DIR} \
269 --output_base=${OUTPUT_DIR}/out \
270 --install_md5= \
271 --workspace_directory=${PWD} \
Kristina Chodorow4873e562016-06-20 14:17:50 +0000272 --nofatal_event_bus_exceptions \
Klaus Aehlig276a8cd2016-07-11 12:06:07 +0000273 ${BAZEL_DIR_STARTUP_OPTIONS} \
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000274 build \
Damien Martin-Guillerez51a204a2016-02-08 14:59:59 +0000275 --ignore_unsupported_sandboxing \
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000276 --startup_time=329 --extract_data_time=523 \
Klaus Aehlig7038d3f2016-03-16 15:41:22 +0000277 --rc_source=/dev/null --isatty=1 \
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000278 --ignore_client_env \
279 --client_cwd=${PWD} \
Damien Martin-Guillerezdaffc352016-01-18 15:20:32 +0000280 "${@}"
281}