]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/AutoGen/GenMake.py
BaseTools: list .nasm include inc files as its dependency
[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 from __future__ import absolute_import
17 import Common.LongFilePathOs as os
18 import sys
19 import string
20 import re
21 import os.path as path
22 from Common.LongFilePathSupport import OpenLongFilePath as open
23 from Common.MultipleWorkspace import MultipleWorkspace as mws
24 from Common.BuildToolError import *
25 from Common.Misc import *
26 from Common.StringUtils import *
27 from .BuildEngine import *
28 import Common.GlobalData as GlobalData
29 from collections import OrderedDict
30 from Common.DataType import TAB_COMPILER_MSFT
31
32 ## Regular expression for finding header file inclusions
33 gIncludePattern = re.compile(r"^[ \t]*[#%]?[ \t]*include(?:[ \t]*(?:\\(?:\r\n|\r|\n))*[ \t]*)*(?:\(?[\"<]?[ \t]*)([-\w.\\/() \t]+)(?:[ \t]*[\">]?\)?)", re.MULTILINE | re.UNICODE | re.IGNORECASE)
34
35 ## Regular expression for matching macro used in header file inclusion
36 gMacroPattern = re.compile("([_A-Z][_A-Z0-9]*)[ \t]*\((.+)\)", re.UNICODE)
37
38 gIsFileMap = {}
39
40 ## pattern for include style in Edk.x code
41 gProtocolDefinition = "Protocol/%(HeaderKey)s/%(HeaderKey)s.h"
42 gGuidDefinition = "Guid/%(HeaderKey)s/%(HeaderKey)s.h"
43 gArchProtocolDefinition = "ArchProtocol/%(HeaderKey)s/%(HeaderKey)s.h"
44 gPpiDefinition = "Ppi/%(HeaderKey)s/%(HeaderKey)s.h"
45 gIncludeMacroConversion = {
46 "EFI_PROTOCOL_DEFINITION" : gProtocolDefinition,
47 "EFI_GUID_DEFINITION" : gGuidDefinition,
48 "EFI_ARCH_PROTOCOL_DEFINITION" : gArchProtocolDefinition,
49 "EFI_PROTOCOL_PRODUCER" : gProtocolDefinition,
50 "EFI_PROTOCOL_CONSUMER" : gProtocolDefinition,
51 "EFI_PROTOCOL_DEPENDENCY" : gProtocolDefinition,
52 "EFI_ARCH_PROTOCOL_PRODUCER" : gArchProtocolDefinition,
53 "EFI_ARCH_PROTOCOL_CONSUMER" : gArchProtocolDefinition,
54 "EFI_ARCH_PROTOCOL_DEPENDENCY" : gArchProtocolDefinition,
55 "EFI_PPI_DEFINITION" : gPpiDefinition,
56 "EFI_PPI_PRODUCER" : gPpiDefinition,
57 "EFI_PPI_CONSUMER" : gPpiDefinition,
58 "EFI_PPI_DEPENDENCY" : gPpiDefinition,
59 }
60
61 ## default makefile type
62 gMakeType = ""
63 if sys.platform == "win32":
64 gMakeType = "nmake"
65 else:
66 gMakeType = "gmake"
67
68
69 ## BuildFile class
70 #
71 # This base class encapsules build file and its generation. It uses template to generate
72 # the content of build file. The content of build file will be got from AutoGen objects.
73 #
74 class BuildFile(object):
75 ## template used to generate the build file (i.e. makefile if using make)
76 _TEMPLATE_ = TemplateString('')
77
78 _DEFAULT_FILE_NAME_ = "Makefile"
79
80 ## default file name for each type of build file
81 _FILE_NAME_ = {
82 "nmake" : "Makefile",
83 "gmake" : "GNUmakefile"
84 }
85
86 ## Fixed header string for makefile
87 _MAKEFILE_HEADER = '''#
88 # DO NOT EDIT
89 # This file is auto-generated by build utility
90 #
91 # Module Name:
92 #
93 # %s
94 #
95 # Abstract:
96 #
97 # Auto-generated makefile for building modules, libraries or platform
98 #
99 '''
100
101 ## Header string for each type of build file
102 _FILE_HEADER_ = {
103 "nmake" : _MAKEFILE_HEADER % _FILE_NAME_["nmake"],
104 "gmake" : _MAKEFILE_HEADER % _FILE_NAME_["gmake"]
105 }
106
107 ## shell commands which can be used in build file in the form of macro
108 # $(CP) copy file command
109 # $(MV) move file command
110 # $(RM) remove file command
111 # $(MD) create dir command
112 # $(RD) remove dir command
113 #
114 _SHELL_CMD_ = {
115 "nmake" : {
116 "CP" : "copy /y",
117 "MV" : "move /y",
118 "RM" : "del /f /q",
119 "MD" : "mkdir",
120 "RD" : "rmdir /s /q",
121 },
122
123 "gmake" : {
124 "CP" : "cp -f",
125 "MV" : "mv -f",
126 "RM" : "rm -f",
127 "MD" : "mkdir -p",
128 "RD" : "rm -r -f",
129 }
130 }
131
132 ## directory separator
133 _SEP_ = {
134 "nmake" : "\\",
135 "gmake" : "/"
136 }
137
138 ## directory creation template
139 _MD_TEMPLATE_ = {
140 "nmake" : 'if not exist %(dir)s $(MD) %(dir)s',
141 "gmake" : "$(MD) %(dir)s"
142 }
143
144 ## directory removal template
145 _RD_TEMPLATE_ = {
146 "nmake" : 'if exist %(dir)s $(RD) %(dir)s',
147 "gmake" : "$(RD) %(dir)s"
148 }
149 ## cp if exist
150 _CP_TEMPLATE_ = {
151 "nmake" : 'if exist %(Src)s $(CP) %(Src)s %(Dst)s',
152 "gmake" : "test -f %(Src)s && $(CP) %(Src)s %(Dst)s"
153 }
154
155 _CD_TEMPLATE_ = {
156 "nmake" : 'if exist %(dir)s cd %(dir)s',
157 "gmake" : "test -e %(dir)s && cd %(dir)s"
158 }
159
160 _MAKE_TEMPLATE_ = {
161 "nmake" : 'if exist %(file)s "$(MAKE)" $(MAKE_FLAGS) -f %(file)s',
162 "gmake" : 'test -e %(file)s && "$(MAKE)" $(MAKE_FLAGS) -f %(file)s'
163 }
164
165 _INCLUDE_CMD_ = {
166 "nmake" : '!INCLUDE',
167 "gmake" : "include"
168 }
169
170 _INC_FLAG_ = {TAB_COMPILER_MSFT : "/I", "GCC" : "-I", "INTEL" : "-I", "RVCT" : "-I"}
171
172 ## Constructor of BuildFile
173 #
174 # @param AutoGenObject Object of AutoGen class
175 #
176 def __init__(self, AutoGenObject):
177 self._AutoGenObject = AutoGenObject
178 self._FileType = gMakeType
179
180 ## Create build file
181 #
182 # @param FileType Type of build file. Only nmake and gmake are supported now.
183 #
184 # @retval TRUE The build file is created or re-created successfully
185 # @retval FALSE The build file exists and is the same as the one to be generated
186 #
187 def Generate(self, FileType=gMakeType):
188 if FileType not in self._FILE_NAME_:
189 EdkLogger.error("build", PARAMETER_INVALID, "Invalid build type [%s]" % FileType,
190 ExtraData="[%s]" % str(self._AutoGenObject))
191 self._FileType = FileType
192 FileContent = self._TEMPLATE_.Replace(self._TemplateDict)
193 FileName = self._FILE_NAME_[FileType]
194 return SaveFileOnChange(os.path.join(self._AutoGenObject.MakeFileDir, FileName), FileContent, False)
195
196 ## Return a list of directory creation command string
197 #
198 # @param DirList The list of directory to be created
199 #
200 # @retval list The directory creation command list
201 #
202 def GetCreateDirectoryCommand(self, DirList):
203 return [self._MD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
204
205 ## Return a list of directory removal command string
206 #
207 # @param DirList The list of directory to be removed
208 #
209 # @retval list The directory removal command list
210 #
211 def GetRemoveDirectoryCommand(self, DirList):
212 return [self._RD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
213
214 def PlaceMacro(self, Path, MacroDefinitions={}):
215 if Path.startswith("$("):
216 return Path
217 else:
218 PathLength = len(Path)
219 for MacroName in MacroDefinitions:
220 MacroValue = MacroDefinitions[MacroName]
221 MacroValueLength = len(MacroValue)
222 if MacroValueLength == 0:
223 continue
224 if MacroValueLength <= PathLength and Path.startswith(MacroValue):
225 Path = "$(%s)%s" % (MacroName, Path[MacroValueLength:])
226 break
227 return Path
228
229 ## ModuleMakefile class
230 #
231 # This class encapsules makefie and its generation for module. It uses template to generate
232 # the content of makefile. The content of makefile will be got from ModuleAutoGen object.
233 #
234 class ModuleMakefile(BuildFile):
235 ## template used to generate the makefile for module
236 _TEMPLATE_ = TemplateString('''\
237 ${makefile_header}
238
239 #
240 # Platform Macro Definition
241 #
242 PLATFORM_NAME = ${platform_name}
243 PLATFORM_GUID = ${platform_guid}
244 PLATFORM_VERSION = ${platform_version}
245 PLATFORM_RELATIVE_DIR = ${platform_relative_directory}
246 PLATFORM_DIR = ${platform_dir}
247 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
248
249 #
250 # Module Macro Definition
251 #
252 MODULE_NAME = ${module_name}
253 MODULE_GUID = ${module_guid}
254 MODULE_NAME_GUID = ${module_name_guid}
255 MODULE_VERSION = ${module_version}
256 MODULE_TYPE = ${module_type}
257 MODULE_FILE = ${module_file}
258 MODULE_FILE_BASE_NAME = ${module_file_base_name}
259 BASE_NAME = $(MODULE_NAME)
260 MODULE_RELATIVE_DIR = ${module_relative_directory}
261 PACKAGE_RELATIVE_DIR = ${package_relative_directory}
262 MODULE_DIR = ${module_dir}
263 FFS_OUTPUT_DIR = ${ffs_output_directory}
264
265 MODULE_ENTRY_POINT = ${module_entry_point}
266 ARCH_ENTRY_POINT = ${arch_entry_point}
267 IMAGE_ENTRY_POINT = ${image_entry_point}
268
269 ${BEGIN}${module_extra_defines}
270 ${END}
271 #
272 # Build Configuration Macro Definition
273 #
274 ARCH = ${architecture}
275 TOOLCHAIN = ${toolchain_tag}
276 TOOLCHAIN_TAG = ${toolchain_tag}
277 TARGET = ${build_target}
278
279 #
280 # Build Directory Macro Definition
281 #
282 # PLATFORM_BUILD_DIR = ${platform_build_directory}
283 BUILD_DIR = ${platform_build_directory}
284 BIN_DIR = $(BUILD_DIR)${separator}${architecture}
285 LIB_DIR = $(BIN_DIR)
286 MODULE_BUILD_DIR = ${module_build_directory}
287 OUTPUT_DIR = ${module_output_directory}
288 DEBUG_DIR = ${module_debug_directory}
289 DEST_DIR_OUTPUT = $(OUTPUT_DIR)
290 DEST_DIR_DEBUG = $(DEBUG_DIR)
291
292 #
293 # Shell Command Macro
294 #
295 ${BEGIN}${shell_command_code} = ${shell_command}
296 ${END}
297
298 #
299 # Tools definitions specific to this module
300 #
301 ${BEGIN}${module_tool_definitions}
302 ${END}
303 MAKE_FILE = ${makefile_path}
304
305 #
306 # Build Macro
307 #
308 ${BEGIN}${file_macro}
309 ${END}
310
311 COMMON_DEPS = ${BEGIN}${common_dependency_file} \\
312 ${END}
313
314 #
315 # Overridable Target Macro Definitions
316 #
317 FORCE_REBUILD = force_build
318 INIT_TARGET = init
319 PCH_TARGET =
320 BC_TARGET = ${BEGIN}${backward_compatible_target} ${END}
321 CODA_TARGET = ${BEGIN}${remaining_build_target} \\
322 ${END}
323
324 #
325 # Default target, which will build dependent libraries in addition to source files
326 #
327
328 all: mbuild
329
330
331 #
332 # Target used when called from platform makefile, which will bypass the build of dependent libraries
333 #
334
335 pbuild: $(INIT_TARGET) $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
336
337 #
338 # ModuleTarget
339 #
340
341 mbuild: $(INIT_TARGET) $(BC_TARGET) gen_libs $(PCH_TARGET) $(CODA_TARGET)
342
343 #
344 # Build Target used in multi-thread build mode, which will bypass the init and gen_libs targets
345 #
346
347 tbuild: $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
348
349 #
350 # Phony target which is used to force executing commands for a target
351 #
352 force_build:
353 \t-@
354
355 #
356 # Target to update the FD
357 #
358
359 fds: mbuild gen_fds
360
361 #
362 # Initialization target: print build information and create necessary directories
363 #
364 init: info dirs
365
366 info:
367 \t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]
368
369 dirs:
370 ${BEGIN}\t-@${create_directory_command}\n${END}
371
372 strdefs:
373 \t-@$(CP) $(DEBUG_DIR)${separator}AutoGen.h $(DEBUG_DIR)${separator}$(MODULE_NAME)StrDefs.h
374
375 #
376 # GenLibsTarget
377 #
378 gen_libs:
379 \t${BEGIN}@"$(MAKE)" $(MAKE_FLAGS) -f ${dependent_library_build_directory}${separator}${makefile_name}
380 \t${END}@cd $(MODULE_BUILD_DIR)
381
382 #
383 # Build Flash Device Image
384 #
385 gen_fds:
386 \t@"$(MAKE)" $(MAKE_FLAGS) -f $(BUILD_DIR)${separator}${makefile_name} fds
387 \t@cd $(MODULE_BUILD_DIR)
388
389 #
390 # Individual Object Build Targets
391 #
392 ${BEGIN}${file_build_target}
393 ${END}
394
395 #
396 # clean all intermediate files
397 #
398 clean:
399 \t${BEGIN}${clean_command}
400 \t${END}\t$(RM) AutoGenTimeStamp
401
402 #
403 # clean all generated files
404 #
405 cleanall:
406 ${BEGIN}\t${cleanall_command}
407 ${END}\t$(RM) *.pdb *.idb > NUL 2>&1
408 \t$(RM) $(BIN_DIR)${separator}$(MODULE_NAME).efi
409 \t$(RM) AutoGenTimeStamp
410
411 #
412 # clean all dependent libraries built
413 #
414 cleanlib:
415 \t${BEGIN}-@${library_build_command} cleanall
416 \t${END}@cd $(MODULE_BUILD_DIR)\n\n''')
417
418 _FILE_MACRO_TEMPLATE = TemplateString("${macro_name} = ${BEGIN} \\\n ${source_file}${END}\n")
419 _BUILD_TARGET_TEMPLATE = TemplateString("${BEGIN}${target} : ${deps}\n${END}\t${cmd}\n")
420
421 ## Constructor of ModuleMakefile
422 #
423 # @param ModuleAutoGen Object of ModuleAutoGen class
424 #
425 def __init__(self, ModuleAutoGen):
426 BuildFile.__init__(self, ModuleAutoGen)
427 self.PlatformInfo = self._AutoGenObject.PlatformInfo
428
429 self.ResultFileList = []
430 self.IntermediateDirectoryList = ["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]
431
432 self.FileBuildTargetList = [] # [(src, target string)]
433 self.BuildTargetList = [] # [target string]
434 self.PendingBuildTargetList = [] # [FileBuildRule objects]
435 self.CommonFileDependency = []
436 self.FileListMacros = {}
437 self.ListFileMacros = {}
438
439 self.FileCache = {}
440 self.LibraryBuildCommandList = []
441 self.LibraryFileList = []
442 self.LibraryMakefileList = []
443 self.LibraryBuildDirectoryList = []
444 self.SystemLibraryList = []
445 self.Macros = OrderedDict()
446 self.Macros["OUTPUT_DIR" ] = self._AutoGenObject.Macros["OUTPUT_DIR"]
447 self.Macros["DEBUG_DIR" ] = self._AutoGenObject.Macros["DEBUG_DIR"]
448 self.Macros["MODULE_BUILD_DIR"] = self._AutoGenObject.Macros["MODULE_BUILD_DIR"]
449 self.Macros["BIN_DIR" ] = self._AutoGenObject.Macros["BIN_DIR"]
450 self.Macros["BUILD_DIR" ] = self._AutoGenObject.Macros["BUILD_DIR"]
451 self.Macros["WORKSPACE" ] = self._AutoGenObject.Macros["WORKSPACE"]
452 self.Macros["FFS_OUTPUT_DIR" ] = self._AutoGenObject.Macros["FFS_OUTPUT_DIR"]
453 self.GenFfsList = ModuleAutoGen.GenFfsList
454 self.MacroList = ['FFS_OUTPUT_DIR', 'MODULE_GUID', 'OUTPUT_DIR']
455 self.FfsOutputFileList = []
456
457 # Compose a dict object containing information used to do replacement in template
458 @property
459 def _TemplateDict(self):
460 if self._FileType not in self._SEP_:
461 EdkLogger.error("build", PARAMETER_INVALID, "Invalid Makefile type [%s]" % self._FileType,
462 ExtraData="[%s]" % str(self._AutoGenObject))
463 MyAgo = self._AutoGenObject
464 Separator = self._SEP_[self._FileType]
465
466 # break build if no source files and binary files are found
467 if len(MyAgo.SourceFileList) == 0 and len(MyAgo.BinaryFileList) == 0:
468 EdkLogger.error("build", AUTOGEN_ERROR, "No files to be built in module [%s, %s, %s]"
469 % (MyAgo.BuildTarget, MyAgo.ToolChain, MyAgo.Arch),
470 ExtraData="[%s]" % str(MyAgo))
471
472 # convert dependent libraries to build command
473 self.ProcessDependentLibrary()
474 if len(MyAgo.Module.ModuleEntryPointList) > 0:
475 ModuleEntryPoint = MyAgo.Module.ModuleEntryPointList[0]
476 else:
477 ModuleEntryPoint = "_ModuleEntryPoint"
478
479 # Intel EBC compiler enforces EfiMain
480 if MyAgo.AutoGenVersion < 0x00010005 and MyAgo.Arch == "EBC":
481 ArchEntryPoint = "EfiMain"
482 else:
483 ArchEntryPoint = ModuleEntryPoint
484
485 if MyAgo.Arch == "EBC":
486 # EBC compiler always use "EfiStart" as entry point. Only applies to EdkII modules
487 ImageEntryPoint = "EfiStart"
488 elif MyAgo.AutoGenVersion < 0x00010005:
489 # Edk modules use entry point specified in INF file
490 ImageEntryPoint = ModuleEntryPoint
491 else:
492 # EdkII modules always use "_ModuleEntryPoint" as entry point
493 ImageEntryPoint = "_ModuleEntryPoint"
494
495 for k, v in MyAgo.Module.Defines.iteritems():
496 if k not in MyAgo.Macros:
497 MyAgo.Macros[k] = v
498
499 if 'MODULE_ENTRY_POINT' not in MyAgo.Macros:
500 MyAgo.Macros['MODULE_ENTRY_POINT'] = ModuleEntryPoint
501 if 'ARCH_ENTRY_POINT' not in MyAgo.Macros:
502 MyAgo.Macros['ARCH_ENTRY_POINT'] = ArchEntryPoint
503 if 'IMAGE_ENTRY_POINT' not in MyAgo.Macros:
504 MyAgo.Macros['IMAGE_ENTRY_POINT'] = ImageEntryPoint
505
506 PCI_COMPRESS_Flag = False
507 for k, v in MyAgo.Module.Defines.iteritems():
508 if 'PCI_COMPRESS' == k and 'TRUE' == v:
509 PCI_COMPRESS_Flag = True
510
511 # tools definitions
512 ToolsDef = []
513 IncPrefix = self._INC_FLAG_[MyAgo.ToolChainFamily]
514 for Tool in MyAgo.BuildOption:
515 for Attr in MyAgo.BuildOption[Tool]:
516 Value = MyAgo.BuildOption[Tool][Attr]
517 if Attr == "FAMILY":
518 continue
519 elif Attr == "PATH":
520 ToolsDef.append("%s = %s" % (Tool, Value))
521 else:
522 # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
523 if Tool == "MAKE":
524 continue
525 # Remove duplicated include path, if any
526 if Attr == "FLAGS":
527 Value = RemoveDupOption(Value, IncPrefix, MyAgo.IncludePathList)
528 if Tool == "OPTROM" and PCI_COMPRESS_Flag:
529 ValueList = Value.split()
530 if ValueList:
531 for i, v in enumerate(ValueList):
532 if '-e' == v:
533 ValueList[i] = '-ec'
534 Value = ' '.join(ValueList)
535
536 ToolsDef.append("%s_%s = %s" % (Tool, Attr, Value))
537 ToolsDef.append("")
538
539 # generate the Response file and Response flag
540 RespDict = self.CommandExceedLimit()
541 RespFileList = os.path.join(MyAgo.OutputDir, 'respfilelist.txt')
542 if RespDict:
543 RespFileListContent = ''
544 for Resp in RespDict:
545 RespFile = os.path.join(MyAgo.OutputDir, str(Resp).lower() + '.txt')
546 StrList = RespDict[Resp].split(' ')
547 UnexpandMacro = []
548 NewStr = []
549 for Str in StrList:
550 if '$' in Str:
551 UnexpandMacro.append(Str)
552 else:
553 NewStr.append(Str)
554 UnexpandMacroStr = ' '.join(UnexpandMacro)
555 NewRespStr = ' '.join(NewStr)
556 SaveFileOnChange(RespFile, NewRespStr, False)
557 ToolsDef.append("%s = %s" % (Resp, UnexpandMacroStr + ' @' + RespFile))
558 RespFileListContent += '@' + RespFile + os.linesep
559 RespFileListContent += NewRespStr + os.linesep
560 SaveFileOnChange(RespFileList, RespFileListContent, False)
561 else:
562 if os.path.exists(RespFileList):
563 os.remove(RespFileList)
564
565 # convert source files and binary files to build targets
566 self.ResultFileList = [str(T.Target) for T in MyAgo.CodaTargetList]
567 if len(self.ResultFileList) == 0 and len(MyAgo.SourceFileList) != 0:
568 EdkLogger.error("build", AUTOGEN_ERROR, "Nothing to build",
569 ExtraData="[%s]" % str(MyAgo))
570
571 self.ProcessBuildTargetList()
572 self.ParserGenerateFfsCmd()
573
574 # Generate macros used to represent input files
575 FileMacroList = [] # macro name = file list
576 for FileListMacro in self.FileListMacros:
577 FileMacro = self._FILE_MACRO_TEMPLATE.Replace(
578 {
579 "macro_name" : FileListMacro,
580 "source_file" : self.FileListMacros[FileListMacro]
581 }
582 )
583 FileMacroList.append(FileMacro)
584
585 # INC_LIST is special
586 FileMacro = ""
587 IncludePathList = []
588 for P in MyAgo.IncludePathList:
589 IncludePathList.append(IncPrefix + self.PlaceMacro(P, self.Macros))
590 if FileBuildRule.INC_LIST_MACRO in self.ListFileMacros:
591 self.ListFileMacros[FileBuildRule.INC_LIST_MACRO].append(IncPrefix + P)
592 FileMacro += self._FILE_MACRO_TEMPLATE.Replace(
593 {
594 "macro_name" : "INC",
595 "source_file" : IncludePathList
596 }
597 )
598 FileMacroList.append(FileMacro)
599
600 # Generate macros used to represent files containing list of input files
601 for ListFileMacro in self.ListFileMacros:
602 ListFileName = os.path.join(MyAgo.OutputDir, "%s.lst" % ListFileMacro.lower()[:len(ListFileMacro) - 5])
603 FileMacroList.append("%s = %s" % (ListFileMacro, ListFileName))
604 SaveFileOnChange(
605 ListFileName,
606 "\n".join(self.ListFileMacros[ListFileMacro]),
607 False
608 )
609
610 # Edk modules need <BaseName>StrDefs.h for string ID
611 #if MyAgo.AutoGenVersion < 0x00010005 and len(MyAgo.UnicodeFileList) > 0:
612 # BcTargetList = ['strdefs']
613 #else:
614 # BcTargetList = []
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.iteritems()],
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" : self._SHELL_CMD_[self._FileType].keys(),
673 "shell_command" : 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 separater, 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 dependecies 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, 'r')
1036 except BaseException as X:
1037 EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=F.Path + "\n\t" + str(X))
1038
1039 FileContent = Fd.read()
1040 Fd.close()
1041 if len(FileContent) == 0:
1042 continue
1043
1044 if FileContent[0] == 0xff or FileContent[0] == 0xfe:
1045 FileContent = unicode(FileContent, "utf-16")
1046 IncludedFileList = gIncludePattern.findall(FileContent)
1047
1048 for Inc in IncludedFileList:
1049 Inc = Inc.strip()
1050 # if there's macro used to reference header file, expand it
1051 HeaderList = gMacroPattern.findall(Inc)
1052 if len(HeaderList) == 1 and len(HeaderList[0]) == 2:
1053 HeaderType = HeaderList[0][0]
1054 HeaderKey = HeaderList[0][1]
1055 if HeaderType in gIncludeMacroConversion:
1056 Inc = gIncludeMacroConversion[HeaderType] % {"HeaderKey" : HeaderKey}
1057 else:
1058 # not known macro used in #include, always build the file by
1059 # returning a empty dependency
1060 self.FileCache[File] = []
1061 return []
1062 Inc = os.path.normpath(Inc)
1063 CurrentFileDependencyList.append(Inc)
1064 DepDb[F] = CurrentFileDependencyList
1065
1066 CurrentFilePath = F.Dir
1067 PathList = [CurrentFilePath] + SearchPathList
1068 for Inc in CurrentFileDependencyList:
1069 for SearchPath in PathList:
1070 FilePath = os.path.join(SearchPath, Inc)
1071 if FilePath in gIsFileMap:
1072 if not gIsFileMap[FilePath]:
1073 continue
1074 # If isfile is called too many times, the performance is slow down.
1075 elif not os.path.isfile(FilePath):
1076 gIsFileMap[FilePath] = False
1077 continue
1078 else:
1079 gIsFileMap[FilePath] = True
1080 FilePath = PathClass(FilePath)
1081 FullPathDependList.append(FilePath)
1082 if FilePath not in DependencySet:
1083 FileStack.append(FilePath)
1084 break
1085 else:
1086 EdkLogger.debug(EdkLogger.DEBUG_9, "%s included by %s was not found "\
1087 "in any given path:\n\t%s" % (Inc, F, "\n\t".join(SearchPathList)))
1088
1089 self.FileCache[F] = FullPathDependList
1090 DependencySet.update(FullPathDependList)
1091
1092 DependencySet.update(ForceList)
1093 if File in DependencySet:
1094 DependencySet.remove(File)
1095 DependencyList = list(DependencySet) # remove duplicate ones
1096
1097 return DependencyList
1098
1099 ## CustomMakefile class
1100 #
1101 # This class encapsules makefie and its generation for module. It uses template to generate
1102 # the content of makefile. The content of makefile will be got from ModuleAutoGen object.
1103 #
1104 class CustomMakefile(BuildFile):
1105 ## template used to generate the makefile for module with custom makefile
1106 _TEMPLATE_ = TemplateString('''\
1107 ${makefile_header}
1108
1109 #
1110 # Platform Macro Definition
1111 #
1112 PLATFORM_NAME = ${platform_name}
1113 PLATFORM_GUID = ${platform_guid}
1114 PLATFORM_VERSION = ${platform_version}
1115 PLATFORM_RELATIVE_DIR = ${platform_relative_directory}
1116 PLATFORM_DIR = ${platform_dir}
1117 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
1118
1119 #
1120 # Module Macro Definition
1121 #
1122 MODULE_NAME = ${module_name}
1123 MODULE_GUID = ${module_guid}
1124 MODULE_NAME_GUID = ${module_name_guid}
1125 MODULE_VERSION = ${module_version}
1126 MODULE_TYPE = ${module_type}
1127 MODULE_FILE = ${module_file}
1128 MODULE_FILE_BASE_NAME = ${module_file_base_name}
1129 BASE_NAME = $(MODULE_NAME)
1130 MODULE_RELATIVE_DIR = ${module_relative_directory}
1131 MODULE_DIR = ${module_dir}
1132
1133 #
1134 # Build Configuration Macro Definition
1135 #
1136 ARCH = ${architecture}
1137 TOOLCHAIN = ${toolchain_tag}
1138 TOOLCHAIN_TAG = ${toolchain_tag}
1139 TARGET = ${build_target}
1140
1141 #
1142 # Build Directory Macro Definition
1143 #
1144 # PLATFORM_BUILD_DIR = ${platform_build_directory}
1145 BUILD_DIR = ${platform_build_directory}
1146 BIN_DIR = $(BUILD_DIR)${separator}${architecture}
1147 LIB_DIR = $(BIN_DIR)
1148 MODULE_BUILD_DIR = ${module_build_directory}
1149 OUTPUT_DIR = ${module_output_directory}
1150 DEBUG_DIR = ${module_debug_directory}
1151 DEST_DIR_OUTPUT = $(OUTPUT_DIR)
1152 DEST_DIR_DEBUG = $(DEBUG_DIR)
1153
1154 #
1155 # Tools definitions specific to this module
1156 #
1157 ${BEGIN}${module_tool_definitions}
1158 ${END}
1159 MAKE_FILE = ${makefile_path}
1160
1161 #
1162 # Shell Command Macro
1163 #
1164 ${BEGIN}${shell_command_code} = ${shell_command}
1165 ${END}
1166
1167 ${custom_makefile_content}
1168
1169 #
1170 # Target used when called from platform makefile, which will bypass the build of dependent libraries
1171 #
1172
1173 pbuild: init all
1174
1175
1176 #
1177 # ModuleTarget
1178 #
1179
1180 mbuild: init all
1181
1182 #
1183 # Build Target used in multi-thread build mode, which no init target is needed
1184 #
1185
1186 tbuild: all
1187
1188 #
1189 # Initialization target: print build information and create necessary directories
1190 #
1191 init:
1192 \t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]
1193 ${BEGIN}\t-@${create_directory_command}\n${END}\
1194
1195 ''')
1196
1197 ## Constructor of CustomMakefile
1198 #
1199 # @param ModuleAutoGen Object of ModuleAutoGen class
1200 #
1201 def __init__(self, ModuleAutoGen):
1202 BuildFile.__init__(self, ModuleAutoGen)
1203 self.PlatformInfo = self._AutoGenObject.PlatformInfo
1204 self.IntermediateDirectoryList = ["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]
1205
1206 # Compose a dict object containing information used to do replacement in template
1207 @property
1208 def _TemplateDict(self):
1209 Separator = self._SEP_[self._FileType]
1210 MyAgo = self._AutoGenObject
1211 if self._FileType not in MyAgo.CustomMakefile:
1212 EdkLogger.error('build', OPTION_NOT_SUPPORTED, "No custom makefile for %s" % self._FileType,
1213 ExtraData="[%s]" % str(MyAgo))
1214 MakefilePath = mws.join(
1215 MyAgo.WorkspaceDir,
1216 MyAgo.CustomMakefile[self._FileType]
1217 )
1218 try:
1219 CustomMakefile = open(MakefilePath, 'r').read()
1220 except:
1221 EdkLogger.error('build', FILE_OPEN_FAILURE, File=str(MyAgo),
1222 ExtraData=MyAgo.CustomMakefile[self._FileType])
1223
1224 # tools definitions
1225 ToolsDef = []
1226 for Tool in MyAgo.BuildOption:
1227 # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
1228 if Tool == "MAKE":
1229 continue
1230 for Attr in MyAgo.BuildOption[Tool]:
1231 if Attr == "FAMILY":
1232 continue
1233 elif Attr == "PATH":
1234 ToolsDef.append("%s = %s" % (Tool, MyAgo.BuildOption[Tool][Attr]))
1235 else:
1236 ToolsDef.append("%s_%s = %s" % (Tool, Attr, MyAgo.BuildOption[Tool][Attr]))
1237 ToolsDef.append("")
1238
1239 MakefileName = self._FILE_NAME_[self._FileType]
1240 MakefileTemplateDict = {
1241 "makefile_header" : self._FILE_HEADER_[self._FileType],
1242 "makefile_path" : os.path.join("$(MODULE_BUILD_DIR)", MakefileName),
1243 "platform_name" : self.PlatformInfo.Name,
1244 "platform_guid" : self.PlatformInfo.Guid,
1245 "platform_version" : self.PlatformInfo.Version,
1246 "platform_relative_directory": self.PlatformInfo.SourceDir,
1247 "platform_output_directory" : self.PlatformInfo.OutputDir,
1248 "platform_dir" : MyAgo.Macros["PLATFORM_DIR"],
1249
1250 "module_name" : MyAgo.Name,
1251 "module_guid" : MyAgo.Guid,
1252 "module_name_guid" : MyAgo.UniqueBaseName,
1253 "module_version" : MyAgo.Version,
1254 "module_type" : MyAgo.ModuleType,
1255 "module_file" : MyAgo.MetaFile,
1256 "module_file_base_name" : MyAgo.MetaFile.BaseName,
1257 "module_relative_directory" : MyAgo.SourceDir,
1258 "module_dir" : mws.join (MyAgo.WorkspaceDir, MyAgo.SourceDir),
1259
1260 "architecture" : MyAgo.Arch,
1261 "toolchain_tag" : MyAgo.ToolChain,
1262 "build_target" : MyAgo.BuildTarget,
1263
1264 "platform_build_directory" : self.PlatformInfo.BuildDir,
1265 "module_build_directory" : MyAgo.BuildDir,
1266 "module_output_directory" : MyAgo.OutputDir,
1267 "module_debug_directory" : MyAgo.DebugDir,
1268
1269 "separator" : Separator,
1270 "module_tool_definitions" : ToolsDef,
1271
1272 "shell_command_code" : self._SHELL_CMD_[self._FileType].keys(),
1273 "shell_command" : self._SHELL_CMD_[self._FileType].values(),
1274
1275 "create_directory_command" : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),
1276 "custom_makefile_content" : CustomMakefile
1277 }
1278
1279 return MakefileTemplateDict
1280
1281 ## PlatformMakefile class
1282 #
1283 # This class encapsules makefie and its generation for platform. It uses
1284 # template to generate the content of makefile. The content of makefile will be
1285 # got from PlatformAutoGen object.
1286 #
1287 class PlatformMakefile(BuildFile):
1288 ## template used to generate the makefile for platform
1289 _TEMPLATE_ = TemplateString('''\
1290 ${makefile_header}
1291
1292 #
1293 # Platform Macro Definition
1294 #
1295 PLATFORM_NAME = ${platform_name}
1296 PLATFORM_GUID = ${platform_guid}
1297 PLATFORM_VERSION = ${platform_version}
1298 PLATFORM_FILE = ${platform_file}
1299 PLATFORM_DIR = ${platform_dir}
1300 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
1301
1302 #
1303 # Build Configuration Macro Definition
1304 #
1305 TOOLCHAIN = ${toolchain_tag}
1306 TOOLCHAIN_TAG = ${toolchain_tag}
1307 TARGET = ${build_target}
1308
1309 #
1310 # Build Directory Macro Definition
1311 #
1312 BUILD_DIR = ${platform_build_directory}
1313 FV_DIR = ${platform_build_directory}${separator}FV
1314
1315 #
1316 # Shell Command Macro
1317 #
1318 ${BEGIN}${shell_command_code} = ${shell_command}
1319 ${END}
1320
1321 MAKE = ${make_path}
1322 MAKE_FILE = ${makefile_path}
1323
1324 #
1325 # Default target
1326 #
1327 all: init build_libraries build_modules
1328
1329 #
1330 # Initialization target: print build information and create necessary directories
1331 #
1332 init:
1333 \t-@echo Building ... $(PLATFORM_FILE) [${build_architecture_list}]
1334 \t${BEGIN}-@${create_directory_command}
1335 \t${END}
1336 #
1337 # library build target
1338 #
1339 libraries: init build_libraries
1340
1341 #
1342 # module build target
1343 #
1344 modules: init build_libraries build_modules
1345
1346 #
1347 # Build all libraries:
1348 #
1349 build_libraries:
1350 ${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${library_makefile_list} pbuild
1351 ${END}\t@cd $(BUILD_DIR)
1352
1353 #
1354 # Build all modules:
1355 #
1356 build_modules:
1357 ${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${module_makefile_list} pbuild
1358 ${END}\t@cd $(BUILD_DIR)
1359
1360 #
1361 # Clean intermediate files
1362 #
1363 clean:
1364 \t${BEGIN}-@${library_build_command} clean
1365 \t${END}${BEGIN}-@${module_build_command} clean
1366 \t${END}@cd $(BUILD_DIR)
1367
1368 #
1369 # Clean all generated files except to makefile
1370 #
1371 cleanall:
1372 ${BEGIN}\t${cleanall_command}
1373 ${END}
1374
1375 #
1376 # Clean all library files
1377 #
1378 cleanlib:
1379 \t${BEGIN}-@${library_build_command} cleanall
1380 \t${END}@cd $(BUILD_DIR)\n
1381 ''')
1382
1383 ## Constructor of PlatformMakefile
1384 #
1385 # @param ModuleAutoGen Object of PlatformAutoGen class
1386 #
1387 def __init__(self, PlatformAutoGen):
1388 BuildFile.__init__(self, PlatformAutoGen)
1389 self.ModuleBuildCommandList = []
1390 self.ModuleMakefileList = []
1391 self.IntermediateDirectoryList = []
1392 self.ModuleBuildDirectoryList = []
1393 self.LibraryBuildDirectoryList = []
1394 self.LibraryMakeCommandList = []
1395
1396 # Compose a dict object containing information used to do replacement in template
1397 @property
1398 def _TemplateDict(self):
1399 Separator = self._SEP_[self._FileType]
1400
1401 MyAgo = self._AutoGenObject
1402 if "MAKE" not in MyAgo.ToolDefinition or "PATH" not in MyAgo.ToolDefinition["MAKE"]:
1403 EdkLogger.error("build", OPTION_MISSING, "No MAKE command defined. Please check your tools_def.txt!",
1404 ExtraData="[%s]" % str(MyAgo))
1405
1406 self.IntermediateDirectoryList = ["$(BUILD_DIR)"]
1407 self.ModuleBuildDirectoryList = self.GetModuleBuildDirectoryList()
1408 self.LibraryBuildDirectoryList = self.GetLibraryBuildDirectoryList()
1409
1410 MakefileName = self._FILE_NAME_[self._FileType]
1411 LibraryMakefileList = []
1412 LibraryMakeCommandList = []
1413 for D in self.LibraryBuildDirectoryList:
1414 D = self.PlaceMacro(D, {"BUILD_DIR":MyAgo.BuildDir})
1415 Makefile = os.path.join(D, MakefileName)
1416 Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":Makefile}
1417 LibraryMakefileList.append(Makefile)
1418 LibraryMakeCommandList.append(Command)
1419 self.LibraryMakeCommandList = LibraryMakeCommandList
1420
1421 ModuleMakefileList = []
1422 ModuleMakeCommandList = []
1423 for D in self.ModuleBuildDirectoryList:
1424 D = self.PlaceMacro(D, {"BUILD_DIR":MyAgo.BuildDir})
1425 Makefile = os.path.join(D, MakefileName)
1426 Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":Makefile}
1427 ModuleMakefileList.append(Makefile)
1428 ModuleMakeCommandList.append(Command)
1429
1430 MakefileTemplateDict = {
1431 "makefile_header" : self._FILE_HEADER_[self._FileType],
1432 "makefile_path" : os.path.join("$(BUILD_DIR)", MakefileName),
1433 "make_path" : MyAgo.ToolDefinition["MAKE"]["PATH"],
1434 "makefile_name" : MakefileName,
1435 "platform_name" : MyAgo.Name,
1436 "platform_guid" : MyAgo.Guid,
1437 "platform_version" : MyAgo.Version,
1438 "platform_file" : MyAgo.MetaFile,
1439 "platform_relative_directory": MyAgo.SourceDir,
1440 "platform_output_directory" : MyAgo.OutputDir,
1441 "platform_build_directory" : MyAgo.BuildDir,
1442 "platform_dir" : MyAgo.Macros["PLATFORM_DIR"],
1443
1444 "toolchain_tag" : MyAgo.ToolChain,
1445 "build_target" : MyAgo.BuildTarget,
1446 "shell_command_code" : self._SHELL_CMD_[self._FileType].keys(),
1447 "shell_command" : self._SHELL_CMD_[self._FileType].values(),
1448 "build_architecture_list" : MyAgo.Arch,
1449 "architecture" : MyAgo.Arch,
1450 "separator" : Separator,
1451 "create_directory_command" : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),
1452 "cleanall_command" : self.GetRemoveDirectoryCommand(self.IntermediateDirectoryList),
1453 "library_makefile_list" : LibraryMakefileList,
1454 "module_makefile_list" : ModuleMakefileList,
1455 "library_build_command" : LibraryMakeCommandList,
1456 "module_build_command" : ModuleMakeCommandList,
1457 }
1458
1459 return MakefileTemplateDict
1460
1461 ## Get the root directory list for intermediate files of all modules build
1462 #
1463 # @retval list The list of directory
1464 #
1465 def GetModuleBuildDirectoryList(self):
1466 DirList = []
1467 for ModuleAutoGen in self._AutoGenObject.ModuleAutoGenList:
1468 if not ModuleAutoGen.IsBinaryModule:
1469 DirList.append(os.path.join(self._AutoGenObject.BuildDir, ModuleAutoGen.BuildDir))
1470 return DirList
1471
1472 ## Get the root directory list for intermediate files of all libraries build
1473 #
1474 # @retval list The list of directory
1475 #
1476 def GetLibraryBuildDirectoryList(self):
1477 DirList = []
1478 for LibraryAutoGen in self._AutoGenObject.LibraryAutoGenList:
1479 if not LibraryAutoGen.IsBinaryModule:
1480 DirList.append(os.path.join(self._AutoGenObject.BuildDir, LibraryAutoGen.BuildDir))
1481 return DirList
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 @property
1503 def _TemplateDict(self):
1504 Separator = self._SEP_[self._FileType]
1505
1506 # any platform autogen object is ok because we just need common information
1507 MyAgo = self._AutoGenObject
1508
1509 if "MAKE" not in MyAgo.ToolDefinition or "PATH" not in MyAgo.ToolDefinition["MAKE"]:
1510 EdkLogger.error("build", OPTION_MISSING, "No MAKE command defined. Please check your tools_def.txt!",
1511 ExtraData="[%s]" % str(MyAgo))
1512
1513 for Arch in MyAgo.ArchList:
1514 self.IntermediateDirectoryList.append(Separator.join(["$(BUILD_DIR)", Arch]))
1515 self.IntermediateDirectoryList.append("$(FV_DIR)")
1516
1517 # TRICK: for not generating GenFds call in makefile if no FDF file
1518 MacroList = []
1519 if MyAgo.FdfFile is not None and MyAgo.FdfFile != "":
1520 FdfFileList = [MyAgo.FdfFile]
1521 # macros passed to GenFds
1522 MacroList.append('"%s=%s"' % ("EFI_SOURCE", GlobalData.gEfiSource.replace('\\', '\\\\')))
1523 MacroList.append('"%s=%s"' % ("EDK_SOURCE", GlobalData.gEdkSource.replace('\\', '\\\\')))
1524 MacroDict = {}
1525 MacroDict.update(GlobalData.gGlobalDefines)
1526 MacroDict.update(GlobalData.gCommandLineDefines)
1527 MacroDict.pop("EFI_SOURCE", "dummy")
1528 MacroDict.pop("EDK_SOURCE", "dummy")
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" : self._SHELL_CMD_[self._FileType].keys(),
1583 "shell_command" : 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