xref: /haiku/build/scripts/build_haiku_image (revision a381c8a06378de22ff08adf4282b4e3f7e50d250)
1#!/bin/sh
2
3# The first argument is the shell script that initializes the variables:
4# sourceDir
5# outputDir
6# tmpDir
7# installDir
8# isImage
9# imagePath
10# imageSize
11# addBuildCompatibilityLibDir
12# sourceDirsToCopy
13# headerDirsToCopy
14# updateOnly
15# dontClearImage
16# isVMwareImage
17#
18# bfsShell
19# copyattr
20# fsShellCommand
21# makebootable
22# resattr
23# rc
24# unzip
25# vmdkheader
26#
27if [ $# -gt 0 ]; then
28	. $1
29	shift
30fi
31
32# this adds the build library dir to LD_LIBRARY_PATH
33eval "$addBuildCompatibilityLibDir"
34
35# map the shell commands
36if [ $isImage ]; then
37	sPrefix=:
38	tPrefix=/myfs/
39	cd="$fsShellCommand cd"
40	scd="$fsShellCommand cd"
41	cp="$fsShellCommand cp"
42	copyAttrs="$fsShellCommand cp -a"
43	ln="$fsShellCommand ln"
44	mkdir="$fsShellCommand mkdir"
45	rm="$fsShellCommand rm"
46	mkindex="$fsShellCommand mkindex"
47else
48	sPrefix=
49	# TODO: This should come from the environment.
50	tPrefix="$installDir/"
51	cd=cd
52	scd=:
53	cp="$copyattr -d"
54	copyAttrs="$copyattr"
55	ln=ln
56	mkdir=mkdir
57	rm=rm
58	mkindex=mkindex
59fi
60
61
62# attribute-safe rm -rf
63# This makes sure there are no leftover attribute file before removing each file
64attrrmrf()
65{
66	test -e "$1" || return
67	if [ -d "$outputDir/attributes" ]; then
68		find "$1" -print0 | xargs -0 stat -c %i | awk "{ print \"$outputDir/attributes/\" \$1 }" | xargs rm -rf
69	fi
70	rm -rf "$1"
71}
72
73unzipFile()
74{
75	# unzipFile <archive> <directory>
76	zipFile=$1
77	targetUnzipDir=$2
78
79	echo "Unzipping $zipFile ..."
80
81	if [ $isImage ]; then
82		unzipDir=$tmpDir/unzip
83		attrrmrf "$unzipDir"
84		mkdir -p "$unzipDir"
85
86		$unzip -q -d "$unzipDir" "$zipFile"
87		$cp -r "${sPrefix}$unzipDir/." "${tPrefix}$targetUnzipDir"
88
89		attrrmrf "$unzipDir"
90	else
91		$unzip -q -o -d "${tPrefix}$targetUnzipDir" "${sPrefix}$zipFile"
92	fi
93}
94
95
96# create the image and mount it
97if [ $isImage ]; then
98	echo
99
100	imageOffsetFlags=
101	if [ $isVMwareImage ]; then
102		imageOffsetFlags="--start-offset 65536"
103	fi
104
105	if [ ! $updateOnly ]; then
106		echo "Creating image ..."
107
108		ddFlags=
109		if [ $isVMwareImage ]; then
110			rm -f $imagePath
111			$vmdkheader -h 64k -i${imageSize}M $imagePath || exit 1
112			ddFlags="conv=notrunc oflag=append"
113			dontClearImage=
114		fi
115
116		if [ ! -e $imagePath -o ! "$dontClearImage" ]; then
117			dd if=/dev/zero of=$imagePath bs=1048576 count=$imageSize $ddFlags
118		fi
119		$bfsShell --initialize $imageOffsetFlags $imagePath Haiku
120		$makebootable $imageOffsetFlags $imagePath
121	fi
122	$bfsShell -n $imageOffsetFlags $imagePath > /dev/null &
123	sleep 1
124fi
125
126# create BEOS:APP_SIG index -- needed to launch apps via signature
127if [ ! $updateOnly ]; then
128	$mkindex BEOS:APP_SIG
129fi
130
131echo "Populating image ..."
132while [ $# -gt 0 ]; do
133	. $1
134	shift
135done
136
137
138# install MIME database
139# TODO: It should be possible to do that in the build system too.
140
141if [ ! $updateOnly ]; then
142	mimeDBSource=$sourceDir/src/data/beos_mime
143	mimeDBDest=${tPrefix}home/config/settings/beos_mime
144
145	echo "Deleting old MIME database ..."
146
147	$rm -rf $mimeDBDest
148	$mkdir -p $mimeDBDest
149	mkdir -p $tmpDir
150	mimeTmpDir=$tmpDir/mime
151	mimeTmpIndex=0
152
153	# create tmp dir for the MIME conversion stuff
154	mkdir -p $mimeTmpDir
155
156	echo "Installing MIME database ..."
157
158	for inSuperFile in $mimeDBSource/*.super; do
159		superType=$(basename $inSuperFile .super)
160		outSuperDir=$mimeDBDest/$superType
161
162		# compile rdef to rsrc file and the rsrc file to attributes
163		mimeTmpIndex=$(($mimeTmpIndex + 1))
164		tmpFile=$mimeTmpDir/mimedb$$_${mimeTmpIndex}.rsrc
165		tmpFile2=$mimeTmpDir/mimedb$$_${mimeTmpIndex}.mime
166		$rc -o $tmpFile $inSuperFile
167		mkdir -p $tmpFile2
168		$resattr -O -o $tmpFile2 $tmpFile
169		$cp -r ${sPrefix}$tmpFile2 $outSuperDir
170
171		# iterate through the sub types
172		for inSubFile in $mimeDBSource/$superType/*; do
173			# check, if the type exists
174			if test -f $inSubFile && grep META:TYPE $inSubFile > /dev/null 2>&1 ; then
175				subType=$(basename $inSubFile)
176				outSubFile=$outSuperDir/$subType
177
178				# compile rdef to rsrc file and the rsrc file to attributes
179				mimeTmpIndex=$(($mimeTmpIndex + 1))
180				tmpFile=$mimeTmpDir/mimedb$$_${mimeTmpIndex}.rsrc
181				tmpFile2=$mimeTmpDir/mimedb$$_${mimeTmpIndex}.mime
182				$rc -o $tmpFile $inSubFile
183				$resattr -O -o $tmpFile2 $tmpFile
184				$cp ${sPrefix}$tmpFile2 $outSubFile
185			fi
186		done
187	done
188
189	# cleanup tmp dir
190	attrrmrf $mimeTmpDir
191fi	# ! updateOnly
192
193
194# install sources
195
196sourcesDest=${tPrefix}home/HaikuSources
197
198# create sources directory
199if [ -n "${sourceDirsToCopy}" ]; then
200	echo "Installing Haiku Sources ..."
201
202	$mkdir -p ${sourcesDest}
203fi
204
205# When building in the root of the source directory, sourceDir is "." and
206# the source directory are not prefixed, which breaks the sed expressions
207# used below. We work around, by adjusting the strings to match and insert.
208sourcePathPrefix=$sourceDir
209destPathPrefix=$sourcesDest
210if [ $sourcePathPrefix = "." ]; then
211	sourcePathPrefix=""
212	destPathPrefix="${sourcesDest}/"
213fi
214
215for sourcesDir in ${sourceDirsToCopy}; do
216	echo "  sources dir: ${sourcesDir}"
217
218	# create all subdirectories
219	subDirs=$(find ${sourcesDir} -type d | grep -v '.svn' |
220		sed -e "s@^${sourcePathPrefix}@${destPathPrefix}@")
221	$mkdir -p ${subDirs}
222
223	# get all files and copy each one individually
224	sourceFiles=$(find $sourcesDir -type f | grep -v '.svn')
225	for sourceFile in $sourceFiles; do
226		destSourceFile=$(echo $sourceFile |
227			sed -e "s@^${sourcePathPrefix}@${destPathPrefix}@")
228		$cp ${sPrefix}$sourceFile $destSourceFile
229	done
230done
231
232
233# install headers
234
235headersDest=${tPrefix}develop
236
237# create headers directory
238if [ -n "${headerDirsToCopy}" ]; then
239	echo "Installing Haiku Headers ..."
240
241	$mkdir -p ${headersDest}
242fi
243
244# See above (sourceDirs)
245sourcePathPrefix=$sourceDir
246destPathPrefix=$headersDest
247if [ $sourcePathPrefix = "." ]; then
248	sourcePathPrefix=""
249	destPathPrefix="${headersDest}/"
250fi
251
252for headersDir in ${headerDirsToCopy}; do
253	echo "  header dir: ${headersDir}"
254
255	# create all subdirectories
256	subDirs=$(find ${headersDir} -type d | grep -v '.svn' |
257		sed -e "s@^${sourcePathPrefix}@${destPathPrefix}@")
258	$mkdir -p ${subDirs}
259
260	# get all files and copy each one individually
261	headerFiles=$(find $headersDir -type f | grep -v '.svn')
262	for headerFile in $headerFiles; do
263		destHeaderFile=$(echo $headerFile |
264			sed -e "s@^${sourcePathPrefix}@${destPathPrefix}@")
265		$cp ${sPrefix}$headerFile $destHeaderFile
266	done
267done
268
269
270# unmount
271if [ $isImage ]; then
272	echo "Unmounting ..."
273	$fsShellCommand sync
274	$fsShellCommand quit
275fi
276