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