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