e359ca4f09eb37adc5db56dfdd893f6027134dd5
[supertux.git] / mk / jam / helper.jam
1 #============================================================================
2 # Helper rules
3 #============================================================================
4
5 CP ?= "cp" ;
6 MV ?= "mv" ;
7
8 ##  Wildcard [ dir : ] patterns
9 ##    Create a list of files in a directory which match the pattern. You can
10 ##    optionally specify a subdirectory. The files will be returned with
11 ##    stripped pathnames. The difference from GLOB is that this rule respects
12 ##    subdirectories which may have been entered with the SubDir rule.
13 rule Wildcard
14 {
15   local files dir sdir wildcards ;
16   
17   # Is a directory given?
18   if $(>) {
19     dir = $(<)/ ;
20     sdir = $(<) ;
21     wildcards = $(>) ;
22   } else {
23     dir = "" ;
24     sdir = "" ;
25     wildcards = $(<) ;
26   }
27
28   files = [ GLOB [ ConcatDirs $(SUBDIR) $(dir) ] : $(wildcards) ] ;
29
30   return $(files:BSR=$(sdir)) ;
31 }
32
33 ##  Prefix list : prefix
34 ##    Adds a prefix to a all elements in list.
35 rule Prefix
36 {
37   return $(>)$(<) ;
38 }
39
40 if $(JAMVERSION) >= 2.5
41 {
42
43 ##  IsElem element : list
44 ##    Returns "true" if the elemnt is in the list. Otherwise nothing is
45 ##    returned.
46 rule IsElem
47 {
48   local i ;
49
50   for i in $(>)
51   {
52     if $(i) = $(<)
53     {
54       return "true" ;
55     }
56   }
57
58   return ;
59 }
60
61 }
62 else
63 {
64
65 # jam<2.4's return statement doesn't exit the function
66 rule IsElem
67 {
68   local i result ;
69
70   for i in $(>)
71   {
72     if $(i) = $(<)
73     {
74       result = "true" ;
75       $(>) = ;
76     }
77   }
78
79   return $(result) ;
80 }
81
82 }
83
84 ##  Filter list : filter
85 ##    Returns the list without the words contained in filter.
86 rule Filter
87 {
88   local i result ;
89
90   for i in $(<)
91   {
92     if ! [ IsElem $(i) : $(>) ]
93     {
94       result += $(i) ;
95     }
96   }
97
98   return $(result) ;
99 }
100
101 ##  RemoveDups list
102 ##    Removes duplicates in the list (this function tries to preserve the list
103 ##    order)
104 rule RemoveDups
105 {
106   local i result ;
107
108   for i in $(<)
109   {
110     if ! [ IsElem $(i) : $(result) ]
111     {
112       result += $(i) ;
113     }  
114   }
115
116   return $(result) ;
117
118
119 rule Reverse
120 {
121   local result ;
122   
123   for i in $(<)
124   {
125     result = $(i) $(result) ;
126   }
127   return $(result) ;
128 }
129
130 ##  GetVar argument
131 ##    Simply returns the value of the variable with name argument.
132 ##    This is useful to query on target variables:
133 ##       bla = [ on TARGET GetVar CFlags ] ;
134 rule GetVar
135 {
136   return $($(<)) ;
137 }
138
139 ##  ConcatDirs dirs
140 ##    Concatenates a set of directories. This is a substitute for FDirName in
141 ##    Jambase. It works also correctly for several rooted paths, where FDirName
142 ##    fails.
143 ##    The advantage over $(dir1)/$(dir2) is that this also works correctly if
144 ##    $(dir1) or $(dir2) is not set.
145 rule ConcatDirs
146 {
147   local i ;
148   local result = $(<[1]) ;
149   if ! $(result) { $result = "" ; }
150   local dir1 dir2 ;
151
152   for i in $(<[2-])
153   {
154     # eleminate multiple slashes because jam is somewhat buggy here
155     dir1 = [ MATCH (.*[^/]?) : $(result) ] ;
156     dir2 = [ MATCH ([^/].*) : $(i) ] ;
157     if ! $(dir1) { dir1 = "" ; }
158     if $(dir1) != "" { dir1 = $(dir1)/ ; }
159     if ! $(dir2) { dir2 = "" ; }
160     result = $(dir1)$(dir2) ;
161   }
162
163   return $(result) ;
164 }
165
166 ##  Copy target : source
167 ##    Copy source file to target.
168 actions Copy
169 {
170   $(CP) "$(>)" "$(<)"
171 }
172
173 actions ignore Move
174 {
175   $(MV) $(>) $(<)
176 }
177