1d0097591SIngo Weinhold#!/bin/sh
2d0097591SIngo Weinhold#
396c72d69SFrançois Revol# parameters <machine> <haiku sourcedir> <buildtools dir> <install dir>
429ef597dSIngo Weinhold
529ef597dSIngo Weinhold# get and check the parameters
660d8d8fcSIngo Weinholdif [ $# -lt 4 ]; then
760d8d8fcSIngo Weinhold	echo Usage: $0 '<machine> <haiku sourcedir> <buildtools dir>' \
8c0e8cc1aSIngo Weinhold		'<install dir>' >&2
929ef597dSIngo Weinhold	exit 1
1029ef597dSIngo Weinholdfi
1129ef597dSIngo Weinhold
12d1feb7cbSwaddlesplashset -e
13d1feb7cbSwaddlesplash
1420ab75e6SIngo WeinholdhaikuMachine=$1
1520ab75e6SIngo WeinholdhaikuSourceDir=$2
1620ab75e6SIngo WeinholdbuildToolsDir=$3
17c0e8cc1aSIngo WeinholdinstallDir=$4
1860d8d8fcSIngo Weinholdshift 4
1960d8d8fcSIngo WeinholdadditionalMakeArgs=$*
204da6cf84SFrançois RevolgdbSourceDir="$HAIKU_USE_GDB"
2129ef597dSIngo Weinhold
22b3ece905SJérôme DuvalccFlags="-O2"
23b3ece905SJérôme DuvalcxxFlags="-O2"
241c91ed03SFrançois RevolbinutilsTargets="$haikuMachine"
254da6cf84SFrançois RevolgdbTarget="$haikuMachine"
2691446a14SFrançois Revolcase $haikuMachine in
271c91ed03SFrançois Revoli586-*)
281c91ed03SFrançois Revol	binutilsConfigureArgs="--disable-multilib"
291c91ed03SFrançois Revol	gccConfigureArgs="--disable-multilib"
301c91ed03SFrançois Revol	binutilsTargets="$binutilsTargets,i386-efi-pe,x86_64-efi-pe"
314da6cf84SFrançois Revol	gdbConfigureArgs="--disable-multilib"
321c91ed03SFrançois Revol	;;
33266681e2SIngo Weinholdx86_64-*)
3481eb68f2SAlex Smith	# GCC's default is to enable multilib, but there is a bug when
3581eb68f2SAlex Smith	# explicitly using --enable-multilib that causes a build
3681eb68f2SAlex Smith	# failure
3781eb68f2SAlex Smith	binutilsConfigureArgs=""
381c91ed03SFrançois Revol	binutilsTargets="$binutilsTargets,i386-efi-pe,x86_64-efi-pe"
394da6cf84SFrançois Revol	gdbConfigureArgs="--disable-multilib"
40266681e2SIngo Weinhold	;;
4191446a14SFrançois Revolm68k-*)
4291446a14SFrançois Revol	binutilsConfigureArgs="--enable-multilib"
4391446a14SFrançois Revol	gccConfigureArgs="--enable-multilib"
444da6cf84SFrançois Revol	gdbConfigureArgs="--disable-multilib"
45e4091183SFrançois Revol	gdbTarget="m68k-unknown-elf"
4691446a14SFrançois Revol	;;
4791446a14SFrançois Revolarm-*)
48b8058ea8SPulkoMandy	# Multilib creates a lot of confusion as the wrong libs end up being linked.
49574dbabbSPulkoMandy	# For now, target Cortex-A8 devices.
50574dbabbSPulkoMandy	binutilsConfigureArgs="--disable-multilib --with-float=hard
51d958d336SAdrien Destugues		--with-cpu=cortex-a8 --with-fpu=vfpv3"
52574dbabbSPulkoMandy	gccConfigureArgs="--disable-multilib --with-float=hard
53d958d336SAdrien Destugues		--with-cpu=cortex-a8 --with-fpu=vfpv3"
54d1e057eeSArvind S Raj
55d1e057eeSArvind S Raj	# TODO: Disable building with TLS support for ARM until implemented.
566d345e6cSArvind S Raj	binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls"
576d345e6cSArvind S Raj	gccConfigureArgs="$gccConfigureArgs --disable-tls"
584da6cf84SFrançois Revol	gdbConfigureArgs="--disable-multilib --disable-werror -enable-interwork"
594da6cf84SFrançois Revol	gdbTarget="arm-unknown-elf"
6091446a14SFrançois Revol	;;
613d4c153eSAlexander von Gluck IVriscv*-*)
623d4c153eSAlexander von Gluck IV	binutilsConfigureArgs="--disable-multilib"
633d4c153eSAlexander von Gluck IV	gccConfigureArgs="--disable-multilib"
643d4c153eSAlexander von Gluck IV	gdbConfigureArgs="--disable-multilib"
653d4c153eSAlexander von Gluck IV
663d4c153eSAlexander von Gluck IV	# TODO: Disable building with TLS support for riscv until implemented.
673d4c153eSAlexander von Gluck IV	binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls"
683d4c153eSAlexander von Gluck IV	gccConfigureArgs="$gccConfigureArgs --disable-tls"
693d4c153eSAlexander von Gluck IV	;;
701c91ed03SFrançois Revolpowerpc-*)
711c91ed03SFrançois Revol	binutilsConfigureArgs="--disable-multilib"
721c91ed03SFrançois Revol	gccConfigureArgs="--disable-multilib"
731c91ed03SFrançois Revol	binutilsTargets="$binutilsTargets,powerpc-apple-linux,powerpc-apple-freebsd,powerpc-apple-vxworks"
744da6cf84SFrançois Revol	gdbConfigureArgs="--disable-multilib --disable-werror"
754da6cf84SFrançois Revol	gdbTarget="powerpc-unknown-elf"
7682876dc4SAlexander von Gluck IV
7782876dc4SAlexander von Gluck IV	# TODO: Disable building with TLS support for PPC until implemented.
7882876dc4SAlexander von Gluck IV	binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls"
7982876dc4SAlexander von Gluck IV	gccConfigureArgs="$gccConfigureArgs --disable-tls"
801c91ed03SFrançois Revol	;;
8191446a14SFrançois Revol*)
8291446a14SFrançois Revol	binutilsConfigureArgs="--disable-multilib"
8391446a14SFrançois Revol	gccConfigureArgs="--disable-multilib"
844da6cf84SFrançois Revol	gdbConfigureArgs="--disable-multilib"
8591446a14SFrançois Revol	;;
8691446a14SFrançois Revolesac
8791446a14SFrançois Revol
88a7e9ce67SJohn Scipioneif [ `uname -s` = 'Haiku' ]; then
89de2188e9SOliver Tappe	# force cross-build if building on Haiku:
90de2188e9SOliver Tappe	buildhostMachine=${haikuMachine}_buildhost
91de2188e9SOliver Tappe	buildHostSpec="--build=$buildhostMachine --host=$buildhostMachine"
92de2188e9SOliver Tappefi
93de2188e9SOliver Tappe
94467f4ab3SJeroen Oortwijnif [ ! -d "$haikuSourceDir" ]; then
9529ef597dSIngo Weinhold	echo "No such directory: \"$haikuSourceDir\"" >&2
9629ef597dSIngo Weinhold	exit 1
9729ef597dSIngo Weinholdfi
9829ef597dSIngo Weinhold
99467f4ab3SJeroen Oortwijnif [ ! -d "$buildToolsDir" ]; then
10029ef597dSIngo Weinhold	echo "No such directory: \"$buildToolsDir\"" >&2
10129ef597dSIngo Weinhold	exit 1
10229ef597dSIngo Weinholdfi
10329ef597dSIngo Weinhold
1044da6cf84SFrançois Revolif [ -n "$gdbSourceDir" -a ! -d "$gdbSourceDir" ]; then
1054da6cf84SFrançois Revol	echo "No such directory: \"$gdbSourceDir\"" >&2
1064da6cf84SFrançois Revol	exit 1
1074da6cf84SFrançois Revolfi
1084da6cf84SFrançois Revol
10929ef597dSIngo Weinhold
11029ef597dSIngo Weinhold# create the output dir
111467f4ab3SJeroen Oortwijnmkdir -p "$installDir" || exit 1
11229ef597dSIngo Weinhold
11329ef597dSIngo Weinhold
11429ef597dSIngo Weinhold# get absolute paths
11529ef597dSIngo WeinholdcurrentDir=$(pwd)
11629ef597dSIngo Weinhold
117467f4ab3SJeroen Oortwijncd "$haikuSourceDir"
11829ef597dSIngo WeinholdhaikuSourceDir=$(pwd)
119467f4ab3SJeroen Oortwijncd "$currentDir"
12029ef597dSIngo Weinhold
121467f4ab3SJeroen Oortwijncd "$buildToolsDir"
12229ef597dSIngo WeinholdbuildToolsDir=$(pwd)
123467f4ab3SJeroen Oortwijncd "$currentDir"
12429ef597dSIngo Weinhold
125467f4ab3SJeroen Oortwijncd "$installDir"
126c0e8cc1aSIngo WeinholdinstallDir=$(pwd)
127467f4ab3SJeroen Oortwijncd "$currentDir"
12829ef597dSIngo Weinhold
1294da6cf84SFrançois Revolif [ -n "$gdbSourceDir" ]; then
1304da6cf84SFrançois Revolcd "$gdbSourceDir"
1314da6cf84SFrançois RevolgdbSourceDir=$(pwd)
1324da6cf84SFrançois Revolcd "$currentDir"
1334da6cf84SFrançois Revolfi
1344da6cf84SFrançois Revol
135467f4ab3SJeroen OortwijnbinutilsSourceDir="$buildToolsDir/binutils"
136467f4ab3SJeroen OortwijngccSourceDir="$buildToolsDir/gcc"
13729ef597dSIngo Weinhold
13829ef597dSIngo Weinhold
13929ef597dSIngo Weinhold# get gcc version
140467f4ab3SJeroen OortwijngccVersion=$(cat "$gccSourceDir/gcc/BASE-VER")
14129ef597dSIngo Weinhold
14229ef597dSIngo Weinholdif [ -z "$gccVersion" ]; then
14329ef597dSIngo Weinhold	echo "Failed to find out gcc version." >&2
14429ef597dSIngo Weinhold	exit 1
14529ef597dSIngo Weinholdfi
14629ef597dSIngo Weinhold
1475a7156f4SAugustin Cavalier# clear out the "missing" scripts so that they don't cause errors on tools
1485a7156f4SAugustin Cavalier# that are not installed, as we will have committed any files generated by
1495a7156f4SAugustin Cavalier# them to the buildtools repo already.
1505a7156f4SAugustin Cavalierecho "#!/bin/sh" >"$binutilsSourceDir"/missing
1515a7156f4SAugustin Cavalierecho "#!/bin/sh" >"$gccSourceDir"/missing
1525a7156f4SAugustin Cavalierecho "#!/bin/sh" >"$gccSourceDir"/isl/missing
153d8b0a135SAugustin Cavalier
15429ef597dSIngo Weinhold# create the object and installation directories for the cross compilation tools
155467f4ab3SJeroen OortwijnobjDir="${installDir}-build"
156467f4ab3SJeroen OortwijnbinutilsObjDir="$objDir/binutils"
157467f4ab3SJeroen OortwijngccObjDir="$objDir/gcc"
1584da6cf84SFrançois RevolgdbObjDir="$objDir/gdb"
159467f4ab3SJeroen OortwijnstdcxxObjDir="$objDir/stdcxx"
160467f4ab3SJeroen OortwijnsysrootDir="$installDir/sysroot"
161467f4ab3SJeroen OortwijntmpIncludeDir="$sysrootDir/boot/system/develop/headers"
162467f4ab3SJeroen OortwijntmpLibDir="$sysrootDir/boot/system/develop/lib"
16329ef597dSIngo Weinhold
164467f4ab3SJeroen Oortwijnrm -rf "$installDir" "$objDir"
16529ef597dSIngo Weinhold
1664da6cf84SFrançois Revolmkdir -p "$installDir" "$objDir" "$binutilsObjDir" "$gccObjDir" "$gdbObjDir" \
1674da6cf84SFrançois Revol	"$stdcxxObjDir" "$tmpIncludeDir" "$tmpLibDir" || exit 1
168467f4ab3SJeroen Oortwijnmkdir -p "$installDir/lib/gcc/$haikuMachine/$gccVersion"
16920ab75e6SIngo Weinhold
1700837d6c6SJerome Duvalif [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
171467f4ab3SJeroen Oortwijn	cloogSourceDir="$buildToolsDir/cloog"
172467f4ab3SJeroen Oortwijn	gmpSourceDir="$buildToolsDir/gcc/gmp"
1731e93288eSJérôme Duval	islSourceDir="$buildToolsDir/isl"
174467f4ab3SJeroen Oortwijn
1751e93288eSJérôme Duval	islObjDir="$objDir/isl"
176467f4ab3SJeroen Oortwijn	gmpObjDir="$objDir/gmp"
177467f4ab3SJeroen Oortwijn	cloogObjDir="$objDir/cloog"
1781e93288eSJérôme Duval	mkdir -p "$islObjDir" "$gmpObjDir" "$cloogObjDir" || exit 1
179467f4ab3SJeroen Oortwijn
1801e93288eSJérôme Duval	gccConfigureArgs="$gccConfigureArgs --with-cloog=$installDir \
1811e93288eSJérôme Duval		--enable-cloog-backend=isl --with-isl=$installDir \
1821e93288eSJérôme Duval		--with-gmp=$installDir \
183575cfdf2SJérôme Duval		--with-host-libstdcxx=\"-lstdc++\""
1840837d6c6SJerome Duvalfi
185b3ece905SJérôme Duvalif [ "$HAIKU_USE_GCC_PIPE" = 1 ]; then
186b3ece905SJérôme Duval	ccFlags="$ccFlags -pipe"
187b3ece905SJérôme Duval	cxxFlags="$cxxFlags -pipe"
188b3ece905SJérôme Duvalfi
1890837d6c6SJerome Duval
19068ccc989SIngo Weinholdif [ -n "$SECONDARY_ARCH" ]; then
191394c099fSIngo Weinhold	gccConfigureArgs="$gccConfigureArgs --with-hybrid-secondary=$SECONDARY_ARCH"
19268ccc989SIngo Weinholdfi
19368ccc989SIngo Weinhold
194d0064875SOliver Tappe# force the POSIX locale, as the build (makeinfo) might choke otherwise
195d0064875SOliver Tappeexport LC_ALL=POSIX
19629ef597dSIngo Weinhold
1974da6cf84SFrançois Revol# build gdb
1984da6cf84SFrançois Revolif [ -n "$HAIKU_USE_GDB" ]; then
1994da6cf84SFrançois Revolcd "$gdbObjDir"
2004da6cf84SFrançois Revol	"$gdbSourceDir/configure" \
2014da6cf84SFrançois Revol		--prefix="$installDir" --target=$gdbTarget \
2024da6cf84SFrançois Revol		$gdbConfigureArgs \
2034da6cf84SFrançois Revol		|| exit 1
2044da6cf84SFrançois Revol	$MAKE $additionalMakeArgs || exit 1
2054da6cf84SFrançois Revol	$MAKE $additionalMakeArgs install || exit 1
2064da6cf84SFrançois Revolfi
2074da6cf84SFrançois Revol
20829ef597dSIngo Weinhold# build binutils
209467f4ab3SJeroen Oortwijncd "$binutilsObjDir"
210b3ece905SJérôme DuvalCFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$binutilsSourceDir/configure" \
211467f4ab3SJeroen Oortwijn	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
2121c91ed03SFrançois Revol	--enable-targets=$binutilsTargets \
213de2188e9SOliver Tappe	--disable-nls --disable-shared --disable-werror \
214849f5810SAugustin Cavalier	--with-sysroot="$sysrootDir" \
21575f7932aSwaddlesplash	--disable-maintainer-mode \
216de2188e9SOliver Tappe	$binutilsConfigureArgs \
217de2188e9SOliver Tappe	|| exit 1
21860d8d8fcSIngo Weinhold$MAKE $additionalMakeArgs || exit 1
21960d8d8fcSIngo Weinhold$MAKE $additionalMakeArgs install || exit 1
22029ef597dSIngo Weinhold
2214dd5af2fSAugustin Cavalierexport PATH="$PATH:$installDir/bin"
22229ef597dSIngo Weinhold
2230837d6c6SJerome Duvalif [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
224e0d19800SJerome Duval	# build gmp
225467f4ab3SJeroen Oortwijn	cd "$gmpObjDir"
226b3ece905SJérôme Duval	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gmpSourceDir/configure" \
227b3ece905SJérôme Duval		--prefix="$installDir" --disable-shared --enable-cxx || exit 1
228e0d19800SJerome Duval	$MAKE $additionalMakeArgs || exit 1
229e0d19800SJerome Duval	$MAKE $additionalMakeArgs install || exit 1
230f0aee2b4SFredrik Holmqvist
2311e93288eSJérôme Duval	# build isl
2321e93288eSJérôme Duval	cd "$islObjDir"
233b3ece905SJérôme Duval	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$islSourceDir/configure" \
2341e93288eSJérôme Duval		--prefix="$installDir" --disable-nls --disable-shared \
2351e93288eSJérôme Duval		 --with-gmp-prefix="$installDir" || exit 1
2361e93288eSJérôme Duval	$MAKE $additionalMakeArgs || exit 1
2371e93288eSJérôme Duval	$MAKE $additionalMakeArgs install || exit 1
2380837d6c6SJerome Duval
2390837d6c6SJerome Duval	# build cloog
240467f4ab3SJeroen Oortwijn	cd "$cloogObjDir"
241b3ece905SJérôme Duval	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$cloogSourceDir/configure" \
242467f4ab3SJeroen Oortwijn		--prefix="$installDir" --disable-nls --disable-shared \
2431e93288eSJérôme Duval		--with-gmp-prefix="$installDir" --with-isl-prefix=="$installDir" \
2441e93288eSJérôme Duval		|| exit 1
2450837d6c6SJerome Duval	$MAKE $additionalMakeArgs || exit 1
2460837d6c6SJerome Duval	$MAKE $additionalMakeArgs install || exit 1
2470837d6c6SJerome Duvalfi
24829ef597dSIngo Weinhold
24929ef597dSIngo Weinhold# build gcc
25029ef597dSIngo Weinhold
25129ef597dSIngo Weinhold# prepare the include files
25229ef597dSIngo Weinholdcopy_headers()
25329ef597dSIngo Weinhold{
25429ef597dSIngo Weinhold	sourceDir=$1
25529ef597dSIngo Weinhold	targetDir=$2
25629ef597dSIngo Weinhold
2574107ac9cSJérôme Duval	headers="$(find $sourceDir -name \*\.h)"
25829ef597dSIngo Weinhold	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
25929ef597dSIngo Weinhold	for f in $headers; do
260467f4ab3SJeroen Oortwijn		headerTargetDir="$targetDir/$(dirname $f)"
261467f4ab3SJeroen Oortwijn		mkdir -p "$headerTargetDir"
262467f4ab3SJeroen Oortwijn		cp "$sourceDir/$f" "$headerTargetDir"
26329ef597dSIngo Weinhold	done
26429ef597dSIngo Weinhold}
26529ef597dSIngo Weinhold
266467f4ab3SJeroen Oortwijncopy_headers "$haikuSourceDir/headers/config" "$tmpIncludeDir/config"
267467f4ab3SJeroen Oortwijncopy_headers "$haikuSourceDir/headers/os" "$tmpIncludeDir/os"
268467f4ab3SJeroen Oortwijncopy_headers "$haikuSourceDir/headers/posix" "$tmpIncludeDir/posix"
26929ef597dSIngo Weinhold
27029ef597dSIngo Weinhold# configure gcc
271467f4ab3SJeroen Oortwijncd "$gccObjDir"
272b3ece905SJérôme DuvalCFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gccSourceDir/configure" \
273467f4ab3SJeroen Oortwijn	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
274de2188e9SOliver Tappe	--disable-nls --disable-shared --with-system-zlib \
275c59b279bSJérôme Duval	--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
2766cc6f047SJessica Hamilton	--enable-__cxa-atexit --enable-threads=posix \
2776cc6f047SJessica Hamilton	--with-default-libstdcxx-abi=gcc4-compatible \
2786cc6f047SJessica Hamilton	--with-sysroot="$sysrootDir" \
27975f7932aSwaddlesplash	--disable-maintainer-mode \
280de2188e9SOliver Tappe	$gccConfigureArgs \
281de2188e9SOliver Tappe	|| exit 1
28229ef597dSIngo Weinhold
28329ef597dSIngo Weinhold# make gcc
28460d8d8fcSIngo Weinhold$MAKE $additionalMakeArgs || {
28529ef597dSIngo Weinhold	echo "ERROR: Building gcc failed." >&2
28629ef597dSIngo Weinhold	exit 1
28729ef597dSIngo Weinhold}
28829ef597dSIngo Weinhold
28929ef597dSIngo Weinhold# install gcc
29060d8d8fcSIngo Weinhold$MAKE $additionalMakeArgs install || {
29129ef597dSIngo Weinhold	echo "ERROR: Installing the cross compiler failed." >&2
29229ef597dSIngo Weinhold	exit 1
29329ef597dSIngo Weinhold}
29429ef597dSIngo Weinhold
29511ae3c5fSOliver Tappecase $haikuMachine in
29611ae3c5fSOliver Tappex86_64-*)
29711ae3c5fSOliver Tappe	# pick up the 32-bit libraries for the bootloader
29811ae3c5fSOliver Tappe	bootLibgcc=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libgcc.a`
29911ae3c5fSOliver Tappe	$installDir/bin/$haikuMachine-strip --strip-debug $bootLibgcc
30011ae3c5fSOliver Tappe	bootLibsupcxx=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libsupc++.a`
30111ae3c5fSOliver Tappe	$installDir/bin/$haikuMachine-strip --strip-debug $bootLibsupcxx
30211ae3c5fSOliver Tappe	;;
30311ae3c5fSOliver Tappeesac
304c864ba1aSAlex Smith
30529ef597dSIngo Weinhold# cleanup
30629ef597dSIngo Weinhold
30729ef597dSIngo Weinhold# remove the system headers from the installation dir
30829ef597dSIngo Weinhold# Only the ones from the source tree should be used.
309467f4ab3SJeroen Oortwijnrm -rf "$installDir/$haikuMachine/sys-include"
31029ef597dSIngo Weinhold
311d511533aSAugustin Cavalier# rename the static libstdc++ to prevent accidental usage
312d511533aSAugustin Cavalier# (it should be used by the bootstrap process only)
313d511533aSAugustin Cavaliermv "$installDir/$haikuMachine/lib/libstdc++.a" \
314d511533aSAugustin Cavalier	"$installDir/$haikuMachine/lib/libstdc++-static.a"
3159bb9cc88SAugustin Cavalier
316f975dc63SIngo Weinhold# remove the sysroot dir
317467f4ab3SJeroen Oortwijnrm -rf "$sysrootDir"
318f975dc63SIngo Weinhold
31929ef597dSIngo Weinhold# remove the objects dir
320467f4ab3SJeroen Oortwijnrm -rf "$objDir"
32129ef597dSIngo Weinhold
32229ef597dSIngo Weinhold
32329ef597dSIngo Weinholdecho "binutils and gcc for cross compilation have been built successfully!"
324