blob: b8ef29a73804fa7891bcc25dfdc1ef7f3d128ea0 [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)
20LIBRARY_JARS=$(find third_party -name '*.jar' | tr "\n" " ")
Kristina Chodorow86746ce2015-06-11 16:46:41 +000021DIRS=$(echo src/{java_tools/singlejar/java/com/google/devtools/build/zip,main/java,tools/xcode-common/java/com/google/devtools/build/xcode/{common,util}} ${OUTPUT_DIR}/src)
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000022
Dmitry Lomov5f661552015-07-24 10:42:07 +000023case "${PLATFORM}" in
24msys*|mingw*)
25 BLAZE_UTIL_SUFFIX=mingw
Dmitry Lomov78c0cc72015-08-11 16:44:21 +000026 BLAZE_UTIL_POSIX=""
Dmitry Lomov5f661552015-07-24 10:42:07 +000027 ;;
28*)
29 BLAZE_UTIL_SUFFIX="${PLATFORM}"
Dmitry Lomov78c0cc72015-08-11 16:44:21 +000030 BLAZE_UTIL_POSIX="src/main/cpp/blaze_util_posix.cc"
Dmitry Lomov5f661552015-07-24 10:42:07 +000031 ;;
32esac
33
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000034BLAZE_CC_FILES=(
Nathan Harmatabf98f392016-01-07 22:58:29 +000035src/main/cpp/blaze_abrupt_exit.cc
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000036src/main/cpp/blaze_startup_options.cc
37src/main/cpp/blaze_startup_options_common.cc
38src/main/cpp/blaze_util.cc
Dmitry Lomov5f661552015-07-24 10:42:07 +000039src/main/cpp/blaze_util_${BLAZE_UTIL_SUFFIX}.cc
Dmitry Lomov78c0cc72015-08-11 16:44:21 +000040${BLAZE_UTIL_POSIX}
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000041src/main/cpp/blaze.cc
42src/main/cpp/option_processor.cc
43src/main/cpp/util/errors.cc
44src/main/cpp/util/file.cc
45src/main/cpp/util/md5.cc
46src/main/cpp/util/numbers.cc
47src/main/cpp/util/port.cc
48src/main/cpp/util/strings.cc
49third_party/ijar/zip.cc
50)
51
52NATIVE_CC_FILES=(
53src/main/cpp/util/md5.cc
54src/main/native/localsocket.cc
55src/main/native/process.cc
56src/main/native/unix_jni.cc
57src/main/native/unix_jni_${PLATFORM}.cc
58)
59
60mkdir -p ${OUTPUT_DIR}/classes
61mkdir -p ${OUTPUT_DIR}/test_classes
62mkdir -p ${OUTPUT_DIR}/src
63mkdir -p ${OUTPUT_DIR}/objs
64mkdir -p ${OUTPUT_DIR}/native
65
66# May be passed in from outside.
67CXXFLAGS="$CXXFLAGS"
68LDFLAGS="$LDFLAGS"
69ZIPOPTS="$ZIPOPTS"
70
71# TODO: CC target architecture needs to match JAVA_HOME.
Doug Rabson709bc612015-08-25 14:12:00 +000072if [ "${PLATFORM}" = "freebsd" ]; then
73 CC=${CC:-clang}
74 CXX=${CXX:-clang++}
75else
76 CC=${CC:-gcc}
77 CXX=${CXX:-g++}
78fi
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000079CXXSTD="c++0x"
80
81unset JAVA_TOOL_OPTIONS
82unset _JAVA_OPTIONS
83
84LDFLAGS=${LDFLAGS:-""}
85
86# Extension for executables (.exe on Windows).
87EXE_EXT=""
88
89MSYS_DLLS=""
90PATHSEP=":"
91
92case "${PLATFORM}" in
93linux)
94 LDFLAGS="-lz -lrt $LDFLAGS"
95 JNILIB="libunix.so"
96 MD5SUM="md5sum"
97 # JAVA_HOME must point to a Java installation.
98 JAVA_HOME="${JAVA_HOME:-$(readlink -f $(which javac) | sed 's_/bin/javac__')}"
Philipp Wollermann19f34132015-06-12 08:39:25 +000099 if [ "${MACHINE_IS_64BIT}" = 'yes' ]; then
100 PROTOC=${PROTOC:-third_party/protobuf/protoc-linux-x86_64.exe}
101 else
Zhong Wang8c288132015-08-12 15:06:08 +0000102 if [ "${MACHINE_IS_ARM}" = 'yes' ]; then
103 PROTOC=${PROTOC:-third_party/protobuf/protoc-linux-arm32.exe}
104 else
105 PROTOC=${PROTOC:-third_party/protobuf/protoc-linux-x86_32.exe}
106 fi
Philipp Wollermann19f34132015-06-12 08:39:25 +0000107 fi
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000108 ;;
109
Doug Rabson709bc612015-08-25 14:12:00 +0000110freebsd)
111 LDFLAGS="-lprocstat -lz -lrt $LDFLAGS"
112 JNILIB="libunix.so"
Googlerbdbf75f2015-11-17 12:50:44 +0000113 MD5SUM="md5"
Doug Rabson709bc612015-08-25 14:12:00 +0000114 # JAVA_HOME must point to a Java installation.
115 JAVA_HOME="${JAVA_HOME:-/usr/local/openjdk8}"
116 # Note: the linux protoc binary works on freebsd using linux emulation.
117 # We choose the 32-bit version for maximum compatiblity since 64-bit
118 # linux binaries are only supported in FreeBSD-11.
119 PROTOC=${PROTOC:-third_party/protobuf/protoc-linux-x86_32.exe}
120 ;;
121
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000122darwin)
123 JNILIB="libunix.dylib"
124 MD5SUM="md5"
125 LDFLAGS="-lz $LDFLAGS"
126 if [[ -z "$JAVA_HOME" ]]; then
127 JAVA_HOME="$(/usr/libexec/java_home -v ${JAVA_VERSION}+ 2> /dev/null)" \
128 || fail "Could not find JAVA_HOME, please ensure a JDK (version ${JAVA_VERSION}+) is installed."
129 fi
Philipp Wollermann19f34132015-06-12 08:39:25 +0000130 if [ "${MACHINE_IS_64BIT}" = 'yes' ]; then
131 PROTOC=${PROTOC:-third_party/protobuf/protoc-osx-x86_64.exe}
132 else
133 PROTOC=${PROTOC:-third_party/protobuf/protoc-osx-x86_32.exe}
134 fi
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000135 ;;
136
137msys*|mingw*)
138 # Use a simplified platform string.
139 PLATFORM="mingw"
140 # Workaround for msys issue which causes omission of std::to_string.
141 CXXFLAGS="$CXXFLAGS -D_GLIBCXX_USE_C99 -D_GLIBCXX_USE_C99_DYNAMIC"
142 LDFLAGS="-lz $LDFLAGS"
143 MD5SUM="md5sum"
144 EXE_EXT=".exe"
145 PATHSEP=";"
146 # Find the latest available version of the SDK.
147 JAVA_HOME="${JAVA_HOME:-$(ls -d /c/Program\ Files/Java/jdk* | sort | tail -n 1)}"
148 # We do not use the JNI library on Windows.
149 JNILIB=""
Googler8a0ce7b2015-07-22 09:10:08 +0000150 if [ "${MACHINE_IS_64BIT}" = 'yes' ]; then
151 PROTOC=${PROTOC:-third_party/protobuf/protoc-windows-x86_64.exe}
152 else
153 PROTOC=${PROTOC:-third_party/protobuf/protoc-windows-x86_32.exe}
154 fi
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000155
156 # The newer version of GCC on msys is stricter and removes some important function
157 # declarations from the environment if using c++0x / c++11.
158 CXXSTD="gnu++11"
159
160 # Ensure that we are using the cygwin gcc, not the mingw64 gcc.
161 ${CC} -v 2>&1 | grep "Target: .*mingw.*" > /dev/null &&
162 fail "mingw gcc detected. Please set CC to point to the msys/Cygwin gcc."
163 ${CXX} -v 2>&1 | grep "Target: .*mingw.*" > /dev/null &&
164 fail "mingw g++ detected. Please set CXX to point to the msys/Cygwin g++."
165
166 MSYS_DLLS="msys-2.0.dll msys-gcc_s-seh-1.dll msys-stdc++-6.dll"
167 for dll in $MSYS_DLLS ; do
168 cp "/usr/bin/$dll" "${OUTPUT_DIR}/$dll"
169 done
170esac
171
172[[ -x "${PROTOC-}" ]] \
173 || fail "Protobuf compiler not found in ${PROTOC-}"
174
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000175# Check that javac -version returns a upper version than $JAVA_VERSION.
Damien Martin-Guillerez338dc562015-06-23 17:15:12 +0000176get_java_version
177[ ${JAVA_VERSION#*.} -le ${JAVAC_VERSION#*.} ] || \
178 fail "JDK version (${JAVAC_VERSION}) is lower than ${JAVA_VERSION}, please set \$JAVA_HOME."
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000179
180JAR="${JAVA_HOME}/bin/jar"
181
182# Compiles java classes.
183function java_compilation() {
184 local name=$1
185 local directories=$2
186 local library_jars=$3
187 local output=$4
188
189 local classpath=${library_jars// /$PATHSEP}:$5
190 local sourcepath=${directories// /$PATHSEP}
191
192 tempdir
193 local tmp="${NEW_TMPDIR}"
194 local paramfile="${tmp}/param"
195 touch $paramfile
196
197 mkdir -p "${output}/classes"
198
199 # Compile .java files (incl. generated ones) using javac
200 log "Compiling $name code..."
201 find ${directories} -name "*.java" > "$paramfile"
202
203 if [ ! -z "$BAZEL_DEBUG_JAVA_COMPILATION" ]; then
204 echo "directories=${directories}" >&2
205 echo "classpath=${classpath}" >&2
206 echo "sourcepath=${sourcepath}" >&2
207 echo "libraries=${library_jars}" >&2
208 echo "output=${output}/classes" >&2
209 echo "List of compiled files:" >&2
210 cat "$paramfile" >&2
211 fi
212
213 run_silent "${JAVAC}" -classpath "${classpath}" -sourcepath "${sourcepath}" \
214 -d "${output}/classes" -source "$JAVA_VERSION" -target "$JAVA_VERSION" \
Damien Martin-Guillerez45d18d42015-09-21 11:45:10 +0000215 -encoding UTF-8 "@${paramfile}"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000216
217 log "Extracting helper classes for $name..."
218 for f in ${library_jars} ; do
219 run_silent unzip -qn ${f} -d "${output}/classes"
220 done
221}
222
223# Create the deploy JAR
224function create_deploy_jar() {
225 local name=$1
226 local mainClass=$2
227 local output=$3
228 shift 3
229 local packages=""
230 for i in $output/classes/*; do
231 local package=$(basename $i)
232 if [[ "$package" != "META-INF" ]]; then
233 packages="$packages -C $output/classes $package"
234 fi
235 done
236
237 log "Creating $name.jar..."
238 echo "Main-Class: $mainClass" > $output/MANIFEST.MF
239 run_silent "$JAR" cmf $output/MANIFEST.MF $output/$name.jar $packages "$@"
240}
241
242function cc_compile() {
243 local OBJDIR=$1
244 shift
245 mkdir -p "${OUTPUT_DIR}/${OBJDIR}"
246 for FILE in "$@"; do
247 if [[ ! "${FILE}" =~ ^-.*$ ]]; then
248 local OBJ=$(basename "${FILE}").o
249 run_silent "${CXX}" \
250 -I. \
251 ${CFLAGS} \
252 -std=$CXXSTD \
253 -c \
254 -DBLAZE_JAVA_CPU=\"k8\" \
255 -DBLAZE_OPENSOURCE=1 \
256 -o "${OUTPUT_DIR}/${OBJDIR}/${OBJ}" \
257 "${FILE}"
258 fi
259 done
260}
261
262function cc_link() {
263 local OBJDIR=$1
264 local OUTPUT=$2
265 shift 2
266 local FILES=()
267 for FILE in "$@"; do
268 local OBJ=$(basename "${FILE}").o
269 FILES+=("${OUTPUT_DIR}/${OBJDIR}/${OBJ}")
270 done
Lukacs Berki30bed8f2015-10-26 12:18:24 +0000271 run_silent "${CXX}" -o ${OUTPUT} "${FILES[@]}" ${LDFLAGS}
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000272}
273
274function cc_build() {
275 local NAME=$1
276 local OBJDIR=$2
277 local OUTPUT=$3
278 shift 3
279 log "Compiling ${NAME} .cc files..."
280 cc_compile "${OBJDIR}" "$@"
281 log "Linking ${NAME}..."
282 cc_link "${OBJDIR}" "${OUTPUT}" "$@"
283}
284
285if [ -z "${BAZEL_SKIP_JAVA_COMPILATION}" ]; then
286 log "Compiling Java stubs for protocol buffers..."
287 for f in $PROTO_FILES ; do
Damien Martin-Guillerez580cd692015-06-11 05:37:26 +0000288 run_silent "${PROTOC}" -Isrc/main/protobuf/ --java_out=${OUTPUT_DIR}/src "$f"
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000289 done
290
291 java_compilation "Bazel Java" "$DIRS" "$LIBRARY_JARS" "${OUTPUT_DIR}"
292
293 # help files: all non java and BUILD files in src/main/java.
294 for i in $(find src/main/java -type f -a \! -name '*.java' -a \! -name 'BUILD' | sed 's|src/main/java/||'); do
295 mkdir -p $(dirname ${OUTPUT_DIR}/classes/$i)
296 cp src/main/java/$i ${OUTPUT_DIR}/classes/$i
297 done
298
Damien Martin-Guillerez12c68aa2016-01-15 13:18:11 +0000299 # Overwrite tools.WORKSPACE, this is only for the bootstrap binary
300 echo "local_repository(name = 'bazel_tools', path = __workspace_dir__)" \
301 > ${OUTPUT_DIR}/classes/com/google/devtools/build/lib/bazel/rules/tools.WORKSPACE
302
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000303 create_deploy_jar "libblaze" "com.google.devtools.build.lib.bazel.BazelMain" \
Han-Wen Nienhuysa4b61822015-11-09 13:55:44 +0000304 ${OUTPUT_DIR}
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000305fi
306
307cc_build "client" "objs" "${OUTPUT_DIR}/client" ${BLAZE_CC_FILES[@]}
308
309if [ ! -z "$JNILIB" ] ; then
310 log "Compiling JNI libraries..."
311 for FILE in "${NATIVE_CC_FILES[@]}"; do
312 OUT=$(basename "${FILE}").o
313 run_silent "${CXX}" \
314 -I . \
315 -I "${JAVA_HOME}/include/" \
316 -I "${JAVA_HOME}/include/${PLATFORM}" \
317 -std=$CXXSTD \
318 -fPIC \
319 -c \
320 -D_JNI_IMPLEMENTATION_ \
321 -DBLAZE_JAVA_CPU=\"k8\" \
322 -DBLAZE_OPENSOURCE=1 \
323 -o "${OUTPUT_DIR}/native/${OUT}" \
324 "${FILE}"
325 done
326
327 log "Linking ${JNILIB}..."
Lukacs Berki30bed8f2015-10-26 12:18:24 +0000328 run_silent "${CXX}" -o ${OUTPUT_DIR}/${JNILIB} $JNI_LD_ARGS -shared ${OUTPUT_DIR}/native/*.o -lstdc++
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000329fi
330
Damien Martin-Guillerezdceee0e2016-01-18 11:57:26 +0000331# Dummy build-runfiles
332cat <<'EOF' >${OUTPUT_DIR}/build-runfiles${EXE_EXT}
333#!/bin/bash
334mkdir -p $2/MANIFEST
335cp $1 $2/MANIFEST
336EOF
337chmod 0755 ${OUTPUT_DIR}/build-runfiles${EXE_EXT}
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000338
339log "Compiling process-wrapper..."
Damien Martin-Guillerezec758db2016-01-18 12:14:28 +0000340cat <<'EOF' >${OUTPUT_DIR}/process-wrapper${EXE_EXT}
341#!/bin/bash
342# Dummy process wrapper, does not support timeout
343shift 2
344stdout="$1"
345stderr="$2"
346shift 2
347
348"$@" 2>"$stderr" >"$stdout"
349exit $?
350EOF
Philipp Wollermann43c4a1a2015-08-25 12:52:57 +0000351
Chris Parsonsf4888182016-01-08 00:42:14 +0000352log "Compiling xcode-locator..."
353if [[ $PLATFORM == "darwin" ]]; then
354 run_silent /usr/bin/xcrun clang -fobjc-arc -framework CoreServices -framework Foundation -o ${OUTPUT_DIR}/xcode-locator src/main/tools/xcode_locator.m
355else
356 cp src/main/tools/xcode_locator_stub.sh ${OUTPUT_DIR}/xcode-locator
357fi
358
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000359cp src/main/tools/build_interface_so ${OUTPUT_DIR}/build_interface_so
360cp src/main/tools/jdk.* ${OUTPUT_DIR}
361
362log "Creating Bazel self-extracting archive..."
Lukacs Berki8b2e6b82015-10-06 07:43:36 +0000363ARCHIVE_DIR=${OUTPUT_DIR}/archive
Damien Martin-Guillereze5b7c592016-01-18 11:03:59 +0000364for i in libblaze.jar ${JNILIB} build-runfiles${EXE_EXT} process-wrapper${EXE_EXT} xcode-locator${EXE_EXT} build_interface_so ${MSYS_DLLS} jdk.BUILD; do
Lukacs Berki8b2e6b82015-10-06 07:43:36 +0000365 mkdir -p $(dirname $ARCHIVE_DIR/$i);
366 cp $OUTPUT_DIR/$i $ARCHIVE_DIR/$i;
367done
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000368
Lukacs Berki8b2e6b82015-10-06 07:43:36 +0000369cp ${OUTPUT_DIR}/client ${ARCHIVE_DIR}
370cp ${OUTPUT_DIR}/libblaze.jar ${ARCHIVE_DIR}/A-server.jar
371
372(cd ${ARCHIVE_DIR}/ ; find . -type f | xargs cat | ${MD5SUM} | awk '{ print $1; }' > install_base_key)
373(cd ${ARCHIVE_DIR}/ ; echo "${JAVA_VERSION}" > java.version)
374(cd ${ARCHIVE_DIR}/ ; find . -type f | xargs -P 10 touch -t 198001010000)
375(cd ${ARCHIVE_DIR}/ ; run_silent zip $ZIPOPTS -r -q package.zip * install_base_key java.version)
376cat ${OUTPUT_DIR}/client ${ARCHIVE_DIR}/package.zip > ${OUTPUT_DIR}/bazel
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000377zip -qA ${OUTPUT_DIR}/bazel \
378 || echo "(Non-critical error, ignore.)"
379
380chmod 755 ${OUTPUT_DIR}/bazel