]> git.ipfire.org Git - thirdparty/grub.git/blame - gentpl.py
grub-mknetdir: Add support for ARM64 EFI
[thirdparty/grub.git] / gentpl.py
CommitLineData
8c411768 1#! /usr/bin/python
e3ec28ab 2# GRUB -- GRand Unified Bootloader
ab4f1501 3# Copyright (C) 2010,2011,2012,2013 Free Software Foundation, Inc.
e3ec28ab
VS
4#
5# GRUB is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# GRUB is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with GRUB. If not, see <http://www.gnu.org/licenses/>.
8c411768 17
aa437b58
MG
18from __future__ import print_function
19
ab4f1501
CW
20__metaclass__ = type
21
22from optparse import OptionParser
23import re
24
8c411768 25#
ab4f1501 26# This is the python script used to generate Makefile.*.am
8c411768
BC
27#
28
29GRUB_PLATFORMS = [ "emu", "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot",
062cdbc1 30 "i386_multiboot", "i386_ieee1275", "x86_64_efi",
9612ebc0 31 "i386_xen", "x86_64_xen",
54da1feb 32 "mips_loongson", "sparc64_ieee1275",
3666d5f6 33 "powerpc_ieee1275", "mips_arc", "ia64_efi",
15a463d7 34 "mips_qemu_mips", "arm_uboot", "arm_efi", "arm64_efi" ]
8c411768
BC
35
36GROUPS = {}
eefe8abd
VS
37
38GROUPS["common"] = GRUB_PLATFORMS[:]
39
40# Groups based on CPU
8427685f
BC
41GROUPS["i386"] = [ "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot", "i386_multiboot", "i386_ieee1275" ]
42GROUPS["x86_64"] = [ "x86_64_efi" ]
43GROUPS["x86"] = GROUPS["i386"] + GROUPS["x86_64"]
3666d5f6 44GROUPS["mips"] = [ "mips_loongson", "mips_qemu_mips", "mips_arc" ]
8427685f
BC
45GROUPS["sparc64"] = [ "sparc64_ieee1275" ]
46GROUPS["powerpc"] = [ "powerpc_ieee1275" ]
389b31cd 47GROUPS["arm"] = [ "arm_uboot", "arm_efi" ]
15a463d7 48GROUPS["arm64"] = [ "arm64_efi" ]
8427685f 49
eefe8abd 50# Groups based on firmware
15a463d7 51GROUPS["efi"] = [ "i386_efi", "x86_64_efi", "ia64_efi", "arm_efi", "arm64_efi" ]
8427685f 52GROUPS["ieee1275"] = [ "i386_ieee1275", "sparc64_ieee1275", "powerpc_ieee1275" ]
389b31cd 53GROUPS["uboot"] = [ "arm_uboot" ]
9612ebc0 54GROUPS["xen"] = [ "i386_xen", "x86_64_xen" ]
8427685f 55
eefe8abd
VS
56# emu is a special case so many core functionality isn't needed on this platform
57GROUPS["noemu"] = GRUB_PLATFORMS[:]; GROUPS["noemu"].remove("emu")
58
59# Groups based on hardware features
a07a81b3
VS
60GROUPS["cmos"] = GROUPS["x86"][:] + ["mips_loongson", "mips_qemu_mips",
61 "sparc64_ieee1275", "powerpc_ieee1275"]
9612ebc0 62GROUPS["cmos"].remove("i386_efi"); GROUPS["cmos"].remove("x86_64_efi");
bee1aeb9 63GROUPS["pci"] = GROUPS["x86"] + ["mips_loongson"]
eefe8abd 64GROUPS["usb"] = GROUPS["pci"]
8427685f 65
eefe8abd 66# If gfxterm is main output console integrate it into kernel
72c9a507 67GROUPS["videoinkernel"] = ["mips_loongson", "i386_coreboot" ]
eefe8abd
VS
68GROUPS["videomodules"] = GRUB_PLATFORMS[:];
69for i in GROUPS["videoinkernel"]: GROUPS["videomodules"].remove(i)
8427685f 70
ee74fa48 71# Similar for terminfo
9612ebc0 72GROUPS["terminfoinkernel"] = [ "emu", "mips_loongson", "mips_arc", "mips_qemu_mips" ] + GROUPS["xen"] + GROUPS["ieee1275"] + GROUPS["uboot"];
ee74fa48
VS
73GROUPS["terminfomodule"] = GRUB_PLATFORMS[:];
74for i in GROUPS["terminfoinkernel"]: GROUPS["terminfomodule"].remove(i)
75
389b31cd 76# Flattened Device Trees (FDT)
15a463d7 77GROUPS["fdt"] = [ "arm64_efi", "arm_uboot", "arm_efi" ]
389b31cd 78
f034fab6
VS
79# Needs software helpers for division
80# Must match GRUB_DIVISION_IN_SOFTWARE in misc.h
81GROUPS["softdiv"] = GROUPS["arm"] + ["ia64_efi"]
82GROUPS["no_softdiv"] = GRUB_PLATFORMS[:]
83for i in GROUPS["softdiv"]: GROUPS["no_softdiv"].remove(i)
84
fd73b3d0 85# Miscellaneous groups scheduled to disappear in future
eefe8abd
VS
86GROUPS["i386_coreboot_multiboot_qemu"] = ["i386_coreboot", "i386_multiboot", "i386_qemu"]
87GROUPS["nopc"] = GRUB_PLATFORMS[:]; GROUPS["nopc"].remove("i386_pc")
8c411768
BC
88
89#
90# Create platform => groups reverse map, where groups covering that
91# platform are ordered by their sizes
92#
93RMAP = {}
94for platform in GRUB_PLATFORMS:
95 # initialize with platform itself as a group
96 RMAP[platform] = [ platform ]
97
98 for k in GROUPS.keys():
99 v = GROUPS[k]
100 # skip groups that don't cover this platform
101 if platform not in v: continue
102
103 bigger = []
104 smaller = []
105 # partition currently known groups based on their size
106 for group in RMAP[platform]:
107 if group in GRUB_PLATFORMS: smaller.append(group)
108 elif len(GROUPS[group]) < len(v): smaller.append(group)
109 else: bigger.append(group)
110 # insert in the middle
111 RMAP[platform] = smaller + [ k ] + bigger
112
ab4f1501
CW
113#
114# Input
115#
116
117# We support a subset of the AutoGen definitions file syntax. Specifically,
34b2003d
CW
118# compound names are disallowed; some preprocessing directives are
119# disallowed (though #if/#endif are allowed; note that, like AutoGen, #if
120# skips everything to the next #endif regardless of the value of the
121# conditional); and shell-generated strings, Scheme-generated strings, and
122# here strings are disallowed.
ab4f1501
CW
123
124class AutogenToken:
125 (autogen, definitions, eof, var_name, other_name, string, number,
126 semicolon, equals, comma, lbrace, rbrace, lbracket, rbracket) = range(14)
127
128class AutogenState:
129 (init, need_def, need_tpl, need_semi, need_name, have_name, need_value,
130 need_idx, need_rbracket, indx_name, have_value, done) = range(12)
131
132class AutogenParseError(Exception):
7e90f5ad 133 def __init__(self, message, path, line):
ab4f1501 134 super(AutogenParseError, self).__init__(message)
7e90f5ad 135 self.path = path
ab4f1501
CW
136 self.line = line
137
138 def __str__(self):
139 return (
140 super(AutogenParseError, self).__str__() +
7e90f5ad 141 " at file %s line %d" % (self.path, self.line))
ab4f1501
CW
142
143class AutogenDefinition(list):
144 def __getitem__(self, key):
145 try:
146 return super(AutogenDefinition, self).__getitem__(key)
147 except TypeError:
148 for name, value in self:
149 if name == key:
150 return value
151
152 def __contains__(self, key):
153 for name, value in self:
154 if name == key:
155 return True
156 return False
157
158 def get(self, key, default):
159 for name, value in self:
160 if name == key:
161 return value
162 else:
163 return default
164
165 def find_all(self, key):
166 for name, value in self:
167 if name == key:
168 yield value
169
170class AutogenParser:
171 def __init__(self):
172 self.definitions = AutogenDefinition()
173 self.def_stack = [("", self.definitions)]
174 self.curdef = None
175 self.new_name = None
7e90f5ad 176 self.cur_path = None
ab4f1501
CW
177 self.cur_line = 0
178
179 @staticmethod
180 def is_unquotable_char(c):
181 return (ord(c) in range(ord("!"), ord("~") + 1) and
182 c not in "#,;<=>[\\]`{}?*'\"()")
183
184 @staticmethod
185 def is_value_name_char(c):
186 return c in ":^-_" or c.isalnum()
187
7e90f5ad
CW
188 def error(self, message):
189 raise AutogenParseError(message, self.cur_file, self.cur_line)
190
ab4f1501
CW
191 def read_tokens(self, f):
192 data = f.read()
193 end = len(data)
194 offset = 0
195 while offset < end:
196 while offset < end and data[offset].isspace():
197 if data[offset] == "\n":
198 self.cur_line += 1
199 offset += 1
200 if offset >= end:
201 break
202 c = data[offset]
34b2003d
CW
203 if c == "#":
204 offset += 1
205 try:
206 end_directive = data.index("\n", offset)
207 directive = data[offset:end_directive]
208 offset = end_directive
209 except ValueError:
210 directive = data[offset:]
211 offset = end
212 name, value = directive.split(None, 1)
213 if name == "if":
214 try:
215 end_if = data.index("\n#endif", offset)
216 new_offset = end_if + len("\n#endif")
217 self.cur_line += data[offset:new_offset].count("\n")
218 offset = new_offset
219 except ValueError:
220 self.error("#if without matching #endif")
221 else:
222 self.error("Unhandled directive '#%s'" % name)
223 elif c == "{":
ab4f1501
CW
224 yield AutogenToken.lbrace, c
225 offset += 1
226 elif c == "=":
227 yield AutogenToken.equals, c
228 offset += 1
229 elif c == "}":
230 yield AutogenToken.rbrace, c
231 offset += 1
232 elif c == "[":
233 yield AutogenToken.lbracket, c
234 offset += 1
235 elif c == "]":
236 yield AutogenToken.rbracket, c
237 offset += 1
238 elif c == ";":
239 yield AutogenToken.semicolon, c
240 offset += 1
241 elif c == ",":
242 yield AutogenToken.comma, c
243 offset += 1
244 elif c in ("'", '"'):
245 s = []
246 while True:
247 offset += 1
248 if offset >= end:
7e90f5ad 249 self.error("EOF in quoted string")
ab4f1501
CW
250 if data[offset] == "\n":
251 self.cur_line += 1
252 if data[offset] == "\\":
253 offset += 1
254 if offset >= end:
7e90f5ad 255 self.error("EOF in quoted string")
ab4f1501
CW
256 if data[offset] == "\n":
257 self.cur_line += 1
258 # Proper escaping unimplemented; this can be filled
259 # out if needed.
260 s.append("\\")
261 s.append(data[offset])
262 elif data[offset] == c:
263 offset += 1
264 break
265 else:
266 s.append(data[offset])
267 yield AutogenToken.string, "".join(s)
34b2003d
CW
268 elif c == "/":
269 offset += 1
270 if data[offset] == "*":
271 offset += 1
272 try:
273 end_comment = data.index("*/", offset)
274 new_offset = end_comment + len("*/")
275 self.cur_line += data[offset:new_offset].count("\n")
276 offset = new_offset
277 except ValueError:
278 self.error("/* without matching */")
279 elif data[offset] == "/":
280 try:
281 offset = data.index("\n", offset)
282 except ValueError:
283 pass
ab4f1501
CW
284 elif (c.isdigit() or
285 (c == "-" and offset < end - 1 and
286 data[offset + 1].isdigit())):
287 end_number = offset + 1
288 while end_number < end and data[end_number].isdigit():
289 end_number += 1
290 yield AutogenToken.number, data[offset:end_number]
291 offset = end_number
292 elif self.is_unquotable_char(c):
293 end_name = offset
294 while (end_name < end and
295 self.is_value_name_char(data[end_name])):
296 end_name += 1
297 if end_name < end and self.is_unquotable_char(data[end_name]):
298 while (end_name < end and
299 self.is_unquotable_char(data[end_name])):
300 end_name += 1
301 yield AutogenToken.other_name, data[offset:end_name]
302 offset = end_name
303 else:
304 s = data[offset:end_name]
305 if s.lower() == "autogen":
306 yield AutogenToken.autogen, s
307 elif s.lower() == "definitions":
308 yield AutogenToken.definitions, s
309 else:
310 yield AutogenToken.var_name, s
311 offset = end_name
312 else:
7e90f5ad 313 self.error("Invalid input character '%s'" % c)
ab4f1501
CW
314 yield AutogenToken.eof, None
315
316 def do_need_name_end(self, token):
317 if len(self.def_stack) > 1:
7e90f5ad 318 self.error("Definition blocks were left open")
ab4f1501
CW
319
320 def do_need_name_var_name(self, token):
321 self.new_name = token
322
323 def do_end_block(self, token):
324 if len(self.def_stack) <= 1:
7e90f5ad 325 self.error("Too many close braces")
ab4f1501
CW
326 new_name, parent_def = self.def_stack.pop()
327 parent_def.append((new_name, self.curdef))
328 self.curdef = parent_def
329
330 def do_empty_val(self, token):
331 self.curdef.append((self.new_name, ""))
332
333 def do_str_value(self, token):
334 self.curdef.append((self.new_name, token))
335
336 def do_start_block(self, token):
337 self.def_stack.append((self.new_name, self.curdef))
338 self.curdef = AutogenDefinition()
339
340 def do_indexed_name(self, token):
341 self.new_name = token
342
7e90f5ad 343 def read_definitions_file(self, f):
ab4f1501
CW
344 self.curdef = self.definitions
345 self.cur_line = 0
346 state = AutogenState.init
347
348 # The following transition table was reduced from the Autogen
349 # documentation:
350 # info -f autogen -n 'Full Syntax'
351 transitions = {
352 AutogenState.init: {
353 AutogenToken.autogen: (AutogenState.need_def, None),
354 },
355 AutogenState.need_def: {
356 AutogenToken.definitions: (AutogenState.need_tpl, None),
357 },
358 AutogenState.need_tpl: {
359 AutogenToken.var_name: (AutogenState.need_semi, None),
360 AutogenToken.other_name: (AutogenState.need_semi, None),
361 AutogenToken.string: (AutogenState.need_semi, None),
362 },
363 AutogenState.need_semi: {
364 AutogenToken.semicolon: (AutogenState.need_name, None),
365 },
366 AutogenState.need_name: {
367 AutogenToken.autogen: (AutogenState.need_def, None),
368 AutogenToken.eof: (AutogenState.done, self.do_need_name_end),
369 AutogenToken.var_name: (
370 AutogenState.have_name, self.do_need_name_var_name),
371 AutogenToken.rbrace: (
372 AutogenState.have_value, self.do_end_block),
373 },
374 AutogenState.have_name: {
375 AutogenToken.semicolon: (
376 AutogenState.need_name, self.do_empty_val),
377 AutogenToken.equals: (AutogenState.need_value, None),
378 AutogenToken.lbracket: (AutogenState.need_idx, None),
379 },
380 AutogenState.need_value: {
381 AutogenToken.var_name: (
382 AutogenState.have_value, self.do_str_value),
383 AutogenToken.other_name: (
384 AutogenState.have_value, self.do_str_value),
385 AutogenToken.string: (
386 AutogenState.have_value, self.do_str_value),
387 AutogenToken.number: (
388 AutogenState.have_value, self.do_str_value),
389 AutogenToken.lbrace: (
390 AutogenState.need_name, self.do_start_block),
391 },
392 AutogenState.need_idx: {
393 AutogenToken.var_name: (
394 AutogenState.need_rbracket, self.do_indexed_name),
395 AutogenToken.number: (
396 AutogenState.need_rbracket, self.do_indexed_name),
397 },
398 AutogenState.need_rbracket: {
399 AutogenToken.rbracket: (AutogenState.indx_name, None),
400 },
401 AutogenState.indx_name: {
402 AutogenToken.semicolon: (
403 AutogenState.need_name, self.do_empty_val),
404 AutogenToken.equals: (AutogenState.need_value, None),
405 },
406 AutogenState.have_value: {
407 AutogenToken.semicolon: (AutogenState.need_name, None),
408 AutogenToken.comma: (AutogenState.need_value, None),
409 },
410 }
411
412 for code, token in self.read_tokens(f):
413 if code in transitions[state]:
414 state, handler = transitions[state][code]
415 if handler is not None:
416 handler(token)
417 else:
7e90f5ad 418 self.error(
ab4f1501 419 "Parse error in state %s: unexpected token '%s'" % (
7e90f5ad 420 state, token))
ab4f1501
CW
421 if state == AutogenState.done:
422 break
423
7e90f5ad
CW
424 def read_definitions(self, path):
425 self.cur_file = path
426 with open(path) as f:
427 self.read_definitions_file(f)
428
ab4f1501
CW
429defparser = AutogenParser()
430
431#
432# Output
433#
434
435outputs = {}
436
437def output(s, section=''):
438 if s == "":
439 return
440 outputs.setdefault(section, [])
441 outputs[section].append(s)
442
443def write_output(section=''):
444 for s in outputs.get(section, []):
aa437b58 445 print(s, end='')
ab4f1501 446
8c411768
BC
447#
448# Global variables
449#
8c411768
BC
450
451def gvar_add(var, value):
ab4f1501 452 output(var + " += " + value + "\n")
8c411768
BC
453
454#
455# Per PROGRAM/SCRIPT variables
456#
457
ab4f1501
CW
458seen_vars = set()
459
460def vars_init(defn, *var_list):
461 name = defn['name']
462
463 if name not in seen_target and name not in seen_vars:
464 for var in var_list:
465 output(var + " = \n", section='decl')
466 seen_vars.add(name)
e1fd1939 467
8c411768 468def var_set(var, value):
ab4f1501 469 output(var + " = " + value + "\n")
8c411768
BC
470
471def var_add(var, value):
ab4f1501 472 output(var + " += " + value + "\n")
8c411768
BC
473
474#
ab4f1501 475# Variable names and rules
8c411768
BC
476#
477
ab4f1501
CW
478canonical_name_re = re.compile(r'[^0-9A-Za-z@_]')
479canonical_name_suffix = ""
480
481def set_canonical_name_suffix(suffix):
482 global canonical_name_suffix
483 canonical_name_suffix = suffix
484
485def cname(defn):
486 return canonical_name_re.sub('_', defn['name'] + canonical_name_suffix)
8c411768 487
911bd640
BC
488def rule(target, source, cmd):
489 if cmd[0] == "\n":
ab4f1501 490 output("\n" + target + ": " + source + cmd.replace("\n", "\n\t") + "\n")
911bd640 491 else:
ab4f1501 492 output("\n" + target + ": " + source + "\n\t" + cmd.replace("\n", "\n\t") + "\n")
8c411768 493
d9b78bce 494#
ab4f1501 495# Handle keys with platform names as values, for example:
d9b78bce
BC
496#
497# kernel = {
498# nostrip = emu;
f6023b61 499# ...
d9b78bce
BC
500# }
501#
ab4f1501
CW
502def platform_tagged(defn, platform, tag):
503 for value in defn.find_all(tag):
504 for group in RMAP[platform]:
505 if value == group:
506 return True
507 return False
8c411768 508
ab4f1501
CW
509def if_platform_tagged(defn, platform, tag, snippet_if, snippet_else=None):
510 if platform_tagged(defn, platform, tag):
511 return snippet_if
512 elif snippet_else is not None:
513 return snippet_else
8c411768 514
d9b78bce 515#
ab4f1501 516# Handle tagged values
8427685f
BC
517#
518# module = {
519# extra_dist = ...
520# extra_dist = ...
521# ...
522# };
523#
ab4f1501
CW
524def foreach_value(defn, tag, closure):
525 r = []
526 for value in defn.find_all(tag):
527 r.append(closure(value))
528 return ''.join(r)
8427685f
BC
529
530#
ab4f1501 531# Handle best matched values for a platform, for example:
d9b78bce
BC
532#
533# module = {
534# cflags = '-Wall';
535# emu_cflags = '-Wall -DGRUB_EMU=1';
f6023b61 536# ...
d9b78bce
BC
537# }
538#
ab4f1501
CW
539def foreach_platform_specific_value(defn, platform, suffix, nonetag, closure):
540 r = []
911bd640 541 for group in RMAP[platform]:
ab4f1501
CW
542 values = list(defn.find_all(group + suffix))
543 if values:
544 for value in values:
545 r.append(closure(value))
546 break
547 else:
548 for value in defn.find_all(nonetag):
549 r.append(closure(value))
550 return ''.join(r)
8667a314 551
8427685f 552#
ab4f1501 553# Handle values from sum of all groups for a platform, for example:
8427685f
BC
554#
555# module = {
556# common = kern/misc.c;
557# emu = kern/emu/misc.c;
558# ...
559# }
560#
ab4f1501
CW
561def foreach_platform_value(defn, platform, suffix, closure):
562 r = []
8427685f 563 for group in RMAP[platform]:
ab4f1501
CW
564 for value in defn.find_all(group + suffix):
565 r.append(closure(value))
566 return ''.join(r)
8427685f 567
ab4f1501
CW
568def platform_conditional(platform, closure):
569 output("\nif COND_" + platform + "\n")
570 closure(platform)
571 output("endif\n")
8c411768 572
8427685f 573#
ab4f1501 574# Handle guarding with platform-specific "enable" keys, for example:
8427685f
BC
575#
576# module = {
577# name = pci;
578# noemu = bus/pci.c;
579# emu = bus/emu/pci.c;
580# emu = commands/lspci.c;
581#
582# enable = emu;
583# enable = i386_pc;
584# enable = x86_efi;
585# enable = i386_ieee1275;
586# enable = i386_coreboot;
587# };
588#
ab4f1501
CW
589def foreach_enabled_platform(defn, closure):
590 if 'enable' in defn:
591 for platform in GRUB_PLATFORMS:
592 if platform_tagged(defn, platform, "enable"):
593 platform_conditional(platform, closure)
594 else:
595 for platform in GRUB_PLATFORMS:
596 platform_conditional(platform, closure)
d9b78bce 597
8427685f 598#
ab4f1501 599# Handle guarding with platform-specific automake conditionals, for example:
8427685f
BC
600#
601# module = {
602# name = usb;
603# common = bus/usb/usb.c;
604# noemu = bus/usb/usbtrans.c;
605# noemu = bus/usb/usbhub.c;
606# enable = emu;
607# enable = i386;
54da1feb 608# enable = mips_loongson;
9d25b0da 609# emu_condition = COND_GRUB_EMU_SDL;
8427685f
BC
610# };
611#
ab4f1501
CW
612def under_platform_specific_conditionals(defn, platform, closure):
613 output(foreach_platform_specific_value(defn, platform, "_condition", "condition", lambda cond: "if " + cond + "\n"))
614 closure(defn, platform)
615 output(foreach_platform_specific_value(defn, platform, "_condition", "condition", lambda cond: "endif " + cond + "\n"))
616
617def platform_specific_values(defn, platform, suffix, nonetag):
618 return foreach_platform_specific_value(defn, platform, suffix, nonetag,
8427685f 619 lambda value: value + " ")
911bd640 620
ab4f1501
CW
621def platform_values(defn, platform, suffix):
622 return foreach_platform_value(defn, platform, suffix, lambda value: value + " ")
623
624def extra_dist(defn):
625 return foreach_value(defn, "extra_dist", lambda value: value + " ")
626
627def platform_sources(defn, p): return platform_values(defn, p, "")
628def platform_nodist_sources(defn, p): return platform_values(defn, p, "_nodist")
629
630def platform_startup(defn, p): return platform_specific_values(defn, p, "_startup", "startup")
631def platform_ldadd(defn, p): return platform_specific_values(defn, p, "_ldadd", "ldadd")
632def platform_dependencies(defn, p): return platform_specific_values(defn, p, "_dependencies", "dependencies")
633def platform_cflags(defn, p): return platform_specific_values(defn, p, "_cflags", "cflags")
634def platform_ldflags(defn, p): return platform_specific_values(defn, p, "_ldflags", "ldflags")
635def platform_cppflags(defn, p): return platform_specific_values(defn, p, "_cppflags", "cppflags")
636def platform_ccasflags(defn, p): return platform_specific_values(defn, p, "_ccasflags", "ccasflags")
637def platform_stripflags(defn, p): return platform_specific_values(defn, p, "_stripflags", "stripflags")
638def platform_objcopyflags(defn, p): return platform_specific_values(defn, p, "_objcopyflags", "objcopyflags")
8c411768 639
e1fd1939
CW
640#
641# Emit snippet only the first time through for the current name.
642#
ab4f1501
CW
643seen_target = set()
644
645def first_time(defn, snippet):
646 if defn['name'] not in seen_target:
647 return snippet
648 return ''
649
b1f742c1
VS
650def is_platform_independent(defn):
651 if 'enable' in defn:
652 return False
653 for suffix in [ "", "_nodist" ]:
654 template = platform_values(defn, GRUB_PLATFORMS[0], suffix)
655 for platform in GRUB_PLATFORMS[1:]:
656 if template != platform_values(defn, platform, suffix):
657 return False
658
659 for suffix in [ "startup", "ldadd", "dependencies", "cflags", "ldflags", "cppflags", "ccasflags", "stripflags", "objcopyflags", "condition" ]:
660 template = platform_specific_values(defn, GRUB_PLATFORMS[0], "_" + suffix, suffix)
661 for platform in GRUB_PLATFORMS[1:]:
662 if template != platform_specific_values(defn, platform, "_" + suffix, suffix):
663 return False
664 for tag in [ "nostrip" ]:
665 template = platform_tagged(defn, GRUB_PLATFORMS[0], tag)
666 for platform in GRUB_PLATFORMS[1:]:
667 if template != platform_tagged(defn, platform, tag):
668 return False
669
670 return True
671
ab4f1501
CW
672def module(defn, platform):
673 name = defn['name']
674 set_canonical_name_suffix(".module")
675
676 gvar_add("platform_PROGRAMS", name + ".module")
677 gvar_add("MODULE_FILES", name + ".module$(EXEEXT)")
678
679 var_set(cname(defn) + "_SOURCES", platform_sources(defn, platform) + " ## platform sources")
680 var_set("nodist_" + cname(defn) + "_SOURCES", platform_nodist_sources(defn, platform) + " ## platform nodist sources")
681 var_set(cname(defn) + "_LDADD", platform_ldadd(defn, platform))
682 var_set(cname(defn) + "_CFLAGS", "$(AM_CFLAGS) $(CFLAGS_MODULE) " + platform_cflags(defn, platform))
683 var_set(cname(defn) + "_LDFLAGS", "$(AM_LDFLAGS) $(LDFLAGS_MODULE) " + platform_ldflags(defn, platform))
684 var_set(cname(defn) + "_CPPFLAGS", "$(AM_CPPFLAGS) $(CPPFLAGS_MODULE) " + platform_cppflags(defn, platform))
685 var_set(cname(defn) + "_CCASFLAGS", "$(AM_CCASFLAGS) $(CCASFLAGS_MODULE) " + platform_ccasflags(defn, platform))
686 var_set(cname(defn) + "_DEPENDENCIES", "$(TARGET_OBJ2ELF) " + platform_dependencies(defn, platform))
687
688 gvar_add("dist_noinst_DATA", extra_dist(defn))
689 gvar_add("BUILT_SOURCES", "$(nodist_" + cname(defn) + "_SOURCES)")
690 gvar_add("CLEANFILES", "$(nodist_" + cname(defn) + "_SOURCES)")
691
692 gvar_add("MOD_FILES", name + ".mod")
693 gvar_add("MARKER_FILES", name + ".marker")
694 gvar_add("CLEANFILES", name + ".marker")
695 output("""
696""" + name + """.marker: $(""" + cname(defn) + """_SOURCES) $(nodist_""" + cname(defn) + """_SOURCES)
697 $(TARGET_CPP) -DGRUB_LST_GENERATOR $(CPPFLAGS_MARKER) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(""" + cname(defn) + """_CPPFLAGS) $(CPPFLAGS) $^ > $@.new || (rm -f $@; exit 1)
6568636e 698 grep 'MARKER' $@.new > $@; rm -f $@.new
ab4f1501
CW
699""")
700
701def kernel(defn, platform):
702 name = defn['name']
703 set_canonical_name_suffix(".exec")
704 gvar_add("platform_PROGRAMS", name + ".exec")
705 var_set(cname(defn) + "_SOURCES", platform_startup(defn, platform))
706 var_add(cname(defn) + "_SOURCES", platform_sources(defn, platform))
707 var_set("nodist_" + cname(defn) + "_SOURCES", platform_nodist_sources(defn, platform) + " ## platform nodist sources")
708 var_set(cname(defn) + "_LDADD", platform_ldadd(defn, platform))
709 var_set(cname(defn) + "_CFLAGS", "$(AM_CFLAGS) $(CFLAGS_KERNEL) " + platform_cflags(defn, platform))
710 var_set(cname(defn) + "_LDFLAGS", "$(AM_LDFLAGS) $(LDFLAGS_KERNEL) " + platform_ldflags(defn, platform))
711 var_set(cname(defn) + "_CPPFLAGS", "$(AM_CPPFLAGS) $(CPPFLAGS_KERNEL) " + platform_cppflags(defn, platform))
712 var_set(cname(defn) + "_CCASFLAGS", "$(AM_CCASFLAGS) $(CCASFLAGS_KERNEL) " + platform_ccasflags(defn, platform))
713 var_set(cname(defn) + "_STRIPFLAGS", "$(AM_STRIPFLAGS) $(STRIPFLAGS_KERNEL) " + platform_stripflags(defn, platform))
714 var_set(cname(defn) + "_DEPENDENCIES", "$(TARGET_OBJ2ELF)")
715
716 gvar_add("dist_noinst_DATA", extra_dist(defn))
717 gvar_add("BUILT_SOURCES", "$(nodist_" + cname(defn) + "_SOURCES)")
718 gvar_add("CLEANFILES", "$(nodist_" + cname(defn) + "_SOURCES)")
719
720 gvar_add("platform_DATA", name + ".img")
721 gvar_add("CLEANFILES", name + ".img")
722 rule(name + ".img", name + ".exec$(EXEEXT)",
723 if_platform_tagged(defn, platform, "nostrip",
a9f25a08 724"""if test x$(TARGET_APPLE_LINKER) = x1; then \
fc97214f 725 $(TARGET_OBJCONV) -f$(TARGET_MODULE_FORMAT) -nr:_grub_mod_init:grub_mod_init -nr:_grub_mod_fini:grub_mod_fini -ed2022 -wd1106 -nu -nd $< $@; \
22899b9c 726 elif test ! -z '$(TARGET_OBJ2ELF)'; then \
60b967be 727 $(TARGET_OBJ2ELF) $< $@ || (rm -f $@; exit 1); \
22899b9c 728 else cp $< $@; fi""",
a9f25a08 729"""if test x$(TARGET_APPLE_LINKER) = x1; then \
ab4f1501 730 $(TARGET_STRIP) -S -x $(""" + cname(defn) + """) -o $@.bin $<; \
c8f7614b 731 $(TARGET_OBJCONV) -f$(TARGET_MODULE_FORMAT) -nr:_grub_mod_init:grub_mod_init -nr:_grub_mod_fini:grub_mod_fini -ed2022 -ed2016 -wd1106 -nu -nd $@.bin $@; \
4d6e9c8a 732 rm -f $@.bin; \
60b967be
VS
733 elif test ! -z '$(TARGET_OBJ2ELF)'; then \
734 """ + "$(TARGET_STRIP) $(" + cname(defn) + "_STRIPFLAGS) -o $@.bin $< && \
735 $(TARGET_OBJ2ELF) $@.bin $@ || (rm -f $@; rm -f $@.bin; exit 1); \
4d6e9c8a 736 rm -f $@.bin; \
ab4f1501 737else """ + "$(TARGET_STRIP) $(" + cname(defn) + "_STRIPFLAGS) -o $@ $<; \
22899b9c 738fi"""))
ab4f1501
CW
739
740def image(defn, platform):
741 name = defn['name']
742 set_canonical_name_suffix(".image")
743 gvar_add("platform_PROGRAMS", name + ".image")
744 var_set(cname(defn) + "_SOURCES", platform_sources(defn, platform))
745 var_set("nodist_" + cname(defn) + "_SOURCES", platform_nodist_sources(defn, platform) + "## platform nodist sources")
746 var_set(cname(defn) + "_LDADD", platform_ldadd(defn, platform))
747 var_set(cname(defn) + "_CFLAGS", "$(AM_CFLAGS) $(CFLAGS_IMAGE) " + platform_cflags(defn, platform))
748 var_set(cname(defn) + "_LDFLAGS", "$(AM_LDFLAGS) $(LDFLAGS_IMAGE) " + platform_ldflags(defn, platform))
749 var_set(cname(defn) + "_CPPFLAGS", "$(AM_CPPFLAGS) $(CPPFLAGS_IMAGE) " + platform_cppflags(defn, platform))
750 var_set(cname(defn) + "_CCASFLAGS", "$(AM_CCASFLAGS) $(CCASFLAGS_IMAGE) " + platform_ccasflags(defn, platform))
751 var_set(cname(defn) + "_OBJCOPYFLAGS", "$(OBJCOPYFLAGS_IMAGE) " + platform_objcopyflags(defn, platform))
752 # var_set(cname(defn) + "_DEPENDENCIES", platform_dependencies(defn, platform) + " " + platform_ldadd(defn, platform))
753
754 gvar_add("dist_noinst_DATA", extra_dist(defn))
755 gvar_add("BUILT_SOURCES", "$(nodist_" + cname(defn) + "_SOURCES)")
756 gvar_add("CLEANFILES", "$(nodist_" + cname(defn) + "_SOURCES)")
757
758 gvar_add("platform_DATA", name + ".img")
759 gvar_add("CLEANFILES", name + ".img")
760 rule(name + ".img", name + ".image$(EXEEXT)", """
a9f25a08 761if test x$(TARGET_APPLE_LINKER) = x1; then \
8c411768
BC
762 $(MACHO2IMG) $< $@; \
763else \
68e158df 764 $(TARGET_OBJCOPY) $(""" + cname(defn) + """_OBJCOPYFLAGS) --strip-unneeded -R .note -R .comment -R .note.gnu.build-id -R .MIPS.abiflags -R .reginfo -R .rel.dyn -R .note.gnu.gold-version -R .ARM.exidx $< $@; \
8c411768
BC
765fi
766""")
ab4f1501
CW
767
768def library(defn, platform):
769 name = defn['name']
770 set_canonical_name_suffix("")
771
772 vars_init(defn,
773 cname(defn) + "_SOURCES",
774 "nodist_" + cname(defn) + "_SOURCES",
775 cname(defn) + "_CFLAGS",
776 cname(defn) + "_CPPFLAGS",
777 cname(defn) + "_CCASFLAGS")
778 # cname(defn) + "_DEPENDENCIES")
779
780 if name not in seen_target:
781 gvar_add("noinst_LIBRARIES", name)
782 var_add(cname(defn) + "_SOURCES", platform_sources(defn, platform))
783 var_add("nodist_" + cname(defn) + "_SOURCES", platform_nodist_sources(defn, platform))
784 var_add(cname(defn) + "_CFLAGS", first_time(defn, "$(AM_CFLAGS) $(CFLAGS_LIBRARY) ") + platform_cflags(defn, platform))
785 var_add(cname(defn) + "_CPPFLAGS", first_time(defn, "$(AM_CPPFLAGS) $(CPPFLAGS_LIBRARY) ") + platform_cppflags(defn, platform))
786 var_add(cname(defn) + "_CCASFLAGS", first_time(defn, "$(AM_CCASFLAGS) $(CCASFLAGS_LIBRARY) ") + platform_ccasflags(defn, platform))
787 # var_add(cname(defn) + "_DEPENDENCIES", platform_dependencies(defn, platform) + " " + platform_ldadd(defn, platform))
788
789 gvar_add("dist_noinst_DATA", extra_dist(defn))
790 if name not in seen_target:
791 gvar_add("BUILT_SOURCES", "$(nodist_" + cname(defn) + "_SOURCES)")
792 gvar_add("CLEANFILES", "$(nodist_" + cname(defn) + "_SOURCES)")
793
794def installdir(defn, default="bin"):
795 return defn.get('installdir', default)
796
797def manpage(defn, adddeps):
798 name = defn['name']
799 mansection = defn['mansection']
800
801 output("if COND_MAN_PAGES\n")
802 gvar_add("man_MANS", name + "." + mansection)
803 rule(name + "." + mansection, name + " " + adddeps, """
804chmod a+x """ + name + """
805PATH=$(builddir):$$PATH pkgdatadir=$(builddir) $(HELP2MAN) --section=""" + mansection + """ -i $(top_srcdir)/docs/man/""" + name + """.h2m -o $@ """ + name + """
8c411768 806""")
ab4f1501
CW
807 gvar_add("CLEANFILES", name + "." + mansection)
808 output("endif\n")
809
810def program(defn, platform, test=False):
811 name = defn['name']
812 set_canonical_name_suffix("")
813
814 if 'testcase' in defn:
815 gvar_add("check_PROGRAMS", name)
816 gvar_add("TESTS", name)
817 else:
818 var_add(installdir(defn) + "_PROGRAMS", name)
819 if 'mansection' in defn:
820 manpage(defn, "")
821
822 var_set(cname(defn) + "_SOURCES", platform_sources(defn, platform))
823 var_set("nodist_" + cname(defn) + "_SOURCES", platform_nodist_sources(defn, platform))
824 var_set(cname(defn) + "_LDADD", platform_ldadd(defn, platform))
825 var_set(cname(defn) + "_CFLAGS", "$(AM_CFLAGS) $(CFLAGS_PROGRAM) " + platform_cflags(defn, platform))
826 var_set(cname(defn) + "_LDFLAGS", "$(AM_LDFLAGS) $(LDFLAGS_PROGRAM) " + platform_ldflags(defn, platform))
827 var_set(cname(defn) + "_CPPFLAGS", "$(AM_CPPFLAGS) $(CPPFLAGS_PROGRAM) " + platform_cppflags(defn, platform))
828 var_set(cname(defn) + "_CCASFLAGS", "$(AM_CCASFLAGS) $(CCASFLAGS_PROGRAM) " + platform_ccasflags(defn, platform))
829 # var_set(cname(defn) + "_DEPENDENCIES", platform_dependencies(defn, platform) + " " + platform_ldadd(defn, platform))
830
831 gvar_add("dist_noinst_DATA", extra_dist(defn))
832 gvar_add("BUILT_SOURCES", "$(nodist_" + cname(defn) + "_SOURCES)")
833 gvar_add("CLEANFILES", "$(nodist_" + cname(defn) + "_SOURCES)")
834
835def data(defn, platform):
836 var_add("dist_" + installdir(defn) + "_DATA", platform_sources(defn, platform))
837 gvar_add("dist_noinst_DATA", extra_dist(defn))
838
368ecfc3
VS
839def transform_data(defn, platform):
840 name = defn['name']
841
842 var_add(installdir(defn) + "_DATA", name)
843
844 rule(name, "$(top_builddir)/config.status " + platform_sources(defn, platform) + platform_dependencies(defn, platform), """
845(for x in """ + platform_sources(defn, platform) + """; do cat $(srcdir)/"$$x"; done) | $(top_builddir)/config.status --file=$@:-
846chmod a+x """ + name + """
847""")
848
849 gvar_add("CLEANFILES", name)
850 gvar_add("EXTRA_DIST", extra_dist(defn))
851 gvar_add("dist_noinst_DATA", platform_sources(defn, platform))
852
ab4f1501
CW
853def script(defn, platform):
854 name = defn['name']
855
856 if 'testcase' in defn:
857 gvar_add("check_SCRIPTS", name)
858 gvar_add ("TESTS", name)
859 else:
860 var_add(installdir(defn) + "_SCRIPTS", name)
861 if 'mansection' in defn:
862 manpage(defn, "grub-mkconfig_lib")
863
864 rule(name, "$(top_builddir)/config.status " + platform_sources(defn, platform) + platform_dependencies(defn, platform), """
865(for x in """ + platform_sources(defn, platform) + """; do cat $(srcdir)/"$$x"; done) | $(top_builddir)/config.status --file=$@:-
866chmod a+x """ + name + """
8c411768
BC
867""")
868
ab4f1501
CW
869 gvar_add("CLEANFILES", name)
870 gvar_add("EXTRA_DIST", extra_dist(defn))
871 gvar_add("dist_noinst_DATA", platform_sources(defn, platform))
8c411768 872
e1fd1939 873def rules(target, closure):
ab4f1501
CW
874 seen_target.clear()
875 seen_vars.clear()
876
877 for defn in defparser.definitions.find_all(target):
b1f742c1
VS
878 if is_platform_independent(defn):
879 under_platform_specific_conditionals(defn, GRUB_PLATFORMS[0], closure)
880 else:
881 foreach_enabled_platform(
882 defn,
883 lambda p: under_platform_specific_conditionals(defn, p, closure))
ab4f1501
CW
884 # Remember that we've seen this target.
885 seen_target.add(defn['name'])
886
887parser = OptionParser(usage="%prog DEFINITION-FILES")
888_, args = parser.parse_args()
889
890for arg in args:
7e90f5ad 891 defparser.read_definitions(arg)
ab4f1501
CW
892
893rules("module", module)
894rules("kernel", kernel)
895rules("image", image)
896rules("library", library)
897rules("program", program)
898rules("script", script)
899rules("data", data)
368ecfc3 900rules("transform_data", transform_data)
ab4f1501
CW
901
902write_output(section='decl')
903write_output()