added jam build system, please try it out - the advantage would be that it already...
[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.  Finally, if jam must deal with 
79     # header files of the same name in different directories,
80     # they can be distinguished with HDRGRIST.
81
82     # $(SEARCH_SOURCE:E) is where cc first looks for #include 
83     # "foo.h" files.  If the source file is in a distant directory, 
84     # look there.  Else, look in "" (the current directory).
85     if $(HDRRULE_$(<:S))
86     {
87         HDRS on $(<) = [ ConcatDirs $(SUBDIR) $(<:D) ]
88             $(SEARCH_SOURCE:E) $(HDRS) $(STDHDRS) ;
89         HDRGRIST on $(<) = $(HDRGRIST) ;                               
90         HDRRULE on $(<) = $(HDRRULE_$(<:S)) ;
91         HDRSCAN on $(<) = $(HDRPATTERN_$(<:S)) ;
92     }
93
94     local targets ;
95     # Invoke filetype specific rule
96     if $(FILETYPE_$(<:S))
97     {
98         targets = [ $(FILETYPE_$(<:S)) $(<) : $(2) ] ;
99     }
100     else
101     {
102         echo "Warning: no rules for filetype $(>:S) defined (at file $(>))." ;
103     }
104
105     if $(targets)
106     {
107         # construct clean target
108         Clean clean : $(targets) ;
109     }
110
111     return $(targets) ;
112 }
113
114 ##  HeaderRule source : headers
115 ##    This rule is the default header rule used by the objects rules. You
116 ##    might register custom rules with the RegisterHeaderRule rule.
117 rule HeaderRule
118 {
119     # N.B.  This rule is called during binding, potentially after
120     # the fate of many targets has been determined, and must be
121     # used with caution: don't add dependencies to unrelated
122     # targets, and don't set variables on $(<).
123                                                                                 
124     # Tell Jam that anything depending on $(<) also depends on $(>),
125     # set SEARCH so Jam can find the headers, but then say we don't
126     # care if we can't actually find the headers (they may have been
127     # within ifdefs),
128     local s = $(>:G=$(HDRGRIST:E)) ;
129
130     Includes $(<) : $(s) ;
131     SEARCH on $(s) = $(HDRS) ;
132     NoCare $(s) ;
133  
134     local i ;
135     for i in $(s)
136     {
137         HDRGRIST on $(s) = $(HDRGRIST) ;
138         HDRS on $(s) = $(HDRS) ;
139         HDRRULE on $(s) = [ on $(<) GetVar HDRRULE ] ;
140         HDRSCAN on $(s) = [ on $(<) GetVar HDRPATTERN ] ;
141     }
142 }
143
144 if $(JAMVERSION) < 2.5
145 {
146 ## XXX XXX XXX a bug in jam 2.4 let's the version above fail. I'll let this
147 ##    non-optimal version in here until jam 2.5 is out.
148
149 rule HeaderRule
150 {
151     local s = $(>:G=$(HDRGRIST:E)) ;
152
153     Includes $(<) : $(s) ;
154     SEARCH on $(s) = $(HDRS) ;
155     NoCare $(s) ;
156  
157     local i ;
158     for i in $(s)
159     {
160         if $(HDRRULE_$(i:S))
161         {
162             HDRGRIST on $(s) = $(HDRGRIST) ;
163             HDRS on $(s) = $(HDRS) ;
164             HDRRULE on $(s) = $(HDRRULE_$(i:S)) ;
165             HDRSCAN on $(s) = $(HDRPATTERN_$(i:S)) ;
166         }
167         else if $(JAM_DEBUG)
168         {
169             #echo "No Header rule for $(i:S) file $(i) " ;
170         }
171     }
172 }
173
174 } # end of if $(JAMVERSION) < 1.5
175
176 # Dummy rule: .o files are used as is.
177 rule UseObjectFile
178 {
179     return $(<) ;
180 }
181 RegisterFileType UseObjectFile : .o ;
182
183 # Ignore header files.
184 rule UseHeaderFile
185 {
186     return ;
187 }
188 RegisterFileType UseHeaderFile : .h .hpp ;
189 RegisterHeaderRule HeaderRule : $(HDRPATTERN) : .h .hpp .inc ;
190
191 # Generates a grist suitable for output objects based on
192 # SUBVARIANT and SUBDIR variable.
193 rule DoObjectGrist
194 {
195     return $(<:G=$(SOURCE_GRIST:E)!$(SUBVARIANT)) ;
196 }
197
198 # Generates a grist suitable for source files based on SUBDIR variable.
199 rule DoSourceGrist
200 {
201     return $(<:G=$(SOURCE_GRIST:E)) ;
202 }
203