ImageRules revision e173a1ec
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, stripDebugSymbols
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	local stripDebugSymbols ;
427	if stripDebugSymbols in $(flags) {
428		stripDebugSymbols = 1 ;
429	}
430
431	ARCHIVE_FILES_TO_INSTALL on $(directory) += $(archiveFile) ;
432	ARCHIVE_SUBDIR_TO_INSTALL_FROM on $(archiveFile) = $(extractedSubDir) ;
433	ARCHIVE_FILES_TO_STRIP on $(archiveFile) = $(stripDebugSymbols) ;
434}
435
436rule AddDriversToContainer container : relativeDirectoryTokens : targets
437{
438	# AddDriversToContainer <container> : <relative directory> : <targets> ;
439	#
440	local systemDirTokens
441		= [ on $(container) return $(HAIKU_CONTAINER_SYSTEM_DIR_TOKENS) ] ;
442	local directoryTokens = $(systemDirTokens) add-ons kernel drivers dev
443		$(relativeDirectoryTokens) ;
444
445	targets = [ FFilterByBuildFeatures $(targets) ] ;
446
447	# A driver can be in multiple categories. Avoid adding it to the bin/
448	# directory more than once.
449	local binTargets ;
450	local target ;
451	for target in $(targets) {
452		local containers
453			= [ on $(target) return $(HAIKU_DRIVER_IN_CONTAINERS) ] ;
454		if ! $(container) in $(containers) {
455			HAIKU_DRIVER_IN_CONTAINERS on $(target)
456				= $(containers) $(container) ;
457			binTargets += $(target) ;
458		}
459	}
460
461	AddFilesToContainer $(container)
462		: $(systemDirTokens) add-ons kernel drivers bin
463		: $(binTargets) ;
464
465	# If the image shall only be updated, we don't add any symlinks.
466	if [ on $(container) return $(HAIKU_CONTAINER_UPDATE_ONLY) ]
467		&& ! [ IncludeAllTargetsInContainer $(container) ] {
468		return ;
469	}
470
471	# get the relative symlink path prefix
472	local linkPrefix = ;
473	for i in $(relativeDirectoryTokens) {
474		linkPrefix += .. ;
475	}
476	linkPrefix += .. bin ;
477
478	# add the symlinks
479	local name ;
480	for name in $(targets:BS) {
481		AddSymlinkToContainer $(container) : $(directoryTokens)
482			: [ FDirName $(linkPrefix) $(name) ] : $(name) ;
483	}
484}
485
486rule AddNewDriversToContainer container : relativeDirectoryTokens
487	: targets
488{
489	# AddNewDriversToContainer <container> : <directory> : <targets> ;
490	#
491	local systemDirTokens
492		= [ on $(container) return $(HAIKU_CONTAINER_SYSTEM_DIR_TOKENS) ] ;
493	local directoryTokens = $(systemDirTokens) add-ons kernel drivers
494		$(relativeDirectoryTokens) ;
495
496	targets = [ FFilterByBuildFeatures $(targets) ] ;
497
498	AddFilesToContainer $(container) : $(directoryTokens)
499		: $(targets) ;
500}
501
502rule AddBootModuleSymlinksToContainer container : targets
503{
504	# AddBootModuleSymlinksToContainer <container> : <targets> ;
505	#
506
507	# If the container shall only be updated, we don't add any symlinks.
508
509	if [ on $(container) return $(HAIKU_CONTAINER_UPDATE_ONLY) ]
510		&& ! [ IncludeAllTargetsInContainer $(container) ] {
511		return ;
512	}
513
514	local systemDirTokens
515		= [ on $(container) return $(HAIKU_CONTAINER_SYSTEM_DIR_TOKENS) ] ;
516	targets = [ FFilterByBuildFeatures $(targets) ] ;
517
518	# add the symlinks
519	local installTargetsVar
520		= [ on $(container) return $(HAIKU_INSTALL_TARGETS_VAR) ] ;
521	local target ;
522	for target in $(targets) {
523		# Symlink to the first place where the target has been installed.
524		local destTarget = [ on $(target) return $($(installTargetsVar)[1]) ] ;
525		local installDir = [ on $(destTarget) return $(INSTALL_DIR) ] ;
526
527		if ! $(installDir) {
528			Echo "ERROR: AddBootModuleSymlinksToContainer: Can't create a "
529				"symlink to target" \"$(target)"\"." ;
530			Exit "ERROR: Add*ToContainer has not been invoked for it yet." ;
531		}
532
533		local name = $(target:BS) ;
534		local linkTarget = [ FDirName ../../.. $(installDir:G=) $(name) ] ;
535
536		AddSymlinkToContainer $(container)
537			: $(systemDirTokens) add-ons kernel boot
538			: $(linkTarget) : $(name) ;
539	}
540}
541
542
543rule AddLibrariesToContainer container : directory : libs
544{
545	# AddLibrariesToContainer <container> : <directory> : <libs>
546	#
547	# Installs libraries with the appropriate links into the container.
548	#
549
550	local lib ;
551	for lib in $(libs) {
552		local abiVersion = [ on $(lib) return $(HAIKU_LIB_ABI_VERSION) ] ;
553		if $(abiVersion) {
554			local abiVersionedLib = $(lib).$(abiVersion) ;
555			AddFilesToContainer $(container) : $(directory) : $(lib)
556				: $(abiVersionedLib) ;
557			AddSymlinkToContainer $(container) : $(directory)
558				: $(abiVersionedLib) : $(lib) ;
559		} else {
560			AddFilesToContainer $(container) : $(directory) : $(lib) ;
561		}
562	}
563}
564
565
566rule CreateContainerMakeDirectoriesScript container : script
567{
568	MakeLocate $(script) : $(HAIKU_OUTPUT_DIR) ;
569	Always $(script) ;
570
571	local initScript = [ InitScript $(script) ] ;
572
573	local scriptBody
574		= [ FSameTargetWithPrependedGrist $(script) : script-body ] ;
575	LOCATE on $(scriptBody) = [ on $(script) return $(LOCATE) ] ;
576	Depends $(scriptBody) : $(initScript) ;
577	Depends $(script) : $(scriptBody) ;
578
579	# collect the directories to create
580	local dirsToCreate ;
581	local directories
582		= [ on $(container) return $(HAIKU_INSTALL_DIRECTORIES) ] ;
583	local dir ;
584	for dir in $(directories) {
585		if ! [ on $(dir) return $(DONT_CREATE) ] {
586			dirsToCreate += $(dir) ;
587		}
588	}
589
590	# If the image shall only be updated, we don't create directories.
591	if $(dirsToCreate)
592		&& ( ! [ on $(container) return $(HAIKU_CONTAINER_UPDATE_ONLY) ]
593			|| [ IncludeAllTargetsInContainer $(container) ]
594			|| [ on $(container) return
595				$(HAIKU_CONTAINER_ALWAYS_CREATE_DIRECTORIES) ] ) {
596		Depends $(scriptBody) : $(dirsToCreate) ;
597		CreateContainerMakeDirectoriesScript1 $(scriptBody) : $(dirsToCreate) ;
598
599		local serializationDependency = $(scriptBody) ;
600			# Used to create a dependency chain between the dummy targets.
601			# This forces jam to build them one after the other, thus preventing
602			# concurrent writes to the script file when building with multiple
603			# jobs.
604
605		# For directories with attributes, we convert those the specified
606		# resource files to files with attributes and add commands to the script
607		# adding the attributes to the directories.
608		for dir in $(directories) {
609			local resourceFiles = [ on $(dir) return $(ATTRIBUTE_FILES) ] ;
610			if $(resourceFiles) {
611				local dirTokens = [ on $(dir) return $(DIRECTORY_TOKENS) ] ;
612
613				# translate resources file to file with attributes
614				local attributeFile = $(script)-attributes-$(dirTokens:J=-) ;
615				ResAttr $(attributeFile) : $(resourceFiles) ;
616
617				# use a unique dummy target for this file, on which we
618				# can define the TARGET_DIR variable
619				local dummyTarget = $(script)-attributes-dummy-$(dir:G=) ;
620				NotFile $(dummyTarget) ;
621				TARGET_DIR on $(dummyTarget) = $(dir:G=) ;
622
623				Depends $(dummyTarget) : $(initScript) $(attributeFile)
624					$(serializationDependency) ;
625				Depends $(script) : $(dummyTarget) ;
626				serializationDependency = $(dummyTarget) ;
627
628				AppendToContainerMakeDirectoriesScriptAttributes $(dummyTarget)
629					: $(initScript) $(attributeFile) ;
630			}
631		}
632	}
633}
634
635actions piecemeal CreateContainerMakeDirectoriesScript1
636{
637	echo \$mkdir -p "\"\${tPrefix}$(2:G=)\"" >> $(1)
638}
639
640actions AppendToContainerMakeDirectoriesScriptAttributes
641{
642	echo \$copyAttrs "\"\${sPrefix}$(2[2])\"" \
643		"\"\${tPrefix}$(TARGET_DIR)\"" >> $(2[1])
644}
645
646rule CreateContainerCopyFilesScript container : script
647{
648	MakeLocate $(script) : $(HAIKU_OUTPUT_DIR) ;
649	Always $(script) ;
650
651	local initScript = [ InitScript $(script) ] ;
652
653	local scriptBody
654		= [ FSameTargetWithPrependedGrist $(script) : script-body ] ;
655	LOCATE on $(scriptBody) = [ on $(script) return $(LOCATE) ] ;
656	Depends $(scriptBody) : $(initScript) ;
657	Depends $(script) : $(scriptBody) ;
658
659	local serializationDependency = $(scriptBody) ;
660		# Used to create a dependency chain between the dummy targets.
661		# This forces jam to build them one after the other, thus preventing
662		# concurrent writes to the script file when building with multiple
663		# jobs.
664
665	local dir ;
666	for dir in [ on $(container) return $(HAIKU_INSTALL_DIRECTORIES) ] {
667		# filter the targets that shall be renamed; they have to be copied
668		# individually
669		local destTargets = [ on $(dir) return $(TARGETS_TO_INSTALL) ] ;
670		local remainingTargets ;
671		local destTarget ;
672		for destTarget in $(destTargets) {
673			local target = [ on $(destTarget) return $(TARGET) ] ;
674			local name = $(destTarget:BS) ;
675			if $(name) != $(target:BS) {
676				# use a unique dummy target for this file, on which we
677				# can define the TARGET_DIR variable
678				local dummyTarget = $(script)-dummy-$(dir:G=)-$(target) ;
679				NotFile $(dummyTarget) ;
680				TARGET_DIR on $(dummyTarget) = $(dir:G=) ;
681				INSTALL_TARGET_NAME on $(dummyTarget) = $(name) ;
682
683				Depends $(dummyTarget) : $(initScript) $(target)
684					$(serializationDependency) ;
685				Depends $(script) : $(dummyTarget) ;
686				serializationDependency = $(dummyTarget) ;
687
688				AppendToContainerCopyFilesScriptSingleFile $(dummyTarget)
689					: $(initScript) $(target) ;
690			} else {
691				remainingTargets += $(target) ;
692			}
693		}
694		targets = $(remainingTargets) ;
695
696		if $(targets) {
697			# use a unique dummy target for this directory, on which we
698			# can define the TARGET_DIR variable
699			local dummyTarget = $(script)-dummy-$(dir:G=) ;
700			NotFile $(dummyTarget) ;
701			TARGET_DIR on $(dummyTarget) = $(dir:G=) ;
702
703			Depends $(dummyTarget) : $(initScript) $(targets)
704				$(serializationDependency) ;
705			Depends $(script) : $(dummyTarget) ;
706			serializationDependency = $(dummyTarget) ;
707
708			OUTPUT_SCRIPT on $(dummyTarget) = $(initScript) ;
709			AppendToContainerCopyFilesScript $(dummyTarget) : $(targets) ;
710		}
711
712		local symlinks = [ on $(dir) return $(SYMLINKS_TO_INSTALL) ] ;
713		local symlink ;
714		for symlink in $(symlinks) {
715			NotFile $(symlink) ;
716
717			Depends $(script) : $(symlink) ;
718			Depends $(symlink) : $(initScript) $(serializationDependency) ;
719			serializationDependency = $(symlink) ;
720
721			AddSymlinkToContainerCopyFilesScript $(symlink) : $(initScript) ;
722		}
723
724		local targetDirs = [ on $(dir) return $(DIRECTORIES_TO_INSTALL) ] ;
725		local targetDir ;
726		for targetDir in $(targetDirs) {
727			NotFile $(targetDir) ;
728
729			Depends $(script) : $(targetDir) ;
730			Depends $(targetDir) : $(initScript) $(serializationDependency) ;
731			serializationDependency = $(targetDir) ;
732
733			AddDirectoryToContainerCopyFilesScript $(targetDir)
734				: $(initScript) ;
735		}
736	}
737}
738
739
740actions piecemeal AppendToContainerCopyFilesScript bind OUTPUT_SCRIPT
741{
742	echo \$cp "\"\${sPrefix}$(2)\"" "\"\${tPrefix}$(TARGET_DIR)\"" \
743		>> $(OUTPUT_SCRIPT)
744}
745
746
747actions AppendToContainerCopyFilesScriptSingleFile
748{
749	echo \$cp "\"\${sPrefix}$(2[2])\"" \
750		"\"\${tPrefix}$(TARGET_DIR)/$(INSTALL_TARGET_NAME)\"" >> $(2[1])
751}
752
753
754actions AddSymlinkToContainerCopyFilesScript
755{
756	echo \$ln -sfn "\"$(SYMLINK_TARGET)\"" "\"\${tPrefix}$(1:G=)\"" >> $(2[1])
757}
758
759
760actions AddDirectoryToContainerCopyFilesScript bind SOURCE_DIRECTORY
761{
762	echo \$cp -r $(EXCLUDE_PATTERNS) "\"\${sPrefix}$(SOURCE_DIRECTORY)/.\"" \
763		"\"\${tPrefix}$(TARGET_DIRECTORY:G=)\"" >> $(2[1])
764}
765
766
767rule CreateContainerExtractFilesScript container : script
768{
769	MakeLocate $(script) : $(HAIKU_OUTPUT_DIR) ;
770	Always $(script) ;
771
772	local initScript = [ InitScript $(script) ] ;
773
774	local scriptBody
775		= [ FSameTargetWithPrependedGrist $(script) : script-body ] ;
776	LOCATE on $(scriptBody) = [ on $(script) return $(LOCATE) ] ;
777	Depends $(scriptBody) : $(initScript) ;
778	Depends $(script) : $(scriptBody) ;
779
780	local serializationDependency = $(scriptBody) ;
781		# Used to create a dependency chain between the dummy targets.
782		# This forces jam to build them one after the other, thus preventing
783		# concurrent writes to the script file when building with multiple
784		# jobs.
785
786	local dir ;
787	for dir in [ on $(container) return $(HAIKU_INSTALL_DIRECTORIES) ] {
788		local archiveFiles = [ on $(dir) return $(ARCHIVE_FILES_TO_INSTALL) ] ;
789		local archiveFile ;
790		for archiveFile in $(archiveFiles) {
791			# use a unique dummy target for this file, on which we
792			# can define the TARGET_DIR variable
793			local dummyTarget = $(script)-dummy-$(dir:G=)-$(archiveFile) ;
794			NotFile $(dummyTarget) ;
795			TARGET_DIR on $(dummyTarget) = $(dir:G=) ;
796
797			local extractedSubDir = [ on $(archiveFile)
798				return $(ARCHIVE_SUBDIR_TO_INSTALL_FROM) ] ;
799			ARCHIVE_SUBDIR_TO_INSTALL_FROM on $(dummyTarget) =
800				$(extractedSubDir:E=.) ;
801
802			local stripDebugSymbols = [ on $(archiveFile)
803				return $(ARCHIVE_FILES_TO_STRIP) ] ;
804			STRIP_DEBUG_SYMBOLS_FROM_ARCHIVE on $(dummyTarget) =
805				$(stripDebugSymbols) ;
806
807			Depends $(dummyTarget) : $(initScript) $(archiveFile)
808				$(serializationDependency) ;
809			Depends $(script) : $(dummyTarget) ;
810			serializationDependency = $(dummyTarget) ;
811
812			AddExtractFileToContainerExtractFilesScript $(dummyTarget)
813				: $(initScript) $(archiveFile) ;
814		}
815	}
816}
817
818
819actions AddExtractFileToContainerExtractFilesScript
820{
821	echo extractFile "\"$(2[2])\"" "\"$(TARGET_DIR)\"" \
822		"\"$(ARCHIVE_SUBDIR_TO_INSTALL_FROM)\"" \
823		"\"$(STRIP_DEBUG_SYMBOLS_FROM_ARCHIVE)\"" >> $(2[1])
824}
825
826
827#pragma mark - Haiku Image rules
828
829rule SetUpdateHaikuImageOnly flag
830{
831	HAIKU_CONTAINER_UPDATE_ONLY on $(HAIKU_IMAGE_CONTAINER_NAME) = $(flag) ;
832}
833
834rule IsUpdateHaikuImageOnly
835{
836	on $(HAIKU_IMAGE_CONTAINER_NAME) return $(HAIKU_CONTAINER_UPDATE_ONLY) ;
837}
838
839rule AddDirectoryToHaikuImage directoryTokens : attributeFiles
840{
841	# AddDirectoryToHaikuImage <directoryTokens> : <attributeFiles>
842
843	return [ AddDirectoryToContainer $(HAIKU_IMAGE_CONTAINER_NAME)
844		: $(directoryTokens) : $(attributeFiles) ] ;
845}
846
847rule AddFilesToHaikuImage directory : targets : destName
848{
849	# AddFilesToHaikuImage <directory> : <targets> [ : dest name ]
850
851	AddFilesToContainer $(HAIKU_IMAGE_CONTAINER_NAME) : $(directory)
852		: $(targets) : $(destName) ;
853}
854
855rule FFilesInHaikuImageDirectory directoryTokens
856{
857	return [ FFilesInContainerDirectory $(HAIKU_IMAGE_CONTAINER_NAME)
858		: $(directoryTokens) ] ;
859}
860
861rule AddSymlinkToHaikuImage directoryTokens : linkTarget : linkName
862{
863	# AddSymlinkToHaikuImage <directory> : <link target> [ : <link name> ] ;
864
865	linkTarget = $(linkTarget:J=/) ;
866
867	AddSymlinkToContainer $(HAIKU_IMAGE_CONTAINER_NAME) : $(directoryTokens)
868		: $(linkTarget) : $(linkName) ;
869}
870
871rule FSymlinksInHaikuImageDirectory directoryTokens
872{
873	return [ FSymlinksInContainerDirectory $(HAIKU_IMAGE_CONTAINER_NAME)
874		: $(directoryTokens) ] ;
875}
876
877rule CopyDirectoryToHaikuImage directoryTokens : sourceDirectory
878	: targetDirectoryName : excludePatterns : flags
879{
880	CopyDirectoryToContainer $(HAIKU_IMAGE_CONTAINER_NAME) : $(directoryTokens)
881		: $(sourceDirectory) : $(targetDirectoryName) : $(excludePatterns)
882		: $(flags) ;
883}
884
885rule AddSourceDirectoryToHaikuImage dirTokens : flags
886{
887	# AddSourceDirectoryToHaikuImage <dirTokens> : <flags> ;
888
889	CopyDirectoryToHaikuImage home HaikuSources
890		: [ FDirName $(HAIKU_TOP) $(dirTokens) ]
891		: : : $(flags) ;
892}
893
894rule AddHeaderDirectoryToHaikuImage dirTokens : dirName : flags
895{
896	# AddHeaderDirectoryToHaikuImage <dirTokens> : [ <dirName> ]
897	#	: <flags> ;
898
899	AddHeaderDirectoryToContainer $(HAIKU_IMAGE_CONTAINER_NAME) : $(dirTokens)
900		: $(dirName) : $(flags) ;
901}
902
903rule AddWifiFirmwareToHaikuImage driver : package : archive : extract
904{
905	# AddWifiFirmwareToHaikuImage <driver> : <package> : <archive> : <extract>
906
907	AddWifiFirmwareToHaikuImage $(HAIKU_IMAGE_CONTAINER_NAME) : $(driver)
908		: $(package) : $(archive) : $(extract) ;
909}
910
911rule ExtractArchiveToHaikuImage dirTokens : archiveFile : flags
912	: extractedSubDir
913{
914	# ExtractArchiveToHaikuImage <dirTokens> : <archiveFile> : <flags>
915	#	: <extractedSubDir> ;
916
917	ExtractArchiveToContainer $(HAIKU_IMAGE_CONTAINER_NAME) : $(dirTokens)
918		: $(archiveFile) : $(flags) : $(extractedSubDir) ;
919}
920
921rule AddDriversToHaikuImage relativeDirectoryTokens : targets
922{
923	# AddDriversToHaikuImage <relative directory> : <targets> ;
924
925	AddDriversToContainer $(HAIKU_IMAGE_CONTAINER_NAME)
926		: $(relativeDirectoryTokens) : $(targets) ;
927}
928
929rule AddNewDriversToHaikuImage relativeDirectoryTokens : targets
930{
931	# AddNewDriversToHaikuImage <relative directory> : <targets> ;
932
933	AddNewDriversToContainer $(HAIKU_IMAGE_CONTAINER_NAME)
934		: $(relativeDirectoryTokens) : $(targets) ;
935}
936
937rule AddBootModuleSymlinksToHaikuImage targets
938{
939	# AddBootModuleSymlinksToHaikuImage <targets> ;
940
941	AddBootModuleSymlinksToContainer $(HAIKU_IMAGE_CONTAINER_NAME)
942		: $(targets) ;
943}
944
945rule AddPackageFilesToHaikuImage location : packages
946{
947	HAIKU_PACKAGES_IN_IMAGE on $(HAIKU_IMAGE_CONTAINER_NAME)
948		= [ on $(HAIKU_IMAGE_CONTAINER_NAME) return $(HAIKU_PACKAGES_IN_IMAGE) ]
949			$(packages) ;
950	HAIKU_PACKAGE_INSTALLATION_LOCATION on $(packages) = $(location) ;
951	AddFilesToHaikuImage $(location) packages : $(packages) ;
952}
953
954rule AddOptionalHaikuImagePackages packages
955{
956	local package ;
957	for package in $(packages) {
958		if ! [ on $(package) return $(HAIKU_OPTIONAL_PACKAGE_ADDED) ] {
959			HAIKU_OPTIONAL_PACKAGE_ADDED on $(package) = 1 ;
960			HAIKU_ADDED_OPTIONAL_PACKAGES += $(package) ;
961		}
962		local dependencies = [ on $(package)
963			return $(HAIKU_OPTIONAL_PACKAGE_DEPENDENCIES) ] ;
964		AddOptionalHaikuImagePackages $(dependencies) ;
965	}
966}
967
968rule SuppressOptionalHaikuImagePackages packages
969{
970	local package ;
971	for package in $(packages) {
972		if ! [ on $(package) return $(HAIKU_OPTIONAL_PACKAGE_SUPPRESSED) ] {
973			HAIKU_OPTIONAL_PACKAGE_SUPPRESSED on $(package) = 1 ;
974		}
975	}
976}
977
978rule IsOptionalHaikuImagePackageAdded package
979{
980	if ! [ on $(package) return $(HAIKU_OPTIONAL_PACKAGE_EXISTS) ] {
981		HAIKU_OPTIONAL_PACKAGE_EXISTS on $(package) = 1 ;
982		HAIKU_EXISTING_OPTIONAL_PACKAGES += $(package) ;
983	}
984
985	if [ on $(package) return $(HAIKU_OPTIONAL_PACKAGE_ADDED) ] &&
986			! [ on $(package) return $(HAIKU_OPTIONAL_PACKAGE_SUPPRESSED) ] {
987		return 1 ;
988	}
989
990	return ;
991}
992
993rule OptionalPackageDependencies package : dependencies
994{
995	HAIKU_OPTIONAL_PACKAGE_DEPENDENCIES on $(package) = $(dependencies) ;
996	if [ on $(package) return $(HAIKU_OPTIONAL_PACKAGE_ADDED) ] {
997		AddOptionalHaikuImagePackages $(dependencies) ;
998	}
999}
1000
1001
1002rule AddHaikuImagePackages packages : installationLocation
1003{
1004	# AddHaikuImagePackages <packages> [ : <installationLocation> ] ;
1005	 # Adds the given package <packages>s to the image. <installationLocation>
1006	 # is either "system" or "common" (the default) and specifies where the
1007	 # packages shall be installed.
1008
1009	installationLocation ?= common ;
1010
1011	local package ;
1012	for package in $(packages) {
1013		if ! [ on $(package) return $(HAIKU_PACKAGE_ADDED) ] {
1014			if ! [ IsPackageAvailable $(package) ] {
1015				Echo "AddHaikuImagePackages: package" $(package)
1016					"not available!" ;
1017				continue ;
1018			}
1019
1020			HAIKU_PACKAGE_ADDED on $(package) = 1 ;
1021			HAIKU_ADDED_PACKAGES += $(package) ;
1022
1023			# download the package file and add it to the image
1024			local file = [ FetchPackage $(package) ] ;
1025
1026			if $(HAIKU_UPDATE_ALL_PACKAGES) {
1027				HAIKU_INCLUDE_IN_IMAGE on $(file) = 1 ;
1028			}
1029
1030			AddPackageFilesToHaikuImage $(installationLocation) : $(file) ;
1031		}
1032	}
1033}
1034
1035
1036rule IsHaikuImagePackageAdded package
1037{
1038	if [ on $(package) return $(HAIKU_PACKAGE_ADDED) ] {
1039		return 1 ;
1040	}
1041
1042	return ;
1043}
1044
1045
1046rule InstallSourceArchive file : url
1047{
1048	if $(HAIKU_INCLUDE_SOURCES) = 1 {
1049		# download archive file
1050		local archiveFile = [ DownloadFile $(file) : $(url) ] ;
1051
1052		# copy directly into image
1053		AddFilesToHaikuImage _sources_ : $(archiveFile) ;
1054	}
1055}
1056
1057rule InstallOptionalHaikuImagePackage url : dirTokens : flags
1058{
1059	# TODO: Remove the non-hpkg cases!
1060	# Currently the semantics differs depending on whether the  cdPackage flag
1061	# has been specified and the type of the package file:
1062	# * For a hpkg <dirTokens> is "common" (default) or "system" and specifies
1063	#   the installation location of the package (will be copied to the
1064	#   packages/ subdirectory).
1065	# * For a regular archive and cdPackage, <dirTokens> is ignored and
1066	#   the package will be copied to the _package_ directory of the CD image.
1067	# * For a regular archive and without cdPackage, <dirTokens> specifies the
1068	#   directory relative to the image's root directory where the content of
1069	#   the archive will be extracted to.
1070	#
1071	# Supported flags: cdPackage, stripDebugSymbols/!stripDebugSymbols
1072
1073	local package = $(url:BS) ;
1074
1075	# download archive file
1076	local archiveFile = [ DownloadFile $(package) : $(url) ] ;
1077
1078	if $(package:S) = .hpkg {
1079		local location = $(dirTokens:E=common) ;
1080		if $(HAIKU_UPDATE_ALL_PACKAGES) {
1081			HAIKU_INCLUDE_IN_IMAGE on $(archiveFile) = 1 ;
1082		}
1083		AddPackageFilesToHaikuImage $(location) : $(archiveFile) ;
1084	} else if cdPackage in $(flags) && $(HAIKU_CD_NAME) {
1085		# TODO: If HAIKU_CD_NAME is set, that doesn't mean we're building a CD
1086		# image!
1087		# copy onto image
1088		AddFilesToHaikuImage _packages_ : $(archiveFile) ;
1089	} else {
1090		if $(HAIKU_STRIP_DEBUG_FROM_OPTIONAL_PACKAGES) = 1
1091			&& ! stripDebugSymbols in $(flags)
1092			&& ! !stripDebugSymbols in $(flags) {
1093			flags += stripDebugSymbols ;
1094		}
1095		# extract onto image
1096		ExtractArchiveToHaikuImage $(dirTokens) : $(archiveFile) : $(flags) ;
1097	}
1098}
1099
1100rule AddEntryToHaikuImageUserGroupFile file : entry
1101{
1102	local allEntries = [ on $(file) return $(HAIKU_IMAGE_USER_GROUP_ENTRIES) ] ;
1103
1104	if $(allEntries) {
1105		allEntries = $(allEntries)|$(entry) ;
1106	} else {
1107		allEntries = $(entry) ;
1108
1109		Always $(file) ;
1110		MakeLocate $(file) : $(HAIKU_COMMON_PLATFORM_OBJECT_DIR) ;
1111		BuildHaikuImageUserGroupFile $(file) ;
1112		AddFilesToHaikuImage common settings etc : $(file) ;
1113	}
1114
1115	HAIKU_IMAGE_USER_GROUP_ENTRIES on $(file) = $(allEntries) ;
1116}
1117
1118actions BuildHaikuImageUserGroupFile
1119{
1120	echo "$(HAIKU_IMAGE_USER_GROUP_ENTRIES)" | tr '|' '\n' > $(1)
1121}
1122
1123rule AddUserToHaikuImage user : uid : gid : home : shell : realName
1124{
1125	if ! $(user) || ! $(uid) || ! $(gid) || ! $(home) {
1126		Exit "Invalid haiku user specification passed to AddUserToHaikuImage." ;
1127	}
1128
1129	local entry
1130		= $(user):x:$(uid):$(gid):$(realName:E=$(user)):$(home):$(shell:E="") ;
1131
1132	AddEntryToHaikuImageUserGroupFile <haiku-image>passwd : $(entry) ;
1133}
1134
1135rule AddGroupToHaikuImage group : gid : members
1136{
1137	if ! $(group) || ! $(gid) {
1138		Exit "Invalid haiku group specification passed to"
1139			"AddGroupToHaikuImage." ;
1140	}
1141
1142	local entry = $(group):x:$(gid):$(members:J=,:E) ;
1143
1144	AddEntryToHaikuImageUserGroupFile <haiku-image>group : $(entry) ;
1145}
1146
1147
1148rule AddLibrariesToHaikuImage directory : libs
1149{
1150	# AddLibraryToHaikuImage <directory> : <libs>
1151	#
1152	# Installs libraries with the appropriate links onto the image.
1153	#
1154
1155	AddLibrariesToContainer $(HAIKU_IMAGE_CONTAINER_NAME) : $(directory)
1156		: $(libs) ;
1157}
1158
1159
1160rule CreateHaikuImageMakeDirectoriesScript script
1161{
1162	CreateContainerMakeDirectoriesScript $(HAIKU_IMAGE_CONTAINER_NAME)
1163		: $(script) ;
1164}
1165
1166rule CreateHaikuImageCopyFilesScript script
1167{
1168	CreateContainerCopyFilesScript $(HAIKU_IMAGE_CONTAINER_NAME) : $(script) ;
1169}
1170
1171rule CreateHaikuImageExtractFilesScript script
1172{
1173	CreateContainerExtractFilesScript $(HAIKU_IMAGE_CONTAINER_NAME)
1174		: $(script) ;
1175}
1176
1177rule BuildHaikuImage haikuImage : scripts : isImage : isVMwareImage
1178{
1179	# BuildHaikuImage <haiku image> : <scripts> : <is image> : <isVMwareImage> ;
1180
1181	if $(isImage) = 1 || $(isImage) = true {
1182		IS_IMAGE on $(haikuImage) = 1 ;
1183	} else {
1184		IS_IMAGE on $(haikuImage) = "" ;
1185	}
1186
1187	if $(isVMwareImage) = 1 || $(isVMwareImage) = true {
1188		IS_VMWARE_IMAGE on $(haikuImage) = 1 ;
1189	} else {
1190		IS_VMWARE_IMAGE on $(haikuImage) = "" ;
1191	}
1192
1193	local mainScript = build_haiku_image ;
1194	SEARCH on $(mainScript) = [ FDirName $(HAIKU_TOP) build scripts ] ;
1195
1196	Depends $(haikuImage) : $(mainScript) $(scripts) ;
1197	BuildHaikuImage1 $(haikuImage) : $(mainScript) $(scripts) ;
1198}
1199
1200actions BuildHaikuImage1
1201{
1202	export imagePath="$(1)"
1203	export isImage="$(IS_IMAGE)"
1204	export isVMwareImage="$(IS_VMWARE_IMAGE)"
1205	$(2[1]) $(2[2-])
1206}
1207
1208rule BuildVMWareImage vmwareImage : plainImage : imageSize
1209{
1210	# BuildVMWareImage <vmware image> : <plain image> : <image size in MB>
1211
1212	IMAGE_SIZE on $(vmwareImage) = $(imageSize) ;
1213
1214	Depends $(vmwareImage) : <build>vmdkheader $(plainImage) ;
1215	BuildVMWareImage1 $(vmwareImage) : <build>vmdkheader $(plainImage) ;
1216}
1217
1218actions BuildVMWareImage1
1219{
1220	$(RM) $(1)
1221	$(2[1]) -h 64k -i$(IMAGE_SIZE)M $(1) &&
1222	cat $(2[2]) >> $(1)
1223}
1224
1225
1226#pragma mark - Network Boot Archive rules
1227
1228rule AddDirectoryToNetBootArchive directoryTokens
1229{
1230	# AddDirectoryToNetBootArchive <directoryTokens>
1231
1232	return [ AddDirectoryToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1233		: $(directoryTokens) ] ;
1234}
1235
1236rule AddFilesToNetBootArchive directory : targets : destName
1237{
1238	# AddFilesToNetBootArchive <directory> : <targets> [ : dest name ]
1239
1240	AddFilesToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME) : $(directory)
1241		: $(targets) : $(destName) ;
1242}
1243
1244rule AddSymlinkToNetBootArchive directoryTokens : linkTarget : linkName
1245{
1246	# AddSymlinkToNetBootArchive <directory> : <link target> [ : <link name> ] ;
1247
1248	AddSymlinkToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1249		: $(directoryTokens) : $(linkTarget) : $(linkName) ;
1250}
1251
1252rule AddDriversToNetBootArchive relativeDirectoryTokens : targets
1253{
1254	# AddDriversToNetBootArchive <relative directory> : <targets> ;
1255
1256	AddDriversToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1257		: $(relativeDirectoryTokens) : $(targets) ;
1258}
1259
1260rule AddNewDriversToNetBootArchive relativeDirectoryTokens : targets
1261{
1262	# AddNewDriversToNetBootArchive <relative directory> : <targets> ;
1263
1264	AddNewDriversToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1265		: $(relativeDirectoryTokens) : $(targets) ;
1266}
1267
1268rule AddDriverRegistrationToNetBootArchive relativeDirectoryTokens : target
1269	: links
1270{
1271	# AddDriverRegistrationToNetBootArchive <directory> : <link target>
1272	#	: <link names> ] ;
1273
1274	AddDriverRegistrationToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1275		: $(relativeDirectoryTokens) : $(target) : $(links) ;
1276}
1277
1278rule AddBootModuleSymlinksToNetBootArchive targets
1279{
1280	# AddBootModuleSymlinksToNetBootArchive <targets> ;
1281
1282	AddBootModuleSymlinksToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1283		: $(targets) ;
1284}
1285
1286rule CreateNetBootArchiveMakeDirectoriesScript script
1287{
1288	CreateContainerMakeDirectoriesScript
1289		$(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME) : $(script) ;
1290}
1291
1292rule CreateNetBootArchiveCopyFilesScript script
1293{
1294	CreateContainerCopyFilesScript $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1295		: $(script) ;
1296}
1297
1298rule BuildNetBootArchive archive : scripts
1299{
1300	# BuildNetBootArchive <archive> : <scripts>  ;
1301
1302	local mainScript = build_archive ;
1303	SEARCH on $(mainScript) = [ FDirName $(HAIKU_TOP) build scripts ] ;
1304
1305	Depends $(archive) : $(mainScript) $(scripts) ;
1306	BuildNetBootArchive1 $(archive) : $(mainScript) $(scripts) ;
1307}
1308
1309actions BuildNetBootArchive1
1310{
1311	$(2[1]) $(1) $(2[2-])
1312}
1313
1314
1315#pragma mark - Floppy Boot Archive rules
1316
1317
1318rule AddDirectoryToFloppyBootArchive directoryTokens
1319{
1320	# AddDirectoryToFloppyBootArchive <directoryTokens>
1321
1322	return [ AddDirectoryToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1323		: $(directoryTokens) ] ;
1324}
1325
1326rule AddFilesToFloppyBootArchive directory : targets : destName
1327{
1328	# AddFilesToFloppyBootArchive <directory> : <targets> [ : dest name ]
1329
1330	AddFilesToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME) : $(directory)
1331		: $(targets) : $(destName) ;
1332}
1333
1334rule AddSymlinkToFloppyBootArchive directoryTokens : linkTarget : linkName
1335{
1336	# AddSymlinkToFloppyBootArchive <directory> : <link target>
1337	#	[ : <link name> ] ;
1338
1339	AddSymlinkToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1340		: $(directoryTokens) : $(linkTarget) : $(linkName) ;
1341}
1342
1343rule AddDriversToFloppyBootArchive relativeDirectoryTokens : targets
1344{
1345	# AddDriversToFloppyBootArchive <relative directory> : <targets> ;
1346
1347	AddDriversToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1348		: $(relativeDirectoryTokens) : $(targets) ;
1349}
1350
1351rule AddNewDriversToFloppyBootArchive relativeDirectoryTokens : targets
1352{
1353	# AddNewDriversToFloppyBootArchive <relative directory> : <targets> ;
1354
1355	AddNewDriversToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1356		: $(relativeDirectoryTokens) : $(targets) ;
1357}
1358
1359rule AddDriverRegistrationToFloppyBootArchive relativeDirectoryTokens : target
1360	: links
1361{
1362	# AddDriverRegistrationToFloppyBootArchive <directory> : <link target>
1363	#	: <link names> ] ;
1364
1365	AddDriverRegistrationToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1366		: $(relativeDirectoryTokens) : $(target) : $(links) ;
1367}
1368
1369rule AddBootModuleSymlinksToFloppyBootArchive targets
1370{
1371	# AddBootModuleSymlinksToFloppyBootArchive <targets> ;
1372
1373	AddBootModuleSymlinksToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1374		: $(targets) ;
1375}
1376
1377rule CreateFloppyBootArchiveMakeDirectoriesScript script
1378{
1379	CreateContainerMakeDirectoriesScript
1380		$(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME) : $(script) ;
1381}
1382
1383rule CreateFloppyBootArchiveCopyFilesScript script
1384{
1385	CreateContainerCopyFilesScript $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1386		: $(script) ;
1387}
1388
1389rule BuildFloppyBootArchive archive : scripts
1390{
1391	# BuildHFloppyBootArchive <archive> : <scripts>  ;
1392
1393	local mainScript = build_archive ;
1394	SEARCH on $(mainScript) = [ FDirName $(HAIKU_TOP) build scripts ] ;
1395
1396	Depends $(archive) : $(mainScript) $(scripts) ;
1397	BuildFloppyBootArchive1 $(archive) : $(mainScript) $(scripts) ;
1398}
1399
1400actions BuildFloppyBootArchive1
1401{
1402	$(2[1]) $(1) $(2[2-])
1403}
1404
1405# warning: that is quite x86 dependant...
1406
1407rule BuildFloppyBootImage image : haikuLoader : archive
1408{
1409	Depends $(image) : $(haikuLoader) ;
1410	Depends $(image) : $(archive) ;
1411	#MakeLocateDebug $(image) ;
1412	FLOPPY_IMAGE_SIZE on $(image) = $(HAIKU_BOOT_FLOPPY_IMAGE_SIZE) ;
1413	ARCHIVE_IMAGE_OFFSET on $(image) = $(HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET) ;
1414	BuildFloppyBootImage1 $(image) : $(haikuLoader) $(archive) ;
1415	if $(HAIKU_BOOT_PLATFORM) = atari_m68k {
1416		Depends $(image) : <build>fixup_tos_boot_checksum ;
1417		BuildFloppyBootImageFixupM68K $(image)
1418			: <build>fixup_tos_boot_checksum ;
1419	}
1420	if $(HAIKU_BOOT_PLATFORM) = amiga_m68k {
1421		Depends $(image) : <build>fixup_amiga_boot_checksum ;
1422		BuildFloppyBootImageFixupM68K $(image)
1423			: <build>fixup_amiga_boot_checksum ;
1424	}
1425}
1426
1427actions BuildFloppyBootImage1
1428{
1429	haiku_loader_size=`stat -c %s "$(>[1])"`
1430	if [ $? -ne 0 ] ; then
1431		# FreeBSD's stat command don't support -c/--format option
1432		# and use %z specifier for file size
1433		haiku_loader_size=`stat -f %z "$(>[1])"`
1434	fi
1435	archive_image_offset=`echo "$(ARCHIVE_IMAGE_OFFSET) * 1024" | bc`
1436	if [ $haiku_loader_size -gt $archive_image_offset ] ; then
1437		echo "Error: $(>[1]) is too big ($haiku_loader_size) to fit "
1438		echo "       before the boot archive starting at $archive_image_offset!"
1439		exit 1
1440	fi
1441	$(RM) $(<)
1442	# make an empty image
1443	dd if=/dev/zero of=$(<) bs=1k count=$(FLOPPY_IMAGE_SIZE)
1444	# add haiku_loader
1445	dd if=$(>[1]) of=$(<) conv=notrunc
1446	# add the boot drivers tgz archive
1447	dd if=$(>[2]) of=$(<) bs=$(ARCHIVE_IMAGE_OFFSET)k seek=1 conv=notrunc
1448}
1449
1450actions BuildFloppyBootImageFixupM68K
1451{
1452	# fixup the boot sector checksum
1453	$(>[1]) $(<)
1454}
1455
1456#pragma mark - CD Boot Image rules
1457
1458rule BuildCDBootImage image : bootfloppy : extrafiles
1459{
1460	Depends $(image) : $(bootfloppy) ;
1461	Depends $(image) : $(extrafiles) ;
1462	BOOTIMG on $(image) = $(bootfloppy) ;
1463
1464	BuildCDBootImage1 $(image) : $(bootfloppy) $(extrafiles) ;
1465}
1466
1467actions BuildCDBootImage1
1468{
1469	$(RM) $(<)
1470	mkisofs -b $(BOOTIMG) -r -J -V bootimg -o $(<) $(>[1]) $(>[2-])
1471}
1472
1473
1474#pragma mark - CD Boot PPC Image rules
1475
1476rule BuildCDBootPPCImage image : hfsmaps : elfloader : coffloader : chrpscript
1477	: extrafiles
1478{
1479	Depends $(image) : $(elfloader) ;
1480	Depends $(image) : $(coffloader) ;
1481	Depends $(image) : $(chrpscript) ;
1482	Depends $(image) : $(extrafiles) ;
1483	Depends $(image) : $(hfsmaps) ;
1484	MAPS on $(image) = $(hfsmaps) ;
1485
1486	BuildCDBootPPCImage1 $(image) : $(elfloader) $(coffloader) $(chrpscript)
1487		$(extrafiles) ;
1488}
1489
1490actions BuildCDBootPPCImage1 bind MAPS
1491{
1492	$(RM) $(<)
1493	mkdir -p $(HAIKU_OUTPUT_DIR)/cd/ppc
1494	mkdir -p $(HAIKU_OUTPUT_DIR)/cd/boot
1495	# CHRP Boot script
1496	cp $(>[3]) $(HAIKU_OUTPUT_DIR)/cd/ppc/bootinfo.txt
1497	cp $(>[3]) $(HAIKU_OUTPUT_DIR)/cd/boot/boot.chrp
1498	# Haiku Bootloaders
1499	cp $(>[2]) $(HAIKU_OUTPUT_DIR)/cd/boot/haikuloader.xcf
1500	cp $(>[1]) $(HAIKU_OUTPUT_DIR)/cd/boot/haikuloader.elf
1501	# Extras (readme files, etc)
1502	cp $(>[4]) $(HAIKU_OUTPUT_DIR)/cd/
1503
1504	mkisofs -v -hfs -part -map $(MAPS) -no-desktop -hfs-volid bootimg \
1505		-V bootimg -hfs-bless $(HAIKU_OUTPUT_DIR)/cd/boot -prep-boot \
1506		boot/haikuloader.xcf -r -o $(<) $(HAIKU_OUTPUT_DIR)/cd \
1507	|| \
1508	genisoimage -v -hfs -part -map $(MAPS) -no-desktop -hfs-volid bootimg \
1509		-V bootimg -hfs-bless $(HAIKU_OUTPUT_DIR)/cd/boot -prep-boot \
1510		boot/haikuloader.xcf -r -o $(<) $(HAIKU_OUTPUT_DIR)/cd
1511	$(RM) -R $(HAIKU_OUTPUT_DIR)/cd
1512}
1513
1514
1515