Skip to content

Commit 270c517

Browse files
committed
Re-do the FreeBSD cross-builds to use Clang and libc++. Fixes #44433.
The main goal here is to use FreeBSD's normal libc++, instead of statically linking the libstdc++ packaged with GCC, because that libstdc++ has bugs that cause rustc to deadlock inside LLVM. But the easiest way to use libc++ is to switch the build from GCC to Clang, and the Clang package in the Ubuntu image already knows how to cross-compile (given a sysroot and preferably cross-binutils), so the toolchain script now uses that instead of building a custom compiler. This also de-duplicates the `build-toolchain.sh` script.
1 parent f5e036a commit 270c517

File tree

6 files changed

+115
-234
lines changed

6 files changed

+115
-234
lines changed

src/ci/docker/dist-i686-freebsd/Dockerfile

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM ubuntu:16.04
22

33
RUN apt-get update && apt-get install -y --no-install-recommends \
4-
g++ \
4+
clang \
55
make \
66
file \
77
curl \
@@ -16,16 +16,16 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1616
libssl-dev \
1717
pkg-config
1818

19-
COPY dist-i686-freebsd/build-toolchain.sh /tmp/
20-
RUN /tmp/build-toolchain.sh i686
19+
COPY scripts/freebsd-toolchain.sh /tmp/
20+
RUN /tmp/freebsd-toolchain.sh i686
2121

2222
COPY scripts/sccache.sh /scripts/
2323
RUN sh /scripts/sccache.sh
2424

2525
ENV \
2626
AR_i686_unknown_freebsd=i686-unknown-freebsd10-ar \
27-
CC_i686_unknown_freebsd=i686-unknown-freebsd10-gcc \
28-
CXX_i686_unknown_freebsd=i686-unknown-freebsd10-g++
27+
CC_i686_unknown_freebsd=i686-unknown-freebsd10-clang \
28+
CXX_i686_unknown_freebsd=i686-unknown-freebsd10-clang++
2929

3030
ENV HOSTS=i686-unknown-freebsd
3131

src/ci/docker/dist-i686-freebsd/build-toolchain.sh

-112
This file was deleted.

src/ci/docker/dist-x86_64-freebsd/Dockerfile

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM ubuntu:16.04
22

33
RUN apt-get update && apt-get install -y --no-install-recommends \
4-
g++ \
4+
clang \
55
make \
66
file \
77
curl \
@@ -16,16 +16,16 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1616
libssl-dev \
1717
pkg-config
1818

19-
COPY dist-x86_64-freebsd/build-toolchain.sh /tmp/
20-
RUN /tmp/build-toolchain.sh x86_64
19+
COPY scripts/freebsd-toolchain.sh /tmp/
20+
RUN /tmp/freebsd-toolchain.sh x86_64
2121

2222
COPY scripts/sccache.sh /scripts/
2323
RUN sh /scripts/sccache.sh
2424

2525
ENV \
2626
AR_x86_64_unknown_freebsd=x86_64-unknown-freebsd10-ar \
27-
CC_x86_64_unknown_freebsd=x86_64-unknown-freebsd10-gcc \
28-
CXX_x86_64_unknown_freebsd=x86_64-unknown-freebsd10-g++
27+
CC_x86_64_unknown_freebsd=x86_64-unknown-freebsd10-clang \
28+
CXX_x86_64_unknown_freebsd=x86_64-unknown-freebsd10-clang++
2929

3030
ENV HOSTS=x86_64-unknown-freebsd
3131

src/ci/docker/dist-x86_64-freebsd/build-toolchain.sh

-112
This file was deleted.
+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

src/librustc_llvm/build.rs

+2
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ fn main() {
253253
let stdcppname = if target.contains("openbsd") {
254254
// OpenBSD has a particular C++ runtime library name
255255
"estdc++"
256+
} else if target.contains("freebsd") {
257+
"c++"
256258
} else if target.contains("netbsd") && llvm_static_stdcpp.is_some() {
257259
// NetBSD uses a separate library when relocation is required
258260
"stdc++_pic"

0 commit comments

Comments
 (0)