]>
Commit | Line | Data |
---|---|---|
07a756b9 | 1 | ## @file BuildEnv.py\r |
2 | # Initialize Environment for building\r | |
3 | #\r | |
4 | # Copyright (c) 2007, Intel Corporation\r | |
5 | #\r | |
6 | # All rights reserved. This program and the accompanying materials\r | |
7 | # are licensed and made available under the terms and conditions of the BSD License\r | |
8 | # which accompanies this distribution. The full text of the license may be found at\r | |
9 | # http://opensource.org/licenses/bsd-license.php\r | |
10 | #\r | |
11 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | #\r | |
14 | \r | |
15 | ##\r | |
16 | # Import Modules\r | |
17 | #\r | |
18 | import os\r | |
19 | import os.path\r | |
20 | import pickle\r | |
21 | import shutil\r | |
22 | import sys\r | |
23 | \r | |
24 | from optparse import OptionParser\r | |
25 | \r | |
26 | # Version and Copyright\r | |
27 | VersionNumber = "0.01"\r | |
28 | __version__ = "%prog Version " + VersionNumber\r | |
29 | __copyright__ = "Copyright (c) 2007, Intel Corporation All rights reserved."\r | |
30 | \r | |
31 | class SetupBuildEnvironmentApp:\r | |
32 | \r | |
33 | def __init__(self):\r | |
34 | (self.Opt, self.Args) = self.ProcessCommandLine()\r | |
35 | self.SetupDefaults()\r | |
36 | self.DetermineEnvironment()\r | |
37 | self.CopyTemplates()\r | |
38 | self.WriteEnvironmentConfigurationScript()\r | |
39 | \r | |
40 | def SetupDefaults(self):\r | |
41 | self.itemsToConfigure = (\r | |
42 | 'compiler',\r | |
43 | 'compiler-prefix',\r | |
44 | 'templates and Conf directory',\r | |
45 | )\r | |
46 | \r | |
47 | self.defaults = {\r | |
48 | 'compiler': {\r | |
49 | 'options': ('gcc', 'icc'),\r | |
50 | 'default': 'gcc',\r | |
51 | },\r | |
52 | 'compiler-prefix': {\r | |
53 | 'options': ('/usr/bin', '/usr/bin/x86_64-pc-mingw32-'),\r | |
54 | 'freeform': True,\r | |
55 | },\r | |
56 | 'templates and Conf directory': {\r | |
57 | 'options': (\r | |
58 | 'copy once (no-overwrite)',\r | |
59 | 'copy with overwrite',\r | |
60 | 'symlink to templates',\r | |
61 | 'do nothing',\r | |
62 | ),\r | |
63 | 'default': 'copy once (no-overwrite)',\r | |
64 | },\r | |
65 | }\r | |
66 | \r | |
67 | def ProcessCommandLine(self):\r | |
6e98e263 | 68 | Parser = OptionParser(description=__copyright__,version=__version__,prog="BaseTools/BuildEnv")\r |
07a756b9 | 69 | Parser.add_option("-q", "--quiet", action="store_true", type=None, help="Disable all messages except FATAL ERRORS.")\r |
70 | Parser.add_option("-v", "--verbose", action="store_true", type=None, help="Turn on verbose output with informational messages printed, "\\r | |
71 | "including library instances selected, final dependency expression, "\\r | |
72 | "and warning messages, etc.")\r | |
73 | Parser.add_option("-d", "--debug", action="store", type="int", help="Enable debug messages at specified level.")\r | |
74 | \r | |
75 | if os.environ.has_key('WORKSPACE'):\r | |
76 | default = os.environ['WORKSPACE']\r | |
77 | else:\r | |
78 | default = os.getcwd()\r | |
79 | Parser.add_option("--workspace", action="store", type="string", help="Base director of tree to configure", default=default)\r | |
80 | \r | |
81 | (Opt, Args)=Parser.parse_args()\r | |
82 | Parser.print_version()\r | |
83 | \r | |
84 | return (Opt, Args)\r | |
85 | \r | |
86 | def DetermineEnvironment(self):\r | |
87 | confFilename = os.path.join(os.path.expanduser('~'), '.edk-build-env.pickle')\r | |
88 | try:\r | |
89 | confFile = open(confFilename, 'r')\r | |
90 | conf = pickle.load(confFile)\r | |
91 | confFile.close()\r | |
92 | except Exception:\r | |
93 | conf = {}\r | |
94 | self.conf = conf\r | |
95 | \r | |
96 | for item in self.itemsToConfigure:\r | |
97 | if not conf.has_key(item):\r | |
98 | self.AskForValueOfOption(item)\r | |
99 | \r | |
100 | while True:\r | |
101 | self.SummarizeCurrentState()\r | |
102 | \r | |
103 | if not self.Opt.quiet:\r | |
104 | response = raw_input('Would you like to change anything? (default=no): ')\r | |
105 | response = response.strip()\r | |
106 | else:\r | |
107 | response = ''\r | |
108 | \r | |
109 | if response.lower() in ('', 'n', 'no'):\r | |
110 | break\r | |
111 | \r | |
112 | for item in self.itemsToConfigure:\r | |
113 | self.AskForValueOfOption(item)\r | |
114 | \r | |
115 | confFile = open(confFilename, 'w')\r | |
116 | pickle.dump(conf, confFile)\r | |
117 | confFile.close()\r | |
118 | \r | |
119 | def AskCompiler(self):\r | |
120 | self.AskForValueOfOption('compiler')\r | |
121 | \r | |
122 | def AskCompilerPrefix(self):\r | |
123 | self.AskForValueOfOption('compiler-prefix')\r | |
124 | \r | |
125 | def AskForValueOfOption(self, option):\r | |
126 | options = self.defaults[option]['options']\r | |
127 | if self.defaults[option].has_key('default'):\r | |
128 | default = self.defaults[option]['default']\r | |
129 | else:\r | |
130 | default = None\r | |
131 | if self.defaults[option].has_key('freeform'):\r | |
132 | freeform = self.defaults[option]['freeform']\r | |
133 | else:\r | |
134 | freeform = False\r | |
135 | self.AskForValue(option, options, default, freeform)\r | |
136 | \r | |
137 | def AskForValue(self, index, options, default=None, freeform=False):\r | |
138 | conf = self.conf\r | |
139 | if conf.has_key(index):\r | |
140 | default = conf[index]\r | |
141 | options = list(options) # in case options came in as a tuple\r | |
142 | assert((default == '') or (default is None) or ('' not in options))\r | |
143 | if (default is not None) and (default not in options):\r | |
144 | options.append(default)\r | |
145 | if (freeform and ('' not in options)):\r | |
146 | options.append('')\r | |
147 | options.sort()\r | |
148 | while True:\r | |
149 | print\r | |
150 | if len(options) > 0:\r | |
151 | print 'Options for', index\r | |
152 | for i in range(len(options)):\r | |
153 | print ' %d.' % (i + 1),\r | |
154 | if options[i] != '':\r | |
155 | print options[i],\r | |
156 | else:\r | |
157 | print '(empty string)',\r | |
158 | if options[i] == default:\r | |
159 | print '(default)'\r | |
160 | else:\r | |
161 | print\r | |
162 | \r | |
163 | if len(options) > 0:\r | |
164 | prompt = 'Select number or type value: '\r | |
165 | else:\r | |
166 | prompt = 'Type value for %s: ' % index\r | |
167 | response = raw_input(prompt)\r | |
168 | response = response.strip()\r | |
169 | \r | |
170 | if response.isdigit():\r | |
171 | response = int(response)\r | |
172 | if response > len(options):\r | |
173 | print 'ERROR: Invalid number selection!'\r | |
174 | continue\r | |
175 | response = options[response - 1]\r | |
176 | elif (response == '') and (default is not None):\r | |
177 | response = default\r | |
178 | \r | |
179 | if (not freeform) and (response not in options):\r | |
180 | print 'ERROR: Invalid selection! (must be from list)'\r | |
181 | continue\r | |
182 | \r | |
183 | break\r | |
184 | \r | |
185 | conf[index] = response\r | |
186 | print 'Using', conf[index], 'for', index\r | |
187 | \r | |
188 | def SummarizeCurrentState(self):\r | |
189 | print\r | |
190 | print 'Current configuration:'\r | |
191 | conf = self.conf\r | |
192 | for item in self.itemsToConfigure:\r | |
193 | value = conf[item]\r | |
194 | if value == '': value = '(empty string)'\r | |
195 | print ' ', item, '->', value\r | |
196 | \r | |
197 | def CopyTemplates(self):\r | |
198 | todo = self.conf['templates and Conf directory']\r | |
199 | workspace = os.path.realpath(self.Opt.workspace)\r | |
200 | templatesDir = \\r | |
6e98e263 | 201 | os.path.join(workspace, 'BaseTools', 'ConfTemplates', sys.platform.title())\r |
07a756b9 | 202 | confDir = \\r |
203 | os.path.join(workspace, 'Conf')\r | |
204 | print\r | |
205 | print 'Templates & Conf directory'\r | |
206 | print ' Templates dir:', self.RelativeToWorkspace(templatesDir)\r | |
207 | for filename in os.listdir(templatesDir):\r | |
208 | if filename.startswith('.'): continue\r | |
209 | \r | |
210 | srcFilename = os.path.join(templatesDir, filename)\r | |
211 | destFilename = os.path.join(confDir, filename)\r | |
212 | print ' ', self.RelativeToWorkspace(destFilename),\r | |
213 | \r | |
214 | if todo == 'copy once (no-overwrite)':\r | |
215 | if os.path.exists(destFilename):\r | |
216 | print '[skipped, already exists]'\r | |
217 | else:\r | |
218 | shutil.copy(srcFilename, destFilename)\r | |
219 | print '[copied]'\r | |
220 | elif todo == 'copy with overwrite':\r | |
221 | overwrite = ''\r | |
222 | if os.path.exists(destFilename):\r | |
223 | os.remove(destFilename)\r | |
224 | overwrite = ', overwritten'\r | |
225 | shutil.copy(srcFilename, destFilename)\r | |
226 | print '[copied' + overwrite + ']'\r | |
227 | elif todo == 'symlink to templates':\r | |
228 | if os.path.exists(destFilename):\r | |
229 | if not os.path.islink(destFilename):\r | |
230 | raise Exception, '%s is not a symlink! (remove file if you want to start using symlinks)' % \\r | |
231 | (self.RelativeToWorkspace(destFilename))\r | |
232 | os.remove(destFilename)\r | |
233 | os.symlink(srcFilename, destFilename)\r | |
234 | print '[symlinked]'\r | |
235 | elif todo == 'do nothing':\r | |
236 | print '[skipped by user request]'\r | |
237 | else:\r | |
238 | raise Exception, 'Unknown action for templates&conf: %s' % todo\r | |
239 | \r | |
240 | def WriteEnvironmentConfigurationScript(self):\r | |
241 | workspace = os.path.realpath(self.Opt.workspace)\r | |
242 | scriptFilename = os.path.join(workspace, 'Conf', 'BuildEnv.sh')\r | |
243 | print\r | |
244 | print 'Storing environment configuration into',\r | |
245 | print self.RelativeToWorkspace(scriptFilename)\r | |
246 | script = open(scriptFilename, 'w')\r | |
247 | \r | |
248 | print >> script, 'export WORKSPACE="%s"' % workspace\r | |
249 | print >> script, 'export TOOLCHAIN="%s"' % self.conf['compiler']\r | |
250 | print >> script, 'export EDK_CC_PATH_PREFIX="%s"' % self.conf['compiler-prefix']\r | |
251 | \r | |
6e98e263 | 252 | EDK_TOOLS_PATH = os.path.join(workspace, 'BaseTools')\r |
253 | print >> script, 'if [ $EDK_TOOLS_PATH=="" ]'\r | |
254 | print >> script, 'then'\r | |
255 | print >> script, ' export EDK_TOOLS_PATH="%s"' % EDK_TOOLS_PATH\r | |
256 | print >> script, 'fi'\r | |
257 | \r | |
07a756b9 | 258 | #\r |
259 | # Change PATH variable\r | |
260 | #\r | |
261 | newPath = os.environ['PATH'].split(os.path.pathsep)\r | |
262 | binDir = \\r | |
6e98e263 | 263 | os.path.join(workspace, 'BaseTools', 'Bin', sys.platform.title())\r |
07a756b9 | 264 | if binDir not in newPath:\r |
265 | newPath.append(binDir)\r | |
266 | newPath = os.path.pathsep.join(newPath)\r | |
267 | print >> script, 'export PATH=%s' % newPath\r | |
268 | \r | |
269 | script.close()\r | |
270 | \r | |
271 | def RelativeToWorkspace(self, path):\r | |
272 | workspace = os.path.realpath(self.Opt.workspace)\r | |
273 | for prefix in (workspace + os.path.sep, workspace):\r | |
274 | if path.startswith(prefix):\r | |
275 | return path[len(prefix):]\r | |
276 | \r | |
277 | \r | |
278 | if __name__ == '__main__':\r | |
279 | SetupBuildEnvironmentApp()\r | |
280 | \r |