xref: /haiku/build/jam/ImageRules (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
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 -f $(1)
41	echo -n > $(1)
42}
43
44rule AddVariableToScript
45{
46	# AddVariableToScript <script> : <variable> : <value> ;
47	#
48	local script = $(1) ;
49	local variable = $(2) ;
50	local value = $(3) ;
51
52	InitScript $(script) ;
53
54	VARIABLE_DEFS on $(script) += "echo $(variable)=\\\"$(value)\\\" >> " ;
55
56	AddVariableToScript1 $(script) ;
57}
58
59actions together AddVariableToScript1
60{
61	$(VARIABLE_DEFS)$(1);
62}
63
64rule AddTargetVariableToScript
65{
66	# AddTargetVariableToScript <script> : <target> [ : <variable> ] ;
67	#
68	local script = $(1) ;
69	local target = $(2) ;
70	local variable = $(3:E=$(target:BS)) ;
71
72	InitScript $(script) ;
73
74	# That's not completely save, if one has more than on target with the
75	# same base name. A unique pseudo target would have to be introduced
76	# to do it more correctly.
77	VARIABLE_NAME($(target:BS)) on $(script) = $(variable) ;
78
79	Depends $(script) : $(target) ;
80	AddTargetVariableToScript1 $(script) : $(target) ;
81}
82
83actions AddTargetVariableToScript1
84{
85	echo "$(VARIABLE_NAME($(2:BS)))=\"$(2)\"" >> $(1)
86}
87
88
89rule AddDirectoryToHaikuImage
90{
91	# AddDirectoryToHaikuImage <directory>
92	local directoryTokens = $(1) ;
93	local directory = [ FDirName $(directoryTokens) ] ;
94	directory = $(directory:G=HaikuImage) ;
95
96	if ! [ on $(directory) return $(__is_on_image) ] {
97		INSTALL_DIRECTORIES on haiku-image-contents += $(directory) ;
98		__is_on_image on $(directory) = true ;
99		NotFile $(directory) ;
100
101		# mark the parent dir as not to be created
102		local parent = [ FReverse $(directoryTokens) ] ;
103		parent = [ FReverse $(parent[2-]) ] ;
104		if $(parent) {
105			parent = [ FDirName $(parent) ] ;
106			parent = $(parent:G=HaikuImage) ;
107			DONT_CREATE on $(parent) = true ;
108		}
109	}
110
111	return $(directory) ;
112}
113
114rule AddFilesToHaikuImage
115{
116	# AddFilesToHaikuImage <directory> : <targets> [ : dest name ]
117	#
118	local directory = [ AddDirectoryToHaikuImage $(1) ] ;
119	local targets = $(2) ;
120	local destName = $(3) ;
121
122	INSTALL_DIR on $(targets) = $(directory) ;
123	TARGETS_TO_INSTALL on $(directory) += $(targets) ;
124
125	if $(destName) {
126		INSTALL_TARGET_NAME on $(targets) = $(destName) ;
127	}
128}
129
130rule AddSymlinkToHaikuImage
131{
132	# AddSymlinkToHaikuImage <directory> : <link target> [ : <link name> ] ;
133	#
134	local directory = [ AddDirectoryToHaikuImage $(1) ] ;
135	local linkTarget = $(2) ;
136	local linkName = $(3) ;
137
138	if ! $(linkName) {
139		local path = [ FReverse [ FSplitPath $(linkTarget) ] ] ;
140		linkName = $(path[1]) ;
141	}
142
143	local link = $(directory)/$(linkName) ;
144	SYMLINK_TARGET on $(link) = $(linkTarget) ;
145	SYMLINKS_TO_INSTALL on $(directory) += $(link) ;
146}
147
148rule AddDriversToHaikuImage
149{
150	# AddDriversToHaikuImage <relative directory> : <targets> ;
151	#
152	local relativeDirectoryTokens = $(1) ;
153	local targets = $(2) ;
154	local directoryTokens = beos system add-ons kernel drivers dev
155		$(relativeDirectoryTokens) ;
156
157	AddFilesToHaikuImage beos system add-ons kernel drivers bin : $(targets) ;
158
159	# get the relative symlink path prefix
160	local linkPrefix = ;
161	for i in $(relativeDirectoryTokens) {
162		linkPrefix += .. ;
163	}
164	linkPrefix += .. bin ;
165
166	# add the symlinks
167	local name ;
168	for name in $(targets:BS) {
169		AddSymlinkToHaikuImage $(directoryTokens)
170			: [ FDirName $(linkPrefix) $(name) ] : $(name) ;
171	}
172}
173
174rule AddBootModuleSymlinks
175{
176	# AddBootModuleSymlinks <targets> ;
177	#
178	local targets = $(1) ;
179
180	# add the symlinks
181	local target ;
182	for target in $(targets) {
183		local installDir = [ on $(target) return $(INSTALL_DIR) ] ;
184
185		if ! $(installDir) {
186			Echo "ERROR: AddBootModuleSymlinks: Can't create a symlink to"
187				"target" \"$(target)"\"." ;
188			Exit "ERROR: Add*ToHaikuImage has not been invoked for it yet." ;
189		}
190
191		local name = $(target:BS) ;
192		local linkTarget = [ FDirName /boot $(installDir:G=) $(name) ] ;
193
194		AddSymlinkToHaikuImage beos system add-ons kernel boot
195			: $(linkTarget) : $(name) ;
196	}
197}
198
199rule CreateHaikuImageMakeDirectoriesScript
200{
201	local script = $(1) ;
202	MakeLocate $(script) : $(HAIKU_OUTPUT_DIR) ;
203	Always $(script) ;
204
205	local initScript = [ InitScript $(script) ] ;
206
207	local scriptBody
208		= [ FSameTargetWithPrependedGrist $(script) : script-body ] ;
209	LOCATE on $(scriptBody) = [ on $(script) return $(LOCATE) ] ;
210	Depends $(scriptBody) : $(initScript) ;
211	Depends $(script) : $(scriptBody) ;
212
213	local dirsToCreate ;
214	local dir ;
215	for dir in [ on haiku-image-contents return $(INSTALL_DIRECTORIES) ] {
216		if ! [ on $(dir) return $(DONT_CREATE) ] {
217			dirsToCreate += $(dir) ;
218		}
219	}
220
221	Depends $(scriptBody) : $(dirsToCreate) ;
222	CreateHaikuImageMakeDirectoriesScript1 $(scriptBody) : $(dirsToCreate) ;
223}
224
225actions piecemeal CreateHaikuImageMakeDirectoriesScript1
226{
227	echo \$mkdir -p "\"\${tPrefix}$(2:G=)\"" >> $(1)
228}
229
230rule CreateHaikuImageCopyFilesScript
231{
232	local script = $(1) ;
233	MakeLocate $(script) : $(HAIKU_OUTPUT_DIR) ;
234	Always $(script) ;
235
236	local initScript = [ InitScript $(script) ] ;
237
238	local scriptBody
239		= [ FSameTargetWithPrependedGrist $(script) : script-body ] ;
240	LOCATE on $(scriptBody) = [ on $(script) return $(LOCATE) ] ;
241	Depends $(scriptBody) : $(initScript) ;
242	Depends $(script) : $(scriptBody) ;
243
244	local dir ;
245	for dir in [ on haiku-image-contents return $(INSTALL_DIRECTORIES) ] {
246		local targets = [ on $(dir) return $(TARGETS_TO_INSTALL) ] ;
247
248		# filter the targets that shall be renamed; they have to be copied
249		# individually
250		local remainingTargets ;
251		local target ;
252		for target in $(targets) {
253			local destName = [ on $(target) return $(INSTALL_TARGET_NAME) ] ;
254			if $(destName) {
255				# use a unique dummy target for this file, on which we
256				# can define the TARGET_DIR variable
257				local dummyTarget = $(script)-dummy-$(dir:G=)-$(target) ;
258				NotFile $(dummyTarget) ;
259				TARGET_DIR on $(dummyTarget) = $(dir:G=) ;
260				INSTALL_TARGET_NAME on $(dummyTarget) = $(destName) ;
261
262				Depends $(dummyTarget) : $(initScript) $(target) ;
263				Depends $(script) : $(dummyTarget) ;
264
265				AppendToHaikuImageCopyFilesScriptSingleFile $(dummyTarget)
266					: $(initScript) $(target) ;
267			} else {
268				remainingTargets += $(target) ;
269			}
270		}
271		targets = $(remainingTargets) ;
272
273		if $(targets) {
274			# use a unique dummy target for this directory, on which we
275			# can define the TARGET_DIR variable
276			local dummyTarget = $(script)-dummy-$(dir:G=) ;
277			NotFile $(dummyTarget) ;
278			TARGET_DIR on $(dummyTarget) = $(dir:G=) ;
279
280			Depends $(dummyTarget) : $(initScript) $(targets) ;
281			Depends $(script) : $(dummyTarget) ;
282
283			OUTPUT_SCRIPT on $(dummyTarget) = $(initScript) ;
284			AppendToHaikuImageCopyFilesScript $(dummyTarget) : $(targets) ;
285		}
286
287		local symlinks = [ on $(dir) return $(SYMLINKS_TO_INSTALL) ] ;
288		local symlink ;
289		for symlink in $(symlinks) {
290			NotFile $(symlink) ;
291
292			Depends $(script) : $(symlink) ;
293			Depends $(symlink) : $(initScript) ;
294
295			AddSymlinkToHaikuImageCopyFilesScript $(symlink) : $(initScript) ;
296		}
297	}
298}
299
300actions piecemeal AppendToHaikuImageCopyFilesScript bind OUTPUT_SCRIPT
301{
302	echo \$cp "\"\${sPrefix}$(2)\"" "\"\${tPrefix}$(TARGET_DIR)\"" >> $(OUTPUT_SCRIPT)
303}
304
305actions AppendToHaikuImageCopyFilesScriptSingleFile
306{
307	echo \$cp "\"\${sPrefix}$(2[2])\"" \
308		"\"\${tPrefix}$(TARGET_DIR)/$(INSTALL_TARGET_NAME)\"" >> $(2[1])
309}
310
311actions AddSymlinkToHaikuImageCopyFilesScript
312{
313	echo \$ln -sf "\"$(SYMLINK_TARGET)\"" "\"\${tPrefix}$(1:G=)\"" >> $(2[1])
314}
315
316rule BuildHaikuImage
317{
318	# BuildHaikuImage <haiku image> : <scripts> : <is image> ;
319	#
320	local haikuImage = $(1) ;
321	local scripts = $(2) ;
322	local isImage = $(3) ;
323
324	if $(isImage) = 1 || $(isImage) = true {
325		IS_IMAGE on $(haikuImage) = 1 ;
326	} else {
327		IS_IMAGE on $(haikuImage) = "" ;
328	}
329
330	local mainScript = build_haiku_image ;
331	SEARCH on $(mainScript) = [ FDirName $(HAIKU_TOP) build scripts ] ;
332
333	Depends $(haikuImage) : $(mainScript) $(scripts) ;
334	BuildHaikuImage1 $(haikuImage) : $(mainScript) $(scripts) ;
335}
336
337actions BuildHaikuImage1
338{
339	export isImage="$(IS_IMAGE)"
340	$(2[1]) $(2[2-])
341}
342