3 # Copyright (C) 2002,2003,2004,2005,2006,2007,2008 Free Software Foundation, Inc.
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.
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
15 def collect_with_index
17 self.each_with_index do |item, index|
18 ret.push(yield(item, index))
26 self.gsub(/[^a-zA-Z0-9_@]/, '_')
30 self.sub(/\.[^\.]*$/, '') + '.' + str
34 self.sub(/\.[^\.]*$/, '').to_var + '.o'
39 def initialize(dir, name)
43 attr_reader :dir, :name
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
52 objs_str = objs.join(' ')
53 deps = objs.collect {|obj| obj.suffix('d')}
54 deps_str = deps.join(' ')
56 "CLEANFILES += #{@name} #{exe} #{objs_str}
57 MOSTLYCLEANFILES += #{deps_str}
59 ifneq ($(TARGET_APPLE_CC),1)
61 $(OBJCOPY) -O $(#{prefix}_FORMAT) --strip-unneeded -R .note -R .comment -R .note.gnu.build-id $< $@
63 ifneq (#{exe},kernel.exec)
64 #{@name}: #{exe} ./grub-macho2img
65 ./grub-macho2img $< $@
67 #{@name}: #{exe} ./grub-macho2img
68 ./grub-macho2img --bss $< $@
73 $(TARGET_CC) -o $@ $^ $(TARGET_LDFLAGS) $(#{prefix}_LDFLAGS)
75 " + objs.collect_with_index do |obj, i|
77 fake_obj = File.basename(src).suffix('o')
79 flag = if /\.c$/ =~ src then 'CFLAGS' else 'ASFLAGS' end
80 extra_flags = if /\.S$/ =~ src then '-DASM_FILE=1' else '' end
81 dir = File.dirname(src)
83 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
84 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -MD -c -o $@ $<
92 # Use PModule instead Module, to avoid name conflicting.
94 def initialize(dir, name)
98 attr_reader :dir, :name
101 prefix = @name.to_var
102 objs = sources.collect do |src|
103 raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src
104 prefix + '-' + src.to_obj
106 objs_str = objs.join(' ')
107 deps = objs.collect {|obj| obj.suffix('d')}
108 deps_str = deps.join(' ')
109 pre_obj = 'pre-' + @name.suffix('o')
110 mod_src = 'mod-' + @name.suffix('c')
111 mod_obj = mod_src.suffix('o')
112 defsym = 'def-' + @name.suffix('lst')
113 undsym = 'und-' + @name.suffix('lst')
114 mod_name = File.basename(@name, '.mod')
115 symbolic_name = mod_name.sub(/\.[^\.]*$/, '')
117 "CLEANFILES += #{@name} #{mod_obj} #{mod_src} #{pre_obj} #{objs_str} #{undsym}
118 ifneq ($(#{prefix}_EXPORTS),no)
119 CLEANFILES += #{defsym}
120 DEFSYMFILES += #{defsym}
122 MOSTLYCLEANFILES += #{deps_str}
123 UNDSYMFILES += #{undsym}
125 ifneq ($(TARGET_APPLE_CC),1)
126 #{@name}: #{pre_obj} #{mod_obj} $(TARGET_OBJ2ELF)
128 $(TARGET_CC) $(#{prefix}_LDFLAGS) $(TARGET_LDFLAGS) -Wl,-r,-d -o $@ #{pre_obj} #{mod_obj}
129 if test ! -z $(TARGET_OBJ2ELF); then ./$(TARGET_OBJ2ELF) $@ || (rm -f $@; exit 1); fi
130 $(STRIP) --strip-unneeded -K grub_mod_init -K grub_mod_fini -K _grub_mod_init -K _grub_mod_fini -R .note -R .comment $@
132 #{@name}: #{pre_obj} #{mod_obj} $(TARGET_OBJ2ELF)
135 $(TARGET_CC) $(#{prefix}_LDFLAGS) $(TARGET_LDFLAGS) -Wl,-r,-d -o $@.bin #{pre_obj} #{mod_obj}
136 $(OBJCONV) -f$(TARGET_MODULE_FORMAT) -nr:_grub_mod_init:grub_mod_init -nr:_grub_mod_fini:grub_mod_fini -wd1106 -nu -nd $@.bin $@
140 #{pre_obj}: $(#{prefix}_DEPENDENCIES) #{objs_str}
142 $(TARGET_CC) $(#{prefix}_LDFLAGS) $(TARGET_LDFLAGS) -Wl,-r,-d -o $@ #{objs_str}
144 #{mod_obj}: #{mod_src}
145 $(TARGET_CC) $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) $(#{prefix}_CFLAGS) -c -o $@ $<
147 #{mod_src}: $(builddir)/moddep.lst $(srcdir)/genmodsrc.sh
148 sh $(srcdir)/genmodsrc.sh '#{mod_name}' $< > $@ || (rm -f $@; exit 1)
150 ifneq ($(#{prefix}_EXPORTS),no)
151 ifneq ($(TARGET_APPLE_CC),1)
152 #{defsym}: #{pre_obj}
153 $(NM) -g --defined-only -P -p $< | sed 's/^\\([^ ]*\\).*/\\1 #{mod_name}/' > $@
155 #{defsym}: #{pre_obj}
156 $(NM) -g -P -p $< | grep -E '^[a-zA-Z0-9_]* [TDS]' | sed 's/^\\([^ ]*\\).*/\\1 #{mod_name}/' > $@
160 #{undsym}: #{pre_obj}
161 echo '#{mod_name}' > $@
162 $(NM) -u -P -p $< | cut -f1 -d' ' >> $@
164 " + objs.collect_with_index do |obj, i|
166 fake_obj = File.basename(src).suffix('o')
167 command = 'cmd-' + obj.suffix('lst')
168 fs = 'fs-' + obj.suffix('lst')
169 partmap = 'partmap-' + obj.suffix('lst')
170 handler = 'handler-' + obj.suffix('lst')
171 parttool = 'parttool-' + obj.suffix('lst')
173 flag = if /\.c$/ =~ src then 'CFLAGS' else 'ASFLAGS' end
174 extra_flags = if /\.S$/ =~ src then '-DASM_FILE=1' else '' end
175 dir = File.dirname(src)
177 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
178 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -MD -c -o $@ $<
181 CLEANFILES += #{command} #{fs} #{partmap} #{handler} #{parttool}
182 COMMANDFILES += #{command}
184 PARTTOOLFILES += #{parttool}
185 PARTMAPFILES += #{partmap}
186 HANDLERFILES += #{handler}
188 #{command}: #{src} $(#{src}_DEPENDENCIES) gencmdlist.sh
190 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
191 | sh $(srcdir)/gencmdlist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
193 #{fs}: #{src} $(#{src}_DEPENDENCIES) genfslist.sh
195 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
196 | sh $(srcdir)/genfslist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
198 #{parttool}: #{src} $(#{src}_DEPENDENCIES) genparttoollist.sh
200 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
201 | sh $(srcdir)/genparttoollist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
203 #{partmap}: #{src} $(#{src}_DEPENDENCIES) genpartmaplist.sh
205 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
206 | sh $(srcdir)/genpartmaplist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
208 #{handler}: #{src} $(#{src}_DEPENDENCIES) genhandlerlist.sh
210 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
211 | sh $(srcdir)/genhandlerlist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
219 def initialize(dir, name)
223 attr_reader :dir, :name
226 prefix = @name.to_var
227 objs = sources.collect do |src|
228 raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src
229 prefix + '-' + src.to_obj
231 objs_str = objs.join(' ');
232 deps = objs.collect {|obj| obj.suffix('d')}
233 deps_str = deps.join(' ');
235 "CLEANFILES += #{@name}$(EXEEXT) #{objs_str}
236 MOSTLYCLEANFILES += #{deps_str}
238 #{@name}: $(#{prefix}_DEPENDENCIES) #{objs_str}
239 $(CC) -o $@ #{objs_str} $(LDFLAGS) $(#{prefix}_LDFLAGS)
241 " + objs.collect_with_index do |obj, i|
243 fake_obj = File.basename(src).suffix('o')
245 dir = File.dirname(src)
247 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
248 $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(CFLAGS) -DGRUB_UTIL=1 $(#{prefix}_CFLAGS) -MD -c -o $@ $<
257 def initialize(dir, name)
261 attr_reader :dir, :name
264 prefix = @name.to_var
265 objs = sources.collect do |src|
266 raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src
267 prefix + '-' + src.to_obj
269 objs_str = objs.join(' ');
270 deps = objs.collect {|obj| obj.suffix('d')}
271 deps_str = deps.join(' ');
273 "CLEANFILES += #{@name} #{objs_str}
274 MOSTLYCLEANFILES += #{deps_str}
276 #{@name}: $(#{prefix}_DEPENDENCIES) #{objs_str}
277 $(TARGET_CC) -o $@ #{objs_str} $(TARGET_LDFLAGS) $(#{prefix}_LDFLAGS)
279 " + objs.collect_with_index do |obj, i|
281 fake_obj = File.basename(src).suffix('o')
283 flag = if /\.c$/ =~ src then 'CFLAGS' else 'ASFLAGS' end
284 extra_flags = if /\.S$/ =~ src then '-DASM_FILE=1' else '' end
285 dir = File.dirname(src)
287 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
288 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -MD -c -o $@ $<
297 def initialize(dir, name)
301 attr_reader :dir, :name
304 if sources.length != 1
305 raise "only a single source file must be specified for a script"
309 raise "unknown source file `#{src}'"
312 "CLEANFILES += #{@name}
314 #{@name}: #{src} $(#{src}_DEPENDENCIES) config.status
315 ./config.status --file=#{name}:#{src}
330 print "# Generated by genmk.rb, please don't edit!\n"
344 str.gsub!(/\\\n/, ' ')
346 if /^([a-zA-Z0-9_]+)\s*\+?=\s*(.*?)\s*$/ =~ str
349 if var =~ /^([a-zA-Z0-9_]+)_([A-Z]+)$/
350 prefix, type = $1, $2
354 images += args.split(/\s+/).collect do |img|
355 Image.new(prefix, img)
359 pmodules += args.split(/\s+/).collect do |pmod|
360 PModule.new(prefix, pmod)
364 utils += args.split(/\s+/).collect do |util|
365 Utility.new(prefix, util)
369 programs += args.split(/\s+/).collect do |prog|
370 Program.new(prefix, prog)
374 scripts += args.split(/\s+/).collect do |script|
375 Script.new(prefix, script)
379 if img = images.detect() {|i| i.name.to_var == prefix}
380 print img.rule(args.split(/\s+/))
381 elsif pmod = pmodules.detect() {|m| m.name.to_var == prefix}
382 print pmod.rule(args.split(/\s+/))
383 elsif util = utils.detect() {|u| u.name.to_var == prefix}
384 print util.rule(args.split(/\s+/))
385 elsif program = programs.detect() {|u| u.name.to_var == prefix}
386 print program.rule(args.split(/\s+/))
387 elsif script = scripts.detect() {|s| s.name.to_var == prefix}
388 print script.rule(args.split(/\s+/))