xref: /haiku/build/jam/FileRules (revision c9ad965c81b08802fed0827fd1dd16f45297928a)
1rule SymLink
2{
3	# SymLink <target> : <source> : <makeDefaultDependencies> ;
4	# Links <target> to <source>.
5	# <source> is the exact link contents. No binding is done.
6	# <makeDefaultDependencies> If true, <target> will be made a dependency
7	# of the `all' pseudo target, i.e. it will be made by default, and removed
8	# on `jam clean'.
9
10	local target = $(1) ;
11	local source = $(2) ;
12	local makeDefaultDependencies = $(3) ;
13	if ! $(makeDefaultDependencies) {
14		makeDefaultDependencies = true ;
15	}
16	LINKCONTENTS on $(target) = $(source) ;
17	SymLink1 $(target) ;
18	if $(makeDefaultDependencies) = true {
19		LocalDepends files : $(target) ;
20		LocalClean clean : $(target) ;
21	}
22}
23
24actions SymLink1
25{
26	$(RM) "$(1)" && $(LN) -s "$(LINKCONTENTS)" "$(1)"
27}
28
29rule RelSymLink
30{
31	# RelSymLink <link> : <link target> : <makeDefaultDependencies> ;
32	# Creates a relative symbolic link from <link> to <link target>.
33	# <link> and <link target> can be usual targets. They may have a grist
34	# and don't need to have any dirname. Their LOCATE variables are used to
35	# find their locations.
36	# <makeDefaultDependencies> If true (which is the default), <link> will be
37	# made a dependency of the `files' pseudo target, i.e. it will be made by
38	# default, and removed on `jam clean'.
39
40	local target = $(1) ;
41	local source = $(2) ;
42	local makeDefaultDependencies = $(3) ;
43	local targetDir = [ on $(target) FDirName $(LOCATE[1]) $(target:D) ] ;
44	local sourceDir = [ on $(source) FDirName $(LOCATE[1]) $(source:D) ] ;
45	local sourcePath = $(source:G=) ;
46	sourcePath = $(sourcePath:D=$(sourceDir)) ;
47	local targetDirComponents = [ FSplitPath $(targetDir) ] ;
48	local sourceComponents = [ FSplitPath $(sourcePath) ] ;
49
50	SymLink $(target)
51		: [ FRelPath $(targetDirComponents) : $(sourceComponents) ]
52		: $(makeDefaultDependencies) ;
53	NOUPDATE $(target) ;
54	Depends $(target) : $(source) ;
55}
56
57rule AbsSymLink
58{
59	# AbsSymLink <link> : <link target> : <link dir>
60	#			: <makeDefaultDependencies> ;
61	# Creates an absolute symbolic link from <link> to <link target>.
62	# <link> and <link target> must be usual targets. If <link dir> is
63	# given, then it is set as LOCATE directory on <link>.
64	# <makeDefaultDependencies> If true (which is the default), <link> will be
65	# made a dependency of the `files' pseudo target, i.e. it will be made by
66	# default, and removed on `jam clean'.
67
68	local makeDefaultDependencies = $(4) ;
69	if ! $(makeDefaultDependencies) {
70		makeDefaultDependencies = true ;
71	}
72
73	Depends $(1) : $(2) ;
74	if $(3) {
75		MakeLocate $(1) : $(3) ;
76	}
77	SEARCH on $(2) += $(SEARCH_SOURCE) ;
78	if $(makeDefaultDependencies) = true {
79		LocalDepends files : $(1) ;
80		LocalClean clean : $(1) ;
81	}
82}
83
84actions AbsSymLink
85{
86	target="$(2)"
87	case "$target" in
88		/*) ;;
89		*) target=`pwd`/"$target";;
90	esac
91	$(RM) "$(1)" && $(LN) -s "$target" "$(1)"
92}
93
94rule HaikuInstall installAndUninstall : dir : sources : installgrist
95	: installRule : targets
96{
97	# Usage: HaikuInstall <[ install [ and uninstall ] pseudotarget ]>
98	#	: <directory> : <sources to install> : [ <installgrist> ]
99	#	: [ <install rule> ] : [ <targets> ] ;
100
101	local install = $(installAndUninstall[1]) ;
102	install ?= install ;
103	local uninstall = $(installAndUninstall[2]) ;
104	uninstall ?= un$(install) ;
105	installgrist ?= $(INSTALLGRIST) ;
106	installRule ?= Install ;
107
108	targets ?= $(sources) ;
109	targets = $(targets:G=$(installgrist)) ;
110
111	NotFile $(install) ;
112	NotFile $(uninstall) ;
113	Depends $(install) : $(targets) ;
114	Clean $(uninstall) : $(targets) ;
115
116	SEARCH on $(sources) += $(SEARCH_SOURCE) ;
117	MakeLocate $(targets) : $(dir) ;
118
119	local source ;
120	for source in $(sources) {
121		local target = $(targets[1]) ;
122		targets = $(targets[2-]) ;
123
124		Depends $(target) : $(source) ;
125		$(installRule) $(target) : $(source) ;
126
127		if [ on $(target) return $(MODE) ] {
128			Chmod $(target) ;
129		}
130
131		if $(OWNER) && $(CHOWN) {
132			Chown $(target) ;
133			OWNER on $(target) = $(OWNER) ;
134		}
135
136		if $(GROUP) && $(CHGRP) {
137			Chgrp $(target) ;
138			GROUP on $(target) = $(GROUP) ;
139		}
140	}
141}
142
143rule InstallAbsSymLinkAdapter
144{
145	# InstallAbsSymLinkAdapter <link> : <link target>
146	if ! [ on $(2) return $(TARGET) ] {
147		TARGET on $(2) = [ on $(2) return $(SEARCH) ] ;
148	}
149	AbsSymLink $(1) : $(2) : : false ;
150}
151
152rule HaikuInstallAbsSymLink
153{
154	# Usage: HaikuInstallAbsSymLink <[ install [ and uninstall ] pseudotarget ]>
155	#							   : <directory> : <sources to install>
156	#							   : [ <installgrist> ] ;
157	HaikuInstall $(1) : $(2) : $(3) : $(4) : InstallAbsSymLinkAdapter ;
158}
159
160rule InstallRelSymLinkAdapter
161{
162	# InstallRelSymLinkAdapter <link> : <link target>
163	if ! [ on $(2) return $(TARGET) ] {
164		TARGET on $(2) = [ on $(2) return $(SEARCH) ] ;
165	}
166	RelSymLink $(1) : $(2) : false ;
167}
168
169rule HaikuInstallRelSymLink
170{
171	# Usage: HaikuInstallRelSymLink <[ install [ and uninstall ] pseudotarget ]>
172	#							   : <directory> : <sources to install>
173	#							   : [ <installgrist> ] ;
174	HaikuInstall $(1) : $(2) : $(3) : $(4) : InstallRelSymLinkAdapter ;
175}
176
177
178rule UnarchiveObjects
179{
180	# UnarchiveObjects <target objects> : <static object>
181
182	MakeLocateArch $(1) ;
183	Depends $(1) : $(2) ;
184	SEARCH on $(2) = $(SEARCH_SOURCE) ;
185}
186
187actions UnarchiveObjects
188{
189	( cd $(1[1]:D) && $(TARGET_AR) $(TARGET_UNARFLAGS) "$(2)" $(1:BS) )
190}
191
192
193rule UnzipArchive directory : entries : zipFile : grist
194{
195	# UnzipArchive <directory> : <entries> : <zipFile> [ : <grist> ]
196	#
197	# Unzips the zip file target <zipFile> to directory <directory>. The rule
198	# can be called multiple times for different <entries> for the same
199	# <directory> and <zipFile> combo.
200	#
201	# <directory> - The directory into which to unzip the zip file. The
202	#               directory is created is by this rule and it is the target
203	#               that the unzip action is associated with.
204	# <entries>   - The entries of the zip file one is interested in. The rule
205	#               always unzips the complete zip file, from the given entries
206	#               the rule creates targets (using <grist>) representing the
207	#               unzipped entries. Those targets are returned by the rule.
208	# <zipFile>   - The zip file target to unzip.
209	# <grist>     - The grist used to create targets from <entries>. Defaults to
210	#               "unzipped".
211
212	grist ?= unzipped ;
213
214	# Turn the entries into targets to build.
215	local targets ;
216	local entry ;
217	for entry in $(entries) {
218		local target = $(entry:G=$(grist)) ;
219		targets += $(target) ;
220	}
221
222	LOCATE on $(targets) = $(directory) ;
223	Depends $(targets) : $(directory) ;
224	NoUpdate $(targets) ;
225
226	# one-time initialization for the main target (the directory)
227	if ! [ on $(directory) return $(INITIALIZED) ] {
228		# make sure the parent dir exists
229		local parentDir = $(directory:PG=dir) ;
230		Depends $(directory) : $(parentDir) ;
231		MkDir $(parentDir) ;
232
233		NoUpdate $(directory) ;
234		Depends $(directory) : $(zipFile) ;
235		UnzipArchive1 $(directory) : $(zipFile) ;
236		INITIALIZED on $(directory) = 1 ;
237	}
238
239	# Use a dummy rule so that it looks to jam like the targets are actually
240	# built from the directory target.
241	UnzipArchiveDummy $(targets) : $(directory) ;
242
243	return $(targets) ;
244}
245
246actions UnzipArchive1
247{
248	mkdir -p $(1[1])
249	unzip -q -u -o -d $(1[1]) $(2)
250}
251
252actions UnzipArchiveDummy
253{
254}
255
256
257rule ObjectReference
258{
259	# ObjectReference <reference object> : <source object>
260	# Makes <reference object> refer to the same file as <source object>.
261	# The filenames must of course be identical.
262	# <source object> must have already been LOCATEd.
263
264	local ref = $(1) ;
265	local source = $(2) ;
266	if $(ref) != $(source) {
267		Depends $(ref) : $(source) ;
268		LOCATE on $(ref) = [ on $(source) return $(LOCATE) ] ;
269	}
270}
271
272rule ObjectReferences
273{
274	# ObjectReferences <source objects>
275	# Creates local references to <source objects>, i.e. identifiers with the
276	# current grist referring to the same files. <source objects> must have
277	# already been LOCATEd.
278
279	local source ;
280	for source in $(1) {
281		ObjectReference [ FGristFiles $(source) ] : $(source) ;
282	}
283}
284
285rule CopySetHaikuRevision target : source
286{
287	# CopySetHaikuRevision <target> : <source>
288	#
289	# Copy <source> to <target>, writing the SVN revision of the working root
290	# directory into the haiku revision section of <target>.
291	#
292	# <target> - Output file target. Gristed and located target.
293	# <source> - ELF object to be copied. Gristed and located target.
294
295	# If existent, make the target depend on the .svn/entries file in the
296	# root directory, so it gets updated when the revision changes due to
297	# "svn up".
298	if [ Glob [ FDirName $(HAIKU_TOP) .svn ] : entries ] {
299		local svnEntries = <haiku-rootdir-svn>entries ;
300		SEARCH on $(svnEntries) = [ FDirName $(HAIKU_TOP) .svn ] ;
301		Depends $(target) : $(svnEntries) ;
302	} else if [ Glob [ FDirName $(HAIKU_TOP) .git ] : index ] {
303		local gitIndex = <haiku-rootdir-git>index ;
304		SEARCH on $(gitIndex) = [ FDirName $(HAIKU_TOP) .git ] ;
305		Depends $(target) : $(gitIndex) ;
306	}
307
308	HAIKU_INCLUDE_IN_IMAGE on $(target)
309		= [ on $(source) return $(HAIKU_INCLUDE_IN_IMAGE) ] ;
310
311	Depends $(target) : <build>copyattr <build>set_haiku_revision $(source) ;
312	CopySetHaikuRevision1 $(target)
313		: <build>copyattr <build>set_haiku_revision $(source) ;
314}
315
316actions CopySetHaikuRevision1
317{
318	$(HOST_ADD_BUILD_COMPATIBILITY_LIB_DIR)
319	# Try svn or git-svn
320	# Extract from "Revision: 12345" line
321	revision=`(LC_ALL=C LANG=C svn info $(HAIKU_TOP) ||
322		(cd $(HAIKU_TOP) && [ -d .git/svn ] && LC_ALL=C LANG=C git svn info)) 2> /dev/null |
323			grep Revision | awk '{printf $2}'`
324	if [ "$revision" = 0 -o "$revision" = "" ]; then
325		# git-svn not present or not configured for this repository
326		# Try searching git logs for last git-svn commit
327		# Extract from " git-svn-id: .../haiku/trunk@12345 ..." line
328		revision=`cd $(HAIKU_TOP) &&
329			git log --max-count=1 --grep="git-svn-id:" 2> /dev/null |
330				grep "git-svn-id:" | cut -d '@' -f 2 |
331					awk '{printf $1}'`
332	fi
333	if [ "$revision" = "" ]; then
334		revision=0
335	fi
336	$(2[1]) --data $(2[3]) $(1) &&
337	$(2[2]) $(1) ${revision}
338}
339
340rule DataFileToSourceFile sourceFile : dataFile : dataVariable : sizeVariable
341{
342	sourceFile = [ FGristFiles $(sourceFile) ] ;
343	MakeLocateCommonPlatform $(sourceFile) ;
344
345	sizeVariable ?= $(dataVariable)Size ;
346
347	DATA_VARIABLE on $(sourceFile) = $(dataVariable) ;
348	SIZE_VARIABLE on $(sourceFile) = $(sizeVariable) ;
349
350	Depends $(sourceFile) : <build>data_to_source $(dataFile) ;
351	DataFileToSourceFile1 $(sourceFile) : <build>data_to_source $(dataFile) ;
352	LocalClean clean : $(sourceFile) ;
353}
354
355actions DataFileToSourceFile1
356{
357	$(HOST_ADD_BUILD_COMPATIBILITY_LIB_DIR)
358	$(2[1]) $(DATA_VARIABLE) $(SIZE_VARIABLE) $(2[2]) $(1)
359}
360
361rule DownloadFile target : url
362{
363	URL on $(target) = $(url) ;
364
365	DownloadFile1 $(target) ;
366}
367
368actions DownloadFile1
369{
370	wget -O $(1) $(URL)
371}
372
373rule DownloadOptionalPackage package : url
374{
375	# download zip file
376	local zipFile = $(package:G=download).zip ;
377
378	# Request the download only once.
379	if [ on $(zipFile) return $(HAIKU_OPTIONAL_PACKAGE_DOWNLOAD) ] {
380		return $(zipFile) ;
381	}
382
383	HAIKU_OPTIONAL_PACKAGE_DOWNLOAD on $(zipFile) = 1 ;
384
385	MakeLocate $(zipFile) : $(HAIKU_DOWNLOAD_DIR) ;
386	DownloadFile $(zipFile) : $(url) ;
387
388	return $(zipFile) ;
389}
390