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