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)
44 attr_reader :dir, :name
49 exe = @name.suffix('exec')
50 objs = sources.collect do |src|
51 raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src
52 prefix + '-' + src.to_obj
54 objs_str = objs.join(' ')
55 deps = objs.collect {|obj| obj.suffix('d')}
56 deps_str = deps.join(' ')
59 clean-image-#{@name}.#{@rule_count}:
60 rm -f #{@name} #{exe} #{objs_str}
62 CLEAN_IMAGE_TARGETS += clean-image-#{@name}.#{@rule_count}
64 mostlyclean-image-#{@name}.#{@rule_count}:
67 MOSTLYCLEAN_IMAGE_TARGETS += mostlyclean-image-#{@name}.#{@rule_count}
69 ifneq ($(TARGET_APPLE_CC),1)
71 $(OBJCOPY) -O $(#{prefix}_FORMAT) --strip-unneeded -R .note -R .comment -R .note.gnu.build-id $< $@
73 ifneq (#{exe},kernel.exec)
74 #{@name}: #{exe} ./grub-macho2img
75 ./grub-macho2img $< $@
77 #{@name}: #{exe} ./grub-macho2img
78 ./grub-macho2img --bss $< $@
83 $(TARGET_CC) -o $@ $^ $(TARGET_LDFLAGS) $(#{prefix}_LDFLAGS)
85 " + objs.collect_with_index do |obj, i|
87 fake_obj = File.basename(src).suffix('o')
89 flag = if /\.c$/ =~ src then 'CFLAGS' else 'ASFLAGS' end
90 extra_flags = if /\.S$/ =~ src then '-DASM_FILE=1' else '' end
91 dir = File.dirname(src)
93 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
94 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -MD -c -o $@ $<
102 # Use PModule instead Module, to avoid name conflicting.
104 def initialize(dir, name)
109 attr_reader :dir, :name
112 prefix = @name.to_var
114 objs = sources.collect do |src|
115 raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src
116 prefix + '-' + src.to_obj
118 objs_str = objs.join(' ')
119 deps = objs.collect {|obj| obj.suffix('d')}
120 deps_str = deps.join(' ')
121 pre_obj = 'pre-' + @name.suffix('o')
122 mod_src = 'mod-' + @name.suffix('c')
123 mod_obj = mod_src.suffix('o')
124 defsym = 'def-' + @name.suffix('lst')
125 undsym = 'und-' + @name.suffix('lst')
126 mod_name = File.basename(@name, '.mod')
127 symbolic_name = mod_name.sub(/\.[^\.]*$/, '')
130 clean-module-#{@name}.#{@rule_count}:
131 rm -f #{@name} #{mod_obj} #{mod_src} #{pre_obj} #{objs_str} #{undsym}
133 CLEAN_MODULE_TARGETS += clean-module-#{@name}.#{@rule_count}
135 ifneq ($(#{prefix}_EXPORTS),no)
136 clean-module-#{@name}-symbol.#{@rule_count}:
139 CLEAN_MODULE_TARGETS += clean-module-#{@name}-symbol.#{@rule_count}
140 DEFSYMFILES += #{defsym}
142 mostlyclean-module-#{@name}.#{@rule_count}:
145 MOSTLYCLEAN_MODULE_TARGETS += mostlyclean-module-#{@name}.#{@rule_count}
146 UNDSYMFILES += #{undsym}
148 ifneq ($(TARGET_APPLE_CC),1)
149 #{@name}: #{pre_obj} #{mod_obj} $(TARGET_OBJ2ELF)
151 $(TARGET_CC) $(#{prefix}_LDFLAGS) $(TARGET_LDFLAGS) -Wl,-r,-d -o $@ #{pre_obj} #{mod_obj}
152 if test ! -z \"$(TARGET_OBJ2ELF)\"; then ./$(TARGET_OBJ2ELF) $@ || (rm -f $@; exit 1); fi
153 $(STRIP) --strip-unneeded -K grub_mod_init -K grub_mod_fini -K _grub_mod_init -K _grub_mod_fini -R .note -R .comment $@
155 #{@name}: #{pre_obj} #{mod_obj} $(TARGET_OBJ2ELF)
158 $(TARGET_CC) $(#{prefix}_LDFLAGS) $(TARGET_LDFLAGS) -Wl,-r,-d -o $@.bin #{pre_obj} #{mod_obj}
159 $(OBJCONV) -f$(TARGET_MODULE_FORMAT) -nr:_grub_mod_init:grub_mod_init -nr:_grub_mod_fini:grub_mod_fini -wd1106 -nu -nd $@.bin $@
163 #{pre_obj}: $(#{prefix}_DEPENDENCIES) #{objs_str}
165 $(TARGET_CC) $(#{prefix}_LDFLAGS) $(TARGET_LDFLAGS) -Wl,-r,-d -o $@ #{objs_str}
167 #{mod_obj}: #{mod_src}
168 $(TARGET_CC) $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) $(#{prefix}_CFLAGS) -c -o $@ $<
170 #{mod_src}: $(builddir)/moddep.lst $(srcdir)/genmodsrc.sh
171 sh $(srcdir)/genmodsrc.sh '#{mod_name}' $< > $@ || (rm -f $@; exit 1)
173 ifneq ($(#{prefix}_EXPORTS),no)
174 ifneq ($(TARGET_APPLE_CC),1)
175 #{defsym}: #{pre_obj}
176 $(NM) -g --defined-only -P -p $< | sed 's/^\\([^ ]*\\).*/\\1 #{mod_name}/' > $@
178 #{defsym}: #{pre_obj}
179 $(NM) -g -P -p $< | grep -E '^[a-zA-Z0-9_]* [TDS]' | sed 's/^\\([^ ]*\\).*/\\1 #{mod_name}/' > $@
183 #{undsym}: #{pre_obj}
184 echo '#{mod_name}' > $@
185 $(NM) -u -P -p $< | cut -f1 -d' ' >> $@
187 " + objs.collect_with_index do |obj, i|
189 fake_obj = File.basename(src).suffix('o')
190 extra_target = obj.sub(/\.[^\.]*$/, '') + '-extra'
191 command = 'cmd-' + obj.suffix('lst')
192 fs = 'fs-' + obj.suffix('lst')
193 partmap = 'partmap-' + obj.suffix('lst')
194 handler = 'handler-' + obj.suffix('lst')
195 parttool = 'parttool-' + obj.suffix('lst')
197 flag = if /\.c$/ =~ src then 'CFLAGS' else 'ASFLAGS' end
198 extra_flags = if /\.S$/ =~ src then '-DASM_FILE=1' else '' end
199 dir = File.dirname(src)
201 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
202 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -MD -c -o $@ $<
205 clean-module-#{extra_target}.#{@rule_count}:
206 rm -f #{command} #{fs} #{partmap} #{handler} #{parttool}
208 CLEAN_MODULE_TARGETS += clean-module-#{extra_target}.#{@rule_count}
210 COMMANDFILES += #{command}
212 PARTTOOLFILES += #{parttool}
213 PARTMAPFILES += #{partmap}
214 HANDLERFILES += #{handler}
216 #{command}: #{src} $(#{src}_DEPENDENCIES) gencmdlist.sh
218 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
219 | sh $(srcdir)/gencmdlist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
221 #{fs}: #{src} $(#{src}_DEPENDENCIES) genfslist.sh
223 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
224 | sh $(srcdir)/genfslist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
226 #{parttool}: #{src} $(#{src}_DEPENDENCIES) genparttoollist.sh
228 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
229 | sh $(srcdir)/genparttoollist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
231 #{partmap}: #{src} $(#{src}_DEPENDENCIES) genpartmaplist.sh
233 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
234 | sh $(srcdir)/genpartmaplist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
236 #{handler}: #{src} $(#{src}_DEPENDENCIES) genhandlerlist.sh
238 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
239 | sh $(srcdir)/genhandlerlist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
247 def initialize(dir, name)
253 prefix = @name.to_var
254 print "#{@name}: $(#{prefix}_DEPENDENCIES) $(#{prefix}_OBJECTS)
255 $(CC) -o $@ $(#{prefix}_OBJECTS) $(LDFLAGS) $(#{prefix}_LDFLAGS)
259 attr_reader :dir, :name
262 prefix = @name.to_var
264 objs = sources.collect do |src|
265 raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src
266 prefix + '-' + src.to_obj
268 objs_str = objs.join(' ');
269 deps = objs.collect {|obj| obj.suffix('d')}
270 deps_str = deps.join(' ');
273 clean-utility-#{@name}.#{@rule_count}:
274 rm -f #{@name}$(EXEEXT) #{objs_str}
276 CLEAN_UTILITY_TARGETS += clean-utility-#{@name}.#{@rule_count}
278 mostlyclean-utility-#{@name}.#{@rule_count}:
281 MOSTLYCLEAN_UTILITY_TARGETS += mostlyclean-utility-#{@name}.#{@rule_count}
283 #{prefix}_OBJECTS += #{objs_str}
285 " + objs.collect_with_index do |obj, i|
287 fake_obj = File.basename(src).suffix('o')
289 dir = File.dirname(src)
291 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
292 $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(CFLAGS) -DGRUB_UTIL=1 $(#{prefix}_CFLAGS) -MD -c -o $@ $<
301 def initialize(dir, name)
305 attr_reader :dir, :name
308 prefix = @name.to_var
309 objs = sources.collect do |src|
310 raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src
311 prefix + '-' + src.to_obj
313 objs_str = objs.join(' ');
314 deps = objs.collect {|obj| obj.suffix('d')}
315 deps_str = deps.join(' ');
317 "CLEANFILES += #{@name} #{objs_str}
318 MOSTLYCLEANFILES += #{deps_str}
320 #{@name}: $(#{prefix}_DEPENDENCIES) #{objs_str}
321 $(TARGET_CC) -o $@ #{objs_str} $(TARGET_LDFLAGS) $(#{prefix}_LDFLAGS)
323 " + objs.collect_with_index do |obj, i|
325 fake_obj = File.basename(src).suffix('o')
327 flag = if /\.c$/ =~ src then 'CFLAGS' else 'ASFLAGS' end
328 extra_flags = if /\.S$/ =~ src then '-DASM_FILE=1' else '' end
329 dir = File.dirname(src)
331 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
332 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -MD -c -o $@ $<
341 def initialize(dir, name)
345 attr_reader :dir, :name
348 if sources.length != 1
349 raise "only a single source file must be specified for a script"
353 raise "unknown source file `#{src}'"
356 "CLEANFILES += #{@name}
358 #{@name}: #{src} $(#{src}_DEPENDENCIES) config.status
359 ./config.status --file=#{name}:#{src}
374 print "# Generated by genmk.rb, please don't edit!\n"
388 str.gsub!(/\\\n/, ' ')
390 if /^([a-zA-Z0-9_]+)\s*\+?=\s*(.*?)\s*$/ =~ str
393 if var =~ /^([a-zA-Z0-9_]+)_([A-Z]+)$/
394 prefix, type = $1, $2
398 images += args.split(/\s+/).collect do |img|
399 Image.new(prefix, img)
403 pmodules += args.split(/\s+/).collect do |pmod|
404 PModule.new(prefix, pmod)
408 utils += args.split(/\s+/).collect do |util|
409 Utility.new(prefix, util)
413 programs += args.split(/\s+/).collect do |prog|
414 Program.new(prefix, prog)
418 scripts += args.split(/\s+/).collect do |script|
419 Script.new(prefix, script)
423 if img = images.detect() {|i| i.name.to_var == prefix}
424 print img.rule(args.split(/\s+/))
425 elsif pmod = pmodules.detect() {|m| m.name.to_var == prefix}
426 print pmod.rule(args.split(/\s+/))
427 elsif util = utils.detect() {|u| u.name.to_var == prefix}
428 print util.rule(args.split(/\s+/))
429 elsif program = programs.detect() {|u| u.name.to_var == prefix}
430 print program.rule(args.split(/\s+/))
431 elsif script = scripts.detect() {|s| s.name.to_var == prefix}
432 print script.rule(args.split(/\s+/))
442 utils.each {|util| util.print_tail()}