ImageRules revision 85eed25cddc53c6622ebc26ddb80806c6b0302c3
1rule FSameTargetWithPrependedGrist
2{
3	# SameTargetWithPrependedGrist <target> : <grist to prepend> ;
4	#
5	local target = $(1) ;
6	local gristToPrepend = $(2) ;
7	local grist = $(target:G) ;
8
9	if $(grist) {
10		grist = $(gristToPrepend)!$(grist) ;
11	} else {
12		grist = $(gristToPrepend) ;
13	}
14
15	return $(target:G=$(grist)) ;
16}
17
18rule InitScript
19{
20	# Note: The script must have been LOCATEd before.
21	local script = $(1) ;
22	local initScript
23		= [ FSameTargetWithPrependedGrist $(script) : init-script ] ;
24
25	if ! [ on $(script) return $(__is_initialized) ] {
26		__is_initialized on $(script) = true ;
27
28		MakeLocate $(initScript) : [ on $(script) return $(LOCATE) ] ;
29		Always $(initScript) ;
30		Depends $(script) : $(initScript) ;
31
32		InitScript1 $(initScript) ;
33	}
34
35	return $(initScript) ;
36}
37
38actions InitScript1
39{
40	$(RM) $(1)
41	touch $(1)
42}
43
44rule AddVariableToScript script : variable : value
45{
46	# AddVariableToScript <script> : <variable> : <value> ;
47
48	# interpret an empty variable value as empty string
49	if ! $(value) {
50		value = "" ;
51	}
52
53	InitScript $(script) ;
54
55	VARIABLE_DEFS on $(script) += "echo $(variable)=\\\"$(value[1])\\\" >> " ;
56
57	# if the value is an array, add the other array elements
58	value = $(value[2-]) ;
59	while $(value) {
60		VARIABLE_DEFS on $(script)
61			+= "echo $(variable)=\\\" \\\$$(variable) $(value[1])\\\" >> " ;
62		value = $(value[2-]) ;
63	}
64
65	AddVariableToScript1 $(script) ;
66}
67
68actions together AddVariableToScript1
69{
70	$(VARIABLE_DEFS)$(1);
71}
72
73
74rule AddTargetVariableToScript script : targets : variable
75{
76	# AddTargetVariableToScript <script> : <targets> [ : <variable> ] ;
77	#
78	# If <targets> contains multiple targets, their paths must not contain
79	# whitespaces or other characters that need to be escaped in the shell.
80	#
81	variable ?= $(3:E=$(targets[1]:BS)) ;
82
83	local initScript = [ InitScript $(script) ] ;
84
85	serialization = [ on $(script) return $(HAIKU_SERIALIZATION) ] ;
86
87	local variableTarget = [ NewUniqueTarget ] ;
88	NotFile $(variableTarget) ;
89	Depends $(variableTarget) : $(initScript) $(targets) $(serialization) ;
90	Depends $(script) : $(variableTarget) ;
91
92	HAIKU_SERIALIZATION on $(script) = $(variableTarget) ;
93
94	HAIKU_VARIABLE_NAME on $(variableTarget) = $(variable) ;
95	AddTargetVariableToScript1 $(variableTarget) : $(initScript) $(targets) ;
96}
97
98
99actions AddTargetVariableToScript1
100{
101	script="$(2[1])"
102	echo "$(HAIKU_VARIABLE_NAME)=" >> "$script"
103
104	firstSeen=
105	for value in "$(2[2-])" ; do
106		if [ -z "$firstSeen" ]; then
107			echo "$(HAIKU_VARIABLE_NAME)=\"$value\"" >> "$script"
108			firstSeen=1
109		else
110			echo "$(HAIKU_VARIABLE_NAME)=\"\$$(HAIKU_VARIABLE_NAME) $value\"" \
111				>> "$script"
112		fi
113	done
114}
115
116
117#pragma mark -
118
119rule AddDirectoryToContainer container : directoryTokens : attributeFiles
120{
121	# AddDirectoryToContainer <container> : <directoryTokens> : <attributeFiles>
122
123	local containerGrist = [ on $(container) return $(HAIKU_CONTAINER_GRIST) ] ;
124	local directory = [ FDirName $(directoryTokens) ] ;
125	directory = $(directory:G=$(containerGrist)) ;
126
127	if ! [ on $(directory) return $(__is_on_image) ] {
128		HAIKU_INSTALL_DIRECTORIES on $(container) += $(directory) ;
129		__is_on_image on $(directory) = true ;
130		DIRECTORY_TOKENS on $(directory) = $(directoryTokens) ;
131		NotFile $(directory) ;
132
133		# mark the parent dir as not to be created
134		local parent = [ FReverse $(directoryTokens) ] ;
135		parent = [ FReverse $(parent[2-]) ] ;
136		if $(parent) {
137			parent = [ FDirName $(parent) ] ;
138			parent = $(parent:G=$(containerGrist)) ;
139			DONT_CREATE on $(parent) = true ;
140		}
141	}
142
143	if $(attributeFiles) {
144		SEARCH on $(attributeFiles)
145			+= [ FDirName $(HAIKU_TOP) data image_directories ] ;
146		ATTRIBUTE_FILES on $(dir) += $(attributeFiles) ;
147	}
148
149	return $(directory) ;
150}
151
152rule FilterContainerUpdateTargets targets : filterVariable
153{
154	# FilterContainerUpdateTargets targets : filterVariable
155
156	local filteredTargets ;
157	local target ;
158	for target in $(targets) {
159		if [ on $(target) return $($(filterVariable)) ] {
160			filteredTargets += $(target) ;
161		}
162	}
163	return $(filteredTargets) ;
164}
165
166
167rule IncludeAllTargetsInContainer container
168{
169	local filterVar
170		= [ on $(container) return $(HAIKU_INCLUDE_IN_CONTAINER_VAR) ] ;
171	if $(filterVar) {
172		return $($(filterVar)) ;
173	}
174
175	return ;
176}
177
178
179rule PropagateContainerUpdateTargetFlags toTarget : fromTarget
180{
181	if [ on $(fromTarget) return $(HAIKU_INCLUDE_IN_IMAGE) ] {
182		HAIKU_INCLUDE_IN_IMAGE on $(toTarget) = 1 ;
183	}
184
185	if [ on $(fromTarget) return $(HAIKU_INCLUDE_IN_PACKAGES) ] {
186		HAIKU_INCLUDE_IN_PACKAGES on $(toTarget) = 1 ;
187	}
188}
189
190
191rule AddFilesToContainer container : directoryTokens : targets : destName
192{
193	# AddFilesToContainer <container> : <directoryTokens> : <targets>
194	#	[ : dest name ]
195	#
196	local containerGrist = [ on $(container) return $(HAIKU_CONTAINER_GRIST) ] ;
197	local systemDirTokens
198		= [ on $(container) return $(HAIKU_CONTAINER_SYSTEM_DIR_TOKENS) ] ;
199
200	targets = [ FFilterByBuildFeatures $(targets) ] ;
201
202	# If the image shall only be updated, we filter out all targets not marked
203	# accordingly.
204	if [ on $(container) return $(HAIKU_CONTAINER_UPDATE_ONLY) ]
205		&& ! [ IncludeAllTargetsInContainer $(container) ] {
206		local filterVar
207			= [ on $(container) return $(HAIKU_INCLUDE_IN_CONTAINER_VAR) ] ;
208		if $(filterVar) {
209			targets = [ FilterContainerUpdateTargets $(targets)
210				: $(filterVar) ] ;
211
212			# If there are any targets, mark the container as to be included in
213			# an update, too, if it has set the update inheritance variable.
214			# This makes updating a target that lives in a package on an image
215			# work.
216			if $(targets) {
217				local updateVariable = [ on $(container) return
218					$(HAIKU_CONTAINER_INHERIT_UPDATE_VARIABLE) ] ;
219				if $(updateVariable) {
220					$(updateVariable) on $(container) = 1 ;
221				}
222			}
223		}
224	}
225
226	if ! $(targets) {
227		return ;
228	}
229
230	local directory = [ AddDirectoryToContainer $(container)
231		: $(directoryTokens) ] ;
232
233	# We create a unique dummy target per target to install.
234	local installTargetsVar
235		= [ on $(container) return $(HAIKU_INSTALL_TARGETS_VAR) ] ;
236	local target ;
237	for target in $(targets) {
238		local name ;
239		if $(destName) {
240			name = $(destName) ;
241		} else {
242			name = $(target:G=:D=) ;
243		}
244
245		local destTarget = $(name:G=$(containerGrist)__$(directory:G=)) ;
246		TARGET on $(destTarget) = $(target) ;
247		INSTALL_DIR on $(destTarget) = $(directory) ;
248		$(installTargetsVar) on $(target) += $(destTarget) ;
249		TARGETS_TO_INSTALL on $(directory) += $(destTarget) ;
250
251		# If the target is associated with catalog files, add those, too.
252		local catalogs = [ on $(target) return $(HAIKU_CATALOG_FILES) ] ;
253		if $(catalogs) {
254			local signature
255				= [ on $(target) return $(HAIKU_CATALOG_SIGNATURE) ] ;
256			AddFilesToContainer $(container)
257				: $(systemDirTokens) data locale catalogs $(signature)
258				: $(catalogs) ;
259		}
260
261		# If the target is associated with MIME DB entries, add those, too.
262		local mimeDBEntries = [ on $(target) return $(HAIKU_MIME_DB_ENTRIES) ] ;
263		if $(mimeDBEntries) {
264			# Make sure we add the entries only once by tracking the containers
265			# we have already added it to.
266			local containers = [ on $(mimeDBEntries)
267				return $(HAIKU_MIME_DB_ENTRIES_IN_CONTAINERS) ] ;
268			if ! $(container) in $(containers) {
269				HAIKU_MIME_DB_ENTRIES_IN_CONTAINERS on $(mimeDBEntries)
270					= $(containers) $(container) ;
271	 			CopyDirectoryToContainer $(container) : data
272 					: $(mimeDBEntries) : mime_db : : alwaysUpdate isTarget ;
273 			}
274		}
275	}
276}
277
278rule FFilesInContainerDirectory container : directoryTokens
279{
280	local containerGrist = [ on $(container) return $(HAIKU_CONTAINER_GRIST) ] ;
281	local directory = [ FDirName $(directoryTokens) ] ;
282	directory = $(directory:G=$(containerGrist)) ;
283
284	if [ on $(directory) return $(__is_on_image) ] {
285		on $(directory) return $(TARGETS_TO_INSTALL) ;
286	}
287
288	return ;
289}
290
291rule AddSymlinkToContainer container : directoryTokens : linkTarget : linkName
292{
293	# AddSymlinkToContainer <container> : <directory> : <link target>
294	#	[ : <link name> ] ;
295	#
296
297	# If the image shall only be updated, we don't add any symlinks.
298	if [ on $(container) return $(HAIKU_CONTAINER_UPDATE_ONLY) ]
299		&& ! [ IncludeAllTargetsInContainer $(container) ] {
300		return ;
301	}
302
303	local directory = [ AddDirectoryToContainer $(container)
304		: $(directoryTokens) ] ;
305
306	if ! $(linkName) {
307		local path = [ FReverse [ FSplitPath $(linkTarget) ] ] ;
308		linkName = $(path[1]) ;
309	}
310
311	local link = $(directory)/$(linkName) ;
312	SYMLINK_TARGET on $(link) = $(linkTarget) ;
313	SYMLINKS_TO_INSTALL on $(directory) += $(link) ;
314}
315
316rule FSymlinksInContainerDirectory container : directoryTokens
317{
318	local containerGrist = [ on $(container) return $(HAIKU_CONTAINER_GRIST) ] ;
319	local directory = [ FDirName $(directoryTokens) ] ;
320	directory = $(directory:G=$(containerGrist)) ;
321
322	if [ on $(directory) return $(__is_on_image) ] {
323		on $(directory) return $(SYMLINKS_TO_INSTALL) ;
324	}
325
326	return ;
327}
328
329rule CopyDirectoryToContainer container : directoryTokens : sourceDirectory
330	: targetDirectoryName : excludePatterns : flags
331{
332	# CopyDirectoryToContainer <container> : <directoryTokens>
333	#	: <sourceDirectory> : <targetDirectoryName> : <excludePatterns>
334	#	[ : <flags> ] ;
335	#
336	# Supported flags: alwaysUpdate, isTarget
337	# isTarget: <sourceDirectory> is a target, not a path
338
339	# If the image shall only be updated, we don't copy any directories
340	if [ on $(container) return $(HAIKU_CONTAINER_UPDATE_ONLY) ]
341			&& ! [ IncludeAllTargetsInContainer $(container) ]
342			&& ! alwaysUpdate in $(flags) {
343		return ;
344	}
345
346	if ! $(targetDirectoryName) {
347		targetDirectoryName = $(sourceDirectory[1]:BS) ;
348	}
349
350	# If sourceDirectory is a path, not a target, make it a target, so we can
351	# treat both the same way.
352	if ! isTarget in $(flags) {
353		sourceDirectory = $(sourceDirectory:G=copy-directory-to-container) ;
354		SEARCH on $(sourceDirectory) = ;
355		TARGET on $(sourceDirectory) = ;
356	}
357
358	local directory = [ AddDirectoryToContainer $(container)
359		: $(directoryTokens) $(targetDirectoryName) ] ;
360
361	local targetDir = $(directory)/-/$(sourceDirectory) ;
362	Depends $(targetDir) : $(sourceDirectory) ;
363	EXCLUDE_PATTERNS on $(targetDir) = $(excludePatterns) ;
364	SOURCE_DIRECTORY on $(targetDir) = $(sourceDirectory) ;
365	TARGET_DIRECTORY on $(targetDir) = $(directory) ;
366	DIRECTORIES_TO_INSTALL on $(directory) += $(targetDir) ;
367}
368
369
370rule AddHeaderDirectoryToContainer container : dirTokens : dirName
371	: flags
372{
373	# AddHeaderDirectoryToContainer <container> : <dirTokens> : [ <dirName> ]
374	#	[ : <flags> ] ;
375	#
376	# Supported flags: alwaysUpdate
377
378	local systemDirTokens
379		= [ on $(container) return $(HAIKU_CONTAINER_SYSTEM_DIR_TOKENS) ] ;
380
381	CopyDirectoryToContainer $(container) : $(systemDirTokens) develop headers
382		: [ FDirName $(HAIKU_TOP) headers $(dirTokens) ]
383		: $(dirName) : -x .svn : $(flags) ;
384}
385
386
387rule AddWifiFirmwareToContainer container : driver : package : archive : extract
388{
389	# AddWifiFirmwareToContainer <container> : <driver> : <package> : <archive>
390	#	: <extract>
391
392	# complete location to wifi firmware archive
393	local firmwareArchive = [ FDirName
394		$(HAIKU_TOP) data system data firmware $(driver) $(archive) ] ;
395
396	local systemDirTokens
397		= [ on $(container) return $(HAIKU_CONTAINER_SYSTEM_DIR_TOKENS) ] ;
398
399	local dirTokens = $(systemDirTokens) data firmware $(driver) ;
400	if $(extract) = true || $(extract) = 1 {
401		ExtractArchiveToContainer $(container) : $(dirTokens)
402			: $(firmwareArchive) : : $(package) ;
403	} else {
404		AddFilesToContainer $(container) : $(dirTokens) : $(firmwareArchive)  ;
405	}
406}
407
408
409rule ExtractArchiveToContainer container : directoryTokens : archiveFile
410	: flags : extractedSubDir
411{
412	# ExtractArchiveToContainer <container> : <directory> : <archiveFile>
413	#	: [ <flags> ] : <extractedSubDir> ;
414	#
415	# Supported flags: alwaysUpdate
416
417	# If the container shall only be updated, we extract only, if explicitely
418	# requested.
419	if [ on $(container) return $(HAIKU_CONTAINER_UPDATE_ONLY) ]
420		&& ! alwaysUpdate in $(flags) {
421		return ;
422	}
423
424	local directory = [ AddDirectoryToContainer $(container)
425		: $(directoryTokens) ] ;
426
427	ARCHIVE_FILES_TO_INSTALL on $(directory) += $(archiveFile) ;
428	ARCHIVE_SUBDIR_TO_INSTALL_FROM on $(archiveFile) = $(extractedSubDir) ;
429}
430
431rule AddDriversToContainer container : relativeDirectoryTokens : targets
432{
433	# AddDriversToContainer <container> : <relative directory> : <targets> ;
434	#
435	local systemDirTokens
436		= [ on $(container) return $(HAIKU_CONTAINER_SYSTEM_DIR_TOKENS) ] ;
437	local directoryTokens = $(systemDirTokens) add-ons kernel drivers dev
438		$(relativeDirectoryTokens) ;
439
440	targets = [ FFilterByBuildFeatures $(targets) ] ;
441
442	# A driver can be in multiple categories. Avoid adding it to the bin/
443	# directory more than once.
444	local binTargets ;
445	local target ;
446	for target in $(targets) {
447		local containers
448			= [ on $(target) return $(HAIKU_DRIVER_IN_CONTAINERS) ] ;
449		if ! $(container) in $(containers) {
450			HAIKU_DRIVER_IN_CONTAINERS on $(target)
451				= $(containers) $(container) ;
452			binTargets += $(target) ;
453		}
454	}
455
456	AddFilesToContainer $(container)
457		: $(systemDirTokens) add-ons kernel drivers bin
458		: $(binTargets) ;
459
460	# If the image shall only be updated, we don't add any symlinks.
461	if [ on $(container) return $(HAIKU_CONTAINER_UPDATE_ONLY) ]
462		&& ! [ IncludeAllTargetsInContainer $(container) ] {
463		return ;
464	}
465
466	# get the relative symlink path prefix
467	local linkPrefix = ;
468	for i in $(relativeDirectoryTokens) {
469		linkPrefix += .. ;
470	}
471	linkPrefix += .. bin ;
472
473	# add the symlinks
474	local name ;
475	for name in $(targets:BS) {
476		AddSymlinkToContainer $(container) : $(directoryTokens)
477			: [ FDirName $(linkPrefix) $(name) ] : $(name) ;
478	}
479}
480
481rule AddNewDriversToContainer container : relativeDirectoryTokens
482	: targets
483{
484	# AddNewDriversToContainer <container> : <directory> : <targets> ;
485	#
486	local systemDirTokens
487		= [ on $(container) return $(HAIKU_CONTAINER_SYSTEM_DIR_TOKENS) ] ;
488	local directoryTokens = $(systemDirTokens) add-ons kernel drivers
489		$(relativeDirectoryTokens) ;
490
491	targets = [ FFilterByBuildFeatures $(targets) ] ;
492
493	AddFilesToContainer $(container) : $(directoryTokens)
494		: $(targets) ;
495}
496
497rule AddBootModuleSymlinksToContainer container : targets
498{
499	# AddBootModuleSymlinksToContainer <container> : <targets> ;
500	#
501
502	# If the container shall only be updated, we don't add any symlinks.
503
504	if [ on $(container) return $(HAIKU_CONTAINER_UPDATE_ONLY) ]
505		&& ! [ IncludeAllTargetsInContainer $(container) ] {
506		return ;
507	}
508
509	local systemDirTokens
510		= [ on $(container) return $(HAIKU_CONTAINER_SYSTEM_DIR_TOKENS) ] ;
511	targets = [ FFilterByBuildFeatures $(targets) ] ;
512
513	# add the symlinks
514	local installTargetsVar
515		= [ on $(container) return $(HAIKU_INSTALL_TARGETS_VAR) ] ;
516	local target ;
517	for target in $(targets) {
518		# Symlink to the first place where the target has been installed.
519		local destTarget = [ on $(target) return $($(installTargetsVar)[1]) ] ;
520		local installDir = [ on $(destTarget) return $(INSTALL_DIR) ] ;
521
522		if ! $(installDir) {
523			Echo "ERROR: AddBootModuleSymlinksToContainer: Can't create a "
524				"symlink to target" \"$(target)"\"." ;
525			Exit "ERROR: Add*ToContainer has not been invoked for it yet." ;
526		}
527
528		local name = $(target:BS) ;
529		local linkTarget = [ FDirName ../../.. $(installDir:G=) $(name) ] ;
530
531		AddSymlinkToContainer $(container)
532			: $(systemDirTokens) add-ons kernel boot
533			: $(linkTarget) : $(name) ;
534	}
535}
536
537
538rule AddLibrariesToContainer container : directory : libs
539{
540	# AddLibrariesToContainer <container> : <directory> : <libs>
541	#
542	# Installs libraries with the appropriate links into the container.
543	#
544
545	local lib ;
546	for lib in $(libs) {
547		local abiVersion = [ on $(lib) return $(HAIKU_LIB_ABI_VERSION) ] ;
548		if $(abiVersion) {
549			local abiVersionedLib = $(lib).$(abiVersion) ;
550			AddFilesToContainer $(container) : $(directory) : $(lib)
551				: $(abiVersionedLib) ;
552			AddSymlinkToContainer $(container) : $(directory)
553				: $(abiVersionedLib) : $(lib) ;
554		} else {
555			AddFilesToContainer $(container) : $(directory) : $(lib) ;
556		}
557	}
558}
559
560
561rule CreateContainerMakeDirectoriesScript container : script
562{
563	MakeLocate $(script) : $(HAIKU_OUTPUT_DIR) ;
564	Always $(script) ;
565
566	local initScript = [ InitScript $(script) ] ;
567
568	local scriptBody
569		= [ FSameTargetWithPrependedGrist $(script) : script-body ] ;
570	LOCATE on $(scriptBody) = [ on $(script) return $(LOCATE) ] ;
571	Depends $(scriptBody) : $(initScript) ;
572	Depends $(script) : $(scriptBody) ;
573
574	# collect the directories to create
575	local dirsToCreate ;
576	local directories
577		= [ on $(container) return $(HAIKU_INSTALL_DIRECTORIES) ] ;
578	local dir ;
579	for dir in $(directories) {
580		if ! [ on $(dir) return $(DONT_CREATE) ] {
581			dirsToCreate += $(dir) ;
582		}
583	}
584
585	# If the image shall only be updated, we don't create directories.
586	if $(dirsToCreate)
587		&& ( ! [ on $(container) return $(HAIKU_CONTAINER_UPDATE_ONLY) ]
588			|| [ IncludeAllTargetsInContainer $(container) ]
589			|| [ on $(container) return
590				$(HAIKU_CONTAINER_ALWAYS_CREATE_DIRECTORIES) ] ) {
591		Depends $(scriptBody) : $(dirsToCreate) ;
592		CreateContainerMakeDirectoriesScript1 $(scriptBody) : $(dirsToCreate) ;
593
594		local serializationDependency = $(scriptBody) ;
595			# Used to create a dependency chain between the dummy targets.
596			# This forces jam to build them one after the other, thus preventing
597			# concurrent writes to the script file when building with multiple
598			# jobs.
599
600		# For directories with attributes, we convert those the specified
601		# resource files to files with attributes and add commands to the script
602		# adding the attributes to the directories.
603		for dir in $(directories) {
604			local resourceFiles = [ on $(dir) return $(ATTRIBUTE_FILES) ] ;
605			if $(resourceFiles) {
606				local dirTokens = [ on $(dir) return $(DIRECTORY_TOKENS) ] ;
607
608				# translate resources file to file with attributes
609				local attributeFile = $(script)-attributes-$(dirTokens:J=-) ;
610				ResAttr $(attributeFile) : $(resourceFiles) ;
611
612				# use a unique dummy target for this file, on which we
613				# can define the TARGET_DIR variable
614				local dummyTarget = $(script)-attributes-dummy-$(dir:G=) ;
615				NotFile $(dummyTarget) ;
616				TARGET_DIR on $(dummyTarget) = $(dir:G=) ;
617
618				Depends $(dummyTarget) : $(initScript) $(attributeFile)
619					$(serializationDependency) ;
620				Depends $(script) : $(dummyTarget) ;
621				serializationDependency = $(dummyTarget) ;
622
623				AppendToContainerMakeDirectoriesScriptAttributes $(dummyTarget)
624					: $(initScript) $(attributeFile) ;
625			}
626		}
627	}
628}
629
630actions piecemeal CreateContainerMakeDirectoriesScript1
631{
632	echo \$mkdir -p "\"\${tPrefix}$(2:G=)\"" >> $(1)
633}
634
635actions AppendToContainerMakeDirectoriesScriptAttributes
636{
637	echo \$copyAttrs "\"\${sPrefix}$(2[2])\"" \
638		"\"\${tPrefix}$(TARGET_DIR)\"" >> $(2[1])
639}
640
641rule CreateContainerCopyFilesScript container : script
642{
643	MakeLocate $(script) : $(HAIKU_OUTPUT_DIR) ;
644	Always $(script) ;
645
646	local initScript = [ InitScript $(script) ] ;
647
648	local scriptBody
649		= [ FSameTargetWithPrependedGrist $(script) : script-body ] ;
650	LOCATE on $(scriptBody) = [ on $(script) return $(LOCATE) ] ;
651	Depends $(scriptBody) : $(initScript) ;
652	Depends $(script) : $(scriptBody) ;
653
654	local serializationDependency = $(scriptBody) ;
655		# Used to create a dependency chain between the dummy targets.
656		# This forces jam to build them one after the other, thus preventing
657		# concurrent writes to the script file when building with multiple
658		# jobs.
659
660	local dir ;
661	for dir in [ on $(container) return $(HAIKU_INSTALL_DIRECTORIES) ] {
662		# filter the targets that shall be renamed; they have to be copied
663		# individually
664		local destTargets = [ on $(dir) return $(TARGETS_TO_INSTALL) ] ;
665		local remainingTargets ;
666		local destTarget ;
667		for destTarget in $(destTargets) {
668			local target = [ on $(destTarget) return $(TARGET) ] ;
669			local name = $(destTarget:BS) ;
670			if $(name) != $(target:BS) {
671				# use a unique dummy target for this file, on which we
672				# can define the TARGET_DIR variable
673				local dummyTarget = $(script)-dummy-$(dir:G=)-$(target) ;
674				NotFile $(dummyTarget) ;
675				TARGET_DIR on $(dummyTarget) = $(dir:G=) ;
676				INSTALL_TARGET_NAME on $(dummyTarget) = $(name) ;
677
678				Depends $(dummyTarget) : $(initScript) $(target)
679					$(serializationDependency) ;
680				Depends $(script) : $(dummyTarget) ;
681				serializationDependency = $(dummyTarget) ;
682
683				AppendToContainerCopyFilesScriptSingleFile $(dummyTarget)
684					: $(initScript) $(target) ;
685			} else {
686				remainingTargets += $(target) ;
687			}
688		}
689		targets = $(remainingTargets) ;
690
691		if $(targets) {
692			# use a unique dummy target for this directory, on which we
693			# can define the TARGET_DIR variable
694			local dummyTarget = $(script)-dummy-$(dir:G=) ;
695			NotFile $(dummyTarget) ;
696			TARGET_DIR on $(dummyTarget) = $(dir:G=) ;
697
698			Depends $(dummyTarget) : $(initScript) $(targets)
699				$(serializationDependency) ;
700			Depends $(script) : $(dummyTarget) ;
701			serializationDependency = $(dummyTarget) ;
702
703			OUTPUT_SCRIPT on $(dummyTarget) = $(initScript) ;
704			AppendToContainerCopyFilesScript $(dummyTarget) : $(targets) ;
705		}
706
707		local symlinks = [ on $(dir) return $(SYMLINKS_TO_INSTALL) ] ;
708		local symlink ;
709		for symlink in $(symlinks) {
710			NotFile $(symlink) ;
711
712			Depends $(script) : $(symlink) ;
713			Depends $(symlink) : $(initScript) $(serializationDependency) ;
714			serializationDependency = $(symlink) ;
715
716			AddSymlinkToContainerCopyFilesScript $(symlink) : $(initScript) ;
717		}
718
719		local targetDirs = [ on $(dir) return $(DIRECTORIES_TO_INSTALL) ] ;
720		local targetDir ;
721		for targetDir in $(targetDirs) {
722			NotFile $(targetDir) ;
723
724			Depends $(script) : $(targetDir) ;
725			Depends $(targetDir) : $(initScript) $(serializationDependency) ;
726			serializationDependency = $(targetDir) ;
727
728			AddDirectoryToContainerCopyFilesScript $(targetDir)
729				: $(initScript) ;
730		}
731	}
732}
733
734
735actions piecemeal AppendToContainerCopyFilesScript bind OUTPUT_SCRIPT
736{
737	echo \$cp "\"\${sPrefix}$(2)\"" "\"\${tPrefix}$(TARGET_DIR)\"" \
738		>> $(OUTPUT_SCRIPT)
739}
740
741
742actions AppendToContainerCopyFilesScriptSingleFile
743{
744	echo \$cp "\"\${sPrefix}$(2[2])\"" \
745		"\"\${tPrefix}$(TARGET_DIR)/$(INSTALL_TARGET_NAME)\"" >> $(2[1])
746}
747
748
749actions AddSymlinkToContainerCopyFilesScript
750{
751	echo \$ln -sfn "\"$(SYMLINK_TARGET)\"" "\"\${tPrefix}$(1:G=)\"" >> $(2[1])
752}
753
754
755actions AddDirectoryToContainerCopyFilesScript bind SOURCE_DIRECTORY
756{
757	echo \$cp -r $(EXCLUDE_PATTERNS) "\"\${sPrefix}$(SOURCE_DIRECTORY)/.\"" \
758		"\"\${tPrefix}$(TARGET_DIRECTORY:G=)\"" >> $(2[1])
759}
760
761
762rule CreateContainerExtractFilesScript container : script
763{
764	MakeLocate $(script) : $(HAIKU_OUTPUT_DIR) ;
765	Always $(script) ;
766
767	local initScript = [ InitScript $(script) ] ;
768
769	local scriptBody
770		= [ FSameTargetWithPrependedGrist $(script) : script-body ] ;
771	LOCATE on $(scriptBody) = [ on $(script) return $(LOCATE) ] ;
772	Depends $(scriptBody) : $(initScript) ;
773	Depends $(script) : $(scriptBody) ;
774
775	local serializationDependency = $(scriptBody) ;
776		# Used to create a dependency chain between the dummy targets.
777		# This forces jam to build them one after the other, thus preventing
778		# concurrent writes to the script file when building with multiple
779		# jobs.
780
781	local dir ;
782	for dir in [ on $(container) return $(HAIKU_INSTALL_DIRECTORIES) ] {
783		local archiveFiles = [ on $(dir) return $(ARCHIVE_FILES_TO_INSTALL) ] ;
784		local archiveFile ;
785		for archiveFile in $(archiveFiles) {
786			# use a unique dummy target for this file, on which we
787			# can define the TARGET_DIR variable
788			local dummyTarget = $(script)-dummy-$(dir:G=)-$(archiveFile) ;
789			NotFile $(dummyTarget) ;
790			TARGET_DIR on $(dummyTarget) = $(dir:G=) ;
791
792			local extractedSubDir = [ on $(archiveFile)
793				return $(ARCHIVE_SUBDIR_TO_INSTALL_FROM) ] ;
794			ARCHIVE_SUBDIR_TO_INSTALL_FROM on $(dummyTarget) =
795				$(extractedSubDir:E=.) ;
796
797			Depends $(dummyTarget) : $(initScript) $(archiveFile)
798				$(serializationDependency) ;
799			Depends $(script) : $(dummyTarget) ;
800			serializationDependency = $(dummyTarget) ;
801
802			AddExtractFileToContainerExtractFilesScript $(dummyTarget)
803				: $(initScript) $(archiveFile) ;
804		}
805	}
806}
807
808
809actions AddExtractFileToContainerExtractFilesScript
810{
811	echo extractFile "\"$(2[2])\"" "\"$(TARGET_DIR)\"" \
812		"\"$(ARCHIVE_SUBDIR_TO_INSTALL_FROM)\"" >> $(2[1])
813}
814
815
816#pragma mark - Haiku Image rules
817
818rule SetUpdateHaikuImageOnly flag
819{
820	HAIKU_CONTAINER_UPDATE_ONLY on $(HAIKU_IMAGE_CONTAINER_NAME) = $(flag) ;
821}
822
823rule IsUpdateHaikuImageOnly
824{
825	on $(HAIKU_IMAGE_CONTAINER_NAME) return $(HAIKU_CONTAINER_UPDATE_ONLY) ;
826}
827
828rule AddDirectoryToHaikuImage directoryTokens : attributeFiles
829{
830	# AddDirectoryToHaikuImage <directoryTokens> : <attributeFiles>
831
832	return [ AddDirectoryToContainer $(HAIKU_IMAGE_CONTAINER_NAME)
833		: $(directoryTokens) : $(attributeFiles) ] ;
834}
835
836rule AddFilesToHaikuImage directory : targets : destName
837{
838	# AddFilesToHaikuImage <directory> : <targets> [ : dest name ]
839
840	AddFilesToContainer $(HAIKU_IMAGE_CONTAINER_NAME) : $(directory)
841		: $(targets) : $(destName) ;
842}
843
844rule FFilesInHaikuImageDirectory directoryTokens
845{
846	return [ FFilesInContainerDirectory $(HAIKU_IMAGE_CONTAINER_NAME)
847		: $(directoryTokens) ] ;
848}
849
850rule AddSymlinkToHaikuImage directoryTokens : linkTarget : linkName
851{
852	# AddSymlinkToHaikuImage <directory> : <link target> [ : <link name> ] ;
853
854	linkTarget = $(linkTarget:J=/) ;
855
856	AddSymlinkToContainer $(HAIKU_IMAGE_CONTAINER_NAME) : $(directoryTokens)
857		: $(linkTarget) : $(linkName) ;
858}
859
860rule FSymlinksInHaikuImageDirectory directoryTokens
861{
862	return [ FSymlinksInContainerDirectory $(HAIKU_IMAGE_CONTAINER_NAME)
863		: $(directoryTokens) ] ;
864}
865
866rule CopyDirectoryToHaikuImage directoryTokens : sourceDirectory
867	: targetDirectoryName : excludePatterns : flags
868{
869	CopyDirectoryToContainer $(HAIKU_IMAGE_CONTAINER_NAME) : $(directoryTokens)
870		: $(sourceDirectory) : $(targetDirectoryName) : $(excludePatterns)
871		: $(flags) ;
872}
873
874rule AddSourceDirectoryToHaikuImage dirTokens : flags
875{
876	# AddSourceDirectoryToHaikuImage <dirTokens> : <flags> ;
877
878	CopyDirectoryToHaikuImage home HaikuSources
879		: [ FDirName $(HAIKU_TOP) $(dirTokens) ]
880		: : : $(flags) ;
881}
882
883rule AddHeaderDirectoryToHaikuImage dirTokens : dirName : flags
884{
885	# AddHeaderDirectoryToHaikuImage <dirTokens> : [ <dirName> ]
886	#	: <flags> ;
887
888	AddHeaderDirectoryToContainer $(HAIKU_IMAGE_CONTAINER_NAME) : $(dirTokens)
889		: $(dirName) : $(flags) ;
890}
891
892rule AddWifiFirmwareToHaikuImage driver : package : archive : extract
893{
894	# AddWifiFirmwareToHaikuImage <driver> : <package> : <archive> : <extract>
895
896	AddWifiFirmwareToHaikuImage $(HAIKU_IMAGE_CONTAINER_NAME) : $(driver)
897		: $(package) : $(archive) : $(extract) ;
898}
899
900rule ExtractArchiveToHaikuImage dirTokens : archiveFile : flags
901	: extractedSubDir
902{
903	# ExtractArchiveToHaikuImage <dirTokens> : <archiveFile> : <flags>
904	#	: <extractedSubDir> ;
905
906	ExtractArchiveToContainer $(HAIKU_IMAGE_CONTAINER_NAME) : $(dirTokens)
907		: $(archiveFile) : $(flags) : $(extractedSubDir) ;
908}
909
910rule AddDriversToHaikuImage relativeDirectoryTokens : targets
911{
912	# AddDriversToHaikuImage <relative directory> : <targets> ;
913
914	AddDriversToContainer $(HAIKU_IMAGE_CONTAINER_NAME)
915		: $(relativeDirectoryTokens) : $(targets) ;
916}
917
918rule AddNewDriversToHaikuImage relativeDirectoryTokens : targets
919{
920	# AddNewDriversToHaikuImage <relative directory> : <targets> ;
921
922	AddNewDriversToContainer $(HAIKU_IMAGE_CONTAINER_NAME)
923		: $(relativeDirectoryTokens) : $(targets) ;
924}
925
926rule AddBootModuleSymlinksToHaikuImage targets
927{
928	# AddBootModuleSymlinksToHaikuImage <targets> ;
929
930	AddBootModuleSymlinksToContainer $(HAIKU_IMAGE_CONTAINER_NAME)
931		: $(targets) ;
932}
933
934rule AddPackageFilesToHaikuImage location : packages
935{
936	HAIKU_PACKAGES_IN_IMAGE on $(HAIKU_IMAGE_CONTAINER_NAME)
937		= [ on $(HAIKU_IMAGE_CONTAINER_NAME) return $(HAIKU_PACKAGES_IN_IMAGE) ]
938			$(packages) ;
939	HAIKU_PACKAGE_INSTALLATION_LOCATION on $(packages) = $(location) ;
940	AddFilesToHaikuImage $(location) packages : $(packages) ;
941}
942
943rule AddOptionalHaikuImagePackages packages
944{
945	local package ;
946	for package in $(packages) {
947		if ! [ on $(package) return $(HAIKU_OPTIONAL_PACKAGE_ADDED) ] {
948			HAIKU_OPTIONAL_PACKAGE_ADDED on $(package) = 1 ;
949			HAIKU_ADDED_OPTIONAL_PACKAGES += $(package) ;
950		}
951		local dependencies = [ on $(package)
952			return $(HAIKU_OPTIONAL_PACKAGE_DEPENDENCIES) ] ;
953		AddOptionalHaikuImagePackages $(dependencies) ;
954	}
955}
956
957rule SuppressOptionalHaikuImagePackages packages
958{
959	local package ;
960	for package in $(packages) {
961		if ! [ on $(package) return $(HAIKU_OPTIONAL_PACKAGE_SUPPRESSED) ] {
962			HAIKU_OPTIONAL_PACKAGE_SUPPRESSED on $(package) = 1 ;
963		}
964	}
965}
966
967rule IsOptionalHaikuImagePackageAdded package
968{
969	if ! [ on $(package) return $(HAIKU_OPTIONAL_PACKAGE_EXISTS) ] {
970		HAIKU_OPTIONAL_PACKAGE_EXISTS on $(package) = 1 ;
971		HAIKU_EXISTING_OPTIONAL_PACKAGES += $(package) ;
972	}
973
974	if [ on $(package) return $(HAIKU_OPTIONAL_PACKAGE_ADDED) ] &&
975			! [ on $(package) return $(HAIKU_OPTIONAL_PACKAGE_SUPPRESSED) ] {
976		return 1 ;
977	}
978
979	return ;
980}
981
982rule OptionalPackageDependencies package : dependencies
983{
984	HAIKU_OPTIONAL_PACKAGE_DEPENDENCIES on $(package) = $(dependencies) ;
985	if [ on $(package) return $(HAIKU_OPTIONAL_PACKAGE_ADDED) ] {
986		AddOptionalHaikuImagePackages $(dependencies) ;
987	}
988}
989
990
991rule AddHaikuImagePackages packages : installationLocation
992{
993	# AddHaikuImagePackages <packages> [ : <installationLocation> ] ;
994	 # Adds the given package <packages>s to the image. <installationLocation>
995	 # is either "system" or "common" (the default) and specifies where the
996	 # packages shall be installed.
997
998	installationLocation ?= common ;
999
1000	local package ;
1001	for package in $(packages) {
1002		local resolvedPackage = [ IsPackageAvailable $(package) ] ;
1003		if ! $(resolvedPackage) {
1004			Echo "AddHaikuImagePackages: package" $(package)
1005				"not available!" ;
1006			continue ;
1007		}
1008
1009		if ! [ on $(resolvedPackage) return $(HAIKU_PACKAGE_ADDED) ] {
1010			HAIKU_PACKAGE_ADDED on $(resolvedPackage) = 1 ;
1011			HAIKU_ADDED_PACKAGES += $(resolvedPackage) ;
1012
1013			# download the package file and add it to the image
1014			local file = [ FetchPackage $(package) ] ;
1015
1016			if $(HAIKU_UPDATE_ALL_PACKAGES) {
1017				HAIKU_INCLUDE_IN_IMAGE on $(file) = 1 ;
1018			}
1019
1020			AddPackageFilesToHaikuImage $(installationLocation) : $(file) ;
1021		}
1022	}
1023}
1024
1025
1026rule IsHaikuImagePackageAdded package
1027{
1028	local resolvedPackage = [ IsPackageAvailable $(package) ] ;
1029	if $(resolvedPackage)
1030		&& [ on $(resolvedPackage) return $(HAIKU_PACKAGE_ADDED) ] {
1031		return 1 ;
1032	}
1033
1034	return ;
1035}
1036
1037
1038rule InstallSourceArchive file : url
1039{
1040	if $(HAIKU_INCLUDE_SOURCES) = 1 {
1041		# download archive file
1042		local archiveFile = [ DownloadFile $(file) : $(url) ] ;
1043
1044		# copy directly into image
1045		AddFilesToHaikuImage _sources_ : $(archiveFile) ;
1046	}
1047}
1048
1049rule InstallOptionalHaikuImagePackage url : dirTokens : flags
1050{
1051	# TODO: Remove the non-hpkg cases!
1052	# Currently the semantics differs depending on whether the  cdPackage flag
1053	# has been specified and the type of the package file:
1054	# * For a hpkg <dirTokens> is "common" (default) or "system" and specifies
1055	#   the installation location of the package (will be copied to the
1056	#   packages/ subdirectory).
1057	# * For a regular archive and cdPackage, <dirTokens> is ignored and
1058	#   the package will be copied to the _package_ directory of the CD image.
1059	# * For a regular archive and without cdPackage, <dirTokens> specifies the
1060	#   directory relative to the image's root directory where the content of
1061	#   the archive will be extracted to.
1062	#
1063	# Supported flags: cdPackage
1064
1065	local package = $(url:BS) ;
1066
1067	# download archive file
1068	local archiveFile = [ DownloadFile $(package) : $(url) ] ;
1069
1070	if $(package:S) = .hpkg {
1071		local location = $(dirTokens:E=common) ;
1072		if $(HAIKU_UPDATE_ALL_PACKAGES) {
1073			HAIKU_INCLUDE_IN_IMAGE on $(archiveFile) = 1 ;
1074		}
1075		AddPackageFilesToHaikuImage $(location) : $(archiveFile) ;
1076	} else if cdPackage in $(flags) && $(HAIKU_CD_NAME) {
1077		# TODO: If HAIKU_CD_NAME is set, that doesn't mean we're building a CD
1078		# image!
1079		# copy onto image
1080		AddFilesToHaikuImage _packages_ : $(archiveFile) ;
1081	} else {
1082		# extract onto image
1083		ExtractArchiveToHaikuImage $(dirTokens) : $(archiveFile) : $(flags) ;
1084	}
1085}
1086
1087rule AddEntryToHaikuImageUserGroupFile file : entry
1088{
1089	local allEntries = [ on $(file) return $(HAIKU_IMAGE_USER_GROUP_ENTRIES) ] ;
1090
1091	if $(allEntries) {
1092		allEntries = $(allEntries)|$(entry) ;
1093	} else {
1094		allEntries = $(entry) ;
1095
1096		Always $(file) ;
1097		MakeLocate $(file) : $(HAIKU_COMMON_PLATFORM_OBJECT_DIR) ;
1098		BuildHaikuImageUserGroupFile $(file) ;
1099		AddFilesToHaikuImage common settings etc : $(file) ;
1100	}
1101
1102	HAIKU_IMAGE_USER_GROUP_ENTRIES on $(file) = $(allEntries) ;
1103}
1104
1105actions BuildHaikuImageUserGroupFile
1106{
1107	echo "$(HAIKU_IMAGE_USER_GROUP_ENTRIES)" | tr '|' '\n' > $(1)
1108}
1109
1110rule AddUserToHaikuImage user : uid : gid : home : shell : realName
1111{
1112	if ! $(user) || ! $(uid) || ! $(gid) || ! $(home) {
1113		Exit "Invalid haiku user specification passed to AddUserToHaikuImage." ;
1114	}
1115
1116	local entry
1117		= $(user):x:$(uid):$(gid):$(realName:E=$(user)):$(home):$(shell:E="") ;
1118
1119	AddEntryToHaikuImageUserGroupFile <haiku-image>passwd : $(entry) ;
1120}
1121
1122rule AddGroupToHaikuImage group : gid : members
1123{
1124	if ! $(group) || ! $(gid) {
1125		Exit "Invalid haiku group specification passed to"
1126			"AddGroupToHaikuImage." ;
1127	}
1128
1129	local entry = $(group):x:$(gid):$(members:J=,:E) ;
1130
1131	AddEntryToHaikuImageUserGroupFile <haiku-image>group : $(entry) ;
1132}
1133
1134
1135rule AddLibrariesToHaikuImage directory : libs
1136{
1137	# AddLibraryToHaikuImage <directory> : <libs>
1138	#
1139	# Installs libraries with the appropriate links onto the image.
1140	#
1141
1142	AddLibrariesToContainer $(HAIKU_IMAGE_CONTAINER_NAME) : $(directory)
1143		: $(libs) ;
1144}
1145
1146
1147rule CreateHaikuImageMakeDirectoriesScript script
1148{
1149	CreateContainerMakeDirectoriesScript $(HAIKU_IMAGE_CONTAINER_NAME)
1150		: $(script) ;
1151}
1152
1153rule CreateHaikuImageCopyFilesScript script
1154{
1155	CreateContainerCopyFilesScript $(HAIKU_IMAGE_CONTAINER_NAME) : $(script) ;
1156}
1157
1158rule CreateHaikuImageExtractFilesScript script
1159{
1160	CreateContainerExtractFilesScript $(HAIKU_IMAGE_CONTAINER_NAME)
1161		: $(script) ;
1162}
1163
1164rule BuildHaikuImage haikuImage : scripts : isImage : isVMwareImage
1165{
1166	# BuildHaikuImage <haiku image> : <scripts> : <is image> : <isVMwareImage> ;
1167
1168	if $(isImage) = 1 || $(isImage) = true {
1169		IS_IMAGE on $(haikuImage) = 1 ;
1170	} else {
1171		IS_IMAGE on $(haikuImage) = "" ;
1172	}
1173
1174	if $(isVMwareImage) = 1 || $(isVMwareImage) = true {
1175		IS_VMWARE_IMAGE on $(haikuImage) = 1 ;
1176	} else {
1177		IS_VMWARE_IMAGE on $(haikuImage) = "" ;
1178	}
1179
1180	local mainScript = build_haiku_image ;
1181	SEARCH on $(mainScript) = [ FDirName $(HAIKU_TOP) build scripts ] ;
1182
1183	Depends $(haikuImage) : $(mainScript) $(scripts) ;
1184	BuildHaikuImage1 $(haikuImage) : $(mainScript) $(scripts) ;
1185}
1186
1187actions BuildHaikuImage1
1188{
1189	export imagePath="$(1)"
1190	export isImage="$(IS_IMAGE)"
1191	export isVMwareImage="$(IS_VMWARE_IMAGE)"
1192	$(2[1]) $(2[2-])
1193}
1194
1195rule BuildVMWareImage vmwareImage : plainImage : imageSize
1196{
1197	# BuildVMWareImage <vmware image> : <plain image> : <image size in MB>
1198
1199	IMAGE_SIZE on $(vmwareImage) = $(imageSize) ;
1200
1201	Depends $(vmwareImage) : <build>vmdkheader $(plainImage) ;
1202	BuildVMWareImage1 $(vmwareImage) : <build>vmdkheader $(plainImage) ;
1203}
1204
1205actions BuildVMWareImage1
1206{
1207	$(RM) $(1)
1208	$(2[1]) -h 64k -i$(IMAGE_SIZE)M $(1) &&
1209	cat $(2[2]) >> $(1)
1210}
1211
1212
1213#pragma mark - Network Boot Archive rules
1214
1215rule AddDirectoryToNetBootArchive directoryTokens
1216{
1217	# AddDirectoryToNetBootArchive <directoryTokens>
1218
1219	return [ AddDirectoryToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1220		: $(directoryTokens) ] ;
1221}
1222
1223rule AddFilesToNetBootArchive directory : targets : destName
1224{
1225	# AddFilesToNetBootArchive <directory> : <targets> [ : dest name ]
1226
1227	AddFilesToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME) : $(directory)
1228		: $(targets) : $(destName) ;
1229}
1230
1231rule AddSymlinkToNetBootArchive directoryTokens : linkTarget : linkName
1232{
1233	# AddSymlinkToNetBootArchive <directory> : <link target> [ : <link name> ] ;
1234
1235	AddSymlinkToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1236		: $(directoryTokens) : $(linkTarget) : $(linkName) ;
1237}
1238
1239rule AddDriversToNetBootArchive relativeDirectoryTokens : targets
1240{
1241	# AddDriversToNetBootArchive <relative directory> : <targets> ;
1242
1243	AddDriversToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1244		: $(relativeDirectoryTokens) : $(targets) ;
1245}
1246
1247rule AddNewDriversToNetBootArchive relativeDirectoryTokens : targets
1248{
1249	# AddNewDriversToNetBootArchive <relative directory> : <targets> ;
1250
1251	AddNewDriversToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1252		: $(relativeDirectoryTokens) : $(targets) ;
1253}
1254
1255rule AddDriverRegistrationToNetBootArchive relativeDirectoryTokens : target
1256	: links
1257{
1258	# AddDriverRegistrationToNetBootArchive <directory> : <link target>
1259	#	: <link names> ] ;
1260
1261	AddDriverRegistrationToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1262		: $(relativeDirectoryTokens) : $(target) : $(links) ;
1263}
1264
1265rule AddBootModuleSymlinksToNetBootArchive targets
1266{
1267	# AddBootModuleSymlinksToNetBootArchive <targets> ;
1268
1269	AddBootModuleSymlinksToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1270		: $(targets) ;
1271}
1272
1273rule CreateNetBootArchiveMakeDirectoriesScript script
1274{
1275	CreateContainerMakeDirectoriesScript
1276		$(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME) : $(script) ;
1277}
1278
1279rule CreateNetBootArchiveCopyFilesScript script
1280{
1281	CreateContainerCopyFilesScript $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1282		: $(script) ;
1283}
1284
1285rule BuildNetBootArchive archive : scripts
1286{
1287	# BuildNetBootArchive <archive> : <scripts>  ;
1288
1289	local mainScript = build_archive ;
1290	SEARCH on $(mainScript) = [ FDirName $(HAIKU_TOP) build scripts ] ;
1291
1292	Depends $(archive) : $(mainScript) $(scripts) ;
1293	BuildNetBootArchive1 $(archive) : $(mainScript) $(scripts) ;
1294}
1295
1296actions BuildNetBootArchive1
1297{
1298	$(2[1]) $(1) $(2[2-])
1299}
1300
1301
1302#pragma mark - Floppy Boot Archive rules
1303
1304
1305rule AddDirectoryToFloppyBootArchive directoryTokens
1306{
1307	# AddDirectoryToFloppyBootArchive <directoryTokens>
1308
1309	return [ AddDirectoryToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1310		: $(directoryTokens) ] ;
1311}
1312
1313rule AddFilesToFloppyBootArchive directory : targets : destName
1314{
1315	# AddFilesToFloppyBootArchive <directory> : <targets> [ : dest name ]
1316
1317	AddFilesToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME) : $(directory)
1318		: $(targets) : $(destName) ;
1319}
1320
1321rule AddSymlinkToFloppyBootArchive directoryTokens : linkTarget : linkName
1322{
1323	# AddSymlinkToFloppyBootArchive <directory> : <link target>
1324	#	[ : <link name> ] ;
1325
1326	AddSymlinkToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1327		: $(directoryTokens) : $(linkTarget) : $(linkName) ;
1328}
1329
1330rule AddDriversToFloppyBootArchive relativeDirectoryTokens : targets
1331{
1332	# AddDriversToFloppyBootArchive <relative directory> : <targets> ;
1333
1334	AddDriversToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1335		: $(relativeDirectoryTokens) : $(targets) ;
1336}
1337
1338rule AddNewDriversToFloppyBootArchive relativeDirectoryTokens : targets
1339{
1340	# AddNewDriversToFloppyBootArchive <relative directory> : <targets> ;
1341
1342	AddNewDriversToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1343		: $(relativeDirectoryTokens) : $(targets) ;
1344}
1345
1346rule AddDriverRegistrationToFloppyBootArchive relativeDirectoryTokens : target
1347	: links
1348{
1349	# AddDriverRegistrationToFloppyBootArchive <directory> : <link target>
1350	#	: <link names> ] ;
1351
1352	AddDriverRegistrationToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1353		: $(relativeDirectoryTokens) : $(target) : $(links) ;
1354}
1355
1356rule AddBootModuleSymlinksToFloppyBootArchive targets
1357{
1358	# AddBootModuleSymlinksToFloppyBootArchive <targets> ;
1359
1360	AddBootModuleSymlinksToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1361		: $(targets) ;
1362}
1363
1364rule CreateFloppyBootArchiveMakeDirectoriesScript script
1365{
1366	CreateContainerMakeDirectoriesScript
1367		$(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME) : $(script) ;
1368}
1369
1370rule CreateFloppyBootArchiveCopyFilesScript script
1371{
1372	CreateContainerCopyFilesScript $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1373		: $(script) ;
1374}
1375
1376rule BuildFloppyBootArchive archive : scripts
1377{
1378	# BuildHFloppyBootArchive <archive> : <scripts>  ;
1379
1380	local mainScript = build_archive ;
1381	SEARCH on $(mainScript) = [ FDirName $(HAIKU_TOP) build scripts ] ;
1382
1383	Depends $(archive) : $(mainScript) $(scripts) ;
1384	BuildFloppyBootArchive1 $(archive) : $(mainScript) $(scripts) ;
1385}
1386
1387actions BuildFloppyBootArchive1
1388{
1389	$(2[1]) $(1) $(2[2-])
1390}
1391
1392# warning: that is quite x86 dependant...
1393
1394rule BuildFloppyBootImage image : haikuLoader : archive
1395{
1396	Depends $(image) : $(haikuLoader) ;
1397	Depends $(image) : $(archive) ;
1398	#MakeLocateDebug $(image) ;
1399	FLOPPY_IMAGE_SIZE on $(image) = $(HAIKU_BOOT_FLOPPY_IMAGE_SIZE) ;
1400	ARCHIVE_IMAGE_OFFSET on $(image) = $(HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET) ;
1401	BuildFloppyBootImage1 $(image) : $(haikuLoader) $(archive) ;
1402	if $(HAIKU_BOOT_PLATFORM) = atari_m68k {
1403		Depends $(image) : <build>fixup_tos_boot_checksum ;
1404		BuildFloppyBootImageFixupM68K $(image)
1405			: <build>fixup_tos_boot_checksum ;
1406	}
1407	if $(HAIKU_BOOT_PLATFORM) = amiga_m68k {
1408		Depends $(image) : <build>fixup_amiga_boot_checksum ;
1409		BuildFloppyBootImageFixupM68K $(image)
1410			: <build>fixup_amiga_boot_checksum ;
1411	}
1412}
1413
1414actions BuildFloppyBootImage1
1415{
1416	haiku_loader_size=`stat -c %s "$(>[1])"`
1417	if [ $? -ne 0 ] ; then
1418		# FreeBSD's stat command don't support -c/--format option
1419		# and use %z specifier for file size
1420		haiku_loader_size=`stat -f %z "$(>[1])"`
1421	fi
1422	archive_image_offset=`echo "$(ARCHIVE_IMAGE_OFFSET) * 1024" | bc`
1423	if [ $haiku_loader_size -gt $archive_image_offset ] ; then
1424		echo "Error: $(>[1]) is too big ($haiku_loader_size) to fit "
1425		echo "       before the boot archive starting at $archive_image_offset!"
1426		exit 1
1427	fi
1428	$(RM) $(<)
1429	# make an empty image
1430	dd if=/dev/zero of=$(<) bs=1k count=$(FLOPPY_IMAGE_SIZE)
1431	# add haiku_loader
1432	dd if=$(>[1]) of=$(<) conv=notrunc
1433	# add the boot drivers tgz archive
1434	dd if=$(>[2]) of=$(<) bs=$(ARCHIVE_IMAGE_OFFSET)k seek=1 conv=notrunc
1435}
1436
1437actions BuildFloppyBootImageFixupM68K
1438{
1439	# fixup the boot sector checksum
1440	$(>[1]) $(<)
1441}
1442
1443#pragma mark - CD Boot Image rules
1444
1445rule BuildCDBootImage image : bootfloppy : extrafiles
1446{
1447	Depends $(image) : $(bootfloppy) ;
1448	Depends $(image) : $(extrafiles) ;
1449	BOOTIMG on $(image) = $(bootfloppy) ;
1450
1451	BuildCDBootImage1 $(image) : $(bootfloppy) $(extrafiles) ;
1452}
1453
1454actions BuildCDBootImage1
1455{
1456	$(RM) $(<)
1457	mkisofs -b $(BOOTIMG) -r -J -V bootimg -o $(<) $(>[1]) $(>[2-])
1458}
1459
1460
1461#pragma mark - CD Boot PPC Image rules
1462
1463rule BuildCDBootPPCImage image : hfsmaps : elfloader : coffloader : chrpscript
1464	: extrafiles
1465{
1466	Depends $(image) : $(elfloader) ;
1467	Depends $(image) : $(coffloader) ;
1468	Depends $(image) : $(chrpscript) ;
1469	Depends $(image) : $(extrafiles) ;
1470	Depends $(image) : $(hfsmaps) ;
1471	MAPS on $(image) = $(hfsmaps) ;
1472
1473	BuildCDBootPPCImage1 $(image) : $(elfloader) $(coffloader) $(chrpscript)
1474		$(extrafiles) ;
1475}
1476
1477actions BuildCDBootPPCImage1 bind MAPS
1478{
1479	$(RM) $(<)
1480	mkdir -p $(HAIKU_OUTPUT_DIR)/cd/ppc
1481	mkdir -p $(HAIKU_OUTPUT_DIR)/cd/boot
1482	# CHRP Boot script
1483	cp $(>[3]) $(HAIKU_OUTPUT_DIR)/cd/ppc/bootinfo.txt
1484	cp $(>[3]) $(HAIKU_OUTPUT_DIR)/cd/boot/boot.chrp
1485	# Haiku Bootloaders
1486	cp $(>[2]) $(HAIKU_OUTPUT_DIR)/cd/boot/haikuloader.xcf
1487	cp $(>[1]) $(HAIKU_OUTPUT_DIR)/cd/boot/haikuloader.elf
1488	# Extras (readme files, etc)
1489	cp $(>[4]) $(HAIKU_OUTPUT_DIR)/cd/
1490
1491	mkisofs -v -hfs -part -map $(MAPS) -no-desktop -hfs-volid bootimg \
1492		-V bootimg -hfs-bless $(HAIKU_OUTPUT_DIR)/cd/boot -prep-boot \
1493		boot/haikuloader.xcf -r -o $(<) $(HAIKU_OUTPUT_DIR)/cd \
1494	|| \
1495	genisoimage -v -hfs -part -map $(MAPS) -no-desktop -hfs-volid bootimg \
1496		-V bootimg -hfs-bless $(HAIKU_OUTPUT_DIR)/cd/boot -prep-boot \
1497		boot/haikuloader.xcf -r -o $(<) $(HAIKU_OUTPUT_DIR)/cd
1498	$(RM) -R $(HAIKU_OUTPUT_DIR)/cd
1499}
1500
1501
1502