Give better error messages for non-existent files.
[mirror_edk2.git] / Tools / Python / GenMake.py
CommitLineData
0026dbe0 1#!/usr/bin/env python
2
07253e8e 3"""Create GNU Makefiles for the Libraries of the MdePkg."""
0026dbe0 4
5import os, sys, getopt, string, xml.dom.minidom, shutil
6from XmlRoutines import *
7from WorkspaceRoutines import *
8
07253e8e 9copyingSources = 1
0026dbe0 10
07253e8e 11Makefile = string.Template("""ARCH = $ARCH
0026dbe0 12
07253e8e 13MAKEROOT ?= ../..
0026dbe0 14
07253e8e 15VPATH = ..
0026dbe0 16
07253e8e 17LIBNAME = $LIBNAME
0026dbe0 18
07253e8e 19OBJECTS = $OBJECTS
20
21include $$(MAKEROOT)/lib.makefile
22""")
23
24def mkdir(path):
25 """Make a directory if it is not there already."""
26
27 try:
28 os.makedirs(path)
29 except:
30 pass
31
b12dbde3 32def openSpd(spdFile, arch):
0026dbe0 33
b12dbde3 34 """Open the spdFile and process the msa files it contains."""
0026dbe0 35
b12dbde3 36 db = xml.dom.minidom.parse(inWorkspace(spdFile))
0026dbe0 37
38 for msaFile in XmlList(db, "/PackageSurfaceArea/MsaFiles/Filename"):
39 msaFileName = XmlElementData(msaFile)
07253e8e 40 doLib(msaFileName, arch)
0026dbe0 41
42 return db
43
44def inMde(f):
45 """Make a path relative to the Mde Pkg root dir."""
46 return inWorkspace(os.path.join("MdePkg", f))
47
07253e8e 48def doLib(msafile, arch):
0026dbe0 49
50 """Create a directory with the sources, AutoGen.h and a makefile."""
51
b23e12fa 52 objects = []
0026dbe0 53
54 msa = xml.dom.minidom.parse(inMde(msafile))
55 libName = str(XmlElement(msa, "/ModuleSurfaceArea/MsaHeader/ModuleName"))
56 base, _ = os.path.splitext(msafile)
57 msabase = os.path.basename(base)
58
59 suppArch = str(XmlElement(msa, "/ModuleSurfaceArea/ModuleDefinitions/SupportedArchitectures"))
07253e8e 60 if not arch in string.split(suppArch, " "):
0026dbe0 61 return
62
07253e8e 63 mkdir(libName);
64
65 buildDir = os.path.join(libName, "build-%s" % arch )
66 mkdir(buildDir)
0026dbe0 67
07253e8e 68 for sourceFile in XmlList(msa, "/ModuleSurfaceArea/SourceFiles/Filename"):
0026dbe0 69
07253e8e 70 sourceFileName = str(XmlElementData(sourceFile))
71 suppArchs = sourceFile.getAttribute("SupArchList").split(" ")
72 toolchain = sourceFile.getAttribute("ToolChainFamily")
73 base, ext = os.path.splitext(sourceFileName)
0026dbe0 74
b23e12fa 75 if ( suppArchs == [""] or arch in suppArchs) and toolchain in ["", "GCC"] and ext in [".c", ".h", ".S"]:
0026dbe0 76 if ext in [".c", ".S"]:
b23e12fa 77 obj = str(base+".o")
78 if obj in objects:
b12dbde3 79 print "Error: The msa file %s is ambiguous. There are mutliple sources that can produce the object file %s. Please fix it." % (msafile, obj)
b23e12fa 80 sys.exit()
81 else:
82 objects.append(obj)
07253e8e 83 sourceDir = os.path.join(libName, os.path.dirname(sourceFileName))
84 mkdir(sourceDir)
85 mkdir(os.path.join(buildDir, os.path.dirname(sourceFileName)))
86 if copyingSources :
87 shutil.copy(inMde(os.path.join(os.path.dirname(msafile), sourceFileName)),
88 sourceDir)
0026dbe0 89
90 # Write a Makefile for this module
07253e8e 91 f = open(os.path.join(buildDir, "Makefile"), "w")
b23e12fa 92 f.write(Makefile.substitute(ARCH=arch, LIBNAME=libName, OBJECTS=string.join(objects, " ")))
0026dbe0 93 f.close()
94
95 # Right now we are getting the AutoGen.h file from a previous build. We
96 # could create it from scratch also.
07253e8e 97 shutil.copy(inWorkspace("Build/Mde/DEBUG_UNIXGCC/%s/MdePkg/Library/%s/%s/DEBUG/AutoGen.h") % (arch, libName, msabase), buildDir)
0026dbe0 98
99# This acts like the main() function for the script, unless it is 'import'ed
100# into another script.
101if __name__ == '__main__':
102
07253e8e 103 for arch in ["IA32", "X64"]:
b12dbde3 104 openSpd("MdePkg/MdePkg.spd", arch);