-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sh
executable file
·217 lines (184 loc) · 6.45 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env sh
REMOTEUSER=${USER} # Remote login for root not allowed in default settings
REMOTEIP=127.0.0.1 # Remote machine is in a VirtualBox on localhost
REMOTEPORT=2222 # VirtualBox might require port forwarding on localhost
# ^^^ DEFINITELY CONFIGURE THE ABOVE ^^^
PREFIX=`pwd`/crossrust
TOOLCHAIN=${PREFIX}/toolchain
SYSROOT=${PREFIX}/sysroot_mount
BUILD=${PREFIX}/build
DOWNLOAD=`pwd`/download
LOG=${PREFIX}/build.log
NCPUS=`sysctl -n hw.ncpu`
GCCMAJOR=8
BINUTILSVERSION=2.30
MPFRVERSION=4.0.1
MPCVERSION=1.1.0
GMPVERSION=6.1.2
# ^^^ TWEAK ABOVE IF NEEDED ^^^
REMOTEVERSIONLONG=`ssh -p ${REMOTEPORT} ${REMOTEUSER}@${REMOTEIP} sysctl -n kern.osreldate` || (echo Error reading remote OS version && exit)
REMOTEVERSION=${REMOTEVERSIONLONG:0:2}
TARGET=x86_64-unknown-freebsd${REMOTEVERSION}
export CC=/usr/local/bin/gcc-${GCCMAJOR}
export CXX=/usr/local/bin/g++-${GCCMAJOR}
export CPP=/usr/local/bin/cpp-${GCCMAJOR}
export LD=/usr/local/bin/gcc-${GCCMAJOR}
if [ ! -d ${PREFIX} ]
then
echo "===> Creating dir ${PREFIX}"
mkdir -p ${PREFIX} || (echo "mkdir -p ${PREFIX} failed. Trying with sudo." && sudo mkdir -p ${PREFIX} && sudo chown ${USER} ${PREFIX} || exit 1)
fi
mkdir -p ${BUILD} || exit 1
mkdir -p ${TOOLCHAIN} || exit 1
mkdir -p ${SYSROOT} || exit 1
mkdir -p ${DOWNLOAD} || exit 1
touch ${LOG}
echo Starting log > ${LOG}
rsync --version >> ${LOG} && echo "===> Found rsync" || (echo "Please install rsync" && exit 1)
ssh -p ${REMOTEPORT} ${REMOTEUSER}@${REMOTEIP} rsync --version >> ${LOG} && echo "===> Found remote rsync" || (echo "Please install rsync on remote machine" && exit 1)
brew -v >> ${LOG} && echo "===> Found brew" || (echo "Please install brew" && exit 1)
if [ ! -f ${PREFIX}/.brew ]
then
echo "===> Installing tools with brew"
brew install gcc@${GCCMAJOR} || exit 1
brew install wget || exit 1
brew cask install osxfuse || exit 1
brew install sshfs || exit 1
touch ${PREFIX}/.brew
else
echo "===> Skipping install tools"
fi
${CC} --version >> ${LOG} && echo "===> Found ${CC}" || (echo "${CC} not found" && exit 1)
GCCVERSION=`${CC} --version | head -n 1 | awk '{print $5}'`
echo "===> Mounting remote root with sshfs as sysroot at ${SYSROOT}"
sshfs -p ${REMOTEPORT} -o idmap=user,follow_symlinks ${REMOTEUSER}@${REMOTEIP}:/ ${SYSROOT} || exit 1
#
# Download tarballs
#
if [ ! -f ${PREFIX}/.fetch ]
then
echo "===> Fetching tarballs"
cd ${DOWNLOAD}
wget -c http://ftp.gnu.org/gnu/binutils/binutils-${BINUTILSVERSION}.tar.gz || exit 1
wget -c http://ftp.gnu.org/gnu/mpfr/mpfr-${MPFRVERSION}.zip || exit 1
wget -c http://ftp.gnu.org/gnu/mpc/mpc-${MPCVERSION}.tar.gz || exit 1
wget -c http://ftp.gnu.org/gnu/gmp/gmp-${GMPVERSION}.tar.xz || exit 1
wget -c http://ftp.gnu.org/gnu/gcc/gcc-${GCCVERSION}/gcc-${GCCVERSION}.tar.gz || exit 1
touch ${PREFIX}/.fetch
else
echo "===> Skipping fetch tarballs"
fi
#
# Extract tarballs
#
if [ ! -f ${PREFIX}/.extract ]
then
echo "===> Extracting tarballs"
cd ${BUILD}
tar xf ${DOWNLOAD}/binutils-${BINUTILSVERSION}.tar.gz || exit 1
unzip -q ${DOWNLOAD}/mpfr-${MPFRVERSION}.zip || exit 1
tar xf ${DOWNLOAD}/mpc-${MPCVERSION}.tar.gz || exit 1
tar xf ${DOWNLOAD}/gmp-${GMPVERSION}.tar.xz || exit 1
tar xf ${DOWNLOAD}/gcc-${GCCVERSION}.tar.gz || exit 1
touch ${PREFIX}/.extract
else
echo "===> Skipping extract tarballs"
fi
#
# Build binutils
#
if [ ! -f ${PREFIX}/.binutils ]
then
echo "===> Building binutils"
cd ${BUILD}
mkdir -p build-binutils || exit 1
cd build-binutils
../binutils-${BINUTILSVERSION}/configure --prefix=${TOOLCHAIN} --target=${TARGET} --with-sysroot=${SYSROOT} \
--enable-interwork --enable-multilib --disable-nls --disable-werror || exit 1
make -j${NCPUS} || exit 1
make install || exit 1
touch ${PREFIX}/.binutils
else
echo "===> Skipping binutils"
fi
#
# GCC
#
# Link in libraries so they will be built together with gcc.
cd ${BUILD}
ln -sf mpfr-${MPFRVERSION} gcc-${GCCVERSION}/mpfr || exit 1
ln -sf mpc-${MPCVERSION} gcc-${GCCVERSION}/mpc || exit 1
ln -sf gmp-${GMPVERSION} gcc-${GCCVERSION}/gmp || exit 1
# Build GCC
if [ ! -f ${PREFIX}/.buildgcc ]
then
echo "===> Building gcc"
cd ${BUILD}
rm -fr build-gcc > /dev/null
mkdir -p build-gcc || exit 1
cd build-gcc
../gcc-${GCCVERSION}/configure --prefix=${TOOLCHAIN} --target=${TARGET} --with-sysroot=${SYSROOT} \
--disable-nls --enable-languages=c,c++ --without-headers --enable-interwork --enable-multilib || exit 1
make -j${NCPUS} || exit 1
make install || exit 1
touch ${PREFIX}/.buildgcc
else
echo "===> Skipping build gcc"
fi
unset CC
unset CXX
unset CPP
unset LD
#
# Rust
#
rustup -V >> ${LOG} && echo "===> Found rustup" || (echo "Please install rustup" && exit 1)
# Download toolchain
if [ ! -f ${PREFIX}/.rustup ]
then
echo "===> Installing rust toolchains"
rustup toolchain install nightly
rustup target add x86_64-unknown-freebsd || exit 1
rustup toolchain install nightly-x86_64-unknown-freebsd || exit 1
touch ${PREFIX}/.rustup
else
echo "===> Skipping install rust toolchains"
fi
# Set linker
if [ ! -f ${PREFIX}/.linker ]
then
echo "===> Setting Cargo linker"
cd ${PREFIX}
mkdir -p .cargo || exit 1
cat <<EOF > .cargo/config
[target.x86_64-unknown-freebsd]
linker = "${TOOLCHAIN}/bin/x86_64-unknown-freebsd${REMOTEVERSION}-gcc"
EOF
touch ${PREFIX}/.linker
else
echo "===> Skipping set Cargo linker"
fi
# Create and build hello world crate
cd ${PREFIX}
if [ ! -f ${PREFIX}/.helloworld ]
then
echo "===> Building rust hello world crate"
rm -fr helloworld > /dev/null
cargo new --bin helloworld || exit 1
cd helloworld
rustup override set nightly || exit 1
cargo build --target x86_64-unknown-freebsd || exit 1
file target/x86_64-unknown-freebsd/debug/helloworld | grep -q FreeBSD && Echo "Successfully cross compiled FreeBSD binary" || (echo "Something is wrong the the compiled rust binary" && exit 1)
touch ${PREFIX}/.helloworld
else
echo "===> Skipping build rust hello world crate"
fi
echo "===> Unmounting sysroot"
umount ${SYSROOT}
#
# Run on remote machine
#
echo "===> Copy binary to remote machine"
scp -P ${REMOTEPORT} ${PREFIX}/helloworld/target/x86_64-unknown-freebsd/debug/helloworld ${REMOTEUSER}@${REMOTEIP}:/tmp/helloworld > /dev/null || exit 1
echo "===> Executing binary on remote FreeBSD machine. Expected output is \"Hello, world!\", actual output is: "
ssh -p ${REMOTEPORT} ${REMOTEUSER}@${REMOTEIP} /tmp/helloworld