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')
146 handler = 'handler-' + obj.suffix('lst')
147 parttool = 'parttool-' + obj.suffix('lst')
149 flag = if /\.c$/ =~ src then 'CFLAGS' else 'ASFLAGS' end
150 extra_flags = if /\.S$/ =~ src then '-DASM_FILE=1' else '' end
151 dir = File.dirname(src)
153 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
154 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -MD -c -o $@ $<
157 CLEANFILES += #{command} #{fs} #{partmap} #{handler} #{parttool}
158 COMMANDFILES += #{command}
160 PARTTOOLFILES += #{parttool}
161 PARTMAPFILES += #{partmap}
162 HANDLERFILES += #{handler}
164 #{command}: #{src} $(#{src}_DEPENDENCIES) gencmdlist.sh
166 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
167 | sh $(srcdir)/gencmdlist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
169 #{fs}: #{src} $(#{src}_DEPENDENCIES) genfslist.sh
171 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
172 | sh $(srcdir)/genfslist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
174 #{parttool}: #{src} $(#{src}_DEPENDENCIES) genparttoollist.sh
176 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
177 | sh $(srcdir)/genparttoollist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
179 #{partmap}: #{src} $(#{src}_DEPENDENCIES) genpartmaplist.sh
181 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
182 | sh $(srcdir)/genpartmaplist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
184 #{handler}: #{src} $(#{src}_DEPENDENCIES) genhandlerlist.sh
186 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) #{extra_flags} $(TARGET_#{flag}) $(#{prefix}_#{flag}) -E $< \
187 | sh $(srcdir)/genhandlerlist.sh #{symbolic_name} > $@ || (rm -f $@; exit 1)
195 def initialize(dir, name)
199 attr_reader :dir, :name
202 prefix = @name.to_var
203 objs = sources.collect do |src|
204 raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src
205 prefix + '-' + src.to_obj
207 objs_str = objs.join(' ');
208 deps = objs.collect {|obj| obj.suffix('d')}
209 deps_str = deps.join(' ');
211 "CLEANFILES += #{@name}$(EXEEXT) #{objs_str}
212 MOSTLYCLEANFILES += #{deps_str}
214 #{@name}: $(#{prefix}_DEPENDENCIES) #{objs_str}
215 $(CC) -o $@ #{objs_str} $(LDFLAGS) $(#{prefix}_LDFLAGS)
217 " + objs.collect_with_index do |obj, i|
219 fake_obj = File.basename(src).suffix('o')
221 dir = File.dirname(src)
223 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
224 $(CC) -I#{dir} -I$(srcdir)/#{dir} $(CPPFLAGS) $(CFLAGS) -DGRUB_UTIL=1 $(#{prefix}_CFLAGS) -MD -c -o $@ $<
233 def initialize(dir, name)
237 attr_reader :dir, :name
240 prefix = @name.to_var
241 objs = sources.collect do |src|
242 raise "unknown source file `#{src}'" if /\.[cS]$/ !~ src
243 prefix + '-' + src.to_obj
245 objs_str = objs.join(' ');
246 deps = objs.collect {|obj| obj.suffix('d')}
247 deps_str = deps.join(' ');
249 "CLEANFILES += #{@name} #{objs_str}
250 MOSTLYCLEANFILES += #{deps_str}
252 #{@name}: $(#{prefix}_DEPENDENCIES) #{objs_str}
253 $(TARGET_CC) -o $@ #{objs_str} $(TARGET_LDFLAGS) $(#{prefix}_LDFLAGS)
255 " + objs.collect_with_index do |obj, i|
257 fake_obj = File.basename(src).suffix('o')
259 dir = File.dirname(src)
261 "#{obj}: #{src} $(#{src}_DEPENDENCIES)
262 $(TARGET_CC) -I#{dir} -I$(srcdir)/#{dir} $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) $(#{prefix}_CFLAGS) -MD -c -o $@ $<
271 def initialize(dir, name)
275 attr_reader :dir, :name
278 if sources.length != 1
279 raise "only a single source file must be specified for a script"
283 raise "unknown source file `#{src}'"
286 "CLEANFILES += #{@name}
288 #{@name}: #{src} $(#{src}_DEPENDENCIES) config.status
289 ./config.status --file=#{name}:#{src}
304 print "# Generated by genmk.rb, please don't edit!\n"
318 str.gsub!(/\\\n/, ' ')
320 if /^([a-zA-Z0-9_]+)\s*\+?=\s*(.*?)\s*$/ =~ str
323 if var =~ /^([a-zA-Z0-9_]+)_([A-Z]+)$/
324 prefix, type = $1, $2
328 images += args.split(/\s+/).collect do |img|
329 Image.new(prefix, img)
333 pmodules += args.split(/\s+/).collect do |pmod|
334 PModule.new(prefix, pmod)
338 utils += args.split(/\s+/).collect do |util|
339 Utility.new(prefix, util)
343 programs += args.split(/\s+/).collect do |prog|
344 Program.new(prefix, prog)
348 scripts += args.split(/\s+/).collect do |script|
349 Script.new(prefix, script)
353 if img = images.detect() {|i| i.name.to_var == prefix}
354 print img.rule(args.split(/\s+/))
355 elsif pmod = pmodules.detect() {|m| m.name.to_var == prefix}
356 print pmod.rule(args.split(/\s+/))
357 elsif util = utils.detect() {|u| u.name.to_var == prefix}
358 print util.rule(args.split(/\s+/))
359 elsif program = programs.detect() {|u| u.name.to_var == prefix}
360 print program.rule(args.split(/\s+/))
361 elsif script = scripts.detect() {|s| s.name.to_var == prefix}
362 print script.rule(args.split(/\s+/))