xref: /haiku/build/jam/ImageRules (revision faf38d0fc0b16b984be6a34c4c60615d10948452)
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:BSM) ;
254			} else {
255				name = $(destName) ;
256			}
257		} else {
258			name = $(target:BSM) ;
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]:BSM) ;
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:BSM) {
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:BSM) ;
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:BSM) {
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
1132rule AddHaikuImageSourcePackages packages
1133{
1134	# AddHaikuImageSourcePackages <packages> ;
1135	# Adds the given source packages for <packages> to the image.
1136
1137	if $(HAIKU_INCLUDE_SOURCES) = 1 {
1138		AddHaikuImagePackages $(packages)_source ;
1139	}
1140}
1141
1142rule IsHaikuImagePackageAdded package
1143{
1144	local resolvedPackage = [ IsPackageAvailable $(package) ] ;
1145	if $(resolvedPackage)
1146		&& [ on $(resolvedPackage) return $(HAIKU_PACKAGE_ADDED) ] {
1147		return 1 ;
1148	}
1149
1150	return ;
1151}
1152
1153
1154rule BuildHaikuImagePackageList target
1155{
1156	if ! $(target) {
1157		return ;
1158	}
1159
1160	# get the file names of all added packages
1161	local packageFiles ;
1162	local package ;
1163	for package in $(HAIKU_ADDED_PACKAGES) {
1164		packageFiles += [ FetchPackage $(package) : nameResolved ] ;
1165	}
1166
1167	# extract the versioned package names (without revision)
1168	packageFiles = [ Match "^([^-]*)" : $(packageFiles:B) ] ;
1169
1170	HAIKU_IMAGE_PACKAGES on $(target) = $(packageFiles) ;
1171}
1172
1173
1174actions BuildHaikuImagePackageList
1175{
1176	echo $(HAIKU_IMAGE_PACKAGES) | xargs -n 1 echo | LC_ALL=C sort -u > $(1)
1177}
1178
1179
1180rule AddEntryToHaikuImageUserGroupFile file : entry
1181{
1182	local allEntries = [ on $(file) return $(HAIKU_IMAGE_USER_GROUP_ENTRIES) ] ;
1183
1184	if $(allEntries) {
1185		allEntries = $(allEntries)|$(entry) ;
1186	} else {
1187		allEntries = $(entry) ;
1188
1189		Always $(file) ;
1190		MakeLocate $(file) : $(HAIKU_COMMON_PLATFORM_OBJECT_DIR) ;
1191		BuildHaikuImageUserGroupFile $(file) ;
1192		AddFilesToHaikuImage system settings etc : $(file) ;
1193	}
1194
1195	HAIKU_IMAGE_USER_GROUP_ENTRIES on $(file) = $(allEntries) ;
1196}
1197
1198actions BuildHaikuImageUserGroupFile
1199{
1200	echo "$(HAIKU_IMAGE_USER_GROUP_ENTRIES)" | tr '|' '\n' > $(1)
1201}
1202
1203rule AddUserToHaikuImage user : uid : gid : home : shell : realName
1204{
1205	if ! $(user) || ! $(uid) || ! $(gid) || ! $(home) {
1206		Exit "Invalid haiku user specification passed to AddUserToHaikuImage." ;
1207	}
1208
1209	local entry
1210		= $(user):x:$(uid):$(gid):$(realName:E=$(user)):$(home):$(shell:E="") ;
1211
1212	AddEntryToHaikuImageUserGroupFile <haiku-image>passwd : $(entry) ;
1213}
1214
1215rule AddGroupToHaikuImage group : gid : members
1216{
1217	if ! $(group) || ! $(gid) {
1218		Exit "Invalid haiku group specification passed to"
1219			"AddGroupToHaikuImage." ;
1220	}
1221
1222	local entry = $(group):x:$(gid):$(members:J=,:E) ;
1223
1224	AddEntryToHaikuImageUserGroupFile <haiku-image>group : $(entry) ;
1225}
1226
1227
1228rule AddLibrariesToHaikuImage directory : libs
1229{
1230	# AddLibraryToHaikuImage <directory> : <libs>
1231	#
1232	# Installs libraries with the appropriate links onto the image.
1233	#
1234
1235	AddLibrariesToContainer $(HAIKU_IMAGE_CONTAINER_NAME) : $(directory)
1236		: $(libs) ;
1237}
1238
1239
1240rule CreateHaikuImageMakeDirectoriesScript script
1241{
1242	CreateContainerMakeDirectoriesScript $(HAIKU_IMAGE_CONTAINER_NAME)
1243		: $(script) ;
1244}
1245
1246rule CreateHaikuImageCopyFilesScript script
1247{
1248	CreateContainerCopyFilesScript $(HAIKU_IMAGE_CONTAINER_NAME) : $(script) ;
1249}
1250
1251rule CreateHaikuImageExtractFilesScript script
1252{
1253	CreateContainerExtractFilesScript $(HAIKU_IMAGE_CONTAINER_NAME)
1254		: $(script) ;
1255}
1256
1257rule BuildHaikuImage haikuImage : scripts : isImage : isVMwareImage
1258{
1259	# BuildHaikuImage <haiku image> : <scripts> : <is image> : <isVMwareImage> ;
1260
1261	if $(isImage) = 1 || $(isImage) = true {
1262		IS_IMAGE on $(haikuImage) = 1 ;
1263	} else {
1264		IS_IMAGE on $(haikuImage) = "" ;
1265	}
1266
1267	if $(isVMwareImage) = 1 || $(isVMwareImage) = true {
1268		IS_VMWARE_IMAGE on $(haikuImage) = 1 ;
1269	} else {
1270		IS_VMWARE_IMAGE on $(haikuImage) = "" ;
1271	}
1272
1273	local mainScript = build_haiku_image ;
1274	SEARCH on $(mainScript) = [ FDirName $(HAIKU_TOP) build scripts ] ;
1275
1276	Depends $(haikuImage) : $(mainScript) $(scripts) ;
1277	BuildHaikuImage1 $(haikuImage) : $(mainScript) $(scripts) ;
1278}
1279
1280actions BuildHaikuImage1
1281{
1282	export imagePath="$(1)"
1283	export isImage="$(IS_IMAGE)"
1284	export isVMwareImage="$(IS_VMWARE_IMAGE)"
1285	$(2[1]) $(2[2-])
1286}
1287
1288rule BuildVMWareImage vmwareImage : plainImage : imageSize
1289{
1290	# BuildVMWareImage <vmware image> : <plain image> : <image size in MB>
1291
1292	IMAGE_SIZE on $(vmwareImage) = $(imageSize) ;
1293
1294	Depends $(vmwareImage) : <build>vmdkheader $(plainImage) ;
1295	BuildVMWareImage1 $(vmwareImage) : <build>vmdkheader $(plainImage) ;
1296}
1297
1298actions BuildVMWareImage1
1299{
1300	$(RM) $(1)
1301	$(2[1]) -h 64k -i$(IMAGE_SIZE)M $(1) &&
1302	cat $(2[2]) >> $(1)
1303}
1304
1305
1306#pragma mark - Network Boot Archive rules
1307
1308rule AddDirectoryToNetBootArchive directoryTokens
1309{
1310	# AddDirectoryToNetBootArchive <directoryTokens>
1311
1312	return [ AddDirectoryToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1313		: $(directoryTokens) ] ;
1314}
1315
1316rule AddFilesToNetBootArchive directory : targets : destName
1317{
1318	# AddFilesToNetBootArchive <directory> : <targets> [ : dest name ]
1319
1320	AddFilesToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME) : $(directory)
1321		: $(targets) : $(destName) ;
1322}
1323
1324rule AddSymlinkToNetBootArchive directoryTokens : linkTarget : linkName
1325{
1326	# AddSymlinkToNetBootArchive <directory> : <link target> [ : <link name> ] ;
1327
1328	AddSymlinkToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1329		: $(directoryTokens) : $(linkTarget) : $(linkName) ;
1330}
1331
1332rule AddDriversToNetBootArchive relativeDirectoryTokens : targets
1333{
1334	# AddDriversToNetBootArchive <relative directory> : <targets> ;
1335
1336	AddDriversToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1337		: $(relativeDirectoryTokens) : $(targets) ;
1338}
1339
1340rule AddNewDriversToNetBootArchive relativeDirectoryTokens : targets
1341{
1342	# AddNewDriversToNetBootArchive <relative directory> : <targets> ;
1343
1344	AddNewDriversToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1345		: $(relativeDirectoryTokens) : $(targets) ;
1346}
1347
1348rule AddDriverRegistrationToNetBootArchive relativeDirectoryTokens : target
1349	: links
1350{
1351	# AddDriverRegistrationToNetBootArchive <directory> : <link target>
1352	#	: <link names> ] ;
1353
1354	AddDriverRegistrationToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1355		: $(relativeDirectoryTokens) : $(target) : $(links) ;
1356}
1357
1358rule AddBootModuleSymlinksToNetBootArchive targets
1359{
1360	# AddBootModuleSymlinksToNetBootArchive <targets> ;
1361
1362	AddBootModuleSymlinksToContainer $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1363		: $(targets) ;
1364}
1365
1366rule CreateNetBootArchiveMakeDirectoriesScript script
1367{
1368	CreateContainerMakeDirectoriesScript
1369		$(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME) : $(script) ;
1370}
1371
1372rule CreateNetBootArchiveCopyFilesScript script
1373{
1374	CreateContainerCopyFilesScript $(HAIKU_NET_BOOT_ARCHIVE_CONTAINER_NAME)
1375		: $(script) ;
1376}
1377
1378rule BuildNetBootArchive archive : scripts
1379{
1380	# BuildNetBootArchive <archive> : <scripts> ;
1381
1382	local mainScript = build_archive ;
1383	SEARCH on $(mainScript) = [ FDirName $(HAIKU_TOP) build scripts ] ;
1384
1385	Depends $(archive) : $(mainScript) $(scripts) ;
1386	BuildNetBootArchive1 $(archive) : $(mainScript) $(scripts) ;
1387}
1388
1389actions BuildNetBootArchive1
1390{
1391	$(2[1]) $(1) $(2[2-])
1392}
1393
1394
1395#pragma mark - Floppy Boot Archive rules
1396
1397
1398rule AddDirectoryToFloppyBootArchive directoryTokens
1399{
1400	# AddDirectoryToFloppyBootArchive <directoryTokens>
1401
1402	return [ AddDirectoryToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1403		: $(directoryTokens) ] ;
1404}
1405
1406rule AddFilesToFloppyBootArchive directory : targets : destName
1407{
1408	# AddFilesToFloppyBootArchive <directory> : <targets> [ : dest name ]
1409
1410	AddFilesToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME) : $(directory)
1411		: $(targets) : $(destName) ;
1412}
1413
1414rule AddSymlinkToFloppyBootArchive directoryTokens : linkTarget : linkName
1415{
1416	# AddSymlinkToFloppyBootArchive <directory> : <link target>
1417	#	[ : <link name> ] ;
1418
1419	AddSymlinkToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1420		: $(directoryTokens) : $(linkTarget) : $(linkName) ;
1421}
1422
1423rule AddDriversToFloppyBootArchive relativeDirectoryTokens : targets
1424{
1425	# AddDriversToFloppyBootArchive <relative directory> : <targets> ;
1426
1427	AddDriversToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1428		: $(relativeDirectoryTokens) : $(targets) ;
1429}
1430
1431rule AddNewDriversToFloppyBootArchive relativeDirectoryTokens : targets
1432{
1433	# AddNewDriversToFloppyBootArchive <relative directory> : <targets> ;
1434
1435	AddNewDriversToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1436		: $(relativeDirectoryTokens) : $(targets) ;
1437}
1438
1439rule AddDriverRegistrationToFloppyBootArchive relativeDirectoryTokens : target
1440	: links
1441{
1442	# AddDriverRegistrationToFloppyBootArchive <directory> : <link target>
1443	#	: <link names> ] ;
1444
1445	AddDriverRegistrationToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1446		: $(relativeDirectoryTokens) : $(target) : $(links) ;
1447}
1448
1449rule AddBootModuleSymlinksToFloppyBootArchive targets
1450{
1451	# AddBootModuleSymlinksToFloppyBootArchive <targets> ;
1452
1453	AddBootModuleSymlinksToContainer $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1454		: $(targets) ;
1455}
1456
1457rule CreateFloppyBootArchiveMakeDirectoriesScript script
1458{
1459	CreateContainerMakeDirectoriesScript
1460		$(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME) : $(script) ;
1461}
1462
1463rule CreateFloppyBootArchiveCopyFilesScript script
1464{
1465	CreateContainerCopyFilesScript $(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME)
1466		: $(script) ;
1467}
1468
1469rule BuildFloppyBootArchive archive : scripts
1470{
1471	# BuildHFloppyBootArchive <archive> : <scripts> ;
1472
1473	local mainScript = build_archive ;
1474	SEARCH on $(mainScript) = [ FDirName $(HAIKU_TOP) build scripts ] ;
1475
1476	Depends $(archive) : $(mainScript) $(scripts) ;
1477	BuildFloppyBootArchive1 $(archive) : $(mainScript) $(scripts) ;
1478}
1479
1480actions BuildFloppyBootArchive1
1481{
1482	$(2[1]) $(1) $(2[2-])
1483}
1484
1485# warning: that is quite x86 dependant...
1486
1487rule BuildFloppyBootImage image : haikuLoader : archive
1488{
1489	Depends $(image) : $(haikuLoader) ;
1490	Depends $(image) : $(archive) ;
1491	#MakeLocateDebug $(image) ;
1492	FLOPPY_IMAGE_SIZE on $(image) = $(HAIKU_BOOT_FLOPPY_IMAGE_SIZE) ;
1493	ARCHIVE_IMAGE_OFFSET on $(image) = $(HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET) ;
1494	BuildFloppyBootImage1 $(image) : $(haikuLoader) $(archive) ;
1495	if $(HAIKU_BOOT_PLATFORM) = atari_m68k {
1496		Depends $(image) : <build>fixup_tos_boot_checksum ;
1497		BuildFloppyBootImageFixupM68K $(image)
1498			: <build>fixup_tos_boot_checksum ;
1499	}
1500	if $(HAIKU_BOOT_PLATFORM) = amiga_m68k {
1501		Depends $(image) : <build>fixup_amiga_boot_checksum ;
1502		BuildFloppyBootImageFixupM68K $(image)
1503			: <build>fixup_amiga_boot_checksum ;
1504	}
1505}
1506
1507actions BuildFloppyBootImage1
1508{
1509	haiku_loader_size=`stat -c %s "$(>[1])"`
1510	drivers_tgz_size=`stat -c %s "$(>[2])"`
1511	if [ $? -ne 0 ] ; then
1512		# FreeBSD's stat command don't support -c/--format option
1513		# and use %z specifier for file size
1514		haiku_loader_size=`stat -f %z "$(>[1])"`
1515		drivers_tgz_size=`stat -f %z "$(>[2])"`
1516	fi
1517	archive_image_offset=`echo "$(ARCHIVE_IMAGE_OFFSET) * 1024" | bc`
1518	floppy_tgz_size=\
1519		`echo "($(FLOPPY_IMAGE_SIZE) - $(ARCHIVE_IMAGE_OFFSET)) * 1024" | bc`
1520	if [ $haiku_loader_size -gt $archive_image_offset ] ; then
1521		echo "Error: $(>[1]) is too big ($haiku_loader_size) to fit "
1522		echo "       before the boot archive starting at $archive_image_offset!"
1523		exit 1
1524	fi
1525	if [ $drivers_tgz_size -gt $floppy_tgz_size ] ; then
1526		echo "Error: $(>[2]) is too big ($drivers_tgz_size) to fit "
1527		echo "       in the boot floppy ($floppy_tgz_size)!"
1528		exit 1
1529	fi
1530	$(RM) $(<)
1531	# make an empty image
1532	dd if=/dev/zero of=$(<) bs=1k count=$(FLOPPY_IMAGE_SIZE)
1533	# add haiku_loader
1534	dd if=$(>[1]) of=$(<) conv=notrunc
1535	# add the boot drivers tgz archive
1536	dd if=$(>[2]) of=$(<) bs=$(ARCHIVE_IMAGE_OFFSET)k seek=1 conv=notrunc
1537}
1538
1539actions BuildFloppyBootImageFixupM68K
1540{
1541	# fixup the boot sector checksum
1542	$(>[1]) $(<)
1543}
1544
1545#pragma mark - CD Boot Image rules
1546
1547rule BuildCDBootImage image : bootfloppy : extrafiles
1548{
1549	Depends $(image) : $(bootfloppy) ;
1550	Depends $(image) : $(extrafiles) ;
1551	BOOTIMG on $(image) = $(bootfloppy) ;
1552
1553	BuildCDBootImage1 $(image) : $(bootfloppy) $(extrafiles) ;
1554}
1555
1556actions BuildCDBootImage1
1557{
1558	$(RM) $(<)
1559	mkisofs -b $(BOOTIMG) -r -J -V bootimg -o $(<) $(>[1]) $(>[2-]) \
1560		|| genisoimage -b $(BOOTIMG) -r -J -V bootimg -o $(<) $(>[1]) $(>[2-])
1561}
1562
1563
1564#pragma mark - CD Boot PPC Image rules
1565
1566rule BuildCDBootPPCImage image : hfsmaps : elfloader : coffloader : chrpscript
1567	: extrafiles
1568{
1569	Depends $(image) : $(elfloader) ;
1570	Depends $(image) : $(coffloader) ;
1571	Depends $(image) : $(chrpscript) ;
1572	Depends $(image) : $(extrafiles) ;
1573	Depends $(image) : $(hfsmaps) ;
1574	MAPS on $(image) = $(hfsmaps) ;
1575
1576	BuildCDBootPPCImage1 $(image) : $(elfloader) $(coffloader) $(chrpscript)
1577		$(extrafiles) ;
1578}
1579
1580actions BuildCDBootPPCImage1 bind MAPS
1581{
1582	$(RM) $(<)
1583	mkdir -p $(HAIKU_OUTPUT_DIR)/cd/ppc
1584	mkdir -p $(HAIKU_OUTPUT_DIR)/cd/boot
1585	# CHRP Boot script
1586	cp $(>[3]) $(HAIKU_OUTPUT_DIR)/cd/ppc/bootinfo.txt
1587	cp $(>[3]) $(HAIKU_OUTPUT_DIR)/cd/boot/boot.chrp
1588	# Haiku Bootloaders
1589	cp $(>[2]) $(HAIKU_OUTPUT_DIR)/cd/boot/haikuloader.xcf
1590	cp $(>[1]) $(HAIKU_OUTPUT_DIR)/cd/boot/haikuloader.elf
1591	# Extras (readme files, etc)
1592	cp $(>[4]) $(HAIKU_OUTPUT_DIR)/cd/
1593
1594	mkisofs -v -hfs -part -map $(MAPS) -no-desktop -hfs-volid bootimg \
1595		-V bootimg -hfs-bless $(HAIKU_OUTPUT_DIR)/cd/boot -prep-boot \
1596		boot/haikuloader.xcf -r -o $(<) $(HAIKU_OUTPUT_DIR)/cd \
1597	|| \
1598	genisoimage -v -hfs -part -map $(MAPS) -no-desktop -hfs-volid bootimg \
1599		-V bootimg -hfs-bless $(HAIKU_OUTPUT_DIR)/cd/boot -prep-boot \
1600		boot/haikuloader.xcf -r -o $(<) $(HAIKU_OUTPUT_DIR)/cd
1601	$(RM) -r $(HAIKU_OUTPUT_DIR)/cd
1602}
1603