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