furhter improve collision detection by reintroducing time of collision, still more...
[supertux.git] / mk / jam / objects.jam
1 #============================================================================
2 # Rules for compiling a set of sources to object files
3 #============================================================================
4 # These are slightly modified versions of the Object and Objects rules from
5 # jam. The problem with the original rules in Jambase is the handling of
6 # custom file types. The solution with the UserObject rule is monolithic, you
7 # can only have 1 such rule. Thus we construct a more flexible toolkit here
8 # which let's you register rules for certain filetypes.
9
10 ##  RegisterFileType Rulename : extensions
11 ##    Register a rule which is used to compile a filetype into object
12 ##    files. The registered rule is called with the name of the
13 ##    sourcefile as argument and should return a list of objectfiles which are
14 ##    created. You should set the grist of the object files by using the
15 ##    DoObjectGrist function.
16 rule RegisterFileType
17 {
18     local suffix ;
19     for suffix in $(>)
20     {
21         FILETYPE_$(suffix) = $(<) ;
22     }
23 }
24
25 ##  RegisterHeaderRule rulename : regexpattern : extensions
26 ##    Registers a rule and a regular expression which will be used for header
27 ##    file scanning of the specified extensions.
28 rule RegisterHeaderRule
29 {
30     local suffix ;
31     for suffix in $(3)
32     {
33         HDRRULE_$(suffix) = $(<) ;
34         HDRPATTERN_$(suffix) = $(>) ;
35     }
36 }
37
38 ##  CompileObjects sources [ : options ]
39 ##    Compile a set of sourcefiles into objectfiles (extension: SUFOBJ,
40 ##    usually .o). This rule takes care of setting LOCATE and SEARCH
41 ##    variables to the $(SEARCH_SOURCE) and $(LOCATE_SOURCE) variables.
42 ##    The Application, Plugin and Library rules already use this rule
43 ##    internally. You should only use this rule if you have to avoid the
44 ##    Application, Plugin or Library rules.
45 rule CompileObjects
46 {
47     local source ;
48     local targets ;
49
50     # Search the source
51     SEARCH on $(<) = $(SEARCH_SOURCE) ;      
52
53     for source in $(<)
54     {
55         # compile the sourcefile to targetfile
56         targets += [ CompileObject $(source) : $(2) ] ;
57     }
58   
59     # locate the targets
60     MakeLocate $(targets) : $(LOCATE_TARGET) ;
61
62     return $(targets) ;
63 }
64
65 #----------------------------------------------------------------------------
66 # private part
67
68 # CompileObject sourcefile [ : options ]
69 # helper rule: Compiles a source file to an object file. Does header file
70 # scanning, sets LOCATE and SEARCH for source and target, grists the files
71 # with the current subdir and searches for the correct registered rule.
72 rule CompileObject
73 {
74     # handle #includes for source: Jam scans for headers with
75     # the regexp pattern $(HDRSCAN) and then invokes $(HDRRULE)
76     # with the scanned file as the target and the found headers
77     # as the sources.  HDRSEARCH is the value of SEARCH used for
78     # the found header files.
79
80     # $(SEARCH_SOURCE:E) is where cc first looks for #include 
81     # "foo.h" files.  If the source file is in a distant directory, 
82     # look there.  Else, look in "" (the current directory).
83     if $(HDRRULE_$(<:S))
84     {
85         HDRSEARCH on $(<) = $(SEARCH_SOURCE:E) $(HDRSEARCH) $(STDHDRSEARCH) ;
86         SEARCH_SOURCE on $(<) = $(SEARCH_SOURCE) ;
87         HDRRULE on $(<) = $(HDRRULE_$(<:S)) ;
88         HDRSCAN on $(<) = $(HDRPATTERN_$(<:S)) ;
89     }
90
91     local targets ;
92     # Invoke filetype specific rule
93     if $(FILETYPE_$(<:S))
94     {
95         targets = [ $(FILETYPE_$(<:S)) $(<) : $(2) ] ;
96     }
97     else
98     {
99         echo "Warning: no rules for filetype $(>:S) defined (at file $(>))." ;
100     }
101
102     if $(targets)
103     {
104         # construct clean target
105         Clean clean : $(targets) ;
106     }
107
108     return $(targets) ;
109 }
110
111 ##  HeaderRule source : headers
112 ##    This rule is the default header rule used by the objects rules. You
113 ##    might register custom rules with the RegisterHeaderRule rule.
114 rule HeaderRule
115 {
116     # N.B.  This rule is called during binding, potentially after
117     # the fate of many targets has been determined, and must be
118     # used with caution: don't add dependencies to unrelated
119     # targets, and don't set variables on $(<).
120                                                                                 
121     # Tell Jam that anything depending on $(<) also depends on $(>),
122     # set SEARCH so Jam can find the headers, but then say we don't
123     # care if we can't actually find the headers (they may have been
124     # within ifdefs),
125     local HDRSEARCH = [ on $(<) GetVar HDRSEARCH ] ;
126     local SEARCH_SOURCE = [ on $(<) GetVar SEARCH_SOURCE ] ;
127
128     Includes $(<) : $(>) ;
129     SEARCH on $(>) = $(HDRSEARCH) $(SEARCH_SOURCE)/$(<:D) ;
130     NoCare $(>) ;
131
132     local i ;
133     for i in $(>)
134     {
135
136         SEARCH on $(>) = $(HDRSEARCH) $(SEARCH_SOURCE)/$(<:D) ;
137         if $(i:D) = "" {
138             SEARCH_SOURCE on $(i) = $(SEARCH_SOURCE)/$(<:D) ;
139         } else {
140             SEARCH_SOURCE on $(i) = $(SEARCH_SOURCE) ;
141         }
142         HDRSEARCH on $(>) = $(HDRSEARCH) ;
143         HDRRULE on $(>) = [ on $(<) GetVar HDRRULE ] ;
144         HDRSCAN on $(>) = [ on $(<) GetVar HDRPATTERN ] ;
145     }
146 }
147
148 if $(JAMVERSION) < 2.5
149 {
150 ## XXX XXX XXX a bug in jam 2.4 let's the version above fail. I'll let this
151 ##    non-optimal version in here until jam 2.5 is out.
152
153 rule HeaderRule
154 {
155     local s = $(>:G=$(HDRGRIST)) ;
156
157     Includes $(<) : $(s) ;
158     SEARCH on $(s) = $(HDRSEARCH) ;
159     NoCare $(s) ;
160  
161     local i ;
162     for i in $(s)
163     {
164         if $(HDRRULE_$(i:S))
165         {
166             HDRGRIST on $(s) = $(HDRGRIST) ;
167             HDRSEARCH on $(s) = $(HDRSEARCH) ;
168             HDRRULE on $(s) = $(HDRRULE_$(i:S)) ;
169             HDRSCAN on $(s) = $(HDRPATTERN_$(i:S)) ;
170         }
171         else if $(JAM_DEBUG)
172         {
173             #echo "No Header rule for $(i:S) file $(i) " ;
174         }
175     }
176 }
177
178 } # end of if $(JAMVERSION) < 1.5
179
180 # Dummy rule: .o files are used as is.
181 rule UseObjectFile
182 {
183     return $(<) ;
184 }
185 RegisterFileType UseObjectFile : .o ;
186
187 # Ignore header files.
188 rule UseHeaderFile
189 {
190     return ;
191 }
192 RegisterFileType UseHeaderFile : .h .hpp ;
193 RegisterHeaderRule HeaderRule : $(HDRPATTERN) : .h .hpp .inc ;
194
195 # Generates a grist suitable for output objects based on
196 # SUBVARIANT and SUBDIR variable.
197 rule DoObjectGrist
198 {
199     return $(<:G=$(SOURCE_GRIST:E)!$(SUBVARIANT)) ;
200 }
201
202 # Generates a grist suitable for source files based on SUBDIR variable.
203 rule DoSourceGrist
204 {
205     return $(<:G=$(SOURCE_GRIST:E)) ;
206 }
207