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}
60 $(OBJCOPY) -O $(#{prefix}_FORMAT) --strip-unneeded -R .note -R .comment -R .note.gnu.build-id $< $@
63 $(TARGET_CC) -o $@ $^ $(TARGET_LDFLAGS) $(#{prefix}_LDFLAGS)
65 " + objs.collect_with_index do |obj, i|
67 fake_obj = File.basename(src).suffix('o')
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)
73 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
74 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -MD -c -o $@ $<
82 # Use PModule instead Module, to avoid name conflicting.
84 def initialize(dir, name)
88 attr_reader :dir, :name
92 objs = sources.collect do |src|
93 raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src
94 prefix + '-' + src.to_obj
96 objs_str = objs.join(' ')
97 deps = objs.collect {|obj| obj.suffix('d')}
98 deps_str = deps.join(' ')
99 pre_obj = 'pre-' + @name.suffix('o')
100 mod_src = 'mod-' + @name.suffix('c')
101 mod_obj = mod_src.suffix('o')
102 defsym = 'def-' + @name.suffix('lst')
103 undsym = 'und-' + @name.suffix('lst')
104 mod_name = File.basename(@name, '.mod')
105 symbolic_name = mod_name.sub(/\.[^\.]*$/, '')
107 "CLEANFILES += #{@name} #{mod_obj} #{mod_src} #{pre_obj} #{objs_str} #{undsym}
108 ifneq ($(#{prefix}_EXPORTS),no)
109 CLEANFILES += #{defsym}
110 DEFSYMFILES += #{defsym}
112 MOSTLYCLEANFILES += #{deps_str}
113 UNDSYMFILES += #{undsym}
115 #{@name}: #{pre_obj} #{mod_obj} $(TARGET_OBJ2ELF)
117 $(TARGET_CC) $(#{prefix}_LDFLAGS) $(TARGET_LDFLAGS) -Wl,-r,-d -o $@ #{pre_obj} #{mod_obj}
118 if test ! -z $(TARGET_OBJ2ELF); then ./$(TARGET_OBJ2ELF) $@ || (rm -f $@; exit 1); fi
119 $(STRIP) --strip-unneeded -K grub_mod_init -K grub_mod_fini -K _grub_mod_init -K _grub_mod_fini -R .note -R .comment $@
121 #{pre_obj}: $(#{prefix}_DEPENDENCIES) #{objs_str}
123 $(TARGET_CC) $(#{prefix}_LDFLAGS) $(TARGET_LDFLAGS) -Wl,-r,-d -o $@ #{objs_str}
125 #{mod_obj}: #{mod_src}
126 $(TARGET_CC) $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) $(#{prefix}_CFLAGS) -c -o $@ $<
128 #{mod_src}: $(builddir)/moddep.lst $(srcdir)/genmodsrc.sh
129 sh $(srcdir)/genmodsrc.sh '#{mod_name}' $< > $@ || (rm -f $@; exit 1)
131 ifneq ($(#{prefix}_EXPORTS),no)
132 #{defsym}: #{pre_obj}
133 $(NM) -g --defined-only -P -p $< | sed 's/^\\([^ ]*\\).*/\\1 #{mod_name}/' > $@
136 #{undsym}: #{pre_obj}
137 echo '#{mod_name}' > $@
138 $(NM) -u -P -p $< | cut -f1 -d' ' >> $@
140 " + objs.collect_with_index do |obj, i|
142 fake_obj = File.basename(src).suffix('o')
143 command = 'cmd-' + obj.suffix('lst')
144 fs = 'fs-' + obj.suffix('lst')
145 partmap = 'partmap-' + obj.suffix('lst')
147 flag = if /\.c$/ =~ src then 'CFLAGS' else 'ASFLAGS' end
148 extra_flags = if /\.S$/ =~ src then '-DASM_FILE=1' else '' end
149 dir = File.dirname(src)
151 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
152 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -MD -c -o $@ $<
155 CLEANFILES += #{command} #{fs} #{partmap}
156 COMMANDFILES += #{command}
158 PARTMAPFILES += #{partmap}
160 #{command}: #{src} $(#{src}_DEPENDENCIES) gencmdlist.sh
162 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
163 | sh $(srcdir)/gencmdlist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
165 #{fs}: #{src} $(#{src}_DEPENDENCIES) genfslist.sh
167 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
168 | sh $(srcdir)/genfslist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
170 #{partmap}: #{src} $(#{src}_DEPENDENCIES) genpartmaplist.sh
172 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
173 | sh $(srcdir)/genpartmaplist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
182 def initialize(dir, name)
186 attr_reader :dir, :name
189 prefix = @name.to_var
190 objs = sources.collect do |src|
191 raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src
192 prefix + '-' + src.to_obj
194 objs_str = objs.join(' ');
195 deps = objs.collect {|obj| obj.suffix('d')}
196 deps_str = deps.join(' ');
198 "CLEANFILES += #{@name}$(EXEEXT) #{objs_str}
199 MOSTLYCLEANFILES += #{deps_str}
201 #{@name}: $(#{prefix}_DEPENDENCIES) #{objs_str}
202 $(CC) -o $@ #{objs_str} $(LDFLAGS) $(#{prefix}_LDFLAGS)
204 " + objs.collect_with_index do |obj, i|
206 fake_obj = File.basename(src).suffix('o')
208 dir = File.dirname(src)
210 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
211 $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(CFLAGS) -DGRUB_UTIL=1 $(#{prefix}_CFLAGS) -MD -c -o $@ $<
220 def initialize(dir, name)
224 attr_reader :dir, :name
227 prefix = @name.to_var
228 objs = sources.collect do |src|
229 raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src
230 prefix + '-' + src.to_obj
232 objs_str = objs.join(' ');
233 deps = objs.collect {|obj| obj.suffix('d')}
234 deps_str = deps.join(' ');
236 "CLEANFILES += #{@name} #{objs_str}
237 MOSTLYCLEANFILES += #{deps_str}
239 #{@name}: $(#{prefix}_DEPENDENCIES) #{objs_str}
240 $(TARGET_CC) -o $@ #{objs_str} $(TARGET_LDFLAGS) $(#{prefix}_LDFLAGS)
242 " + objs.collect_with_index do |obj, i|
244 fake_obj = File.basename(src).suffix('o')
246 dir = File.dirname(src)
248 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
249 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) $(#{prefix}_CFLAGS) -MD -c -o $@ $<
258 def initialize(dir, name)
262 attr_reader :dir, :name
265 if sources.length != 1
266 raise "only a single source file must be specified for a script"
270 raise "unknown source file `#{src}'"
273 "CLEANFILES += #{@name}
275 #{@name}: #{src} $(#{src}_DEPENDENCIES) config.status
276 ./config.status --file=#{name}:#{src}
291 print "# Generated by genmk.rb, please don't edit!\n"
307 if /^([a-zA-Z0-9_]+)\s*\+?=\s*(.*?)\s*$/ =~ s
310 if var =~ /^([a-zA-Z0-9_]+)_([A-Z]+)$/
311 prefix, type = $1, $2
315 images += args.split(/\s+/).collect do |img|
316 Image.new(prefix, img)
320 pmodules += args.split(/\s+/).collect do |pmod|
321 PModule.new(prefix, pmod)
325 utils += args.split(/\s+/).collect do |util|
326 Utility.new(prefix, util)
330 programs += args.split(/\s+/).collect do |prog|
331 Program.new(prefix, prog)
335 scripts += args.split(/\s+/).collect do |script|
336 Script.new(prefix, script)
340 if img = images.detect() {|i| i.name.to_var == prefix}
341 print img.rule(args.split(/\s+/))
342 elsif pmod = pmodules.detect() {|m| m.name.to_var == prefix}
343 print pmod.rule(args.split(/\s+/))
344 elsif util = utils.detect() {|u| u.name.to_var == prefix}
345 print util.rule(args.split(/\s+/))
346 elsif program = programs.detect() {|u| u.name.to_var == prefix}
347 print program.rule(args.split(/\s+/))
348 elsif script = scripts.detect() {|s| s.name.to_var == prefix}
349 print script.rule(args.split(/\s+/))