]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Scripts/RunMakefile.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Scripts / RunMakefile.py
1 ## @file
2 # Run a makefile as part of a PREBUILD or POSTBUILD action.
3 #
4 # Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
6 #
7
8 '''
9 RunMakefile.py
10 '''
11
12 import os
13 import sys
14 import argparse
15 import subprocess
16
17 #
18 # Globals for help information
19 #
20 __prog__ = 'RunMakefile'
21 __version__ = '%s Version %s' % (__prog__, '1.0')
22 __copyright__ = 'Copyright (c) 2017, Intel Corporation. All rights reserved.'
23 __description__ = 'Run a makefile as part of a PREBUILD or POSTBUILD action.\n'
24
25 #
26 # Globals
27 #
28 gArgs = None
29
30 def Log(Message):
31 if not gArgs.Verbose:
32 return
33 sys.stdout.write (__prog__ + ': ' + Message + '\n')
34
35 def Error(Message, ExitValue=1):
36 sys.stderr.write (__prog__ + ': ERROR: ' + Message + '\n')
37 sys.exit (ExitValue)
38
39 def RelativePath(target):
40 return os.path.relpath (target, gWorkspace)
41
42 def NormalizePath(target):
43 if isinstance(target, tuple):
44 return os.path.normpath (os.path.join (*target))
45 else:
46 return os.path.normpath (target)
47
48 if __name__ == '__main__':
49 #
50 # Create command line argument parser object
51 #
52 parser = argparse.ArgumentParser (
53 prog = __prog__,
54 version = __version__,
55 description = __description__ + __copyright__,
56 conflict_handler = 'resolve'
57 )
58 parser.add_argument (
59 '-a', '--arch', dest = 'Arch', nargs = '+', action = 'append',
60 required = True,
61 help = '''ARCHS is one of list: IA32, X64, IPF, ARM, AARCH64 or EBC,
62 which overrides target.txt's TARGET_ARCH definition. To
63 specify more archs, please repeat this option.'''
64 )
65 parser.add_argument (
66 '-t', '--tagname', dest = 'ToolChain', required = True,
67 help = '''Using the Tool Chain Tagname to build the platform,
68 overriding target.txt's TOOL_CHAIN_TAG definition.'''
69 )
70 parser.add_argument (
71 '-p', '--platform', dest = 'PlatformFile', required = True,
72 help = '''Build the platform specified by the DSC file name argument,
73 overriding target.txt's ACTIVE_PLATFORM definition.'''
74 )
75 parser.add_argument (
76 '-b', '--buildtarget', dest = 'BuildTarget', required = True,
77 help = '''Using the TARGET to build the platform, overriding
78 target.txt's TARGET definition.'''
79 )
80 parser.add_argument (
81 '--conf=', dest = 'ConfDirectory', required = True,
82 help = '''Specify the customized Conf directory.'''
83 )
84 parser.add_argument (
85 '-D', '--define', dest = 'Define', nargs='*', action = 'append',
86 help = '''Macro: "Name [= Value]".'''
87 )
88 parser.add_argument (
89 '--makefile', dest = 'Makefile', required = True,
90 help = '''Makefile to run passing in arguments as makefile defines.'''
91 )
92 parser.add_argument (
93 '-v', '--verbose', dest = 'Verbose', action = 'store_true',
94 help = '''Turn on verbose output with informational messages printed'''
95 )
96
97 #
98 # Parse command line arguments
99 #
100 gArgs, remaining = parser.parse_known_args()
101 gArgs.BuildType = 'all'
102 for BuildType in ['all', 'fds', 'genc', 'genmake', 'clean', 'cleanall', 'modules', 'libraries', 'run']:
103 if BuildType in remaining:
104 gArgs.BuildType = BuildType
105 remaining.remove(BuildType)
106 break
107 gArgs.Remaining = ' '.join(remaining)
108
109 #
110 # Start
111 #
112 Log ('Start')
113
114 #
115 # Find makefile in WORKSPACE or PACKAGES_PATH
116 #
117 PathList = ['']
118 try:
119 PathList.append(os.environ['WORKSPACE'])
120 except:
121 Error ('WORKSPACE environment variable not set')
122 try:
123 PathList += os.environ['PACKAGES_PATH'].split(os.pathsep)
124 except:
125 pass
126 for Path in PathList:
127 Makefile = NormalizePath((Path, gArgs.Makefile))
128 if os.path.exists (Makefile):
129 break
130 if not os.path.exists(Makefile):
131 Error ('makefile %s not found' % (gArgs.Makefile))
132
133 #
134 # Build command line arguments converting build arguments to makefile defines
135 #
136 CommandLine = [Makefile]
137 CommandLine.append('TARGET_ARCH="%s"' % (' '.join([Item[0] for Item in gArgs.Arch])))
138 CommandLine.append('TOOL_CHAIN_TAG="%s"' % (gArgs.ToolChain))
139 CommandLine.append('TARGET="%s"' % (gArgs.BuildTarget))
140 CommandLine.append('ACTIVE_PLATFORM="%s"' % (gArgs.PlatformFile))
141 CommandLine.append('CONF_DIRECTORY="%s"' % (gArgs.ConfDirectory))
142 if gArgs.Define:
143 for Item in gArgs.Define:
144 if '=' not in Item[0]:
145 continue
146 Item = Item[0].split('=', 1)
147 CommandLine.append('%s="%s"' % (Item[0], Item[1]))
148 CommandLine.append('EXTRA_FLAGS="%s"' % (gArgs.Remaining))
149 CommandLine.append(gArgs.BuildType)
150 if sys.platform == "win32":
151 CommandLine = 'nmake /f %s' % (' '.join(CommandLine))
152 else:
153 CommandLine = 'make -f %s' % (' '.join(CommandLine))
154
155 #
156 # Run the makefile
157 #
158 try:
159 Process = subprocess.Popen(CommandLine, shell=True)
160 except:
161 Error ('make command not available. Please verify PATH')
162 Process.communicate()
163
164 #
165 # Done
166 #
167 Log ('Done')
168
169 #
170 # Return status from running the makefile
171 #
172 sys.exit(Process.returncode)