]>
Commit | Line | Data |
---|---|---|
6a161fa9 | 1 | #! /usr/bin/ruby -w |
2 | # | |
5822ff87 | 3 | # Copyright (C) 2002,2003,2004,2005 Free Software Foundation, Inc. |
6a161fa9 | 4 | # |
5 | # This genmk.rb is free software; the author | |
6 | # gives unlimited permission to copy and/or distribute it, | |
7 | # with or without modifications, as long as this notice is preserved. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY, to the extent permitted by law; without | |
11 | # even the implied warranty of MERCHANTABILITY or FITNESS FOR A | |
12 | # PARTICULAR PURPOSE. | |
13 | ||
14 | module Enumerable | |
15 | def collect_with_index | |
16 | ret = [] | |
17 | self.each_with_index do |item, index| | |
18 | ret.push(yield(item, index)) | |
19 | end | |
20 | ret | |
21 | end | |
22 | end | |
23 | ||
24 | class String | |
25 | def to_var | |
26 | self.gsub(/[^a-zA-Z0-9_@]/, '_') | |
27 | end | |
28 | ||
29 | def suffix(str) | |
30 | self.sub(/\.[^\.]*$/, '') + '.' + str | |
31 | end | |
32 | ||
33 | def to_obj | |
34 | self.sub(/\.[^\.]*$/, '').to_var + '.o' | |
35 | end | |
36 | end | |
37 | ||
38 | class Image | |
39 | def initialize(dir, name) | |
40 | @dir = dir | |
41 | @name = name | |
42 | end | |
43 | attr_reader :dir, :name | |
44 | ||
45 | def rule(sources) | |
46 | prefix = @name.to_var | |
47 | exe = @name.suffix('exec') | |
48 | objs = sources.collect do |src| | |
49 | raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src | |
50 | prefix + '-' + src.to_obj | |
51 | end | |
52 | objs_str = objs.join(' ') | |
53 | deps = objs.collect {|obj| obj.suffix('d')} | |
54 | deps_str = deps.join(' ') | |
55 | ||
56 | "CLEANFILES += #{@name} #{exe} #{objs_str} | |
57 | MOSTLYCLEANFILES += #{deps_str} | |
58 | ||
59 | #{@name}: #{exe} | |
60 | $(OBJCOPY) -O binary -R .note -R .comment $< $@ | |
61 | ||
62 | #{exe}: #{objs_str} | |
1f5ab428 | 63 | $(CC) -o $@ $^ $(LDFLAGS) $(#{prefix}_LDFLAGS) |
6a161fa9 | 64 | |
65 | " + objs.collect_with_index do |obj, i| | |
66 | src = sources[i] | |
67 | fake_obj = File.basename(src).suffix('o') | |
68 | dep = deps[i] | |
69 | flag = if /\.c$/ =~ src then 'CFLAGS' else 'ASFLAGS' end | |
70 | extra_flags = if /\.S$/ =~ src then '-DASM_FILE=1' else '' end | |
71 | dir = File.dirname(src) | |
72 | ||
73 | "#{obj}: #{src} | |
74 | $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) #{extra_flags} $(#{flag}) $(#{prefix}_#{flag}) -c -o $@ $< | |
75 | ||
76 | #{dep}: #{src} | |
77 | set -e; \ | |
78 | $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) #{extra_flags} $(#{flag}) $(#{prefix}_#{flag}) -M $< \ | |
79 | | sed 's,#{Regexp.quote(fake_obj)}[ :]*,#{obj} $@ : ,g' > $@; \ | |
80 | [ -s $@ ] || rm -f $@ | |
81 | ||
82 | -include #{dep} | |
83 | ||
84 | " | |
85 | end.join('') | |
86 | end | |
87 | end | |
88 | ||
89 | # Use PModule instead Module, to avoid name conflicting. | |
90 | class PModule | |
91 | def initialize(dir, name) | |
92 | @dir = dir | |
93 | @name = name | |
94 | end | |
95 | attr_reader :dir, :name | |
96 | ||
97 | def rule(sources) | |
98 | prefix = @name.to_var | |
99 | objs = sources.collect do |src| | |
100 | raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src | |
101 | prefix + '-' + src.to_obj | |
102 | end | |
103 | objs_str = objs.join(' ') | |
104 | deps = objs.collect {|obj| obj.suffix('d')} | |
105 | deps_str = deps.join(' ') | |
106 | pre_obj = 'pre-' + @name.suffix('o') | |
107 | mod_src = 'mod-' + @name.suffix('c') | |
108 | mod_obj = mod_src.suffix('o') | |
109 | defsym = 'def-' + @name.suffix('lst') | |
110 | undsym = 'und-' + @name.suffix('lst') | |
111 | mod_name = File.basename(@name, '.mod') | |
5822ff87 | 112 | symbolic_name = mod_name.sub(/\.[^\.]*$/, '') |
6a161fa9 | 113 | |
114 | "CLEANFILES += #{@name} #{mod_obj} #{mod_src} #{pre_obj} #{objs_str} #{defsym} #{undsym} | |
115 | MOSTLYCLEANFILES += #{deps_str} | |
116 | DEFSYMFILES += #{defsym} | |
117 | UNDSYMFILES += #{undsym} | |
118 | ||
119 | #{@name}: #{pre_obj} #{mod_obj} | |
120 | -rm -f $@ | |
59b8208a | 121 | $(LD) $(#{prefix}_LDFLAGS) $(LDFLAGS) -r -d -o $@ $^ |
4b13b216 | 122 | $(STRIP) --strip-unneeded -K grub_mod_init -K grub_mod_fini -R .note -R .comment $@ |
6a161fa9 | 123 | |
124 | #{pre_obj}: #{objs_str} | |
125 | -rm -f $@ | |
59b8208a | 126 | $(LD) $(#{prefix}_LDFLAGS) -r -d -o $@ $^ |
6a161fa9 | 127 | |
128 | #{mod_obj}: #{mod_src} | |
62ddcc8f | 129 | $(CC) $(CPPFLAGS) $(CFLAGS) $(#{prefix}_CFLAGS) -c -o $@ $< |
6a161fa9 | 130 | |
131 | #{mod_src}: moddep.lst genmodsrc.sh | |
132 | sh $(srcdir)/genmodsrc.sh '#{mod_name}' $< > $@ || (rm -f $@; exit 1) | |
133 | ||
134 | #{defsym}: #{pre_obj} | |
135 | $(NM) -g --defined-only -P -p $< | sed 's/^\\([^ ]*\\).*/\\1 #{mod_name}/' > $@ | |
136 | ||
137 | #{undsym}: #{pre_obj} | |
138 | echo '#{mod_name}' > $@ | |
98d15063 | 139 | $(NM) -u -P -p $< | cut -f1 -d' ' >> $@ |
6a161fa9 | 140 | |
141 | " + objs.collect_with_index do |obj, i| | |
142 | src = sources[i] | |
143 | fake_obj = File.basename(src).suffix('o') | |
233b1628 | 144 | command = 'cmd-' + obj.suffix('lst') |
145 | fs = 'fs-' + obj.suffix('lst') | |
6a161fa9 | 146 | dep = deps[i] |
147 | flag = if /\.c$/ =~ src then 'CFLAGS' else 'ASFLAGS' end | |
148 | dir = File.dirname(src) | |
149 | ||
150 | "#{obj}: #{src} | |
151 | $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(#{flag}) $(#{prefix}_#{flag}) -c -o $@ $< | |
152 | ||
153 | #{dep}: #{src} | |
154 | set -e; \ | |
155 | $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(#{flag}) $(#{prefix}_#{flag}) -M $< \ | |
156 | | sed 's,#{Regexp.quote(fake_obj)}[ :]*,#{obj} $@ : ,g' > $@; \ | |
157 | [ -s $@ ] || rm -f $@ | |
158 | ||
159 | -include #{dep} | |
160 | ||
39c9d41d | 161 | CLEANFILES += #{command} #{fs} |
7b1f4b57 | 162 | COMMANDFILES += #{command} |
39c9d41d | 163 | FSFILES += #{fs} |
7b1f4b57 | 164 | |
165 | #{command}: #{src} gencmdlist.sh | |
166 | set -e; \ | |
167 | $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(#{flag}) $(#{prefix}_#{flag}) -E $< \ | |
5822ff87 | 168 | | sh $(srcdir)/gencmdlist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1) |
7b1f4b57 | 169 | |
39c9d41d | 170 | #{fs}: #{src} genfslist.sh |
171 | set -e; \ | |
172 | $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(#{flag}) $(#{prefix}_#{flag}) -E $< \ | |
173 | | sh $(srcdir)/genfslist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1) | |
174 | ||
175 | ||
6a161fa9 | 176 | " |
177 | end.join('') | |
178 | end | |
179 | end | |
180 | ||
181 | class Utility | |
182 | def initialize(dir, name) | |
183 | @dir = dir | |
184 | @name = name | |
185 | end | |
186 | attr_reader :dir, :name | |
187 | ||
188 | def rule(sources) | |
189 | prefix = @name.to_var | |
190 | objs = sources.collect do |src| | |
e56cdf21 | 191 | raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src |
6a161fa9 | 192 | prefix + '-' + src.to_obj |
193 | end | |
194 | objs_str = objs.join(' '); | |
195 | deps = objs.collect {|obj| obj.suffix('d')} | |
196 | deps_str = deps.join(' '); | |
197 | ||
198 | "CLEANFILES += #{@name} #{objs_str} | |
199 | MOSTLYCLEANFILES += #{deps_str} | |
200 | ||
201 | #{@name}: #{objs_str} | |
1f5ab428 | 202 | $(BUILD_CC) -o $@ $^ $(BUILD_LDFLAGS) $(#{prefix}_LDFLAGS) |
6a161fa9 | 203 | |
204 | " + objs.collect_with_index do |obj, i| | |
205 | src = sources[i] | |
206 | fake_obj = File.basename(src).suffix('o') | |
207 | dep = deps[i] | |
208 | dir = File.dirname(src) | |
209 | ||
210 | "#{obj}: #{src} | |
4b13b216 | 211 | $(BUILD_CC) -I#{dir} -I$(srcdir)/#{dir} $(BUILD_CPPFLAGS) $(BUILD_CFLAGS) -DGRUB_UTIL=1 $(#{prefix}_CFLAGS) -c -o $@ $< |
6a161fa9 | 212 | |
213 | #{dep}: #{src} | |
214 | set -e; \ | |
4b13b216 | 215 | $(BUILD_CC) -I#{dir} -I$(srcdir)/#{dir} $(BUILD_CPPFLAGS) $(BUILD_CFLAGS) -DGRUB_UTIL=1 $(#{prefix}_CFLAGS) -M $< \ |
6a161fa9 | 216 | | sed 's,#{Regexp.quote(fake_obj)}[ :]*,#{obj} $@ : ,g' > $@; \ |
217 | [ -s $@ ] || rm -f $@ | |
218 | ||
219 | -include #{dep} | |
220 | ||
221 | " | |
222 | end.join('') | |
223 | end | |
224 | end | |
225 | ||
924b6140 | 226 | class Program |
227 | def initialize(dir, name) | |
228 | @dir = dir | |
229 | @name = name | |
230 | end | |
231 | attr_reader :dir, :name | |
232 | ||
233 | def rule(sources) | |
234 | prefix = @name.to_var | |
235 | objs = sources.collect do |src| | |
236 | raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src | |
237 | prefix + '-' + src.to_obj | |
238 | end | |
239 | objs_str = objs.join(' '); | |
240 | deps = objs.collect {|obj| obj.suffix('d')} | |
241 | deps_str = deps.join(' '); | |
242 | ||
243 | "CLEANFILES += #{@name} #{objs_str} | |
244 | MOSTLYCLEANFILES += #{deps_str} | |
245 | ||
246 | #{@name}: #{objs_str} | |
121c1d83 | 247 | $(CC) -o $@ $^ $(LDFLAGS) $(#{prefix}_LDFLAGS) |
924b6140 | 248 | |
249 | " + objs.collect_with_index do |obj, i| | |
250 | src = sources[i] | |
251 | fake_obj = File.basename(src).suffix('o') | |
252 | dep = deps[i] | |
253 | dir = File.dirname(src) | |
254 | ||
255 | "#{obj}: #{src} | |
121c1d83 | 256 | $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(CFLAGS) $(#{prefix}_CFLAGS) -c -o $@ $< |
924b6140 | 257 | |
258 | #{dep}: #{src} | |
259 | set -e; \ | |
121c1d83 | 260 | $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(CFLAGS) $(#{prefix}_CFLAGS) -M $< \ |
924b6140 | 261 | | sed 's,#{Regexp.quote(fake_obj)}[ :]*,#{obj} $@ : ,g' > $@; \ |
262 | [ -s $@ ] || rm -f $@ | |
263 | ||
264 | -include #{dep} | |
265 | ||
266 | " | |
267 | end.join('') | |
268 | end | |
269 | end | |
270 | ||
075a1650 | 271 | class Script |
272 | def initialize(dir, name) | |
273 | @dir = dir | |
274 | @name = name | |
275 | end | |
276 | attr_reader :dir, :name | |
277 | ||
278 | def rule(sources) | |
279 | if sources.length != 1 | |
280 | raise "only a single source file must be specified for a script" | |
281 | end | |
282 | src = sources[0] | |
283 | if /\.in$/ !~ src | |
284 | raise "unknown source file `#{src}'" | |
285 | end | |
286 | ||
287 | "CLEANFILES += #{@name} | |
288 | ||
289 | #{@name}: #{src} config.status | |
290 | ./config.status --file=#{name}:#{src} | |
f4917dfd | 291 | chmod +x $@ |
075a1650 | 292 | |
293 | " | |
294 | end | |
295 | end | |
296 | ||
6a161fa9 | 297 | images = [] |
298 | utils = [] | |
299 | pmodules = [] | |
924b6140 | 300 | programs = [] |
075a1650 | 301 | scripts = [] |
6a161fa9 | 302 | |
303 | cont = false | |
304 | s = nil | |
305 | while l = gets | |
306 | if cont | |
307 | s += l | |
308 | else | |
309 | s = l | |
310 | end | |
311 | ||
312 | print l | |
313 | cont = (/\\$/ =~ l) | |
314 | unless cont | |
315 | s.gsub!(/\\\n/, ' ') | |
316 | ||
233b1628 | 317 | if /^([a-zA-Z0-9_]+)\s*\+?=\s*(.*?)\s*$/ =~ s |
6a161fa9 | 318 | var, args = $1, $2 |
319 | ||
320 | if var =~ /^([a-zA-Z0-9_]+)_([A-Z]+)$/ | |
321 | prefix, type = $1, $2 | |
322 | ||
323 | case type | |
324 | when 'IMAGES' | |
325 | images += args.split(/\s+/).collect do |img| | |
326 | Image.new(prefix, img) | |
327 | end | |
328 | ||
329 | when 'MODULES' | |
330 | pmodules += args.split(/\s+/).collect do |pmod| | |
331 | PModule.new(prefix, pmod) | |
332 | end | |
333 | ||
334 | when 'UTILITIES' | |
335 | utils += args.split(/\s+/).collect do |util| | |
336 | Utility.new(prefix, util) | |
337 | end | |
338 | ||
924b6140 | 339 | when 'PROGRAMS' |
075a1650 | 340 | programs += args.split(/\s+/).collect do |prog| |
341 | Program.new(prefix, prog) | |
342 | end | |
343 | ||
344 | when 'SCRIPTS' | |
345 | scripts += args.split(/\s+/).collect do |script| | |
346 | Script.new(prefix, script) | |
924b6140 | 347 | end |
348 | ||
6a161fa9 | 349 | when 'SOURCES' |
350 | if img = images.detect() {|i| i.name.to_var == prefix} | |
351 | print img.rule(args.split(/\s+/)) | |
352 | elsif pmod = pmodules.detect() {|m| m.name.to_var == prefix} | |
353 | print pmod.rule(args.split(/\s+/)) | |
354 | elsif util = utils.detect() {|u| u.name.to_var == prefix} | |
355 | print util.rule(args.split(/\s+/)) | |
924b6140 | 356 | elsif program = programs.detect() {|u| u.name.to_var == prefix} |
357 | print program.rule(args.split(/\s+/)) | |
075a1650 | 358 | elsif script = scripts.detect() {|s| s.name.to_var == prefix} |
359 | print script.rule(args.split(/\s+/)) | |
6a161fa9 | 360 | end |
361 | end | |
362 | end | |
363 | ||
364 | end | |
365 | ||
366 | end | |
367 | ||
368 | end | |
369 |