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