xref: /haiku/build/scripts/build_haiku_image (revision 675ffabd70492a962f8c0288a32208c22ce5de18)
1#!/bin/sh
2set -o errexit
3
4# The first argument is the shell script that initializes the variables:
5# sourceDir
6# outputDir
7# tmpDir
8# addBuildCompatibilityLibDir
9# gccVersion
10# isHybridBuild
11# The following are only for image types:
12# installDir
13# isImage
14# imagePath
15# imageSize
16# imageLabel
17# updateOnly
18# dontClearImage
19# isVMwareImage
20# optionalPackageDescriptions
21#
22# addattr
23# copyattr
24# rc
25# rmAttrs
26# unzip
27# The following are only for image types:
28# bfsShell
29# fsShellCommand
30# makebootable
31# resattr
32# vmdkimage
33# The following is only for cd types:
34# generate_attribute_stores
35# isCD
36#
37if [ $# -gt 0 ]; then
38	. $1
39	shift
40fi
41
42if [ ! $isCD ]; then
43	# If the haiku image path is a symlink resolve it now (makebootable needs the
44	# path of the actual device path under Linux).
45	normalizedImagePath=''
46	if readlink -f "$imagePath" > /dev/null 2>&1 ; then
47		normalizedImagePath=$(readlink -f "$imagePath")
48	elif realpath "$imagePath" > /dev/null 2>&1 ; then
49		normalizedImagePath=$(realpath "$imagePath")
50	elif greadlink -f "$imagePath" > /dev/null 2>&1 ; then
51		normalizedImagePath=$(greadlink -f "$imagePath")
52	fi
53	if [ -n "$normalizedImagePath" ]; then
54		imagePath="$normalizedImagePath"
55	fi
56fi
57
58# this adds the build library dir to LD_LIBRARY_PATH
59eval "$addBuildCompatibilityLibDir"
60
61# map the shell commands
62if [ $isCD ]; then
63	outputDir=$tmpDir/cdsource
64
65	sPrefix=
66	tPrefix="$outputDir/"
67	cd=cd
68	scd=:
69	cp="$copyattr -d"
70	copyAttrs="$copyattr"
71	ln=ln
72	mkdir=mkdir
73	rm=rm
74elif [ $isImage ]; then
75	sPrefix=:
76	tPrefix=/myfs/
77	cd="$fsShellCommand cd"
78	scd="$fsShellCommand cd"
79	cp="$fsShellCommand cp -f"
80	copyAttrs="$fsShellCommand cp -a"
81	ln="$fsShellCommand ln"
82	mkdir="$fsShellCommand mkdir"
83	rm="$fsShellCommand rm"
84	mkindex="$fsShellCommand mkindex"
85else
86	sPrefix=
87	# TODO: This should come from the environment.
88	tPrefix="$installDir/"
89	cd=cd
90	scd=:
91	cp="$copyattr -d"
92	copyAttrs="$copyattr"
93	ln=ln
94	mkdir=mkdir
95	rm=rm
96	mkindex=mkindex
97fi
98
99
100extractFile()
101{
102	# extractFile <archive> <directory>
103	archiveFile=$1
104	targetExtractedDir=$2
105	extractedSubDir=$3
106	isGCCAgnostic=$4
107
108	echo "Extracting $archiveFile ..."
109
110	extractDir=$tmpDir/extract
111	$rmAttrs -rf "$extractDir"
112	mkdir -p "$extractDir"
113
114	case "$archiveFile" in
115		*.zip)
116			$unzip -q -d "$extractDir" "$archiveFile"
117			;;
118		*.tgz|*.tar.gz)
119			tar -C "$extractDir" -xf "$archiveFile"
120			;;
121		*)
122			echo "Unhandled archive extension in build_haiku_image extractFile()"
123			exit 1
124			;;
125	esac
126
127	if [ -f $extractDir/.OptionalPackageDescription ]; then
128		cat $extractDir/.OptionalPackageDescription >> $copyrightsFile
129		echo >> $copyrightsFile
130		rm $extractDir/.OptionalPackageDescription
131	fi
132
133	if [ "$isGCCAgnostic" = "true" ] && [ $isHybridBuild ] ; then
134		extractedLibs=`find "$extractDir/$extractedSubDir/" -name "*.so"`
135		moreExtractedLibs=`find "$extractDir/$extractedSubDir/" -name "*.so.*"`
136		staticLibs=`find "$extractDir/$extractedSubDir/" -name "*.a"`
137		#NOTE: should we symlink libtool's *.la files?
138		#laLibs=`find "$extractDir/$extractedSubDir/" -name "*.la"`
139		createSymlinksForHybrid $extractedLibs $moreExtractedLibs $staticLibs
140	fi
141
142	$cp -r "${sPrefix}$extractDir/$extractedSubDir/." "${tPrefix}$targetExtractedDir"
143
144	$rmAttrs -rf "$extractDir"
145}
146
147
148createSymlinksForHybrid()
149{
150	# createSymlinksForHybrid <libraries>
151
152	# Determine the alternative gcc subdir.
153	if [ $gccVersion -eq 2 ]; then
154		gccAltDir=gcc4
155	else
156		gccAltDir=gcc2
157	fi
158
159	# Iterate over the library file paths that were passed in.
160	for srcPathLib in $@; do
161		# determine the relative path of the library
162		relPathLib=`echo $srcPathLib | \
163			sed -e "s:${extractDir}/${extractedSubDir}/::"`
164		relPath=`dirname $relPathLib`
165
166		# check if that relative path is one of the standard library paths
167		standardLibPaths="system/lib common/lib home/config/lib"
168		isStandardPath=
169		for stdLibPath in $standardLibPaths; do
170			if [ "$relPath" = "$stdLibPath" ]; then
171				isStandardPath=true
172				break
173			fi
174		done
175
176		# create alt gcc symlinks for libs inside standard paths
177		if [ $isStandardPath ]; then
178			srcPath=`dirname $srcPathLib`
179			srcLib=`basename $srcPathLib`
180			destLinkDir="${srcPath}/${gccAltDir}"
181			destLinkTarget="../$srcLib"
182
183			mkdir -p $destLinkDir
184			ln -sf $destLinkTarget $destLinkDir
185		fi
186	done
187}
188
189
190mkdir -p $tmpDir
191copyrightsFile=$tmpDir/copyrights
192$rmAttrs -f $copyrightsFile
193if [ "$optionalPackageDescriptions" ]; then
194	cp "$optionalPackageDescriptions" $copyrightsFile
195fi
196
197if [ $isCD ]; then
198	# setup output dir
199	$rmAttrs -rf "$outputDir"
200	mkdir -p "$outputDir"
201fi
202
203# create the image and mount it
204if [ $isImage ]; then
205	echo
206
207	imageOffsetFlags=
208	if [ $isVMwareImage ]; then
209		imageOffsetFlags="--start-offset 65536"
210	fi
211
212	if [ ! $updateOnly ]; then
213		echo "Creating image ..."
214
215		imageFlags="-i${imageSize}M"
216		if [ ! "$dontClearImage" ]; then
217			imageFlags="$imageFlags -c"
218		fi
219
220		if [ $isVMwareImage ]; then
221			$vmdkimage -h 64k $imageFlags "$imagePath"
222		else
223			$createImage $imageFlags "$imagePath"
224		fi
225
226		$bfsShell --initialize $imageOffsetFlags "$imagePath" \
227			"$imageLabel" "block_size 2048"
228		$makebootable $imageOffsetFlags "$imagePath"
229	fi
230	$bfsShell -n $imageOffsetFlags "$imagePath" > /dev/null &
231	sleep 1
232	# bail out, if mounting fails
233	$cd .
234fi
235
236echo "Populating image ..."
237while [ $# -gt 0 ]; do
238	. $1
239	shift
240done
241
242
243# install MIME database
244# TODO: It should be possible to do that in the build system too.
245
246if [ ! $updateOnly ]; then
247	mimeDBSource=$sourceDir/src/data/beos_mime
248	mimeDBDest=${tPrefix}home/config/settings/beos_mime
249
250	echo "Deleting old MIME database ..."
251
252	$rm -rf $mimeDBDest
253	$mkdir -p $mimeDBDest
254	mimeTmpDir=$tmpDir/mime
255	mimeDBTmpDir=$tmpDir/mime/db
256	mimeTmpIndex=0
257	mimeTmpFile=$mimeTmpDir/mimedb$$.rsrc
258
259	# create tmp dir for the MIME conversion stuff
260	mkdir -p $mimeDBTmpDir
261
262	echo "Installing MIME database ..."
263
264	for inSuperFile in $mimeDBSource/*.super; do
265		superType=$(basename $inSuperFile .super)
266		tmpSuperDir=$mimeDBTmpDir/$superType
267
268		# compile rdef to rsrc file and the rsrc file to attributes
269		$rc -o $mimeTmpFile $inSuperFile
270		mkdir -p $tmpSuperDir
271		$resattr -O -o $tmpSuperDir $mimeTmpFile
272		$rmAttrs $mimeTmpFile
273
274		# iterate through the sub types
275		for inSubFile in $mimeDBSource/$superType/*; do
276			# check, if the type exists
277			if test -f $inSubFile && grep META:TYPE $inSubFile > /dev/null 2>&1 ; then
278				subType=$(basename $inSubFile)
279				tmpSubFile=$mimeDBTmpDir/$superType/$subType
280
281				# compile rdef to rsrc file and the rsrc file to attributes
282				$rc -o $mimeTmpFile $inSubFile
283				$resattr -O -o $tmpSubFile $mimeTmpFile
284				$rmAttrs $mimeTmpFile
285			fi
286		done
287	done
288
289	$cp -r ${sPrefix}$mimeDBTmpDir/. $mimeDBDest
290
291	# cleanup tmp dir
292	$rmAttrs -rf $mimeTmpDir
293fi	# ! updateOnly
294
295
296# add the concatenated copyrights as an attribute to AboutSystem
297
298if [ ! $updateOnly ]; then
299	if [ -f $copyrightsFile ]; then
300		copyrightAttrs=$tmpDir/copyrightAttrs
301		$rmAttrs -f $copyrightAttrs
302		touch $copyrightAttrs
303		$addattr -f $copyrightsFile COPYRIGHTS $copyrightAttrs
304		$copyAttrs ${sPrefix}$copyrightAttrs ${tPrefix}system/apps/AboutSystem
305	fi
306fi
307
308if [ $isCD ]; then
309	# generate the attribute stores
310	echo "Generating attribute stores ..."
311	$generate_attribute_stores "$tPrefix"
312
313	echo "Copying boot image ..."
314	$cp "$cdBootFloppy" "$outputDir"
315
316	# build the iso image
317	echo "Building CD image ..."
318	mkisofs -uid 0 -gid 0 -b `basename $cdBootFloppy` -R -V "$cdLabel" -o "$cdImagePath" "$tPrefix"
319
320	# cleanup output dir
321	$rmAttrs -rf "$outputDir"
322fi
323
324# unmount
325if [ $isImage ]; then
326	echo "Unmounting ..."
327	$fsShellCommand sync
328	$fsShellCommand quit
329fi
330