2 # The engine for building files
4 # Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 import Common
.LongFilePathOs
as os
21 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
23 from Common
.GlobalData
import *
24 from Common
.BuildToolError
import *
25 from Common
.Misc
import tdict
, PathClass
26 from Common
.String
import NormPath
27 from Common
.DataType
import *
29 import Common
.EdkLogger
as EdkLogger
31 ## Convert file type to file list macro name
33 # @param FileType The name of file type
35 # @retval string The name of macro
37 def FileListMacro(FileType
):
38 return "%sS" % FileType
.replace("-", "_").upper()
40 ## Convert file type to list file macro name
42 # @param FileType The name of file type
44 # @retval string The name of macro
46 def ListFileMacro(FileType
):
47 return "%s_LIST" % FileListMacro(FileType
)
49 class TargetDescBlock(object):
50 _Cache_
= {} # {TargetFile : TargetDescBlock object}
53 def __new__(Class
, Inputs
, Outputs
, Commands
, Dependencies
):
54 if Outputs
[0] in Class
._Cache
_:
55 Tdb
= Class
._Cache
_[Outputs
[0]]
59 Tdb
= super(TargetDescBlock
, Class
).__new
__(Class
)
60 Tdb
._Init
(Inputs
, Outputs
, Commands
, Dependencies
)
61 #Class._Cache_[Outputs[0]] = Tdb
64 def _Init(self
, Inputs
, Outputs
, Commands
, Dependencies
):
66 self
.Outputs
= Outputs
67 self
.Commands
= Commands
68 self
.Dependencies
= Dependencies
70 self
.Target
= self
.Outputs
[0]
75 return self
.Target
.Path
78 return hash(self
.Target
.Path
)
80 def __eq__(self
, Other
):
81 if type(Other
) == type(self
):
82 return Other
.Target
.Path
== self
.Target
.Path
84 return str(Other
) == self
.Target
.Path
86 def AddInput(self
, Input
):
87 if Input
not in self
.Inputs
:
88 self
.Inputs
.append(Input
)
90 def IsMultipleInput(self
):
91 return len(self
.Inputs
) > 1
95 TargetDescBlock
._Cache
_ = {}
97 ## Class for one build rule
99 # This represents a build rule which can give out corresponding command list for
100 # building the given source file(s). The result can be used for generating the
101 # target for makefile.
104 INC_LIST_MACRO
= "INC_LIST"
109 # @param Input The dictionary represeting input file(s) for a rule
110 # @param Output The list represeting output file(s) for a rule
111 # @param Command The list containing commands to generate the output from input
113 def __init__(self
, Type
, Input
, Output
, Command
, ExtraDependency
=None):
114 # The Input should not be empty
122 self
.FileListMacro
= FileListMacro(Type
)
123 self
.ListFileMacro
= ListFileMacro(Type
)
124 self
.IncListFileMacro
= self
.INC_LIST_MACRO
126 self
.SourceFileType
= Type
127 # source files listed not in "*" or "?" pattern format
128 if not ExtraDependency
:
129 self
.ExtraSourceFileList
= []
131 self
.ExtraSourceFileList
= ExtraDependency
134 # Search macros used in command lines for <FILE_TYPE>_LIST and INC_LIST.
135 # If found, generate a file to keep the input files used to get over the
136 # limitation of command line length
139 self
.CommandList
= []
140 for CmdLine
in Command
:
141 self
.MacroList
.extend(gMacroRefPattern
.findall(CmdLine
))
142 # replace path separator with native one
143 self
.CommandList
.append(CmdLine
)
145 # Indicate what should be generated
146 if self
.FileListMacro
in self
.MacroList
:
147 self
.GenFileListMacro
= True
149 self
.GenFileListMacro
= False
151 if self
.ListFileMacro
in self
.MacroList
:
152 self
.GenListFile
= True
153 self
.GenFileListMacro
= True
155 self
.GenListFile
= False
157 if self
.INC_LIST_MACRO
in self
.MacroList
:
158 self
.GenIncListFile
= True
160 self
.GenIncListFile
= False
163 self
.IsMultipleInput
= False
164 self
.SourceFileExtList
= set()
166 Base
, Ext
= os
.path
.splitext(File
)
167 if Base
.find("*") >= 0:
168 # There's "*" in the file name
169 self
.IsMultipleInput
= True
170 self
.GenFileListMacro
= True
171 elif Base
.find("?") < 0:
172 # There's no "*" and "?" in file name
173 self
.ExtraSourceFileList
.append(File
)
175 self
.SourceFileExtList
.add(Ext
)
178 self
.DestFileList
= []
180 self
.DestFileList
.append(File
)
182 # All build targets generated by this rule for a module
183 self
.BuildTargets
= {}
185 ## str() function support
191 SourceString
+= " %s %s %s" % (self
.SourceFileType
, " ".join(self
.SourceFileExtList
), self
.ExtraSourceFileList
)
192 DestString
= ", ".join(self
.DestFileList
)
193 CommandString
= "\n\t".join(self
.CommandList
)
194 return "%s : %s\n\t%s" % (DestString
, SourceString
, CommandString
)
196 def Instantiate(self
, Macros
={}):
197 NewRuleObject
= copy
.copy(self
)
198 NewRuleObject
.BuildTargets
= {}
199 NewRuleObject
.DestFileList
= []
200 for File
in self
.DestFileList
:
201 NewRuleObject
.DestFileList
.append(PathClass(NormPath(File
, Macros
)))
204 ## Apply the rule to given source file(s)
206 # @param SourceFile One file or a list of files to be built
207 # @param RelativeToDir The relative path of the source file
208 # @param PathSeparator Path separator
210 # @retval tuple (Source file in full path, List of individual sourcefiles, Destionation file, List of build commands)
212 def Apply(self
, SourceFile
, BuildRuleOrder
=None):
213 if not self
.CommandList
or not self
.DestFileList
:
217 if self
.IsMultipleInput
:
223 # SourceFile must be a list
224 SrcFile
= "$(%s)" % self
.FileListMacro
226 SrcFileName
, SrcFileBase
, SrcFileExt
= SourceFile
.Name
, SourceFile
.BaseName
, SourceFile
.Ext
228 SrcFileDir
= SourceFile
.SubDir
233 SrcFile
= SourceFile
.Path
234 SrcPath
= SourceFile
.Dir
236 # destination file (the first one)
237 if self
.DestFileList
:
238 DestFile
= self
.DestFileList
[0].Path
239 DestPath
= self
.DestFileList
[0].Dir
240 DestFileName
= self
.DestFileList
[0].Name
241 DestFileBase
, DestFileExt
= self
.DestFileList
[0].BaseName
, self
.DestFileList
[0].Ext
249 BuildRulePlaceholderDict
= {
253 "s_dir" : SrcFileDir
,
254 "s_name" : SrcFileName
,
255 "s_base" : SrcFileBase
,
256 "s_ext" : SrcFileExt
,
260 "d_name" : DestFileName
,
261 "d_base" : DestFileBase
,
262 "d_ext" : DestFileExt
,
266 for File
in self
.DestFileList
:
267 File
= string
.Template(str(File
)).safe_substitute(BuildRulePlaceholderDict
)
268 File
= string
.Template(str(File
)).safe_substitute(BuildRulePlaceholderDict
)
269 DstFile
.append(PathClass(File
, IsBinary
=True))
271 if DstFile
[0] in self
.BuildTargets
:
272 TargetDesc
= self
.BuildTargets
[DstFile
[0]]
273 if BuildRuleOrder
and SourceFile
.Ext
in BuildRuleOrder
:
274 Index
= BuildRuleOrder
.index(SourceFile
.Ext
)
275 for Input
in TargetDesc
.Inputs
:
276 if Input
.Ext
not in BuildRuleOrder
or BuildRuleOrder
.index(Input
.Ext
) > Index
:
278 # Command line should be regenerated since some macros are different
280 CommandList
= self
._BuildCommand
(BuildRulePlaceholderDict
)
281 TargetDesc
._Init
([SourceFile
], DstFile
, CommandList
, self
.ExtraSourceFileList
)
284 TargetDesc
.AddInput(SourceFile
)
286 CommandList
= self
._BuildCommand
(BuildRulePlaceholderDict
)
287 TargetDesc
= TargetDescBlock([SourceFile
], DstFile
, CommandList
, self
.ExtraSourceFileList
)
288 TargetDesc
.ListFileMacro
= self
.ListFileMacro
289 TargetDesc
.FileListMacro
= self
.FileListMacro
290 TargetDesc
.IncListFileMacro
= self
.IncListFileMacro
291 TargetDesc
.GenFileListMacro
= self
.GenFileListMacro
292 TargetDesc
.GenListFile
= self
.GenListFile
293 TargetDesc
.GenIncListFile
= self
.GenIncListFile
294 self
.BuildTargets
[DstFile
[0]] = TargetDesc
297 def _BuildCommand(self
, Macros
):
299 for CommandString
in self
.CommandList
:
300 CommandString
= string
.Template(CommandString
).safe_substitute(Macros
)
301 CommandString
= string
.Template(CommandString
).safe_substitute(Macros
)
302 CommandList
.append(CommandString
)
305 ## Class for build rules
307 # BuildRule class parses rules defined in a file or passed by caller, and converts
308 # the rule into FileBuildRule object.
311 _SectionHeader
= "SECTIONHEADER"
313 _SubSectionHeader
= "SUBSECTIONHEADER"
314 _SubSection
= "SUBSECTION"
315 _InputFile
= "INPUTFILE"
316 _OutputFile
= "OUTPUTFILE"
317 _ExtraDependency
= "EXTRADEPENDENCY"
319 _UnknownSection
= "UNKNOWNSECTION"
321 _SubSectionList
= [_InputFile
, _OutputFile
, _Command
]
324 _FileTypePattern
= re
.compile("^[_a-zA-Z][_\-0-9a-zA-Z]*$")
325 _BinaryFileRule
= FileBuildRule(TAB_DEFAULT_BINARY_FILE
, [], [os
.path
.join("$(OUTPUT_DIR)", "${s_name}")],
326 ["$(CP) ${src} ${dst}"], [])
330 # @param File The file containing build rules in a well defined format
331 # @param Content The string list of build rules in a well defined format
332 # @param LineIndex The line number from which the parsing will begin
333 # @param SupportedFamily The list of supported tool chain families
335 def __init__(self
, File
=None, Content
=None, LineIndex
=0, SupportedFamily
=["MSFT", "INTEL", "GCC", "RVCT"]):
337 # Read build rules from file if it's not none
340 self
.RuleContent
= open(File
, 'r').readlines()
342 EdkLogger
.error("build", FILE_OPEN_FAILURE
, ExtraData
=File
)
343 elif Content
is not None:
344 self
.RuleContent
= Content
346 EdkLogger
.error("build", PARAMETER_MISSING
, ExtraData
="No rule file or string given")
348 self
.SupportedToolChainFamilyList
= SupportedFamily
349 self
.RuleDatabase
= tdict(True, 4) # {FileExt, ModuleType, Arch, Family : FileBuildRule object}
350 self
.Ext2FileType
= {} # {ext : file-type}
351 self
.FileTypeList
= set()
353 self
._LineIndex
= LineIndex
355 self
._RuleInfo
= tdict(True, 2) # {toolchain family : {"InputFile": {}, "OutputFile" : [], "Command" : []}}
357 self
._BuildTypeList
= set()
358 self
._ArchList
= set()
359 self
._FamilyList
= []
360 self
._TotalToolChainFamilySet
= set()
361 self
._RuleObjectList
= [] # FileBuildRule object list
362 self
._FileVersion
= ""
366 # some intrinsic rules
367 self
.RuleDatabase
[TAB_DEFAULT_BINARY_FILE
, "COMMON", "COMMON", "COMMON"] = self
._BinaryFileRule
368 self
.FileTypeList
.add(TAB_DEFAULT_BINARY_FILE
)
370 ## Parse the build rule strings
372 self
._State
= self
._Section
373 for Index
in range(self
._LineIndex
, len(self
.RuleContent
)):
374 # Clean up the line and replace path separator with native one
375 Line
= self
.RuleContent
[Index
].strip().replace(self
._PATH
_SEP
, os
.path
.sep
)
376 self
.RuleContent
[Index
] = Line
378 # find the build_rule_version
379 if Line
and Line
[0] == "#" and Line
.find(TAB_BUILD_RULE_VERSION
) <> -1:
380 if Line
.find("=") <> -1 and Line
.find("=") < (len(Line
) - 1) and (Line
[(Line
.find("=") + 1):]).split():
381 self
._FileVersion
= (Line
[(Line
.find("=") + 1):]).split()[0]
382 # skip empty or comment line
383 if Line
== "" or Line
[0] == "#":
386 # find out section header, enclosed by []
387 if Line
[0] == '[' and Line
[-1] == ']':
388 # merge last section information into rule database
390 self
._State
= self
._SectionHeader
391 # find out sub-section header, enclosed by <>
392 elif Line
[0] == '<' and Line
[-1] == '>':
393 if self
._State
!= self
._UnknownSection
:
394 self
._State
= self
._SubSectionHeader
396 # call section handler to parse each (sub)section
397 self
._StateHandler
[self
._State
](self
, Index
)
398 # merge last section information into rule database
401 ## Parse definitions under a section
403 # @param LineIndex The line index of build rule text
405 def ParseSection(self
, LineIndex
):
408 ## Parse definitions under a subsection
410 # @param LineIndex The line index of build rule text
412 def ParseSubSection(self
, LineIndex
):
413 # currenly nothing here
416 ## Placeholder for not supported sections
418 # @param LineIndex The line index of build rule text
420 def SkipSection(self
, LineIndex
):
423 ## Merge section information just got into rule database
424 def EndOfSection(self
):
425 Database
= self
.RuleDatabase
426 # if there's specific toochain family, 'COMMON' doesn't make sense any more
427 if len(self
._TotalToolChainFamilySet
) > 1 and 'COMMON' in self
._TotalToolChainFamilySet
:
428 self
._TotalToolChainFamilySet
.remove('COMMON')
429 for Family
in self
._TotalToolChainFamilySet
:
430 Input
= self
._RuleInfo
[Family
, self
._InputFile
]
431 Output
= self
._RuleInfo
[Family
, self
._OutputFile
]
432 Command
= self
._RuleInfo
[Family
, self
._Command
]
433 ExtraDependency
= self
._RuleInfo
[Family
, self
._ExtraDependency
]
435 BuildRule
= FileBuildRule(self
._FileType
, Input
, Output
, Command
, ExtraDependency
)
436 for BuildType
in self
._BuildTypeList
:
437 for Arch
in self
._ArchList
:
438 Database
[self
._FileType
, BuildType
, Arch
, Family
] = BuildRule
439 for FileExt
in BuildRule
.SourceFileExtList
:
440 self
.Ext2FileType
[FileExt
] = self
._FileType
442 ## Parse section header
444 # @param LineIndex The line index of build rule text
446 def ParseSectionHeader(self
, LineIndex
):
447 self
._RuleInfo
= tdict(True, 2)
448 self
._BuildTypeList
= set()
449 self
._ArchList
= set()
450 self
._FamilyList
= []
451 self
._TotalToolChainFamilySet
= set()
453 RuleNameList
= self
.RuleContent
[LineIndex
][1:-1].split(',')
454 for RuleName
in RuleNameList
:
457 TokenList
= [Token
.strip().upper() for Token
in RuleName
.split('.')]
458 # old format: Build.File-Type
459 if TokenList
[0] == "BUILD":
460 if len(TokenList
) == 1:
461 EdkLogger
.error("build", FORMAT_INVALID
, "Invalid rule section",
462 File
=self
.RuleFile
, Line
=LineIndex
+ 1,
463 ExtraData
=self
.RuleContent
[LineIndex
])
465 FileType
= TokenList
[1]
467 EdkLogger
.error("build", FORMAT_INVALID
, "No file type given",
468 File
=self
.RuleFile
, Line
=LineIndex
+ 1,
469 ExtraData
=self
.RuleContent
[LineIndex
])
470 if self
._FileTypePattern
.match(FileType
) is None:
471 EdkLogger
.error("build", FORMAT_INVALID
, File
=self
.RuleFile
, Line
=LineIndex
+ 1,
472 ExtraData
="Only character, number (non-first character), '_' and '-' are allowed in file type")
473 # new format: File-Type.Build-Type.Arch
476 FileType
= TokenList
[0]
477 elif FileType
!= TokenList
[0]:
478 EdkLogger
.error("build", FORMAT_INVALID
,
479 "Different file types are not allowed in the same rule section",
480 File
=self
.RuleFile
, Line
=LineIndex
+ 1,
481 ExtraData
=self
.RuleContent
[LineIndex
])
482 if len(TokenList
) > 1:
483 BuildType
= TokenList
[1]
484 if len(TokenList
) > 2:
486 self
._BuildTypeList
.add(BuildType
)
487 self
._ArchList
.add(Arch
)
489 if 'COMMON' in self
._BuildTypeList
and len(self
._BuildTypeList
) > 1:
490 EdkLogger
.error("build", FORMAT_INVALID
,
491 "Specific build types must not be mixed with common one",
492 File
=self
.RuleFile
, Line
=LineIndex
+ 1,
493 ExtraData
=self
.RuleContent
[LineIndex
])
494 if 'COMMON' in self
._ArchList
and len(self
._ArchList
) > 1:
495 EdkLogger
.error("build", FORMAT_INVALID
,
496 "Specific ARCH must not be mixed with common one",
497 File
=self
.RuleFile
, Line
=LineIndex
+ 1,
498 ExtraData
=self
.RuleContent
[LineIndex
])
500 self
._FileType
= FileType
501 self
._State
= self
._Section
502 self
.FileTypeList
.add(FileType
)
504 ## Parse sub-section header
506 # @param LineIndex The line index of build rule text
508 def ParseSubSectionHeader(self
, LineIndex
):
510 List
= self
.RuleContent
[LineIndex
][1:-1].split(',')
513 TokenList
= Section
.split('.')
514 Type
= TokenList
[0].strip().upper()
516 if SectionType
== "":
518 elif SectionType
!= Type
:
519 EdkLogger
.error("build", FORMAT_INVALID
,
520 "Two different section types are not allowed in the same sub-section",
521 File
=self
.RuleFile
, Line
=LineIndex
+ 1,
522 ExtraData
=self
.RuleContent
[LineIndex
])
524 if len(TokenList
) > 1:
525 Family
= TokenList
[1].strip().upper()
529 if Family
not in FamilyList
:
530 FamilyList
.append(Family
)
532 self
._FamilyList
= FamilyList
533 self
._TotalToolChainFamilySet
.update(FamilyList
)
534 self
._State
= SectionType
.upper()
535 if 'COMMON' in FamilyList
and len(FamilyList
) > 1:
536 EdkLogger
.error("build", FORMAT_INVALID
,
537 "Specific tool chain family should not be mixed with general one",
538 File
=self
.RuleFile
, Line
=LineIndex
+ 1,
539 ExtraData
=self
.RuleContent
[LineIndex
])
540 if self
._State
not in self
._StateHandler
:
541 EdkLogger
.error("build", FORMAT_INVALID
, File
=self
.RuleFile
, Line
=LineIndex
+ 1,
542 ExtraData
="Unknown subsection: %s" % self
.RuleContent
[LineIndex
])
543 ## Parse <InputFile> sub-section
545 # @param LineIndex The line index of build rule text
547 def ParseInputFile(self
, LineIndex
):
548 FileList
= [File
.strip() for File
in self
.RuleContent
[LineIndex
].split(",")]
549 for ToolChainFamily
in self
._FamilyList
:
550 InputFiles
= self
._RuleInfo
[ToolChainFamily
, self
._State
]
551 if InputFiles
is None:
553 self
._RuleInfo
[ToolChainFamily
, self
._State
] = InputFiles
554 InputFiles
.extend(FileList
)
556 ## Parse <ExtraDependency> sub-section
558 # @param LineIndex The line index of build rule text
560 def ParseCommon(self
, LineIndex
):
561 for ToolChainFamily
in self
._FamilyList
:
562 Items
= self
._RuleInfo
[ToolChainFamily
, self
._State
]
565 self
._RuleInfo
[ToolChainFamily
, self
._State
] = Items
566 Items
.append(self
.RuleContent
[LineIndex
])
568 ## Get a build rule via [] operator
570 # @param FileExt The extension of a file
571 # @param ToolChainFamily The tool chain family name
572 # @param BuildVersion The build version number. "*" means any rule
575 # @retval FileType The file type string
576 # @retval FileBuildRule The object of FileBuildRule
578 # Key = (FileExt, ModuleType, Arch, ToolChainFamily)
579 def __getitem__(self
, Key
):
583 if Key
[0] in self
.Ext2FileType
:
584 Type
= self
.Ext2FileType
[Key
[0]]
585 elif Key
[0].upper() in self
.FileTypeList
:
586 Type
= Key
[0].upper()
591 Key
= (Type
,) + Key
[1:]
594 return self
.RuleDatabase
[Key
]
597 _SectionHeader
: ParseSectionHeader
,
598 _Section
: ParseSection
,
599 _SubSectionHeader
: ParseSubSectionHeader
,
600 _SubSection
: ParseSubSection
,
601 _InputFile
: ParseInputFile
,
602 _OutputFile
: ParseCommon
,
603 _ExtraDependency
: ParseCommon
,
604 _Command
: ParseCommon
,
605 _UnknownSection
: SkipSection
,
608 # This acts like the main() function for the script, unless it is 'import'ed into another
610 if __name__
== '__main__':
612 EdkLogger
.Initialize()
613 if len(sys
.argv
) > 1:
614 Br
= BuildRule(sys
.argv
[1])
615 print str(Br
[".c", "DXE_DRIVER", "IA32", "MSFT"][1])
617 print str(Br
[".c", "DXE_DRIVER", "IA32", "INTEL"][1])
619 print str(Br
[".c", "DXE_DRIVER", "IA32", "GCC"][1])
621 print str(Br
[".ac", "ACPI_TABLE", "IA32", "MSFT"][1])
623 print str(Br
[".h", "ACPI_TABLE", "IA32", "INTEL"][1])
625 print str(Br
[".ac", "ACPI_TABLE", "IA32", "MSFT"][1])
627 print str(Br
[".s", "SEC", "IPF", "COMMON"][1])
629 print str(Br
[".s", "SEC"][1])