gentpl.py 23.7 KB
Newer Older
1
#! /usr/bin/python
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#  GRUB  --  GRand Unified Bootloader
#  Copyright (C) 2010,2011  Free Software Foundation, Inc.
#
#  GRUB is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  GRUB is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22

#
# This is the python script used to generate Makefile.tpl
#

GRUB_PLATFORMS = [ "emu", "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot",
23
                   "i386_multiboot", "i386_ieee1275", "x86_64_efi",
24
                   "i386_xen", "x86_64_xen",
25
                   "mips_loongson", "sparc64_ieee1275",
26
                   "powerpc_ieee1275", "mips_arc", "ia64_efi",
27
                   "mips_qemu_mips", "arm_uboot", "arm_efi" ]
28 29

GROUPS = {}
30 31 32 33

GROUPS["common"]   = GRUB_PLATFORMS[:]

# Groups based on CPU
34 35 36
GROUPS["i386"]     = [ "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot", "i386_multiboot", "i386_ieee1275" ]
GROUPS["x86_64"]   = [ "x86_64_efi" ]
GROUPS["x86"]      = GROUPS["i386"] + GROUPS["x86_64"]
37
GROUPS["mips"]     = [ "mips_loongson", "mips_qemu_mips", "mips_arc" ]
38 39
GROUPS["sparc64"]  = [ "sparc64_ieee1275" ]
GROUPS["powerpc"]  = [ "powerpc_ieee1275" ]
40
GROUPS["arm"]      = [ "arm_uboot", "arm_efi" ]
41

42
# Groups based on firmware
43
GROUPS["efi"]  = [ "i386_efi", "x86_64_efi", "ia64_efi", "arm_efi" ]
44
GROUPS["ieee1275"]   = [ "i386_ieee1275", "sparc64_ieee1275", "powerpc_ieee1275" ]
45
GROUPS["uboot"] = [ "arm_uboot" ]
46
GROUPS["xen"]  = [ "i386_xen", "x86_64_xen" ]
47

48 49 50 51
# emu is a special case so many core functionality isn't needed on this platform
GROUPS["noemu"]   = GRUB_PLATFORMS[:]; GROUPS["noemu"].remove("emu")

# Groups based on hardware features
52 53
GROUPS["cmos"] = GROUPS["x86"][:] + ["mips_loongson", "mips_qemu_mips",
                                     "sparc64_ieee1275", "powerpc_ieee1275"]
54
GROUPS["cmos"].remove("i386_efi"); GROUPS["cmos"].remove("x86_64_efi");
55
GROUPS["pci"]      = GROUPS["x86"] + ["mips_loongson"]
56
GROUPS["usb"]      = GROUPS["pci"]
57

58
# If gfxterm is main output console integrate it into kernel
59
GROUPS["videoinkernel"] = ["mips_loongson", "i386_coreboot" ]
60 61
GROUPS["videomodules"]   = GRUB_PLATFORMS[:];
for i in GROUPS["videoinkernel"]: GROUPS["videomodules"].remove(i)
62

63
# Similar for terminfo
64
GROUPS["terminfoinkernel"] = [ "emu", "mips_loongson", "mips_arc", "mips_qemu_mips" ] + GROUPS["xen"] + GROUPS["ieee1275"] + GROUPS["uboot"];
65 66 67
GROUPS["terminfomodule"]   = GRUB_PLATFORMS[:];
for i in GROUPS["terminfoinkernel"]: GROUPS["terminfomodule"].remove(i)

68 69 70
# Flattened Device Trees (FDT)
GROUPS["fdt"] = [ "arm_uboot", "arm_efi" ]

71 72 73
# Miscelaneous groups schedulded to disappear in future
GROUPS["i386_coreboot_multiboot_qemu"] = ["i386_coreboot", "i386_multiboot", "i386_qemu"]
GROUPS["nopc"] = GRUB_PLATFORMS[:]; GROUPS["nopc"].remove("i386_pc")
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

#
# Create platform => groups reverse map, where groups covering that
# platform are ordered by their sizes
#
RMAP = {}
for platform in GRUB_PLATFORMS:
    # initialize with platform itself as a group
    RMAP[platform] = [ platform ]

    for k in GROUPS.keys():
        v = GROUPS[k]
        # skip groups that don't cover this platform
        if platform not in v: continue

        bigger = []
        smaller = []
        # partition currently known groups based on their size
        for group in RMAP[platform]:
            if group in GRUB_PLATFORMS: smaller.append(group)
            elif len(GROUPS[group]) < len(v): smaller.append(group)
            else: bigger.append(group)
        # insert in the middle
        RMAP[platform] = smaller + [ k ] + bigger

#
# Global variables
#
Colin Watson's avatar
Colin Watson committed
102
GVARS = set()
103 104

def gvar_add(var, value):
Colin Watson's avatar
Colin Watson committed
105
    GVARS.add(var)
106 107 108 109
    return var + " += " + value + "\n"

def global_variable_initializers():
    r = ""
Colin Watson's avatar
Colin Watson committed
110
    for var in sorted(GVARS):
111 112 113 114 115 116 117
        r += var + " ?= \n"
    return r

#
# Per PROGRAM/SCRIPT variables 
#

Colin Watson's avatar
Colin Watson committed
118 119 120 121 122 123 124 125 126 127
def vars_init(*var_list):
    r = "[+ IF (if (not (assoc-ref seen-vars (get \".name\"))) \"seen\") +]"
    r += "[+ (out-suspend \"v\") +]"
    for var in var_list:
        r += var + "  = \n"
    r += "[+ (out-resume \"v\") +]"
    r += "[+ (set! seen-vars (assoc-set! seen-vars (get \".name\") 0)) +]"
    r += "[+ ENDIF +]"
    return first_time(r)

128 129 130 131 132 133 134 135 136 137
def var_set(var, value):
    return var + "  = " + value + "\n"

def var_add(var, value):
    return var + " += " + value + "\n"

#
# Autogen constructs
#

138
def set_canonical_name_suffix(suffix): return "[+ % name `export cname=$(echo %s" + suffix + " | sed -e 's/[^0-9A-Za-z@_]/_/g')` +]"
BVK Chaitanya's avatar
BVK Chaitanya committed
139
def cname(): return "[+ % name `echo $cname` +]"
140

BVK Chaitanya's avatar
BVK Chaitanya committed
141 142 143 144 145
def rule(target, source, cmd):
    if cmd[0] == "\n":
        return "\n" + target + ": " + source + cmd.replace("\n", "\n\t") + "\n"
    else:
        return "\n" + target + ": " + source + "\n\t" + cmd.replace("\n", "\n\t") + "\n"
146

BVK Chaitanya's avatar
BVK Chaitanya committed
147 148 149 150 151
#
# Template for keys with platform names as values, for example:
#
# kernel = {
#   nostrip = emu;
BVK Chaitanya's avatar
BVK Chaitanya committed
152
#   ...
BVK Chaitanya's avatar
BVK Chaitanya committed
153 154 155
# }
#
def if_platform_tagged(platform, tag, snippet_if, snippet_else=None):
BVK Chaitanya's avatar
BVK Chaitanya committed
156 157 158
    r = ""
    r += "[+ IF " + tag + " defined +]"
    r += "[+ FOR " + tag + " +][+ CASE " + tag + " +]"
159
    for group in RMAP[platform]:
BVK Chaitanya's avatar
BVK Chaitanya committed
160 161
        r += "[+ = \"" + group + "\" +]" + snippet_if

BVK Chaitanya's avatar
BVK Chaitanya committed
162
    if snippet_else != None: r += "[+ * +]" + snippet_else
BVK Chaitanya's avatar
BVK Chaitanya committed
163
    r += "[+ ESAC +][+ ENDFOR +]"
164

BVK Chaitanya's avatar
BVK Chaitanya committed
165
    if snippet_else == None:
166
        r += "[+ ENDIF +]"
BVK Chaitanya's avatar
BVK Chaitanya committed
167
        return r
168

BVK Chaitanya's avatar
BVK Chaitanya committed
169
    r += "[+ ELSE +]" + snippet_else + "[+ ENDIF +]"
170 171
    return r

BVK Chaitanya's avatar
BVK Chaitanya committed
172
#
173 174 175 176 177 178 179 180 181 182 183 184 185
# Template for tagged values
#
# module = {
#   extra_dist = ...
#   extra_dist = ...
#   ...
# };
#
def foreach_value(tag, closure):
    return "[+ FOR " + tag + " +]" + closure("[+ ." + tag + " +]") + "[+ ENDFOR +]"

#
# Template for handling best matched values for a platform, for example:
BVK Chaitanya's avatar
BVK Chaitanya committed
186 187 188 189
#
# module = {
#   cflags = '-Wall';
#   emu_cflags = '-Wall -DGRUB_EMU=1';
BVK Chaitanya's avatar
BVK Chaitanya committed
190
#   ...
BVK Chaitanya's avatar
BVK Chaitanya committed
191 192
# }
#
193
def foreach_platform_specific_value(platform, suffix, nonetag, closure):
194
    r = ""
BVK Chaitanya's avatar
BVK Chaitanya committed
195
    for group in RMAP[platform]:
BVK Chaitanya's avatar
BVK Chaitanya committed
196
        gtag = group + suffix
BVK Chaitanya's avatar
BVK Chaitanya committed
197 198 199 200 201 202

        if group == RMAP[platform][0]:
            r += "[+ IF " + gtag + " +]"
        else:
            r += "[+ ELIF " + gtag + " +]"

BVK Chaitanya's avatar
BVK Chaitanya committed
203
        r += "[+ FOR " + gtag + " +]" + closure("[+ ." + gtag + " +]") + "[+ ENDFOR +]"
204 205 206
    r += "[+ ELSE +][+ FOR " + nonetag + " +]" + closure("[+ ." + nonetag + " +]") + "[+ ENDFOR +][+ ENDIF +]"
    return r

207 208 209 210 211 212 213 214 215 216 217
#
# Returns autogen code that defines an autogen macro using the
# definition given in the 'snippet'.
#
def define_autogen_macro(name, snippet):
    r = ""
    r += "[+ DEFINE " + name + " +]"
    r += snippet
    r += "[+ ENDDEF +]\n"
    return r

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
#
# Template for handling values from sum of all groups for a platform,
# for example:
#
# module = {
#   common = kern/misc.c;
#   emu = kern/emu/misc.c;
#   ...
# }
#
def foreach_platform_value (platform, suffix, closure):
    r = ""
    for group in RMAP[platform]:
        gtag = group + suffix

        r += "[+ IF " + gtag + " +]"
        r += "[+ FOR " + gtag + " +]" + closure("[+ ." + gtag + " +]") + "[+ ENDFOR +]"
        r += "[+ ENDIF +]"
236 237
    return r

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
#
# Template for gaurding with platform specific "enable" keys, for example:
#
#  module = {
#    name = pci;
#    noemu = bus/pci.c;
#    emu = bus/emu/pci.c;
#    emu = commands/lspci.c;
#
#    enable = emu;
#    enable = i386_pc;
#    enable = x86_efi;
#    enable = i386_ieee1275;
#    enable = i386_coreboot;
#  };
#
def foreach_enabled_platform(closure):
BVK Chaitanya's avatar
BVK Chaitanya committed
255 256 257 258 259 260
    r = "[+ IF - enable undefined +]"
    for platform in GRUB_PLATFORMS:
        r += "\nif COND_" + platform + "\n" + closure(platform) + "endif\n"
    r += "[+ ELSE +]"
    for platform in GRUB_PLATFORMS:
        x = "\nif COND_" + platform + "\n" + closure(platform) + "endif\n"
BVK Chaitanya's avatar
BVK Chaitanya committed
261
        r += if_platform_tagged(platform, "enable", x)
BVK Chaitanya's avatar
BVK Chaitanya committed
262
    r += "[+ ENDIF +]"
BVK Chaitanya's avatar
BVK Chaitanya committed
263 264
    return r

265 266 267 268 269 270 271 272 273 274 275
#
# Template for gaurding with platform specific automake conditionals,
# for example:
#
#  module = {
#    name = usb;
#    common = bus/usb/usb.c;
#    noemu = bus/usb/usbtrans.c;
#    noemu = bus/usb/usbhub.c;
#    enable = emu;
#    enable = i386;
276
#    enable = mips_loongson;
277 278 279
#    emu_condition = COND_GRUB_EMU_USB;
#  };
#
280 281 282 283 284 285 286 287
def define_macro_for_platform_conditionals_if_statement(p):
    return define_autogen_macro(
        "if_" + p + "_conditionals",
        foreach_platform_specific_value(platform, "_condition", "condition", lambda cond: "if " + cond + "\n"))
def define_macro_for_platform_conditionals_endif_statement(p):
    return define_autogen_macro(
        "endif_" + p + "_conditionals",
        foreach_platform_specific_value(platform, "_condition", "condition", lambda cond: "endif " + cond + "\n"))
BVK Chaitanya's avatar
BVK Chaitanya committed
288
def under_platform_specific_conditionals(platform, snippet):
289
    r  = "[+ if_" + platform + "_conditionals +]"
BVK Chaitanya's avatar
BVK Chaitanya committed
290
    r += snippet
291
    r += "[+ endif_" + platform + "_conditionals +]"
BVK Chaitanya's avatar
BVK Chaitanya committed
292 293
    return r

294 295 296
def platform_specific_values(platform, suffix, nonetag):
    return foreach_platform_specific_value(platform, suffix, nonetag,
                                           lambda value: value + " ")
BVK Chaitanya's avatar
BVK Chaitanya committed
297

298 299
def platform_values(platform, suffix):
    return foreach_platform_value(platform, suffix, lambda value: value + " ")
BVK Chaitanya's avatar
BVK Chaitanya committed
300

301 302
def extra_dist():
    return foreach_value("extra_dist", lambda value: value + " ")
BVK Chaitanya's avatar
BVK Chaitanya committed
303

304 305 306 307 308 309 310 311 312 313
def define_macro_for_platform_sources(p):
    return define_autogen_macro(
        "get_" + p + "_sources",
        platform_values(p, ""))
def define_macro_for_platform_nodist_sources(p):
    return define_autogen_macro(
        "get_" + p + "_nodist_sources",
        platform_values(p, "_nodist"))
def platform_sources(p): return "[+ get_" + p + "_sources +]"
def platform_nodist_sources(p): return "[+ get_" + p + "_nodist_sources +]"
314

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
#
# Returns Autogen code which defines the autogen macros that collect
# platform specific values for cflags, ldflags, etc. tags.
#
def define_macro_for_platform_startup(p):
    return define_autogen_macro(
        "get_" + p + "_startup",
        platform_specific_values(p, "_startup", "startup"))
def define_macro_for_platform_cflags(p):
    return define_autogen_macro(
        "get_" + p + "_cflags",
        platform_specific_values(p, "_cflags", "cflags"))
def define_macro_for_platform_ldadd(p):
    return define_autogen_macro(
        "get_" + p + "_ldadd",
        platform_specific_values(p, "_ldadd", "ldadd"))
331 332 333 334
def define_macro_for_platform_dependencies(p):
    return define_autogen_macro(
        "get_" + p + "_dependencies",
        platform_specific_values(p, "_dependencies", "dependencies"))
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
def define_macro_for_platform_ldflags(p):
    return define_autogen_macro(
        "get_" + p + "_ldflags",
        platform_specific_values(p, "_ldflags", "ldflags"))
def define_macro_for_platform_cppflags(p):
    return define_autogen_macro(
        "get_" + p + "_cppflags",
        platform_specific_values(p, "_cppflags", "cppflags"))
def define_macro_for_platform_ccasflags(p):
    return define_autogen_macro(
        "get_" + p + "_ccasflags",
        platform_specific_values(p, "_ccasflags", "ccasflags"))
def define_macro_for_platform_stripflags(p):
    return define_autogen_macro(
        "get_" + p + "_stripflags",
        platform_specific_values(p, "_stripflags", "stripflags"))
def define_macro_for_platform_objcopyflags(p):
    return define_autogen_macro(
        "get_" + p + "_objcopyflags",
        platform_specific_values(p, "_objcopyflags", "objcopyflags"))
#
# Autogen calls to invoke the above macros.
#
def platform_startup(p): return "[+ get_" + p + "_startup +]"
def platform_ldadd(p): return "[+ get_" + p + "_ldadd +]"
360
def platform_dependencies(p): return "[+ get_" + p + "_dependencies +]"
361 362 363 364 365 366
def platform_cflags(p): return "[+ get_" + p + "_cflags +]"
def platform_ldflags(p): return "[+ get_" + p + "_ldflags +]"
def platform_cppflags(p): return "[+ get_" + p + "_cppflags +]"
def platform_ccasflags(p): return "[+ get_" + p + "_ccasflags +]"
def platform_stripflags(p): return "[+ get_" + p + "_stripflags +]"
def platform_objcopyflags(p): return "[+ get_" + p + "_objcopyflags +]"
367

Colin Watson's avatar
Colin Watson committed
368 369 370 371 372 373 374 375 376
#
# Emit snippet only the first time through for the current name.
#
def first_time(snippet):
    r = "[+ IF (if (not (assoc-ref seen-target (get \".name\"))) \"seen\") +]"
    r += snippet
    r += "[+ ENDIF +]"
    return r

377
def module(platform):
BVK Chaitanya's avatar
BVK Chaitanya committed
378 379
    r = set_canonical_name_suffix(".module")

380
    r += gvar_add("platform_PROGRAMS", "[+ name +].module")
BVK Chaitanya's avatar
BVK Chaitanya committed
381
    r += gvar_add("MODULE_FILES", "[+ name +].module$(EXEEXT)")
382

BVK Chaitanya's avatar
BVK Chaitanya committed
383 384 385 386 387 388 389
    r += var_set(cname() + "_SOURCES", platform_sources(platform) + " ## platform sources")
    r += var_set("nodist_" + cname() + "_SOURCES", platform_nodist_sources(platform) + " ## platform nodist sources")
    r += var_set(cname() + "_LDADD", platform_ldadd(platform))
    r += var_set(cname() + "_CFLAGS", "$(AM_CFLAGS) $(CFLAGS_MODULE) " + platform_cflags(platform))
    r += var_set(cname() + "_LDFLAGS", "$(AM_LDFLAGS) $(LDFLAGS_MODULE) " + platform_ldflags(platform))
    r += var_set(cname() + "_CPPFLAGS", "$(AM_CPPFLAGS) $(CPPFLAGS_MODULE) " + platform_cppflags(platform))
    r += var_set(cname() + "_CCASFLAGS", "$(AM_CCASFLAGS) $(CCASFLAGS_MODULE) " + platform_ccasflags(platform))
390
    r += var_set(cname() + "_DEPENDENCIES", "$(TARGET_OBJ2ELF) " + platform_dependencies(platform))
391

392
    r += gvar_add("dist_noinst_DATA", extra_dist())
BVK Chaitanya's avatar
BVK Chaitanya committed
393 394
    r += gvar_add("BUILT_SOURCES", "$(nodist_" + cname() + "_SOURCES)")
    r += gvar_add("CLEANFILES", "$(nodist_" + cname() + "_SOURCES)")
395 396

    r += gvar_add("MOD_FILES", "[+ name +].mod")
397 398
    r += gvar_add("MARKER_FILES", "[+ name +].marker")
    r += gvar_add("CLEANFILES", "[+ name +].marker")
399
    r += """
400 401 402
[+ name +].marker: $(""" + cname() + """_SOURCES) $(nodist_""" + cname() + """_SOURCES)
	$(TARGET_CPP) -DGRUB_LST_GENERATOR $(CPPFLAGS_MARKER) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(""" + cname() + """_CPPFLAGS) $(CPPFLAGS) $^ > $@.new || (rm -f $@; exit 1)
	grep 'MARKER' $@.new > $@; rm -f $@.new
403 404 405 406
"""
    return r

def kernel(platform):
BVK Chaitanya's avatar
BVK Chaitanya committed
407
    r = set_canonical_name_suffix(".exec")
408
    r += gvar_add("platform_PROGRAMS", "[+ name +].exec")
409 410
    r += var_set(cname() + "_SOURCES", platform_startup(platform))
    r += var_add(cname() + "_SOURCES", platform_sources(platform))
BVK Chaitanya's avatar
BVK Chaitanya committed
411 412 413 414 415 416 417
    r += var_set("nodist_" + cname() + "_SOURCES", platform_nodist_sources(platform) + " ## platform nodist sources")
    r += var_set(cname() + "_LDADD", platform_ldadd(platform))
    r += var_set(cname() + "_CFLAGS", "$(AM_CFLAGS) $(CFLAGS_KERNEL) " + platform_cflags(platform))
    r += var_set(cname() + "_LDFLAGS", "$(AM_LDFLAGS) $(LDFLAGS_KERNEL) " + platform_ldflags(platform))
    r += var_set(cname() + "_CPPFLAGS", "$(AM_CPPFLAGS) $(CPPFLAGS_KERNEL) " + platform_cppflags(platform))
    r += var_set(cname() + "_CCASFLAGS", "$(AM_CCASFLAGS) $(CCASFLAGS_KERNEL) " + platform_ccasflags(platform))
    r += var_set(cname() + "_STRIPFLAGS", "$(AM_STRIPFLAGS) $(STRIPFLAGS_KERNEL) " + platform_stripflags(platform))
418
    r += var_set(cname() + "_DEPENDENCIES", "$(TARGET_OBJ2ELF)")
419

420
    r += gvar_add("dist_noinst_DATA", extra_dist())
BVK Chaitanya's avatar
BVK Chaitanya committed
421 422
    r += gvar_add("BUILT_SOURCES", "$(nodist_" + cname() + "_SOURCES)")
    r += gvar_add("CLEANFILES", "$(nodist_" + cname() + "_SOURCES)")
423 424

    r += gvar_add("platform_DATA", "[+ name +].img")
BVK Chaitanya's avatar
BVK Chaitanya committed
425 426
    r += gvar_add("CLEANFILES", "[+ name +].img")
    r += rule("[+ name +].img", "[+ name +].exec$(EXEEXT)",
427 428
              if_platform_tagged(platform, "nostrip",
"""if test x$(USE_APPLE_CC_FIXES) = xyes; then \
429
     $(TARGET_OBJCONV) -f$(TARGET_MODULE_FORMAT) -nr:_grub_mod_init:grub_mod_init -nr:_grub_mod_fini:grub_mod_fini -ed2022 -wd1106 -nu -nd $< $@; \
430 431 432 433
   elif test ! -z '$(TARGET_OBJ2ELF)'; then \
     cp $< $@.bin; $(TARGET_OBJ2ELF) $@.bin && cp $@.bin $@ || (rm -f $@.bin; exit 1); \
   else cp $< $@; fi""",
"""if test x$(USE_APPLE_CC_FIXES) = xyes; then \
434 435 436
  $(TARGET_STRIP) $(""" + cname() + """) -o $@.bin $<; \
  $(TARGET_OBJCONV) -f$(TARGET_MODULE_FORMAT) -nr:_grub_mod_init:grub_mod_init -nr:_grub_mod_fini:grub_mod_fini -ed2022 -wd1106 -nu -nd $@.bin $@; \
else """  + "$(TARGET_STRIP) $(" + cname() + "_STRIPFLAGS) -o $@ $<; \
437
fi"""))
438 439 440
    return r

def image(platform):
BVK Chaitanya's avatar
BVK Chaitanya committed
441
    r = set_canonical_name_suffix(".image")
442
    r += gvar_add("platform_PROGRAMS", "[+ name +].image")
BVK Chaitanya's avatar
BVK Chaitanya committed
443 444 445 446 447 448 449
    r += var_set(cname() + "_SOURCES", platform_sources(platform))
    r += var_set("nodist_" + cname() + "_SOURCES", platform_nodist_sources(platform) + "## platform nodist sources")
    r += var_set(cname() + "_LDADD", platform_ldadd(platform))
    r += var_set(cname() + "_CFLAGS", "$(AM_CFLAGS) $(CFLAGS_IMAGE) " + platform_cflags(platform))
    r += var_set(cname() + "_LDFLAGS", "$(AM_LDFLAGS) $(LDFLAGS_IMAGE) " + platform_ldflags(platform))
    r += var_set(cname() + "_CPPFLAGS", "$(AM_CPPFLAGS) $(CPPFLAGS_IMAGE) " + platform_cppflags(platform))
    r += var_set(cname() + "_CCASFLAGS", "$(AM_CCASFLAGS) $(CCASFLAGS_IMAGE) " + platform_ccasflags(platform))
BVK Chaitanya's avatar
BVK Chaitanya committed
450
    r += var_set(cname() + "_OBJCOPYFLAGS", "$(OBJCOPYFLAGS_IMAGE) " + platform_objcopyflags(platform))
BVK Chaitanya's avatar
BVK Chaitanya committed
451
    # r += var_set(cname() + "_DEPENDENCIES", platform_dependencies(platform) + " " + platform_ldadd(platform))
452

453
    r += gvar_add("dist_noinst_DATA", extra_dist())
BVK Chaitanya's avatar
BVK Chaitanya committed
454 455
    r += gvar_add("BUILT_SOURCES", "$(nodist_" + cname() + "_SOURCES)")
    r += gvar_add("CLEANFILES", "$(nodist_" + cname() + "_SOURCES)")
456 457

    r += gvar_add("platform_DATA", "[+ name +].img")
BVK Chaitanya's avatar
BVK Chaitanya committed
458
    r += gvar_add("CLEANFILES", "[+ name +].img")
BVK Chaitanya's avatar
BVK Chaitanya committed
459
    r += rule("[+ name +].img", "[+ name +].image$(EXEEXT)", """
460 461 462
if test x$(USE_APPLE_CC_FIXES) = xyes; then \
  $(MACHO2IMG) $< $@; \
else \
463
  $(TARGET_OBJCOPY) $(""" + cname() + """_OBJCOPYFLAGS) --strip-unneeded -R .note -R .comment -R .note.gnu.build-id -R .reginfo -R .rel.dyn -R .note.gnu.gold-version $< $@; \
464 465 466 467 468
fi
""")
    return r

def library(platform):
BVK Chaitanya's avatar
BVK Chaitanya committed
469
    r = set_canonical_name_suffix("")
470

Colin Watson's avatar
Colin Watson committed
471 472 473 474 475 476
    r += vars_init(cname() + "_SOURCES",
                   "nodist_" + cname() + "_SOURCES",
                   cname() + "_CFLAGS",
                   cname() + "_CPPFLAGS",
                   cname() + "_CCASFLAGS")
    #              cname() + "_DEPENDENCIES")
477

Colin Watson's avatar
Colin Watson committed
478 479 480 481 482 483 484 485
    r += first_time(gvar_add("noinst_LIBRARIES", "[+ name +]"))
    r += var_add(cname() + "_SOURCES", platform_sources(platform))
    r += var_add("nodist_" + cname() + "_SOURCES", platform_nodist_sources(platform))
    r += var_add(cname() + "_CFLAGS", first_time("$(AM_CFLAGS) $(CFLAGS_LIBRARY) ") + platform_cflags(platform))
    r += var_add(cname() + "_CPPFLAGS", first_time("$(AM_CPPFLAGS) $(CPPFLAGS_LIBRARY) ") + platform_cppflags(platform))
    r += var_add(cname() + "_CCASFLAGS", first_time("$(AM_CCASFLAGS) $(CCASFLAGS_LIBRARY) ") + platform_ccasflags(platform))
    # r += var_add(cname() + "_DEPENDENCIES", platform_dependencies(platform) + " " + platform_ldadd(platform))

486
    r += gvar_add("dist_noinst_DATA", extra_dist())
Colin Watson's avatar
Colin Watson committed
487 488
    r += first_time(gvar_add("BUILT_SOURCES", "$(nodist_" + cname() + "_SOURCES)"))
    r += first_time(gvar_add("CLEANFILES", "$(nodist_" + cname() + "_SOURCES)"))
489 490 491 492 493
    return r

def installdir(default="bin"):
    return "[+ IF installdir +][+ installdir +][+ ELSE +]" + default + "[+ ENDIF +]"

494
def manpage(adddeps):
495
    r  = "if COND_MAN_PAGES\n"
BVK Chaitanya's avatar
BVK Chaitanya committed
496
    r += gvar_add("man_MANS", "[+ name +].[+ mansection +]\n")
497
    r += rule("[+ name +].[+ mansection +]", "[+ name +] " + adddeps, """
498
chmod a+x [+ name +]
499
PATH=$(builddir):$$PATH pkgdatadir=$(builddir) $(HELP2MAN) --section=[+ mansection +] -i $(top_srcdir)/docs/man/[+ name +].h2m -o $@ [+ name +]
500 501 502 503 504 505
""")
    r += gvar_add("CLEANFILES", "[+ name +].[+ mansection +]")
    r += "endif\n"
    return r

def program(platform, test=False):
BVK Chaitanya's avatar
BVK Chaitanya committed
506 507 508 509 510 511
    r = set_canonical_name_suffix("")

    r += "[+ IF testcase defined +]"
    r += gvar_add("check_PROGRAMS", "[+ name +]")
    r += gvar_add("TESTS", "[+ name +]")
    r += "[+ ELSE +]"
Colin Watson's avatar
Colin Watson committed
512
    r += var_add(installdir() + "_PROGRAMS", "[+ name +]")
513
    r += "[+ IF mansection +]" + manpage("") + "[+ ENDIF +]"
BVK Chaitanya's avatar
BVK Chaitanya committed
514 515 516 517 518 519 520 521 522
    r += "[+ ENDIF +]"

    r += var_set(cname() + "_SOURCES", platform_sources(platform))
    r += var_set("nodist_" + cname() + "_SOURCES", platform_nodist_sources(platform))
    r += var_set(cname() + "_LDADD", platform_ldadd(platform))
    r += var_set(cname() + "_CFLAGS", "$(AM_CFLAGS) $(CFLAGS_PROGRAM) " + platform_cflags(platform))
    r += var_set(cname() + "_LDFLAGS", "$(AM_LDFLAGS) $(LDFLAGS_PROGRAM) " + platform_ldflags(platform))
    r += var_set(cname() + "_CPPFLAGS", "$(AM_CPPFLAGS) $(CPPFLAGS_PROGRAM) " + platform_cppflags(platform))
    r += var_set(cname() + "_CCASFLAGS", "$(AM_CCASFLAGS) $(CCASFLAGS_PROGRAM) " + platform_ccasflags(platform))
BVK Chaitanya's avatar
BVK Chaitanya committed
523
    # r += var_set(cname() + "_DEPENDENCIES", platform_dependencies(platform) + " " + platform_ldadd(platform))
524

525
    r += gvar_add("dist_noinst_DATA", extra_dist())
BVK Chaitanya's avatar
BVK Chaitanya committed
526 527
    r += gvar_add("BUILT_SOURCES", "$(nodist_" + cname() + "_SOURCES)")
    r += gvar_add("CLEANFILES", "$(nodist_" + cname() + "_SOURCES)")
528 529 530
    return r

def data(platform):
531 532
    r  = var_add("dist_" + installdir() + "_DATA", platform_sources(platform))
    r += gvar_add("dist_noinst_DATA", extra_dist())
BVK Chaitanya's avatar
BVK Chaitanya committed
533
    return r
534

BVK Chaitanya's avatar
BVK Chaitanya committed
535 536 537 538 539
def script(platform):
    r  = "[+ IF testcase defined +]"
    r += gvar_add("check_SCRIPTS", "[+ name +]")
    r += gvar_add ("TESTS", "[+ name +]")
    r += "[+ ELSE +]"
Colin Watson's avatar
Colin Watson committed
540
    r += var_add(installdir() + "_SCRIPTS", "[+ name +]")
541
    r += "[+ IF mansection +]" + manpage("grub-mkconfig_lib") + "[+ ENDIF +]"
BVK Chaitanya's avatar
BVK Chaitanya committed
542
    r += "[+ ENDIF +]"
543

544 545
    r += rule("[+ name +]", "$(top_builddir)/config.status " + platform_sources(platform), """
(skip=1; for x in $^; do if [ $$skip = 1 ]; then skip=0; else cat "$$x"; fi; done) | $(top_builddir)/config.status --file=$@:-
546 547 548 549
chmod a+x [+ name +]
""")

    r += gvar_add("CLEANFILES", "[+ name +]")
550
    r += gvar_add("EXTRA_DIST", extra_dist())
551
    r += gvar_add("dist_noinst_DATA", platform_sources(platform))
552 553
    return r

Colin Watson's avatar
Colin Watson committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
def rules(target, closure):
    # Create association lists for the benefit of first_time and vars_init.
    r = "[+ (define seen-target '()) +]"
    r += "[+ (define seen-vars '()) +]"
    # Most output goes to a diversion.  This allows us to emit variable
    # initializations before everything else.
    r += "[+ (out-push-new) +]"

    r += "[+ FOR " + target + " +]"
    r += foreach_enabled_platform(
        lambda p: under_platform_specific_conditionals(p, closure(p)))
    # Remember that we've seen this target.
    r += "[+ (set! seen-target (assoc-set! seen-target (get \".name\") 0)) +]"
    r += "[+ ENDFOR +]"
    r += "[+ (out-pop #t) +]"
    return r

571
def module_rules():
Colin Watson's avatar
Colin Watson committed
572
    return rules("module", module)
573 574

def kernel_rules():
Colin Watson's avatar
Colin Watson committed
575
    return rules("kernel", kernel)
576 577

def image_rules():
Colin Watson's avatar
Colin Watson committed
578
    return rules("image", image)
579 580

def library_rules():
Colin Watson's avatar
Colin Watson committed
581
    return rules("library", library)
582 583

def program_rules():
Colin Watson's avatar
Colin Watson committed
584
    return rules("program", program)
585 586

def script_rules():
Colin Watson's avatar
Colin Watson committed
587
    return rules("script", script)
588 589

def data_rules():
Colin Watson's avatar
Colin Watson committed
590
    return rules("data", data)
591 592 593 594 595 596 597 598 599 600

a = module_rules()
b = kernel_rules()
c = image_rules()
d = library_rules()
e = program_rules()
f = script_rules()
g = data_rules()
z = global_variable_initializers()

601
print ("[+ AutoGen5 template +]\n")
602
for p in GRUB_PLATFORMS:
603 604
    print (define_macro_for_platform_sources(p))
    print (define_macro_for_platform_nodist_sources(p))
605

606 607 608
    print (define_macro_for_platform_startup(p))
    print (define_macro_for_platform_cflags(p))
    print (define_macro_for_platform_ldadd(p))
609
    print (define_macro_for_platform_dependencies(p))
610 611 612 613 614 615 616 617
    print (define_macro_for_platform_ldflags(p))
    print (define_macro_for_platform_cppflags(p))
    print (define_macro_for_platform_ccasflags(p))
    print (define_macro_for_platform_stripflags(p))
    print (define_macro_for_platform_objcopyflags(p))

    print (define_macro_for_platform_conditionals_if_statement(p))
    print (define_macro_for_platform_conditionals_endif_statement(p))
BVK Chaitanya's avatar
BVK Chaitanya committed
618
# print z # initializer for all vars
619 620 621 622 623 624 625
print (a)
print (b)
print (c)
print (d)
print (e)
print (f)
print (g)