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