|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2016-2017 The Rust Project Developers. See the COPYRIGHT |
| 3 | +# file at the top-level directory of this distribution and at |
| 4 | +# http://rust-lang.org/COPYRIGHT. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 8 | +# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 9 | +# option. This file may not be copied, modified, or distributed |
| 10 | +# except according to those terms. |
| 11 | + |
| 12 | +set -eux |
| 13 | + |
| 14 | +arch=$1 |
| 15 | +binutils_version=2.25.1 |
| 16 | +freebsd_version=10.3 |
| 17 | +triple=$arch-unknown-freebsd10 |
| 18 | +sysroot=/usr/local/$triple |
| 19 | + |
| 20 | +hide_output() { |
| 21 | + set +x |
| 22 | + local on_err=" |
| 23 | +echo ERROR: An error was encountered with the build. |
| 24 | +cat /tmp/build.log |
| 25 | +exit 1 |
| 26 | +" |
| 27 | + trap "$on_err" ERR |
| 28 | + bash -c "while true; do sleep 30; echo \$(date) - building ...; done" & |
| 29 | + local ping_loop_pid=$! |
| 30 | + $@ &> /tmp/build.log |
| 31 | + trap - ERR |
| 32 | + kill $ping_loop_pid |
| 33 | + set -x |
| 34 | +} |
| 35 | + |
| 36 | +# First up, build binutils |
| 37 | +mkdir binutils |
| 38 | +cd binutils |
| 39 | +curl https://ftp.gnu.org/gnu/binutils/binutils-${binutils_version}.tar.bz2 | tar xjf - |
| 40 | +mkdir binutils-build |
| 41 | +cd binutils-build |
| 42 | +hide_output ../binutils-${binutils_version}/configure \ |
| 43 | + --target="$triple" --with-sysroot="$sysroot" |
| 44 | +hide_output make -j"$(getconf _NPROCESSORS_ONLN)" |
| 45 | +hide_output make install |
| 46 | +cd ../.. |
| 47 | +rm -rf binutils |
| 48 | + |
| 49 | +# Next, download the FreeBSD libraries and header files |
| 50 | +mkdir -p "$sysroot" |
| 51 | +case $arch in |
| 52 | + (x86_64) freebsd_arch=amd64 ;; |
| 53 | + (i686) freebsd_arch=i386 ;; |
| 54 | +esac |
| 55 | + |
| 56 | +files_to_extract=( |
| 57 | +"./usr/include" |
| 58 | +"./usr/lib/*crt*.o" |
| 59 | +) |
| 60 | +# Try to unpack only the libraries the build needs, to save space. |
| 61 | +for lib in c cxxrt gcc_s m thr util; do |
| 62 | + files_to_extract=("${files_to_extract[@]}" "./lib/lib${lib}.*" "./usr/lib/lib${lib}.*") |
| 63 | +done |
| 64 | +for lib in c++ c_nonshared compiler_rt execinfo gcc pthread rt ssp_nonshared; do |
| 65 | + files_to_extract=("${files_to_extract[@]}" "./usr/lib/lib${lib}.*") |
| 66 | +done |
| 67 | + |
| 68 | +URL=https://download.freebsd.org/ftp/releases/${freebsd_arch}/${freebsd_version}-RELEASE/base.txz |
| 69 | +curl "$URL" | tar xJf - -C "$sysroot" --wildcards "${files_to_extract[@]}" |
| 70 | + |
| 71 | +# Fix up absolute symlinks from the system image. This can be removed |
| 72 | +# for FreeBSD 11. (If there's an easy way to make them relative |
| 73 | +# symlinks instead, feel free to change this.) |
| 74 | +set +x |
| 75 | +find "$sysroot" -type l | while read symlink_path; do |
| 76 | + symlink_target=$(readlink "$symlink_path") |
| 77 | + case $symlink_target in |
| 78 | + (/*) |
| 79 | + echo "Fixing symlink ${symlink_path} -> ${sysroot}${symlink_target}" >&2 |
| 80 | + ln -nfs "${sysroot}${symlink_target}" "${symlink_path}" ;; |
| 81 | + esac |
| 82 | +done |
| 83 | +set -x |
| 84 | + |
| 85 | +# Clang can do cross-builds out of the box, if we give it the right |
| 86 | +# flags. (The local binutils seem to work, but they set the ELF |
| 87 | +# header "OS/ABI" (EI_OSABI) field to SysV rather than FreeBSD, so |
| 88 | +# there might be other problems.) |
| 89 | +# |
| 90 | +# The --target option is last because the cross-build of LLVM uses |
| 91 | +# --target without an OS version ("-freebsd" vs. "-freebsd10"). This |
| 92 | +# makes Clang default to libstdc++ (which no longer exists), and also |
| 93 | +# controls other features, like GNU-style symbol table hashing and |
| 94 | +# anything predicated on the version number in the __FreeBSD__ |
| 95 | +# preprocessor macro. |
| 96 | +for tool in clang clang++; do |
| 97 | + tool_path=/usr/local/bin/${triple}-${tool} |
| 98 | + cat > "$tool_path" <<EOF |
| 99 | +#!/bin/sh |
| 100 | +exec $tool --sysroot=$sysroot --prefix=${sysroot}/bin "\$@" --target=$triple |
| 101 | +EOF |
| 102 | + chmod +x "$tool_path" |
| 103 | +done |
0 commit comments