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