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