xref: /haiku/build/scripts/build_haiku_image (revision 508f54795f39c3e7552d87c95aae9dd8ec6f505b)
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		createSymlinksForHybrid $extractedLibs $moreExtractedLibs
137	fi
138
139	$cp -r "${sPrefix}$extractDir/$extractedSubDir/." "${tPrefix}$targetExtractedDir"
140
141	$rmAttrs -rf "$extractDir"
142}
143
144
145createSymlinksForHybrid()
146{
147	# createSymlinksForHybrid <libraries>
148
149	# Determine the alternative gcc subdir.
150	if [ $gccVersion -eq 2 ]; then
151		gccAltDir=gcc4
152	else
153		gccAltDir=gcc2
154	fi
155
156	# Iterate over the library file paths that were passed in.
157	for srcPathLib in $@; do
158		# determine the relative path of the library
159		relPathLib=`echo $srcPathLib | \
160			sed -e "s:${extractDir}/${extractedSubDir}/::"`
161		relPath=`dirname $relPathLib`
162
163		# check if that relative path is one of the standard library paths
164		standardLibPaths="system/lib common/lib home/config/lib"
165		isStandardPath=
166		for stdLibPath in $standardLibPaths; do
167			if [ "$relPath" = "$stdLibPath" ]; then
168				isStandardPath=true
169				break
170			fi
171		done
172
173		# create alt gcc symlinks for libs inside standard paths
174		if [ $isStandardPath ]; then
175			srcPath=`dirname $srcPathLib`
176			srcLib=`basename $srcPathLib`
177			destLinkDir="${srcPath}/${gccAltDir}"
178			destLinkTarget="../$srcLib"
179
180			mkdir -p $destLinkDir
181			ln -sf $destLinkTarget $destLinkDir
182		fi
183	done
184}
185
186
187mkdir -p $tmpDir
188copyrightsFile=$tmpDir/copyrights
189$rmAttrs -f $copyrightsFile
190if [ "$optionalPackageDescriptions" ]; then
191	cp "$optionalPackageDescriptions" $copyrightsFile
192fi
193
194if [ $isCD ]; then
195	# setup output dir
196	$rmAttrs -rf "$outputDir"
197	mkdir -p "$outputDir"
198fi
199
200# create the image and mount it
201if [ $isImage ]; then
202	echo
203
204	imageOffsetFlags=
205	if [ $isVMwareImage ]; then
206		imageOffsetFlags="--start-offset 65536"
207	fi
208
209	if [ ! $updateOnly ]; then
210		echo "Creating image ..."
211
212		imageFlags="-i${imageSize}M"
213		if [ ! "$dontClearImage" ]; then
214			imageFlags="$imageFlags -c"
215		fi
216
217		if [ $isVMwareImage ]; then
218			$vmdkimage -h 64k $imageFlags "$imagePath"
219		else
220			$createImage $imageFlags "$imagePath"
221		fi
222
223		$bfsShell --initialize $imageOffsetFlags "$imagePath" \
224			"$imageLabel" "block_size 2048"
225		$makebootable $imageOffsetFlags "$imagePath"
226	fi
227	$bfsShell -n $imageOffsetFlags "$imagePath" > /dev/null &
228	sleep 1
229	# bail out, if mounting fails
230	$cd .
231fi
232
233echo "Populating image ..."
234while [ $# -gt 0 ]; do
235	. $1
236	shift
237done
238
239
240# install MIME database
241# TODO: It should be possible to do that in the build system too.
242
243if [ ! $updateOnly ]; then
244	mimeDBSource=$sourceDir/src/data/beos_mime
245	mimeDBDest=${tPrefix}home/config/settings/beos_mime
246
247	echo "Deleting old MIME database ..."
248
249	$rm -rf $mimeDBDest
250	$mkdir -p $mimeDBDest
251	mimeTmpDir=$tmpDir/mime
252	mimeDBTmpDir=$tmpDir/mime/db
253	mimeTmpIndex=0
254	mimeTmpFile=$mimeTmpDir/mimedb$$.rsrc
255
256	# create tmp dir for the MIME conversion stuff
257	mkdir -p $mimeDBTmpDir
258
259	echo "Installing MIME database ..."
260
261	for inSuperFile in $mimeDBSource/*.super; do
262		superType=$(basename $inSuperFile .super)
263		tmpSuperDir=$mimeDBTmpDir/$superType
264
265		# compile rdef to rsrc file and the rsrc file to attributes
266		$rc -o $mimeTmpFile $inSuperFile
267		mkdir -p $tmpSuperDir
268		$resattr -O -o $tmpSuperDir $mimeTmpFile
269		$rmAttrs $mimeTmpFile
270
271		# iterate through the sub types
272		for inSubFile in $mimeDBSource/$superType/*; do
273			# check, if the type exists
274			if test -f $inSubFile && grep META:TYPE $inSubFile > /dev/null 2>&1 ; then
275				subType=$(basename $inSubFile)
276				tmpSubFile=$mimeDBTmpDir/$superType/$subType
277
278				# compile rdef to rsrc file and the rsrc file to attributes
279				$rc -o $mimeTmpFile $inSubFile
280				$resattr -O -o $tmpSubFile $mimeTmpFile
281				$rmAttrs $mimeTmpFile
282			fi
283		done
284	done
285
286	$cp -r ${sPrefix}$mimeDBTmpDir/. $mimeDBDest
287
288	# cleanup tmp dir
289	$rmAttrs -rf $mimeTmpDir
290fi	# ! updateOnly
291
292
293# add the concatenated copyrights as an attribute to AboutSystem
294
295if [ ! $updateOnly ]; then
296	if [ -f $copyrightsFile ]; then
297		copyrightAttrs=$tmpDir/copyrightAttrs
298		$rmAttrs -f $copyrightAttrs
299		touch $copyrightAttrs
300		$addattr -f $copyrightsFile COPYRIGHTS $copyrightAttrs
301		$copyAttrs ${sPrefix}$copyrightAttrs ${tPrefix}system/apps/AboutSystem
302	fi
303fi
304
305if [ $isCD ]; then
306	# generate the attribute stores
307	echo "Generating attribute stores ..."
308	$generate_attribute_stores "$tPrefix"
309
310	echo "Copying boot image ..."
311	$cp "$cdBootFloppy" "$outputDir"
312
313	# build the iso image
314	echo "Building CD image ..."
315	mkisofs -uid 0 -gid 0 -b `basename $cdBootFloppy` -R -V "$cdLabel" -o "$cdImagePath" "$tPrefix"
316
317	# cleanup output dir
318	$rmAttrs -rf "$outputDir"
319fi
320
321# unmount
322if [ $isImage ]; then
323	echo "Unmounting ..."
324	$fsShellCommand sync
325	$fsShellCommand quit
326fi
327