]>
Commit | Line | Data |
---|---|---|
ff7c6d11 | 1 | #!/bin/bash |
9fa01778 XL |
2 | # ignore-tidy-linelength |
3 | ||
ff7c6d11 XL |
4 | set -eux |
5 | ||
6 | arch=$1 | |
7 | binutils_version=2.25.1 | |
923072b8 FG |
8 | freebsd_version=12.3 |
9 | triple=$arch-unknown-freebsd12 | |
ff7c6d11 XL |
10 | sysroot=/usr/local/$triple |
11 | ||
12 | hide_output() { | |
13 | set +x | |
14 | local on_err=" | |
15 | echo ERROR: An error was encountered with the build. | |
16 | cat /tmp/build.log | |
17 | exit 1 | |
18 | " | |
19 | trap "$on_err" ERR | |
20 | bash -c "while true; do sleep 30; echo \$(date) - building ...; done" & | |
21 | local ping_loop_pid=$! | |
29967ef6 | 22 | "$@" &> /tmp/build.log |
ff7c6d11 XL |
23 | trap - ERR |
24 | kill $ping_loop_pid | |
25 | set -x | |
26 | } | |
27 | ||
28 | # First up, build binutils | |
29 | mkdir binutils | |
30 | cd binutils | |
31 | curl https://ftp.gnu.org/gnu/binutils/binutils-${binutils_version}.tar.bz2 | tar xjf - | |
32 | mkdir binutils-build | |
33 | cd binutils-build | |
34 | hide_output ../binutils-${binutils_version}/configure \ | |
35 | --target="$triple" --with-sysroot="$sysroot" | |
36 | hide_output make -j"$(getconf _NPROCESSORS_ONLN)" | |
37 | hide_output make install | |
38 | cd ../.. | |
39 | rm -rf binutils | |
40 | ||
41 | # Next, download the FreeBSD libraries and header files | |
42 | mkdir -p "$sysroot" | |
43 | case $arch in | |
44 | (x86_64) freebsd_arch=amd64 ;; | |
45 | (i686) freebsd_arch=i386 ;; | |
46 | esac | |
47 | ||
48 | files_to_extract=( | |
49 | "./usr/include" | |
50 | "./usr/lib/*crt*.o" | |
51 | ) | |
52 | # Try to unpack only the libraries the build needs, to save space. | |
53 | for lib in c cxxrt gcc_s m thr util; do | |
54 | files_to_extract=("${files_to_extract[@]}" "./lib/lib${lib}.*" "./usr/lib/lib${lib}.*") | |
55 | done | |
a2a8927a | 56 | for lib in c++ c_nonshared compiler_rt execinfo gcc pthread rt ssp_nonshared procstat devstat kvm; do |
ff7c6d11 XL |
57 | files_to_extract=("${files_to_extract[@]}" "./usr/lib/lib${lib}.*") |
58 | done | |
59 | ||
9fa01778 | 60 | # Originally downloaded from: |
3dfed10e | 61 | # URL=https://download.freebsd.org/ftp/releases/${freebsd_arch}/${freebsd_version}-RELEASE/base.txz |
923072b8 | 62 | URL=https://ci-mirrors.rust-lang.org/rustc/2022-05-06-freebsd-${freebsd_version}-${freebsd_arch}-base.txz |
ff7c6d11 XL |
63 | curl "$URL" | tar xJf - -C "$sysroot" --wildcards "${files_to_extract[@]}" |
64 | ||
ff7c6d11 XL |
65 | # Clang can do cross-builds out of the box, if we give it the right |
66 | # flags. (The local binutils seem to work, but they set the ELF | |
67 | # header "OS/ABI" (EI_OSABI) field to SysV rather than FreeBSD, so | |
68 | # there might be other problems.) | |
69 | # | |
70 | # The --target option is last because the cross-build of LLVM uses | |
923072b8 | 71 | # --target without an OS version ("-freebsd" vs. "-freebsd12"). This |
ff7c6d11 XL |
72 | # makes Clang default to libstdc++ (which no longer exists), and also |
73 | # controls other features, like GNU-style symbol table hashing and | |
74 | # anything predicated on the version number in the __FreeBSD__ | |
75 | # preprocessor macro. | |
76 | for tool in clang clang++; do | |
77 | tool_path=/usr/local/bin/${triple}-${tool} | |
78 | cat > "$tool_path" <<EOF | |
79 | #!/bin/sh | |
80 | exec $tool --sysroot=$sysroot --prefix=${sysroot}/bin "\$@" --target=$triple | |
81 | EOF | |
82 | chmod +x "$tool_path" | |
83 | done |