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