]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/AutoGen/GenMake.py
BaseTools: fix the open file's read and write bugs
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / GenMake.py
1 ## @file
2 # Create makefile for MS nmake and GNU make
3 #
4 # Copyright (c) 2007 - 2018, 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
9 #
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.
12 #
13
14 ## Import Modules
15 #
16 import Common.LongFilePathOs as os
17 import sys
18 import string
19 import re
20 import os.path as path
21 from Common.LongFilePathSupport import OpenLongFilePath as open
22 from Common.MultipleWorkspace import MultipleWorkspace as mws
23 from Common.BuildToolError import *
24 from Common.Misc import *
25 from Common.StringUtils import *
26 from .BuildEngine import *
27 import Common.GlobalData as GlobalData
28 from collections import OrderedDict
29 from Common.DataType import TAB_COMPILER_MSFT
30
31 ## Regular expression for finding header file inclusions
32 gIncludePattern = re.compile(r"^[ \t]*#?[ \t]*include(?:[ \t]*(?:\\(?:\r\n|\r|\n))*[ \t]*)*(?:\(?[\"<]?[ \t]*)([-\w.\\/() \t]+)(?:[ \t]*[\">]?\)?)", re.MULTILINE | re.UNICODE | re.IGNORECASE)
33
34 ## Regular expression for matching macro used in header file inclusion
35 gMacroPattern = re.compile("([_A-Z][_A-Z0-9]*)[ \t]*\((.+)\)", re.UNICODE)
36
37 gIsFileMap = {}
38
39 ## pattern for include style in Edk.x code
40 gProtocolDefinition = "Protocol/%(HeaderKey)s/%(HeaderKey)s.h"
41 gGuidDefinition = "Guid/%(HeaderKey)s/%(HeaderKey)s.h"
42 gArchProtocolDefinition = "ArchProtocol/%(HeaderKey)s/%(HeaderKey)s.h"
43 gPpiDefinition = "Ppi/%(HeaderKey)s/%(HeaderKey)s.h"
44 gIncludeMacroConversion = {
45 "EFI_PROTOCOL_DEFINITION" : gProtocolDefinition,
46 "EFI_GUID_DEFINITION" : gGuidDefinition,
47 "EFI_ARCH_PROTOCOL_DEFINITION" : gArchProtocolDefinition,
48 "EFI_PROTOCOL_PRODUCER" : gProtocolDefinition,
49 "EFI_PROTOCOL_CONSUMER" : gProtocolDefinition,
50 "EFI_PROTOCOL_DEPENDENCY" : gProtocolDefinition,
51 "EFI_ARCH_PROTOCOL_PRODUCER" : gArchProtocolDefinition,
52 "EFI_ARCH_PROTOCOL_CONSUMER" : gArchProtocolDefinition,
53 "EFI_ARCH_PROTOCOL_DEPENDENCY" : gArchProtocolDefinition,
54 "EFI_PPI_DEFINITION" : gPpiDefinition,
55 "EFI_PPI_PRODUCER" : gPpiDefinition,
56 "EFI_PPI_CONSUMER" : gPpiDefinition,
57 "EFI_PPI_DEPENDENCY" : gPpiDefinition,
58 }
59
60 ## default makefile type
61 gMakeType = ""
62 if sys.platform == "win32":
63 gMakeType = "nmake"
64 else:
65 gMakeType = "gmake"
66
67
68 ## BuildFile class
69 #
70 # This base class encapsules build file and its generation. It uses template to generate
71 # the content of build file. The content of build file will be got from AutoGen objects.
72 #
73 class BuildFile(object):
74 ## template used to generate the build file (i.e. makefile if using make)
75 _TEMPLATE_ = TemplateString('')
76
77 _DEFAULT_FILE_NAME_ = "Makefile"
78
79 ## default file name for each type of build file
80 _FILE_NAME_ = {
81 "nmake" : "Makefile",
82 "gmake" : "GNUmakefile"
83 }
84
85 ## Fixed header string for makefile
86 _MAKEFILE_HEADER = '''#
87 # DO NOT EDIT
88 # This file is auto-generated by build utility
89 #
90 # Module Name:
91 #
92 # %s
93 #
94 # Abstract:
95 #
96 # Auto-generated makefile for building modules, libraries or platform
97 #
98 '''
99
100 ## Header string for each type of build file
101 _FILE_HEADER_ = {
102 "nmake" : _MAKEFILE_HEADER % _FILE_NAME_["nmake"],
103 "gmake" : _MAKEFILE_HEADER % _FILE_NAME_["gmake"]
104 }
105
106 ## shell commands which can be used in build file in the form of macro
107 # $(CP) copy file command
108 # $(MV) move file command
109 # $(RM) remove file command
110 # $(MD) create dir command
111 # $(RD) remove dir command
112 #
113 _SHELL_CMD_ = {
114 "nmake" : {
115 "CP" : "copy /y",
116 "MV" : "move /y",
117 "RM" : "del /f /q",
118 "MD" : "mkdir",
119 "RD" : "rmdir /s /q",
120 },
121
122 "gmake" : {
123 "CP" : "cp -f",
124 "MV" : "mv -f",
125 "RM" : "rm -f",
126 "MD" : "mkdir -p",
127 "RD" : "rm -r -f",
128 }
129 }
130
131 ## directory separator
132 _SEP_ = {
133 "nmake" : "\\",
134 "gmake" : "/"
135 }
136
137 ## directory creation template
138 _MD_TEMPLATE_ = {
139 "nmake" : 'if not exist %(dir)s $(MD) %(dir)s',
140 "gmake" : "$(MD) %(dir)s"
141 }
142
143 ## directory removal template
144 _RD_TEMPLATE_ = {
145 "nmake" : 'if exist %(dir)s $(RD) %(dir)s',
146 "gmake" : "$(RD) %(dir)s"
147 }
148 ## cp if exist
149 _CP_TEMPLATE_ = {
150 "nmake" : 'if exist %(Src)s $(CP) %(Src)s %(Dst)s',
151 "gmake" : "test -f %(Src)s && $(CP) %(Src)s %(Dst)s"
152 }
153
154 _CD_TEMPLATE_ = {
155 "nmake" : 'if exist %(dir)s cd %(dir)s',
156 "gmake" : "test -e %(dir)s && cd %(dir)s"
157 }
158
159 _MAKE_TEMPLATE_ = {
160 "nmake" : 'if exist %(file)s "$(MAKE)" $(MAKE_FLAGS) -f %(file)s',
161 "gmake" : 'test -e %(file)s && "$(MAKE)" $(MAKE_FLAGS) -f %(file)s'
162 }
163
164 _INCLUDE_CMD_ = {
165 "nmake" : '!INCLUDE',
166 "gmake" : "include"
167 }
168
169 _INC_FLAG_ = {TAB_COMPILER_MSFT : "/I", "GCC" : "-I", "INTEL" : "-I", "RVCT" : "-I"}
170
171 ## Constructor of BuildFile
172 #
173 # @param AutoGenObject Object of AutoGen class
174 #
175 def __init__(self, AutoGenObject):
176 self._AutoGenObject = AutoGenObject
177 self._FileType = gMakeType
178
179 ## Create build file
180 #
181 # @param FileType Type of build file. Only nmake and gmake are supported now.
182 #
183 # @retval TRUE The build file is created or re-created successfully
184 # @retval FALSE The build file exists and is the same as the one to be generated
185 #
186 def Generate(self, FileType=gMakeType):
187 if FileType not in self._FILE_NAME_:
188 EdkLogger.error("build", PARAMETER_INVALID, "Invalid build type [%s]" % FileType,
189 ExtraData="[%s]" % str(self._AutoGenObject))
190 self._FileType = FileType
191 FileContent = self._TEMPLATE_.Replace(self._TemplateDict)
192 FileName = self._FILE_NAME_[FileType]
193 return SaveFileOnChange(os.path.join(self._AutoGenObject.MakeFileDir, FileName), FileContent, False)
194
195 ## Return a list of directory creation command string
196 #
197 # @param DirList The list of directory to be created
198 #
199 # @retval list The directory creation command list
200 #
201 def GetCreateDirectoryCommand(self, DirList):
202 return [self._MD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
203
204 ## Return a list of directory removal command string
205 #
206 # @param DirList The list of directory to be removed
207 #
208 # @retval list The directory removal command list
209 #
210 def GetRemoveDirectoryCommand(self, DirList):
211 return [self._RD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
212
213 def PlaceMacro(self, Path, MacroDefinitions={}):
214 if Path.startswith("$("):
215 return Path
216 else:
217 PathLength = len(Path)
218 for MacroName in MacroDefinitions:
219 MacroValue = MacroDefinitions[MacroName]
220 MacroValueLength = len(MacroValue)
221 if MacroValueLength == 0:
222 continue
223 if MacroValueLength <= PathLength and Path.startswith(MacroValue):
224 Path = "$(%s)%s" % (MacroName, Path[MacroValueLength:])
225 break
226 return Path
227
228 ## ModuleMakefile class
229 #
230 # This class encapsules makefie and its generation for module. It uses template to generate
231 # the content of makefile. The content of makefile will be got from ModuleAutoGen object.
232 #
233 class ModuleMakefile(BuildFile):
234 ## template used to generate the makefile for module
235 _TEMPLATE_ = TemplateString('''\
236 ${makefile_header}
237
238 #
239 # Platform Macro Definition
240 #
241 PLATFORM_NAME = ${platform_name}
242 PLATFORM_GUID = ${platform_guid}
243 PLATFORM_VERSION = ${platform_version}
244 PLATFORM_RELATIVE_DIR = ${platform_relative_directory}
245 PLATFORM_DIR = ${platform_dir}
246 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
247
248 #
249 # Module Macro Definition
250 #
251 MODULE_NAME = ${module_name}
252 MODULE_GUID = ${module_guid}
253 MODULE_NAME_GUID = ${module_name_guid}
254 MODULE_VERSION = ${module_version}
255 MODULE_TYPE = ${module_type}
256 MODULE_FILE = ${module_file}
257 MODULE_FILE_BASE_NAME = ${module_file_base_name}
258 BASE_NAME = $(MODULE_NAME)
259 MODULE_RELATIVE_DIR = ${module_relative_directory}
260 PACKAGE_RELATIVE_DIR = ${package_relative_directory}
261 MODULE_DIR = ${module_dir}
262 FFS_OUTPUT_DIR = ${ffs_output_directory}
263
264 MODULE_ENTRY_POINT = ${module_entry_point}
265 ARCH_ENTRY_POINT = ${arch_entry_point}
266 IMAGE_ENTRY_POINT = ${image_entry_point}
267
268 ${BEGIN}${module_extra_defines}
269 ${END}
270 #
271 # Build Configuration Macro Definition
272 #
273 ARCH = ${architecture}
274 TOOLCHAIN = ${toolchain_tag}
275 TOOLCHAIN_TAG = ${toolchain_tag}
276 TARGET = ${build_target}
277
278 #
279 # Build Directory Macro Definition
280 #
281 # PLATFORM_BUILD_DIR = ${platform_build_directory}
282 BUILD_DIR = ${platform_build_directory}
283 BIN_DIR = $(BUILD_DIR)${separator}${architecture}
284 LIB_DIR = $(BIN_DIR)
285 MODULE_BUILD_DIR = ${module_build_directory}
286 OUTPUT_DIR = ${module_output_directory}
287 DEBUG_DIR = ${module_debug_directory}
288 DEST_DIR_OUTPUT = $(OUTPUT_DIR)
289 DEST_DIR_DEBUG = $(DEBUG_DIR)
290
291 #
292 # Shell Command Macro
293 #
294 ${BEGIN}${shell_command_code} = ${shell_command}
295 ${END}
296
297 #
298 # Tools definitions specific to this module
299 #
300 ${BEGIN}${module_tool_definitions}
301 ${END}
302 MAKE_FILE = ${makefile_path}
303
304 #
305 # Build Macro
306 #
307 ${BEGIN}${file_macro}
308 ${END}
309
310 COMMON_DEPS = ${BEGIN}${common_dependency_file} \\
311 ${END}
312
313 #
314 # Overridable Target Macro Definitions
315 #
316 FORCE_REBUILD = force_build
317 INIT_TARGET = init
318 PCH_TARGET =
319 BC_TARGET = ${BEGIN}${backward_compatible_target} ${END}
320 CODA_TARGET = ${BEGIN}${remaining_build_target} \\
321 ${END}
322
323 #
324 # Default target, which will build dependent libraries in addition to source files
325 #
326
327 all: mbuild
328
329
330 #
331 # Target used when called from platform makefile, which will bypass the build of dependent libraries
332 #
333
334 pbuild: $(INIT_TARGET) $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
335
336 #
337 # ModuleTarget
338 #
339
340 mbuild: $(INIT_TARGET) $(BC_TARGET) gen_libs $(PCH_TARGET) $(CODA_TARGET)
341
342 #
343 # Build Target used in multi-thread build mode, which will bypass the init and gen_libs targets
344 #
345
346 tbuild: $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
347
348 #
349 # Phony target which is used to force executing commands for a target
350 #
351 force_build:
352 \t-@
353
354 #
355 # Target to update the FD
356 #
357
358 fds: mbuild gen_fds
359
360 #
361 # Initialization target: print build information and create necessary directories
362 #
363 init: info dirs
364
365 info:
366 \t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]
367
368 dirs:
369 ${BEGIN}\t-@${create_directory_command}\n${END}
370
371 strdefs:
372 \t-@$(CP) $(DEBUG_DIR)${separator}AutoGen.h $(DEBUG_DIR)${separator}$(MODULE_NAME)StrDefs.h
373
374 #
375 # GenLibsTarget
376 #
377 gen_libs:
378 \t${BEGIN}@"$(MAKE)" $(MAKE_FLAGS) -f ${dependent_library_build_directory}${separator}${makefile_name}
379 \t${END}@cd $(MODULE_BUILD_DIR)
380
381 #
382 # Build Flash Device Image
383 #
384 gen_fds:
385 \t@"$(MAKE)" $(MAKE_FLAGS) -f $(BUILD_DIR)${separator}${makefile_name} fds
386 \t@cd $(MODULE_BUILD_DIR)
387
388 #
389 # Individual Object Build Targets
390 #
391 ${BEGIN}${file_build_target}
392 ${END}
393
394 #
395 # clean all intermediate files
396 #
397 clean:
398 \t${BEGIN}${clean_command}
399 \t${END}\t$(RM) AutoGenTimeStamp
400
401 #
402 # clean all generated files
403 #
404 cleanall:
405 ${BEGIN}\t${cleanall_command}
406 ${END}\t$(RM) *.pdb *.idb > NUL 2>&1
407 \t$(RM) $(BIN_DIR)${separator}$(MODULE_NAME).efi
408 \t$(RM) AutoGenTimeStamp
409
410 #
411 # clean all dependent libraries built
412 #
413 cleanlib:
414 \t${BEGIN}-@${library_build_command} cleanall
415 \t${END}@cd $(MODULE_BUILD_DIR)\n\n''')
416
417 _FILE_MACRO_TEMPLATE = TemplateString("${macro_name} = ${BEGIN} \\\n ${source_file}${END}\n")
418 _BUILD_TARGET_TEMPLATE = TemplateString("${BEGIN}${target} : ${deps}\n${END}\t${cmd}\n")
419
420 ## Constructor of ModuleMakefile
421 #
422 # @param ModuleAutoGen Object of ModuleAutoGen class
423 #
424 def __init__(self, ModuleAutoGen):
425 BuildFile.__init__(self, ModuleAutoGen)
426 self.PlatformInfo = self._AutoGenObject.PlatformInfo
427
428 self.ResultFileList = []
429 self.IntermediateDirectoryList = ["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]
430
431 self.FileBuildTargetList = [] # [(src, target string)]
432 self.BuildTargetList = [] # [target string]
433 self.PendingBuildTargetList = [] # [FileBuildRule objects]
434 self.CommonFileDependency = []
435 self.FileListMacros = {}
436 self.ListFileMacros = {}
437
438 self.FileCache = {}
439 self.LibraryBuildCommandList = []
440 self.LibraryFileList = []
441 self.LibraryMakefileList = []
442 self.LibraryBuildDirectoryList = []
443 self.SystemLibraryList = []
444 self.Macros = OrderedDict()
445 self.Macros["OUTPUT_DIR" ] = self._AutoGenObject.Macros["OUTPUT_DIR"]
446 self.Macros["DEBUG_DIR" ] = self._AutoGenObject.Macros["DEBUG_DIR"]
447 self.Macros["MODULE_BUILD_DIR"] = self._AutoGenObject.Macros["MODULE_BUILD_DIR"]
448 self.Macros["BIN_DIR" ] = self._AutoGenObject.Macros["BIN_DIR"]
449 self.Macros["BUILD_DIR" ] = self._AutoGenObject.Macros["BUILD_DIR"]
450 self.Macros["WORKSPACE" ] = self._AutoGenObject.Macros["WORKSPACE"]
451 self.Macros["FFS_OUTPUT_DIR" ] = self._AutoGenObject.Macros["FFS_OUTPUT_DIR"]
452 self.GenFfsList = ModuleAutoGen.GenFfsList
453 self.MacroList = ['FFS_OUTPUT_DIR', 'MODULE_GUID', 'OUTPUT_DIR']
454 self.FfsOutputFileList = []
455
456 # Compose a dict object containing information used to do replacement in template
457 @property
458 def _TemplateDict(self):
459 if self._FileType not in self._SEP_:
460 EdkLogger.error("build", PARAMETER_INVALID, "Invalid Makefile type [%s]" % self._FileType,
461 ExtraData="[%s]" % str(self._AutoGenObject))
462 MyAgo = self._AutoGenObject
463 Separator = self._SEP_[self._FileType]
464
465 # break build if no source files and binary files are found
466 if len(MyAgo.SourceFileList) == 0 and len(MyAgo.BinaryFileList) == 0:
467 EdkLogger.error("build", AUTOGEN_ERROR, "No files to be built in module [%s, %s, %s]"
468 % (MyAgo.BuildTarget, MyAgo.ToolChain, MyAgo.Arch),
469 ExtraData="[%s]" % str(MyAgo))
470
471 # convert dependent libraries to build command
472 self.ProcessDependentLibrary()
473 if len(MyAgo.Module.ModuleEntryPointList) > 0:
474 ModuleEntryPoint = MyAgo.Module.ModuleEntryPointList[0]
475 else:
476 ModuleEntryPoint = "_ModuleEntryPoint"
477
478 # Intel EBC compiler enforces EfiMain
479 if MyAgo.AutoGenVersion < 0x00010005 and MyAgo.Arch == "EBC":
480 ArchEntryPoint = "EfiMain"
481 else:
482 ArchEntryPoint = ModuleEntryPoint
483
484 if MyAgo.Arch == "EBC":
485 # EBC compiler always use "EfiStart" as entry point. Only applies to EdkII modules
486 ImageEntryPoint = "EfiStart"
487 elif MyAgo.AutoGenVersion < 0x00010005:
488 # Edk modules use entry point specified in INF file
489 ImageEntryPoint = ModuleEntryPoint
490 else:
491 # EdkII modules always use "_ModuleEntryPoint" as entry point
492 ImageEntryPoint = "_ModuleEntryPoint"
493
494 for k, v in MyAgo.Module.Defines.items():
495 if k not in MyAgo.Macros:
496 MyAgo.Macros[k] = v
497
498 if 'MODULE_ENTRY_POINT' not in MyAgo.Macros:
499 MyAgo.Macros['MODULE_ENTRY_POINT'] = ModuleEntryPoint
500 if 'ARCH_ENTRY_POINT' not in MyAgo.Macros:
501 MyAgo.Macros['ARCH_ENTRY_POINT'] = ArchEntryPoint
502 if 'IMAGE_ENTRY_POINT' not in MyAgo.Macros:
503 MyAgo.Macros['IMAGE_ENTRY_POINT'] = ImageEntryPoint
504
505 PCI_COMPRESS_Flag = False
506 for k, v in MyAgo.Module.Defines.items():
507 if 'PCI_COMPRESS' == k and 'TRUE' == v:
508 PCI_COMPRESS_Flag = True
509
510 # tools definitions
511 ToolsDef = []
512 IncPrefix = self._INC_FLAG_[MyAgo.ToolChainFamily]
513 for Tool in MyAgo.BuildOption:
514 for Attr in MyAgo.BuildOption[Tool]:
515 Value = MyAgo.BuildOption[Tool][Attr]
516 if Attr == "FAMILY":
517 continue
518 elif Attr == "PATH":
519 ToolsDef.append("%s = %s" % (Tool, Value))
520 else:
521 # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
522 if Tool == "MAKE":
523 continue
524 # Remove duplicated include path, if any
525 if Attr == "FLAGS":
526 Value = RemoveDupOption(Value, IncPrefix, MyAgo.IncludePathList)
527 if Tool == "OPTROM" and PCI_COMPRESS_Flag:
528 ValueList = Value.split()
529 if ValueList:
530 for i, v in enumerate(ValueList):
531 if '-e' == v:
532 ValueList[i] = '-ec'
533 Value = ' '.join(ValueList)
534
535 ToolsDef.append("%s_%s = %s" % (Tool, Attr, Value))
536 ToolsDef.append("")
537
538 # generate the Response file and Response flag
539 RespDict = self.CommandExceedLimit()
540 RespFileList = os.path.join(MyAgo.OutputDir, 'respfilelist.txt')
541 if RespDict:
542 RespFileListContent = ''
543 for Resp in RespDict:
544 RespFile = os.path.join(MyAgo.OutputDir, str(Resp).lower() + '.txt')
545 StrList = RespDict[Resp].split(' ')
546 UnexpandMacro = []
547 NewStr = []
548 for Str in StrList:
549 if '$' in Str:
550 UnexpandMacro.append(Str)
551 else:
552 NewStr.append(Str)
553 UnexpandMacroStr = ' '.join(UnexpandMacro)
554 NewRespStr = ' '.join(NewStr)
555 SaveFileOnChange(RespFile, NewRespStr, False)
556 ToolsDef.append("%s = %s" % (Resp, UnexpandMacroStr + ' @' + RespFile))
557 RespFileListContent += '@' + RespFile + os.linesep
558 RespFileListContent += NewRespStr + os.linesep
559 SaveFileOnChange(RespFileList, RespFileListContent, False)
560 else:
561 if os.path.exists(RespFileList):
562 os.remove(RespFileList)
563
564 # convert source files and binary files to build targets
565 self.ResultFileList = [str(T.Target) for T in MyAgo.CodaTargetList]
566 if len(self.ResultFileList) == 0 and len(MyAgo.SourceFileList) != 0:
567 EdkLogger.error("build", AUTOGEN_ERROR, "Nothing to build",
568 ExtraData="[%s]" % str(MyAgo))
569
570 self.ProcessBuildTargetList()
571 self.ParserGenerateFfsCmd()
572
573 # Generate macros used to represent input files
574 FileMacroList = [] # macro name = file list
575 for FileListMacro in self.FileListMacros:
576 FileMacro = self._FILE_MACRO_TEMPLATE.Replace(
577 {
578 "macro_name" : FileListMacro,
579 "source_file" : self.FileListMacros[FileListMacro]
580 }
581 )
582 FileMacroList.append(FileMacro)
583
584 # INC_LIST is special
585 FileMacro = ""
586 IncludePathList = []
587 for P in MyAgo.IncludePathList:
588 IncludePathList.append(IncPrefix + self.PlaceMacro(P, self.Macros))
589 if FileBuildRule.INC_LIST_MACRO in self.ListFileMacros:
590 self.ListFileMacros[FileBuildRule.INC_LIST_MACRO].append(IncPrefix + P)
591 FileMacro += self._FILE_MACRO_TEMPLATE.Replace(
592 {
593 "macro_name" : "INC",
594 "source_file" : IncludePathList
595 }
596 )
597 FileMacroList.append(FileMacro)
598
599 # Generate macros used to represent files containing list of input files
600 for ListFileMacro in self.ListFileMacros:
601 ListFileName = os.path.join(MyAgo.OutputDir, "%s.lst" % ListFileMacro.lower()[:len(ListFileMacro) - 5])
602 FileMacroList.append("%s = %s" % (ListFileMacro, ListFileName))
603 SaveFileOnChange(
604 ListFileName,
605 "\n".join(self.ListFileMacros[ListFileMacro]),
606 False
607 )
608
609 # Edk modules need <BaseName>StrDefs.h for string ID
610 #if MyAgo.AutoGenVersion < 0x00010005 and len(MyAgo.UnicodeFileList) > 0:
611 # BcTargetList = ['strdefs']
612 #else:
613 # BcTargetList = []
614 BcTargetList = []
615
616 MakefileName = self._FILE_NAME_[self._FileType]
617 LibraryMakeCommandList = []
618 for D in self.LibraryBuildDirectoryList:
619 Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":os.path.join(D, MakefileName)}
620 LibraryMakeCommandList.append(Command)
621
622 package_rel_dir = MyAgo.SourceDir
623 current_dir = self.Macros["WORKSPACE"]
624 found = False
625 while not found and os.sep in package_rel_dir:
626 index = package_rel_dir.index(os.sep)
627 current_dir = mws.join(current_dir, package_rel_dir[:index])
628 if os.path.exists(current_dir):
629 for fl in os.listdir(current_dir):
630 if fl.endswith('.dec'):
631 found = True
632 break
633 package_rel_dir = package_rel_dir[index + 1:]
634
635 MakefileTemplateDict = {
636 "makefile_header" : self._FILE_HEADER_[self._FileType],
637 "makefile_path" : os.path.join("$(MODULE_BUILD_DIR)", MakefileName),
638 "makefile_name" : MakefileName,
639 "platform_name" : self.PlatformInfo.Name,
640 "platform_guid" : self.PlatformInfo.Guid,
641 "platform_version" : self.PlatformInfo.Version,
642 "platform_relative_directory": self.PlatformInfo.SourceDir,
643 "platform_output_directory" : self.PlatformInfo.OutputDir,
644 "ffs_output_directory" : MyAgo.Macros["FFS_OUTPUT_DIR"],
645 "platform_dir" : MyAgo.Macros["PLATFORM_DIR"],
646
647 "module_name" : MyAgo.Name,
648 "module_guid" : MyAgo.Guid,
649 "module_name_guid" : MyAgo.UniqueBaseName,
650 "module_version" : MyAgo.Version,
651 "module_type" : MyAgo.ModuleType,
652 "module_file" : MyAgo.MetaFile.Name,
653 "module_file_base_name" : MyAgo.MetaFile.BaseName,
654 "module_relative_directory" : MyAgo.SourceDir,
655 "module_dir" : mws.join (self.Macros["WORKSPACE"], MyAgo.SourceDir),
656 "package_relative_directory": package_rel_dir,
657 "module_extra_defines" : ["%s = %s" % (k, v) for k, v in MyAgo.Module.Defines.items()],
658
659 "architecture" : MyAgo.Arch,
660 "toolchain_tag" : MyAgo.ToolChain,
661 "build_target" : MyAgo.BuildTarget,
662
663 "platform_build_directory" : self.PlatformInfo.BuildDir,
664 "module_build_directory" : MyAgo.BuildDir,
665 "module_output_directory" : MyAgo.OutputDir,
666 "module_debug_directory" : MyAgo.DebugDir,
667
668 "separator" : Separator,
669 "module_tool_definitions" : ToolsDef,
670
671 "shell_command_code" : self._SHELL_CMD_[self._FileType].keys(),
672 "shell_command" : self._SHELL_CMD_[self._FileType].values(),
673
674 "module_entry_point" : ModuleEntryPoint,
675 "image_entry_point" : ImageEntryPoint,
676 "arch_entry_point" : ArchEntryPoint,
677 "remaining_build_target" : self.ResultFileList,
678 "common_dependency_file" : self.CommonFileDependency,
679 "create_directory_command" : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),
680 "clean_command" : self.GetRemoveDirectoryCommand(["$(OUTPUT_DIR)"]),
681 "cleanall_command" : self.GetRemoveDirectoryCommand(["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]),
682 "dependent_library_build_directory" : self.LibraryBuildDirectoryList,
683 "library_build_command" : LibraryMakeCommandList,
684 "file_macro" : FileMacroList,
685 "file_build_target" : self.BuildTargetList,
686 "backward_compatible_target": BcTargetList,
687 }
688
689 return MakefileTemplateDict
690
691 def ParserGenerateFfsCmd(self):
692 #Add Ffs cmd to self.BuildTargetList
693 OutputFile = ''
694 DepsFileList = []
695
696 for Cmd in self.GenFfsList:
697 if Cmd[2]:
698 for CopyCmd in Cmd[2]:
699 Src, Dst = CopyCmd
700 Src = self.ReplaceMacro(Src)
701 Dst = self.ReplaceMacro(Dst)
702 if Dst not in self.ResultFileList:
703 self.ResultFileList.append(Dst)
704 if '%s :' %(Dst) not in self.BuildTargetList:
705 self.BuildTargetList.append("%s :" %(Dst))
706 self.BuildTargetList.append('\t' + self._CP_TEMPLATE_[self._FileType] %{'Src': Src, 'Dst': Dst})
707
708 FfsCmdList = Cmd[0]
709 for index, Str in enumerate(FfsCmdList):
710 if '-o' == Str:
711 OutputFile = FfsCmdList[index + 1]
712 if '-i' == Str:
713 if DepsFileList == []:
714 DepsFileList = [FfsCmdList[index + 1]]
715 else:
716 DepsFileList.append(FfsCmdList[index + 1])
717 DepsFileString = ' '.join(DepsFileList).strip()
718 if DepsFileString == '':
719 continue
720 OutputFile = self.ReplaceMacro(OutputFile)
721 self.ResultFileList.append(OutputFile)
722 DepsFileString = self.ReplaceMacro(DepsFileString)
723 self.BuildTargetList.append('%s : %s' % (OutputFile, DepsFileString))
724 CmdString = ' '.join(FfsCmdList).strip()
725 CmdString = self.ReplaceMacro(CmdString)
726 self.BuildTargetList.append('\t%s' % CmdString)
727
728 self.ParseSecCmd(DepsFileList, Cmd[1])
729 for SecOutputFile, SecDepsFile, SecCmd in self.FfsOutputFileList :
730 self.BuildTargetList.append('%s : %s' % (self.ReplaceMacro(SecOutputFile), self.ReplaceMacro(SecDepsFile)))
731 self.BuildTargetList.append('\t%s' % self.ReplaceMacro(SecCmd))
732 self.FfsOutputFileList = []
733
734 def ParseSecCmd(self, OutputFileList, CmdTuple):
735 for OutputFile in OutputFileList:
736 for SecCmdStr in CmdTuple:
737 SecDepsFileList = []
738 SecCmdList = SecCmdStr.split()
739 CmdName = SecCmdList[0]
740 for index, CmdItem in enumerate(SecCmdList):
741 if '-o' == CmdItem and OutputFile == SecCmdList[index + 1]:
742 index = index + 1
743 while index + 1 < len(SecCmdList):
744 if not SecCmdList[index+1].startswith('-'):
745 SecDepsFileList.append(SecCmdList[index + 1])
746 index = index + 1
747 if CmdName == 'Trim':
748 SecDepsFileList.append(os.path.join('$(DEBUG_DIR)', os.path.basename(OutputFile).replace('offset', 'efi')))
749 if OutputFile.endswith('.ui') or OutputFile.endswith('.ver'):
750 SecDepsFileList.append(os.path.join('$(MODULE_DIR)', '$(MODULE_FILE)'))
751 self.FfsOutputFileList.append((OutputFile, ' '.join(SecDepsFileList), SecCmdStr))
752 if len(SecDepsFileList) > 0:
753 self.ParseSecCmd(SecDepsFileList, CmdTuple)
754 break
755 else:
756 continue
757
758 def ReplaceMacro(self, str):
759 for Macro in self.MacroList:
760 if self._AutoGenObject.Macros[Macro] and self._AutoGenObject.Macros[Macro] in str:
761 str = str.replace(self._AutoGenObject.Macros[Macro], '$(' + Macro + ')')
762 return str
763
764 def CommandExceedLimit(self):
765 FlagDict = {
766 'CC' : { 'Macro' : '$(CC_FLAGS)', 'Value' : False},
767 'PP' : { 'Macro' : '$(PP_FLAGS)', 'Value' : False},
768 'APP' : { 'Macro' : '$(APP_FLAGS)', 'Value' : False},
769 'ASLPP' : { 'Macro' : '$(ASLPP_FLAGS)', 'Value' : False},
770 'VFRPP' : { 'Macro' : '$(VFRPP_FLAGS)', 'Value' : False},
771 'ASM' : { 'Macro' : '$(ASM_FLAGS)', 'Value' : False},
772 'ASLCC' : { 'Macro' : '$(ASLCC_FLAGS)', 'Value' : False},
773 }
774
775 RespDict = {}
776 FileTypeList = []
777 IncPrefix = self._INC_FLAG_[self._AutoGenObject.ToolChainFamily]
778
779 # base on the source files to decide the file type
780 for File in self._AutoGenObject.SourceFileList:
781 for type in self._AutoGenObject.FileTypes:
782 if File in self._AutoGenObject.FileTypes[type]:
783 if type not in FileTypeList:
784 FileTypeList.append(type)
785
786 # calculate the command-line length
787 if FileTypeList:
788 for type in FileTypeList:
789 BuildTargets = self._AutoGenObject.BuildRules[type].BuildTargets
790 for Target in BuildTargets:
791 CommandList = BuildTargets[Target].Commands
792 for SingleCommand in CommandList:
793 Tool = ''
794 SingleCommandLength = len(SingleCommand)
795 SingleCommandList = SingleCommand.split()
796 if len(SingleCommandList) > 0:
797 for Flag in FlagDict:
798 if '$('+ Flag +')' in SingleCommandList[0]:
799 Tool = Flag
800 break
801 if Tool:
802 if 'PATH' not in self._AutoGenObject.BuildOption[Tool]:
803 EdkLogger.error("build", AUTOGEN_ERROR, "%s_PATH doesn't exist in %s ToolChain and %s Arch." %(Tool, self._AutoGenObject.ToolChain, self._AutoGenObject.Arch), ExtraData="[%s]" % str(self._AutoGenObject))
804 SingleCommandLength += len(self._AutoGenObject.BuildOption[Tool]['PATH'])
805 for item in SingleCommandList[1:]:
806 if FlagDict[Tool]['Macro'] in item:
807 if 'FLAGS' not in self._AutoGenObject.BuildOption[Tool]:
808 EdkLogger.error("build", AUTOGEN_ERROR, "%s_FLAGS doesn't exist in %s ToolChain and %s Arch." %(Tool, self._AutoGenObject.ToolChain, self._AutoGenObject.Arch), ExtraData="[%s]" % str(self._AutoGenObject))
809 Str = self._AutoGenObject.BuildOption[Tool]['FLAGS']
810 for Option in self._AutoGenObject.BuildOption:
811 for Attr in self._AutoGenObject.BuildOption[Option]:
812 if Str.find(Option + '_' + Attr) != -1:
813 Str = Str.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])
814 while(Str.find('$(') != -1):
815 for macro in self._AutoGenObject.Macros:
816 MacroName = '$('+ macro + ')'
817 if (Str.find(MacroName) != -1):
818 Str = Str.replace(MacroName, self._AutoGenObject.Macros[macro])
819 break
820 else:
821 break
822 SingleCommandLength += len(Str)
823 elif '$(INC)' in item:
824 SingleCommandLength += self._AutoGenObject.IncludePathLength + len(IncPrefix) * len(self._AutoGenObject.IncludePathList)
825 elif item.find('$(') != -1:
826 Str = item
827 for Option in self._AutoGenObject.BuildOption:
828 for Attr in self._AutoGenObject.BuildOption[Option]:
829 if Str.find(Option + '_' + Attr) != -1:
830 Str = Str.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])
831 while(Str.find('$(') != -1):
832 for macro in self._AutoGenObject.Macros:
833 MacroName = '$('+ macro + ')'
834 if (Str.find(MacroName) != -1):
835 Str = Str.replace(MacroName, self._AutoGenObject.Macros[macro])
836 break
837 else:
838 break
839 SingleCommandLength += len(Str)
840
841 if SingleCommandLength > GlobalData.gCommandMaxLength:
842 FlagDict[Tool]['Value'] = True
843
844 # generate the response file content by combine the FLAGS and INC
845 for Flag in FlagDict:
846 if FlagDict[Flag]['Value']:
847 Key = Flag + '_RESP'
848 RespMacro = FlagDict[Flag]['Macro'].replace('FLAGS', 'RESP')
849 Value = self._AutoGenObject.BuildOption[Flag]['FLAGS']
850 for inc in self._AutoGenObject.IncludePathList:
851 Value += ' ' + IncPrefix + inc
852 for Option in self._AutoGenObject.BuildOption:
853 for Attr in self._AutoGenObject.BuildOption[Option]:
854 if Value.find(Option + '_' + Attr) != -1:
855 Value = Value.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])
856 while (Value.find('$(') != -1):
857 for macro in self._AutoGenObject.Macros:
858 MacroName = '$('+ macro + ')'
859 if (Value.find(MacroName) != -1):
860 Value = Value.replace(MacroName, self._AutoGenObject.Macros[macro])
861 break
862 else:
863 break
864
865 if self._AutoGenObject.ToolChainFamily == 'GCC':
866 RespDict[Key] = Value.replace('\\', '/')
867 else:
868 RespDict[Key] = Value
869 for Target in BuildTargets:
870 for i, SingleCommand in enumerate(BuildTargets[Target].Commands):
871 if FlagDict[Flag]['Macro'] in SingleCommand:
872 BuildTargets[Target].Commands[i] = SingleCommand.replace('$(INC)', '').replace(FlagDict[Flag]['Macro'], RespMacro)
873 return RespDict
874
875 def ProcessBuildTargetList(self):
876 #
877 # Search dependency file list for each source file
878 #
879 ForceIncludedFile = []
880 for File in self._AutoGenObject.AutoGenFileList:
881 if File.Ext == '.h':
882 ForceIncludedFile.append(File)
883 SourceFileList = []
884 OutPutFileList = []
885 for Target in self._AutoGenObject.IntroTargetList:
886 SourceFileList.extend(Target.Inputs)
887 OutPutFileList.extend(Target.Outputs)
888
889 if OutPutFileList:
890 for Item in OutPutFileList:
891 if Item in SourceFileList:
892 SourceFileList.remove(Item)
893
894 FileDependencyDict = self.GetFileDependency(
895 SourceFileList,
896 ForceIncludedFile,
897 self._AutoGenObject.IncludePathList + self._AutoGenObject.BuildOptionIncPathList
898 )
899 DepSet = None
900 for File,Dependency in FileDependencyDict.items():
901 if not Dependency:
902 FileDependencyDict[File] = ['$(FORCE_REBUILD)']
903 continue
904
905 self._AutoGenObject.AutoGenDepSet |= set(Dependency)
906
907 # skip non-C files
908 if File.Ext not in [".c", ".C"] or File.Name == "AutoGen.c":
909 continue
910 elif DepSet is None:
911 DepSet = set(Dependency)
912 else:
913 DepSet &= set(Dependency)
914 # in case nothing in SourceFileList
915 if DepSet is None:
916 DepSet = set()
917 #
918 # Extract common files list in the dependency files
919 #
920 for File in DepSet:
921 self.CommonFileDependency.append(self.PlaceMacro(File.Path, self.Macros))
922
923 for File in FileDependencyDict:
924 # skip non-C files
925 if File.Ext not in [".c", ".C"] or File.Name == "AutoGen.c":
926 continue
927 NewDepSet = set(FileDependencyDict[File])
928 NewDepSet -= DepSet
929 FileDependencyDict[File] = ["$(COMMON_DEPS)"] + list(NewDepSet)
930
931 # Convert target description object to target string in makefile
932 for Type in self._AutoGenObject.Targets:
933 for T in self._AutoGenObject.Targets[Type]:
934 # Generate related macros if needed
935 if T.GenFileListMacro and T.FileListMacro not in self.FileListMacros:
936 self.FileListMacros[T.FileListMacro] = []
937 if T.GenListFile and T.ListFileMacro not in self.ListFileMacros:
938 self.ListFileMacros[T.ListFileMacro] = []
939 if T.GenIncListFile and T.IncListFileMacro not in self.ListFileMacros:
940 self.ListFileMacros[T.IncListFileMacro] = []
941
942 Deps = []
943 # Add force-dependencies
944 for Dep in T.Dependencies:
945 Deps.append(self.PlaceMacro(str(Dep), self.Macros))
946 # Add inclusion-dependencies
947 if len(T.Inputs) == 1 and T.Inputs[0] in FileDependencyDict:
948 for F in FileDependencyDict[T.Inputs[0]]:
949 Deps.append(self.PlaceMacro(str(F), self.Macros))
950 # Add source-dependencies
951 for F in T.Inputs:
952 NewFile = self.PlaceMacro(str(F), self.Macros)
953 # In order to use file list macro as dependency
954 if T.GenListFile:
955 # gnu tools need forward slash path separater, even on Windows
956 self.ListFileMacros[T.ListFileMacro].append(str(F).replace ('\\', '/'))
957 self.FileListMacros[T.FileListMacro].append(NewFile)
958 elif T.GenFileListMacro:
959 self.FileListMacros[T.FileListMacro].append(NewFile)
960 else:
961 Deps.append(NewFile)
962
963 # Use file list macro as dependency
964 if T.GenFileListMacro:
965 Deps.append("$(%s)" % T.FileListMacro)
966 if Type in [TAB_OBJECT_FILE, TAB_STATIC_LIBRARY]:
967 Deps.append("$(%s)" % T.ListFileMacro)
968
969 TargetDict = {
970 "target" : self.PlaceMacro(T.Target.Path, self.Macros),
971 "cmd" : "\n\t".join(T.Commands),
972 "deps" : Deps
973 }
974 self.BuildTargetList.append(self._BUILD_TARGET_TEMPLATE.Replace(TargetDict))
975
976 ## For creating makefile targets for dependent libraries
977 def ProcessDependentLibrary(self):
978 for LibraryAutoGen in self._AutoGenObject.LibraryAutoGenList:
979 if not LibraryAutoGen.IsBinaryModule:
980 self.LibraryBuildDirectoryList.append(self.PlaceMacro(LibraryAutoGen.BuildDir, self.Macros))
981
982 ## Return a list containing source file's dependencies
983 #
984 # @param FileList The list of source files
985 # @param ForceInculeList The list of files which will be included forcely
986 # @param SearchPathList The list of search path
987 #
988 # @retval dict The mapping between source file path and its dependencies
989 #
990 def GetFileDependency(self, FileList, ForceInculeList, SearchPathList):
991 Dependency = {}
992 for F in FileList:
993 Dependency[F] = self.GetDependencyList(F, ForceInculeList, SearchPathList)
994 return Dependency
995
996 ## Find dependencies for one source file
997 #
998 # By searching recursively "#include" directive in file, find out all the
999 # files needed by given source file. The dependecies will be only searched
1000 # in given search path list.
1001 #
1002 # @param File The source file
1003 # @param ForceInculeList The list of files which will be included forcely
1004 # @param SearchPathList The list of search path
1005 #
1006 # @retval list The list of files the given source file depends on
1007 #
1008 def GetDependencyList(self, File, ForceList, SearchPathList):
1009 EdkLogger.debug(EdkLogger.DEBUG_1, "Try to get dependency files for %s" % File)
1010 FileStack = [File] + ForceList
1011 DependencySet = set()
1012
1013 if self._AutoGenObject.Arch not in gDependencyDatabase:
1014 gDependencyDatabase[self._AutoGenObject.Arch] = {}
1015 DepDb = gDependencyDatabase[self._AutoGenObject.Arch]
1016
1017 while len(FileStack) > 0:
1018 F = FileStack.pop()
1019
1020 FullPathDependList = []
1021 if F in self.FileCache:
1022 for CacheFile in self.FileCache[F]:
1023 FullPathDependList.append(CacheFile)
1024 if CacheFile not in DependencySet:
1025 FileStack.append(CacheFile)
1026 DependencySet.update(FullPathDependList)
1027 continue
1028
1029 CurrentFileDependencyList = []
1030 if F in DepDb:
1031 CurrentFileDependencyList = DepDb[F]
1032 else:
1033 try:
1034 Fd = open(F.Path, 'rb')
1035 except BaseException as X:
1036 EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=F.Path + "\n\t" + str(X))
1037
1038 FileContent = Fd.read()
1039 Fd.close()
1040 if len(FileContent) == 0:
1041 continue
1042
1043 if FileContent[0] == 0xff or FileContent[0] == 0xfe:
1044 FileContent = str(FileContent, encoding="utf-16")
1045 IncludedFileList = gIncludePattern.findall(FileContent)
1046 else:
1047 try:
1048 FileContent = str(FileContent, encoding="utf-8")
1049 IncludedFileList = gIncludePattern.findall(FileContent)
1050 except:
1051 continue
1052
1053 for Inc in IncludedFileList:
1054 Inc = Inc.strip()
1055 # if there's macro used to reference header file, expand it
1056 HeaderList = gMacroPattern.findall(Inc)
1057 if len(HeaderList) == 1 and len(HeaderList[0]) == 2:
1058 HeaderType = HeaderList[0][0]
1059 HeaderKey = HeaderList[0][1]
1060 if HeaderType in gIncludeMacroConversion:
1061 Inc = gIncludeMacroConversion[HeaderType] % {"HeaderKey" : HeaderKey}
1062 else:
1063 # not known macro used in #include, always build the file by
1064 # returning a empty dependency
1065 self.FileCache[File] = []
1066 return []
1067 Inc = os.path.normpath(Inc)
1068 CurrentFileDependencyList.append(Inc)
1069 DepDb[F] = CurrentFileDependencyList
1070
1071 CurrentFilePath = F.Dir
1072 PathList = [CurrentFilePath] + SearchPathList
1073 for Inc in CurrentFileDependencyList:
1074 for SearchPath in PathList:
1075 FilePath = os.path.join(SearchPath, Inc)
1076 if FilePath in gIsFileMap:
1077 if not gIsFileMap[FilePath]:
1078 continue
1079 # If isfile is called too many times, the performance is slow down.
1080 elif not os.path.isfile(FilePath):
1081 gIsFileMap[FilePath] = False
1082 continue
1083 else:
1084 gIsFileMap[FilePath] = True
1085 FilePath = PathClass(FilePath)
1086 FullPathDependList.append(FilePath)
1087 if FilePath not in DependencySet:
1088 FileStack.append(FilePath)
1089 break
1090 else:
1091 EdkLogger.debug(EdkLogger.DEBUG_9, "%s included by %s was not found "\
1092 "in any given path:\n\t%s" % (Inc, F, "\n\t".join(SearchPathList)))
1093
1094 self.FileCache[F] = FullPathDependList
1095 DependencySet.update(FullPathDependList)
1096
1097 DependencySet.update(ForceList)
1098 if File in DependencySet:
1099 DependencySet.remove(File)
1100 DependencyList = list(DependencySet) # remove duplicate ones
1101
1102 return DependencyList
1103
1104 ## CustomMakefile class
1105 #
1106 # This class encapsules makefie and its generation for module. It uses template to generate
1107 # the content of makefile. The content of makefile will be got from ModuleAutoGen object.
1108 #
1109 class CustomMakefile(BuildFile):
1110 ## template used to generate the makefile for module with custom makefile
1111 _TEMPLATE_ = TemplateString('''\
1112 ${makefile_header}
1113
1114 #
1115 # Platform Macro Definition
1116 #
1117 PLATFORM_NAME = ${platform_name}
1118 PLATFORM_GUID = ${platform_guid}
1119 PLATFORM_VERSION = ${platform_version}
1120 PLATFORM_RELATIVE_DIR = ${platform_relative_directory}
1121 PLATFORM_DIR = ${platform_dir}
1122 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
1123
1124 #
1125 # Module Macro Definition
1126 #
1127 MODULE_NAME = ${module_name}
1128 MODULE_GUID = ${module_guid}
1129 MODULE_NAME_GUID = ${module_name_guid}
1130 MODULE_VERSION = ${module_version}
1131 MODULE_TYPE = ${module_type}
1132 MODULE_FILE = ${module_file}
1133 MODULE_FILE_BASE_NAME = ${module_file_base_name}
1134 BASE_NAME = $(MODULE_NAME)
1135 MODULE_RELATIVE_DIR = ${module_relative_directory}
1136 MODULE_DIR = ${module_dir}
1137
1138 #
1139 # Build Configuration Macro Definition
1140 #
1141 ARCH = ${architecture}
1142 TOOLCHAIN = ${toolchain_tag}
1143 TOOLCHAIN_TAG = ${toolchain_tag}
1144 TARGET = ${build_target}
1145
1146 #
1147 # Build Directory Macro Definition
1148 #
1149 # PLATFORM_BUILD_DIR = ${platform_build_directory}
1150 BUILD_DIR = ${platform_build_directory}
1151 BIN_DIR = $(BUILD_DIR)${separator}${architecture}
1152 LIB_DIR = $(BIN_DIR)
1153 MODULE_BUILD_DIR = ${module_build_directory}
1154 OUTPUT_DIR = ${module_output_directory}
1155 DEBUG_DIR = ${module_debug_directory}
1156 DEST_DIR_OUTPUT = $(OUTPUT_DIR)
1157 DEST_DIR_DEBUG = $(DEBUG_DIR)
1158
1159 #
1160 # Tools definitions specific to this module
1161 #
1162 ${BEGIN}${module_tool_definitions}
1163 ${END}
1164 MAKE_FILE = ${makefile_path}
1165
1166 #
1167 # Shell Command Macro
1168 #
1169 ${BEGIN}${shell_command_code} = ${shell_command}
1170 ${END}
1171
1172 ${custom_makefile_content}
1173
1174 #
1175 # Target used when called from platform makefile, which will bypass the build of dependent libraries
1176 #
1177
1178 pbuild: init all
1179
1180
1181 #
1182 # ModuleTarget
1183 #
1184
1185 mbuild: init all
1186
1187 #
1188 # Build Target used in multi-thread build mode, which no init target is needed
1189 #
1190
1191 tbuild: all
1192
1193 #
1194 # Initialization target: print build information and create necessary directories
1195 #
1196 init:
1197 \t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]
1198 ${BEGIN}\t-@${create_directory_command}\n${END}\
1199
1200 ''')
1201
1202 ## Constructor of CustomMakefile
1203 #
1204 # @param ModuleAutoGen Object of ModuleAutoGen class
1205 #
1206 def __init__(self, ModuleAutoGen):
1207 BuildFile.__init__(self, ModuleAutoGen)
1208 self.PlatformInfo = self._AutoGenObject.PlatformInfo
1209 self.IntermediateDirectoryList = ["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]
1210
1211 # Compose a dict object containing information used to do replacement in template
1212 @property
1213 def _TemplateDict(self):
1214 Separator = self._SEP_[self._FileType]
1215 MyAgo = self._AutoGenObject
1216 if self._FileType not in MyAgo.CustomMakefile:
1217 EdkLogger.error('build', OPTION_NOT_SUPPORTED, "No custom makefile for %s" % self._FileType,
1218 ExtraData="[%s]" % str(MyAgo))
1219 MakefilePath = mws.join(
1220 MyAgo.WorkspaceDir,
1221 MyAgo.CustomMakefile[self._FileType]
1222 )
1223 try:
1224 CustomMakefile = open(MakefilePath, 'r').read()
1225 except:
1226 EdkLogger.error('build', FILE_OPEN_FAILURE, File=str(MyAgo),
1227 ExtraData=MyAgo.CustomMakefile[self._FileType])
1228
1229 # tools definitions
1230 ToolsDef = []
1231 for Tool in MyAgo.BuildOption:
1232 # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
1233 if Tool == "MAKE":
1234 continue
1235 for Attr in MyAgo.BuildOption[Tool]:
1236 if Attr == "FAMILY":
1237 continue
1238 elif Attr == "PATH":
1239 ToolsDef.append("%s = %s" % (Tool, MyAgo.BuildOption[Tool][Attr]))
1240 else:
1241 ToolsDef.append("%s_%s = %s" % (Tool, Attr, MyAgo.BuildOption[Tool][Attr]))
1242 ToolsDef.append("")
1243
1244 MakefileName = self._FILE_NAME_[self._FileType]
1245 MakefileTemplateDict = {
1246 "makefile_header" : self._FILE_HEADER_[self._FileType],
1247 "makefile_path" : os.path.join("$(MODULE_BUILD_DIR)", MakefileName),
1248 "platform_name" : self.PlatformInfo.Name,
1249 "platform_guid" : self.PlatformInfo.Guid,
1250 "platform_version" : self.PlatformInfo.Version,
1251 "platform_relative_directory": self.PlatformInfo.SourceDir,
1252 "platform_output_directory" : self.PlatformInfo.OutputDir,
1253 "platform_dir" : MyAgo.Macros["PLATFORM_DIR"],
1254
1255 "module_name" : MyAgo.Name,
1256 "module_guid" : MyAgo.Guid,
1257 "module_name_guid" : MyAgo.UniqueBaseName,
1258 "module_version" : MyAgo.Version,
1259 "module_type" : MyAgo.ModuleType,
1260 "module_file" : MyAgo.MetaFile,
1261 "module_file_base_name" : MyAgo.MetaFile.BaseName,
1262 "module_relative_directory" : MyAgo.SourceDir,
1263 "module_dir" : mws.join (MyAgo.WorkspaceDir, MyAgo.SourceDir),
1264
1265 "architecture" : MyAgo.Arch,
1266 "toolchain_tag" : MyAgo.ToolChain,
1267 "build_target" : MyAgo.BuildTarget,
1268
1269 "platform_build_directory" : self.PlatformInfo.BuildDir,
1270 "module_build_directory" : MyAgo.BuildDir,
1271 "module_output_directory" : MyAgo.OutputDir,
1272 "module_debug_directory" : MyAgo.DebugDir,
1273
1274 "separator" : Separator,
1275 "module_tool_definitions" : ToolsDef,
1276
1277 "shell_command_code" : self._SHELL_CMD_[self._FileType].keys(),
1278 "shell_command" : self._SHELL_CMD_[self._FileType].values(),
1279
1280 "create_directory_command" : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),
1281 "custom_makefile_content" : CustomMakefile
1282 }
1283
1284 return MakefileTemplateDict
1285
1286 ## PlatformMakefile class
1287 #
1288 # This class encapsules makefie and its generation for platform. It uses
1289 # template to generate the content of makefile. The content of makefile will be
1290 # got from PlatformAutoGen object.
1291 #
1292 class PlatformMakefile(BuildFile):
1293 ## template used to generate the makefile for platform
1294 _TEMPLATE_ = TemplateString('''\
1295 ${makefile_header}
1296
1297 #
1298 # Platform Macro Definition
1299 #
1300 PLATFORM_NAME = ${platform_name}
1301 PLATFORM_GUID = ${platform_guid}
1302 PLATFORM_VERSION = ${platform_version}
1303 PLATFORM_FILE = ${platform_file}
1304 PLATFORM_DIR = ${platform_dir}
1305 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
1306
1307 #
1308 # Build Configuration Macro Definition
1309 #
1310 TOOLCHAIN = ${toolchain_tag}
1311 TOOLCHAIN_TAG = ${toolchain_tag}
1312 TARGET = ${build_target}
1313
1314 #
1315 # Build Directory Macro Definition
1316 #
1317 BUILD_DIR = ${platform_build_directory}
1318 FV_DIR = ${platform_build_directory}${separator}FV
1319
1320 #
1321 # Shell Command Macro
1322 #
1323 ${BEGIN}${shell_command_code} = ${shell_command}
1324 ${END}
1325
1326 MAKE = ${make_path}
1327 MAKE_FILE = ${makefile_path}
1328
1329 #
1330 # Default target
1331 #
1332 all: init build_libraries build_modules
1333
1334 #
1335 # Initialization target: print build information and create necessary directories
1336 #
1337 init:
1338 \t-@echo Building ... $(PLATFORM_FILE) [${build_architecture_list}]
1339 \t${BEGIN}-@${create_directory_command}
1340 \t${END}
1341 #
1342 # library build target
1343 #
1344 libraries: init build_libraries
1345
1346 #
1347 # module build target
1348 #
1349 modules: init build_libraries build_modules
1350
1351 #
1352 # Build all libraries:
1353 #
1354 build_libraries:
1355 ${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${library_makefile_list} pbuild
1356 ${END}\t@cd $(BUILD_DIR)
1357
1358 #
1359 # Build all modules:
1360 #
1361 build_modules:
1362 ${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${module_makefile_list} pbuild
1363 ${END}\t@cd $(BUILD_DIR)
1364
1365 #
1366 # Clean intermediate files
1367 #
1368 clean:
1369 \t${BEGIN}-@${library_build_command} clean
1370 \t${END}${BEGIN}-@${module_build_command} clean
1371 \t${END}@cd $(BUILD_DIR)
1372
1373 #
1374 # Clean all generated files except to makefile
1375 #
1376 cleanall:
1377 ${BEGIN}\t${cleanall_command}
1378 ${END}
1379
1380 #
1381 # Clean all library files
1382 #
1383 cleanlib:
1384 \t${BEGIN}-@${library_build_command} cleanall
1385 \t${END}@cd $(BUILD_DIR)\n
1386 ''')
1387
1388 ## Constructor of PlatformMakefile
1389 #
1390 # @param ModuleAutoGen Object of PlatformAutoGen class
1391 #
1392 def __init__(self, PlatformAutoGen):
1393 BuildFile.__init__(self, PlatformAutoGen)
1394 self.ModuleBuildCommandList = []
1395 self.ModuleMakefileList = []
1396 self.IntermediateDirectoryList = []
1397 self.ModuleBuildDirectoryList = []
1398 self.LibraryBuildDirectoryList = []
1399 self.LibraryMakeCommandList = []
1400
1401 # Compose a dict object containing information used to do replacement in template
1402 @property
1403 def _TemplateDict(self):
1404 Separator = self._SEP_[self._FileType]
1405
1406 MyAgo = self._AutoGenObject
1407 if "MAKE" not in MyAgo.ToolDefinition or "PATH" not in MyAgo.ToolDefinition["MAKE"]:
1408 EdkLogger.error("build", OPTION_MISSING, "No MAKE command defined. Please check your tools_def.txt!",
1409 ExtraData="[%s]" % str(MyAgo))
1410
1411 self.IntermediateDirectoryList = ["$(BUILD_DIR)"]
1412 self.ModuleBuildDirectoryList = self.GetModuleBuildDirectoryList()
1413 self.LibraryBuildDirectoryList = self.GetLibraryBuildDirectoryList()
1414
1415 MakefileName = self._FILE_NAME_[self._FileType]
1416 LibraryMakefileList = []
1417 LibraryMakeCommandList = []
1418 for D in self.LibraryBuildDirectoryList:
1419 D = self.PlaceMacro(D, {"BUILD_DIR":MyAgo.BuildDir})
1420 Makefile = os.path.join(D, MakefileName)
1421 Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":Makefile}
1422 LibraryMakefileList.append(Makefile)
1423 LibraryMakeCommandList.append(Command)
1424 self.LibraryMakeCommandList = LibraryMakeCommandList
1425
1426 ModuleMakefileList = []
1427 ModuleMakeCommandList = []
1428 for D in self.ModuleBuildDirectoryList:
1429 D = self.PlaceMacro(D, {"BUILD_DIR":MyAgo.BuildDir})
1430 Makefile = os.path.join(D, MakefileName)
1431 Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":Makefile}
1432 ModuleMakefileList.append(Makefile)
1433 ModuleMakeCommandList.append(Command)
1434
1435 MakefileTemplateDict = {
1436 "makefile_header" : self._FILE_HEADER_[self._FileType],
1437 "makefile_path" : os.path.join("$(BUILD_DIR)", MakefileName),
1438 "make_path" : MyAgo.ToolDefinition["MAKE"]["PATH"],
1439 "makefile_name" : MakefileName,
1440 "platform_name" : MyAgo.Name,
1441 "platform_guid" : MyAgo.Guid,
1442 "platform_version" : MyAgo.Version,
1443 "platform_file" : MyAgo.MetaFile,
1444 "platform_relative_directory": MyAgo.SourceDir,
1445 "platform_output_directory" : MyAgo.OutputDir,
1446 "platform_build_directory" : MyAgo.BuildDir,
1447 "platform_dir" : MyAgo.Macros["PLATFORM_DIR"],
1448
1449 "toolchain_tag" : MyAgo.ToolChain,
1450 "build_target" : MyAgo.BuildTarget,
1451 "shell_command_code" : self._SHELL_CMD_[self._FileType].keys(),
1452 "shell_command" : self._SHELL_CMD_[self._FileType].values(),
1453 "build_architecture_list" : MyAgo.Arch,
1454 "architecture" : MyAgo.Arch,
1455 "separator" : Separator,
1456 "create_directory_command" : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),
1457 "cleanall_command" : self.GetRemoveDirectoryCommand(self.IntermediateDirectoryList),
1458 "library_makefile_list" : LibraryMakefileList,
1459 "module_makefile_list" : ModuleMakefileList,
1460 "library_build_command" : LibraryMakeCommandList,
1461 "module_build_command" : ModuleMakeCommandList,
1462 }
1463
1464 return MakefileTemplateDict
1465
1466 ## Get the root directory list for intermediate files of all modules build
1467 #
1468 # @retval list The list of directory
1469 #
1470 def GetModuleBuildDirectoryList(self):
1471 DirList = []
1472 for ModuleAutoGen in self._AutoGenObject.ModuleAutoGenList:
1473 if not ModuleAutoGen.IsBinaryModule:
1474 DirList.append(os.path.join(self._AutoGenObject.BuildDir, ModuleAutoGen.BuildDir))
1475 return DirList
1476
1477 ## Get the root directory list for intermediate files of all libraries build
1478 #
1479 # @retval list The list of directory
1480 #
1481 def GetLibraryBuildDirectoryList(self):
1482 DirList = []
1483 for LibraryAutoGen in self._AutoGenObject.LibraryAutoGenList:
1484 if not LibraryAutoGen.IsBinaryModule:
1485 DirList.append(os.path.join(self._AutoGenObject.BuildDir, LibraryAutoGen.BuildDir))
1486 return DirList
1487
1488 ## TopLevelMakefile class
1489 #
1490 # This class encapsules makefie and its generation for entrance makefile. It
1491 # uses template to generate the content of makefile. The content of makefile
1492 # will be got from WorkspaceAutoGen object.
1493 #
1494 class TopLevelMakefile(BuildFile):
1495 ## template used to generate toplevel makefile
1496 _TEMPLATE_ = TemplateString('''${BEGIN}\tGenFds -f ${fdf_file} --conf=${conf_directory} -o ${platform_build_directory} -t ${toolchain_tag} -b ${build_target} -p ${active_platform} -a ${build_architecture_list} ${extra_options}${END}${BEGIN} -r ${fd} ${END}${BEGIN} -i ${fv} ${END}${BEGIN} -C ${cap} ${END}${BEGIN} -D ${macro} ${END}''')
1497
1498 ## Constructor of TopLevelMakefile
1499 #
1500 # @param Workspace Object of WorkspaceAutoGen class
1501 #
1502 def __init__(self, Workspace):
1503 BuildFile.__init__(self, Workspace)
1504 self.IntermediateDirectoryList = []
1505
1506 # Compose a dict object containing information used to do replacement in template
1507 @property
1508 def _TemplateDict(self):
1509 Separator = self._SEP_[self._FileType]
1510
1511 # any platform autogen object is ok because we just need common information
1512 MyAgo = self._AutoGenObject
1513
1514 if "MAKE" not in MyAgo.ToolDefinition or "PATH" not in MyAgo.ToolDefinition["MAKE"]:
1515 EdkLogger.error("build", OPTION_MISSING, "No MAKE command defined. Please check your tools_def.txt!",
1516 ExtraData="[%s]" % str(MyAgo))
1517
1518 for Arch in MyAgo.ArchList:
1519 self.IntermediateDirectoryList.append(Separator.join(["$(BUILD_DIR)", Arch]))
1520 self.IntermediateDirectoryList.append("$(FV_DIR)")
1521
1522 # TRICK: for not generating GenFds call in makefile if no FDF file
1523 MacroList = []
1524 if MyAgo.FdfFile is not None and MyAgo.FdfFile != "":
1525 FdfFileList = [MyAgo.FdfFile]
1526 # macros passed to GenFds
1527 MacroList.append('"%s=%s"' % ("EFI_SOURCE", GlobalData.gEfiSource.replace('\\', '\\\\')))
1528 MacroList.append('"%s=%s"' % ("EDK_SOURCE", GlobalData.gEdkSource.replace('\\', '\\\\')))
1529 MacroDict = {}
1530 MacroDict.update(GlobalData.gGlobalDefines)
1531 MacroDict.update(GlobalData.gCommandLineDefines)
1532 MacroDict.pop("EFI_SOURCE", "dummy")
1533 MacroDict.pop("EDK_SOURCE", "dummy")
1534 for MacroName in MacroDict:
1535 if MacroDict[MacroName] != "":
1536 MacroList.append('"%s=%s"' % (MacroName, MacroDict[MacroName].replace('\\', '\\\\')))
1537 else:
1538 MacroList.append('"%s"' % MacroName)
1539 else:
1540 FdfFileList = []
1541
1542 # pass extra common options to external program called in makefile, currently GenFds.exe
1543 ExtraOption = ''
1544 LogLevel = EdkLogger.GetLevel()
1545 if LogLevel == EdkLogger.VERBOSE:
1546 ExtraOption += " -v"
1547 elif LogLevel <= EdkLogger.DEBUG_9:
1548 ExtraOption += " -d %d" % (LogLevel - 1)
1549 elif LogLevel == EdkLogger.QUIET:
1550 ExtraOption += " -q"
1551
1552 if GlobalData.gCaseInsensitive:
1553 ExtraOption += " -c"
1554 if GlobalData.gEnableGenfdsMultiThread:
1555 ExtraOption += " --genfds-multi-thread"
1556 if GlobalData.gIgnoreSource:
1557 ExtraOption += " --ignore-sources"
1558
1559 for pcd in GlobalData.BuildOptionPcd:
1560 if pcd[2]:
1561 pcdname = '.'.join(pcd[0:3])
1562 else:
1563 pcdname = '.'.join(pcd[0:2])
1564 if pcd[3].startswith('{'):
1565 ExtraOption += " --pcd " + pcdname + '=' + 'H' + '"' + pcd[3] + '"'
1566 else:
1567 ExtraOption += " --pcd " + pcdname + '=' + pcd[3]
1568
1569 MakefileName = self._FILE_NAME_[self._FileType]
1570 SubBuildCommandList = []
1571 for A in MyAgo.ArchList:
1572 Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":os.path.join("$(BUILD_DIR)", A, MakefileName)}
1573 SubBuildCommandList.append(Command)
1574
1575 MakefileTemplateDict = {
1576 "makefile_header" : self._FILE_HEADER_[self._FileType],
1577 "makefile_path" : os.path.join("$(BUILD_DIR)", MakefileName),
1578 "make_path" : MyAgo.ToolDefinition["MAKE"]["PATH"],
1579 "platform_name" : MyAgo.Name,
1580 "platform_guid" : MyAgo.Guid,
1581 "platform_version" : MyAgo.Version,
1582 "platform_build_directory" : MyAgo.BuildDir,
1583 "conf_directory" : GlobalData.gConfDirectory,
1584
1585 "toolchain_tag" : MyAgo.ToolChain,
1586 "build_target" : MyAgo.BuildTarget,
1587 "shell_command_code" : self._SHELL_CMD_[self._FileType].keys(),
1588 "shell_command" : self._SHELL_CMD_[self._FileType].values(),
1589 'arch' : list(MyAgo.ArchList),
1590 "build_architecture_list" : ','.join(MyAgo.ArchList),
1591 "separator" : Separator,
1592 "create_directory_command" : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),
1593 "cleanall_command" : self.GetRemoveDirectoryCommand(self.IntermediateDirectoryList),
1594 "sub_build_command" : SubBuildCommandList,
1595 "fdf_file" : FdfFileList,
1596 "active_platform" : str(MyAgo),
1597 "fd" : MyAgo.FdTargetList,
1598 "fv" : MyAgo.FvTargetList,
1599 "cap" : MyAgo.CapTargetList,
1600 "extra_options" : ExtraOption,
1601 "macro" : MacroList,
1602 }
1603
1604 return MakefileTemplateDict
1605
1606 ## Get the root directory list for intermediate files of all modules build
1607 #
1608 # @retval list The list of directory
1609 #
1610 def GetModuleBuildDirectoryList(self):
1611 DirList = []
1612 for ModuleAutoGen in self._AutoGenObject.ModuleAutoGenList:
1613 if not ModuleAutoGen.IsBinaryModule:
1614 DirList.append(os.path.join(self._AutoGenObject.BuildDir, ModuleAutoGen.BuildDir))
1615 return DirList
1616
1617 ## Get the root directory list for intermediate files of all libraries build
1618 #
1619 # @retval list The list of directory
1620 #
1621 def GetLibraryBuildDirectoryList(self):
1622 DirList = []
1623 for LibraryAutoGen in self._AutoGenObject.LibraryAutoGenList:
1624 if not LibraryAutoGen.IsBinaryModule:
1625 DirList.append(os.path.join(self._AutoGenObject.BuildDir, LibraryAutoGen.BuildDir))
1626 return DirList
1627
1628 # This acts like the main() function for the script, unless it is 'import'ed into another script.
1629 if __name__ == '__main__':
1630 pass
1631