1#!/bin/sh
2#
3# Builds an GCC (optional) package from the sources.
4
5usage()
6{
7	echo "Usage: $0 [ <options> ] <gcc date>"
8}
9
10
11# get the relevant directories
12currentDir=`pwd`
13cd `dirname "$0"`
14buildtoolsDir=`pwd`
15cd "$currentDir"
16
17binutilsSources="$buildtoolsDir/binutils"
18gccSources="$buildtoolsDir/gcc"
19
20buildDir="$currentDir/gcc-objects"
21binutilsBuildDir="$buildDir/binutils"
22gccBuildDir="$buildDir/gcc"
23targetArch=x86
24gccMakeTarget=bootstrap
25gccConfigureArgs="--enable-shared"
26binutilsConfigureArgs="--enable-shared"
27
28# parse the arguments
29jobArgs=
30while [ $# -gt 0 ]; do
31	case "$1" in
32		-h|--help)	usage; exit 0;;
33		-j*)		jobArgs="$1"; shift 1;;
34		--arch)
35			case "$2" in
36				x86)	HAIKU_GCC_MACHINE=i586-pc-haiku;;
37				x86_64)	HAIKU_GCC_MACHINE=x86_64-unknown-haiku; targetArch=x86_64;;
38				ppc)	HAIKU_GCC_MACHINE=powerpc-apple-haiku; targetArch=ppc;
39						gccMakeTarget= gccConfigureArgs="--disable-shared --disable-multilib"
40						binutilsConfigureArgs="--disable-shared --disable-multilib" ;;
41				m68k)	HAIKU_GCC_MACHINE=m68k-unknown-haiku; targetArch=m68k;
42						gccMakeTarget= gccConfigureArgs="--disable-shared --disable-multilib"
43						binutilsConfigureArgs="--disable-shared --disable-multilib" ;;
44				arm)	HAIKU_GCC_MACHINE=arm-unknown-haiku; targetArch=arm;
45						gccMakeTarget= gccConfigureArgs="--disable-shared --disable-multilib"
46						binutilsConfigureArgs="--disable-shared --disable-multilib" ;;
47				mipsel)	HAIKU_GCC_MACHINE=mipsel-unknown-haiku; targetArch=mips;
48						gccMakeTarget= gccConfigureArgs="--disable-shared --disable-multilib"
49						binutilsConfigureArgs="--disable-shared --disable-multilib" ;;
50				*)		echo "Unsupported target architecture: $2"
51						exit 1;;
52			esac
53			shift 2
54			targetCrossToolsMachine="--target=${HAIKU_GCC_MACHINE}";;
55		--source-dir)	haikuSourceDir="$2"; shift 2;;
56		*)			break;;
57	esac
58done
59
60# get the GCC date
61if [ $# -ne 1 ]; then
62	usage >&2
63	exit 1
64fi
65
66gccDate=$1
67
68case "$gccDate" in
69	[0-9][0-9][0-9][0-9][0-9][0-9])	true;;
70	*)	echo "Invalid GCC date string '$gccDate'." >&2; exit 1;;
71esac
72
73# get the GCC version
74gccVersion=`cat $gccSources/gcc/BASE-VER`
75if [ -z "$gccVersion" ]; then
76	echo "Failed to get GCC version." >&2
77	exit 1
78fi
79
80gccVersionedName=gcc-${gccVersion}-haiku-${gccDate}
81
82
83# check whether the installation dir does already exit
84installDir=/boot/develop/abi/$targetArch/gcc4/tools/$gccVersionedName
85if [ -e "$installDir" ]; then
86	echo "The installation directory '$installDir' does already exist." >&2
87	echo "Remove it first." >&2
88	exit 1
89fi
90
91
92# print some info before we start the action
93echo "Building binutils and gcc optional packages from the source."
94echo "sources:     $buildtoolsDir"
95echo "build dir:   $buildDir"
96echo "target arch: $targetArch"
97echo "GCC date:    $gccDate"
98echo "GCC version: $gccVersion"
99echo "install dir: $installDir"
100echo
101echo "This is going to take a while ..."
102sleep 3
103
104# From now on fail, if anything goes wrong.
105set -o errexit
106
107
108# forcefeed the POSIX locale, as the build (makeinfo) might choke otherwise
109export LC_ALL=POSIX
110
111# remove and recreate the build directories
112rm -rf "$buildDir"
113mkdir -p "$binutilsBuildDir" "$gccBuildDir"
114
115
116# build and install the binutils
117cd "$binutilsBuildDir"
118CFLAGS="-O2" CXXFLAGS="-O2" "$binutilsSources/configure" \
119	--prefix="$installDir" $targetCrossToolsMachine --disable-nls \
120	$binutilsConfigureArgs --with-htmldir=html-docs
121make $jobArgs
122make install install-html
123
124# prepare the include files
125copy_headers()
126{
127	sourceDir=$1
128	targetDir=$2
129
130	headers="$(find $sourceDir -name \*\.h | grep -v /.svn)"
131	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
132	for f in $headers; do
133		headerTargetDir=$targetDir/$(dirname $f)
134		mkdir -p $headerTargetDir
135		cp $sourceDir/$f $headerTargetDir
136	done
137}
138
139if [ -n "$haikuSourceDir" ]; then
140	tmpIncludeDir=$currentDir/sysincludes
141	tmpLibDir=$currentDir/syslibs
142	mkdir -p $tmpIncludeDir $tmpLibDir
143	copy_headers $haikuSourceDir/headers/config $tmpIncludeDir/config
144	copy_headers $haikuSourceDir/headers/os $tmpIncludeDir/os
145	copy_headers $haikuSourceDir/headers/posix $tmpIncludeDir/posix
146	headersLibsArgs="--with-headers=$tmpIncludeDir --with-libs=$tmpLibDir"
147fi
148
149# build and install gcc
150cd "$gccBuildDir"
151CFLAGS="-O2" CXXFLAGS="-O2" "$gccSources/configure" \
152	--prefix="$installDir" $gccConfigureArgs --enable-languages=c,c++ \
153	$targetCrossToolsMachine --disable-nls --without-libiconv-prefix \
154	--disable-libstdcxx-pch --with-htmldir=html-docs --enable-lto \
155	--enable-frame-pointer $headersLibsArgs
156make $jobArgs $gccMakeTarget
157make install-strip install-html
158
159
160# remove installed stuff we don't want
161rm -rf "$installDir/info" "$installDir/man" "$installDir/share" \
162	"$installDir/lib/libstdc++.so"
163
164
165# strip the executables of debug info (somewhat crude heuristics to identify
166# actual executables: files >= 20 kiB with execute permission and not in a "lib"
167# directory)
168strip --strip-debug \
169	`find "$installDir" -type f -a -perm -u=x -a -size +20k | grep -v /lib/`
170
171
172# add C++ header symlink
173ln -s c++/$gccVersion $installDir/include/g++
174
175
176# zip everything up
177gccVersionYear=20$(echo $gccDate | cut -c1-2)
178gccVersionMonth=$(echo $gccDate | cut -c3-4)
179gccVersionDay=$(echo $gccDate | cut -c5-6)
180packageFile="$currentDir/gcc-${gccVersion}-${targetArch}-gcc4-${gccVersionYear}-${gccVersionMonth}-${gccVersionDay}.zip"
181
182cd /boot
183zip -ry "$packageFile" `echo $installDir | cut -d/ -f3-`
184
185
186# add the "current" version symlink
187cd "$buildDir"
188mkdir -p develop/abi/$targetArch/gcc4/tools/
189ln -s $gccVersionedName develop/abi/$targetArch/gcc4/tools/current
190zip -y "$packageFile" develop/abi/$targetArch/gcc4/tools/current
191
192
193# add the optional package description
194cd "$buildDir"
195echo "Package:		GCC
196Version:		${gccVersion}-${targetArch}-haiku-${gccDate}
197Copyright:		1988-2012 Free Software Foundation, Inc.
198License:		GNU GPL v3
199License:		GNU LGPL v3
200URL:			http://www.gnu.org/software/gcc/
201" > .OptionalPackageDescription
202
203zip "$packageFile" .OptionalPackageDescription
204
205
206# clean up
207cd "$currentDir"
208rm -rf "$buildDir"
209