--- /dev/null
+## @file BuildEnv.py\r
+# Initialize Environment for building\r
+#\r
+# Copyright (c) 2007, Intel Corporation\r
+#\r
+# All rights reserved. This program and the accompanying materials\r
+# are licensed and made available under the terms and conditions of the BSD License\r
+# which accompanies this distribution. The full text of the license may be found at\r
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+\r
+##\r
+# Import Modules\r
+#\r
+import os\r
+import os.path\r
+import pickle\r
+import shutil\r
+import sys\r
+\r
+from optparse import OptionParser\r
+\r
+# Version and Copyright\r
+VersionNumber = "0.01"\r
+__version__ = "%prog Version " + VersionNumber\r
+__copyright__ = "Copyright (c) 2007, Intel Corporation All rights reserved."\r
+\r
+class SetupBuildEnvironmentApp:\r
+\r
+ def __init__(self):\r
+ (self.Opt, self.Args) = self.ProcessCommandLine()\r
+ self.SetupDefaults()\r
+ self.DetermineEnvironment()\r
+ self.CopyTemplates()\r
+ self.WriteEnvironmentConfigurationScript()\r
+\r
+ def SetupDefaults(self):\r
+ self.itemsToConfigure = (\r
+ 'compiler',\r
+ 'compiler-prefix',\r
+ 'templates and Conf directory',\r
+ )\r
+\r
+ self.defaults = {\r
+ 'compiler': {\r
+ 'options': ('gcc', 'icc'),\r
+ 'default': 'gcc',\r
+ },\r
+ 'compiler-prefix': {\r
+ 'options': ('/usr/bin', '/usr/bin/x86_64-pc-mingw32-'),\r
+ 'freeform': True,\r
+ },\r
+ 'templates and Conf directory': {\r
+ 'options': (\r
+ 'copy once (no-overwrite)',\r
+ 'copy with overwrite',\r
+ 'symlink to templates',\r
+ 'do nothing',\r
+ ),\r
+ 'default': 'copy once (no-overwrite)',\r
+ },\r
+ }\r
+\r
+ def ProcessCommandLine(self):\r
+ Parser = OptionParser(description=__copyright__,version=__version__,prog="Tools/BuildEnv")\r
+ Parser.add_option("-q", "--quiet", action="store_true", type=None, help="Disable all messages except FATAL ERRORS.")\r
+ Parser.add_option("-v", "--verbose", action="store_true", type=None, help="Turn on verbose output with informational messages printed, "\\r
+ "including library instances selected, final dependency expression, "\\r
+ "and warning messages, etc.")\r
+ Parser.add_option("-d", "--debug", action="store", type="int", help="Enable debug messages at specified level.")\r
+\r
+ if os.environ.has_key('WORKSPACE'):\r
+ default = os.environ['WORKSPACE']\r
+ else:\r
+ default = os.getcwd()\r
+ Parser.add_option("--workspace", action="store", type="string", help="Base director of tree to configure", default=default)\r
+\r
+ (Opt, Args)=Parser.parse_args()\r
+ Parser.print_version()\r
+\r
+ return (Opt, Args)\r
+\r
+ def DetermineEnvironment(self):\r
+ confFilename = os.path.join(os.path.expanduser('~'), '.edk-build-env.pickle')\r
+ try:\r
+ confFile = open(confFilename, 'r')\r
+ conf = pickle.load(confFile)\r
+ confFile.close()\r
+ except Exception:\r
+ conf = {}\r
+ self.conf = conf\r
+\r
+ for item in self.itemsToConfigure:\r
+ if not conf.has_key(item):\r
+ self.AskForValueOfOption(item)\r
+\r
+ while True:\r
+ self.SummarizeCurrentState()\r
+\r
+ if not self.Opt.quiet:\r
+ response = raw_input('Would you like to change anything? (default=no): ')\r
+ response = response.strip()\r
+ else:\r
+ response = ''\r
+\r
+ if response.lower() in ('', 'n', 'no'):\r
+ break\r
+\r
+ for item in self.itemsToConfigure:\r
+ self.AskForValueOfOption(item)\r
+\r
+ confFile = open(confFilename, 'w')\r
+ pickle.dump(conf, confFile)\r
+ confFile.close()\r
+\r
+ def AskCompiler(self):\r
+ self.AskForValueOfOption('compiler')\r
+\r
+ def AskCompilerPrefix(self):\r
+ self.AskForValueOfOption('compiler-prefix')\r
+\r
+ def AskForValueOfOption(self, option):\r
+ options = self.defaults[option]['options']\r
+ if self.defaults[option].has_key('default'):\r
+ default = self.defaults[option]['default']\r
+ else:\r
+ default = None\r
+ if self.defaults[option].has_key('freeform'):\r
+ freeform = self.defaults[option]['freeform']\r
+ else:\r
+ freeform = False\r
+ self.AskForValue(option, options, default, freeform)\r
+\r
+ def AskForValue(self, index, options, default=None, freeform=False):\r
+ conf = self.conf\r
+ if conf.has_key(index):\r
+ default = conf[index]\r
+ options = list(options) # in case options came in as a tuple\r
+ assert((default == '') or (default is None) or ('' not in options))\r
+ if (default is not None) and (default not in options):\r
+ options.append(default)\r
+ if (freeform and ('' not in options)):\r
+ options.append('')\r
+ options.sort()\r
+ while True:\r
+ print\r
+ if len(options) > 0:\r
+ print 'Options for', index\r
+ for i in range(len(options)):\r
+ print ' %d.' % (i + 1),\r
+ if options[i] != '':\r
+ print options[i],\r
+ else:\r
+ print '(empty string)',\r
+ if options[i] == default:\r
+ print '(default)'\r
+ else:\r
+ print\r
+\r
+ if len(options) > 0:\r
+ prompt = 'Select number or type value: '\r
+ else:\r
+ prompt = 'Type value for %s: ' % index\r
+ response = raw_input(prompt)\r
+ response = response.strip()\r
+\r
+ if response.isdigit():\r
+ response = int(response)\r
+ if response > len(options):\r
+ print 'ERROR: Invalid number selection!'\r
+ continue\r
+ response = options[response - 1]\r
+ elif (response == '') and (default is not None):\r
+ response = default\r
+\r
+ if (not freeform) and (response not in options):\r
+ print 'ERROR: Invalid selection! (must be from list)'\r
+ continue\r
+\r
+ break\r
+\r
+ conf[index] = response\r
+ print 'Using', conf[index], 'for', index\r
+\r
+ def SummarizeCurrentState(self):\r
+ print\r
+ print 'Current configuration:'\r
+ conf = self.conf\r
+ for item in self.itemsToConfigure:\r
+ value = conf[item]\r
+ if value == '': value = '(empty string)'\r
+ print ' ', item, '->', value\r
+\r
+ def CopyTemplates(self):\r
+ todo = self.conf['templates and Conf directory']\r
+ workspace = os.path.realpath(self.Opt.workspace)\r
+ templatesDir = \\r
+ os.path.join(workspace, 'Tools', 'BaseTools', 'ConfTemplates', sys.platform.title())\r
+ confDir = \\r
+ os.path.join(workspace, 'Conf')\r
+ print\r
+ print 'Templates & Conf directory'\r
+ print ' Templates dir:', self.RelativeToWorkspace(templatesDir)\r
+ for filename in os.listdir(templatesDir):\r
+ srcFilename = os.path.join(templatesDir, filename)\r
+ destFilename = os.path.join(confDir, filename)\r
+ print ' ', self.RelativeToWorkspace(destFilename),\r
+\r
+ if todo == 'copy once (no-overwrite)':\r
+ if os.path.exists(destFilename):\r
+ print '[skipped, already exists]'\r
+ else:\r
+ shutil.copy(srcFilename, destFilename)\r
+ print '[copied]'\r
+ elif todo == 'copy with overwrite':\r
+ overwrite = ''\r
+ if os.path.exists(destFilename):\r
+ os.remove(destFilename)\r
+ overwrite = ', overwritten'\r
+ shutil.copy(srcFilename, destFilename)\r
+ print '[copied' + overwrite + ']'\r
+ elif todo == 'symlink to templates':\r
+ if os.path.exists(destFilename):\r
+ if not os.path.islink(destFilename):\r
+ raise Exception, '%s is not a symlink! (remove file if you want to start using symlinks)' % \\r
+ (self.RelativeToWorkspace(destFilename))\r
+ os.remove(destFilename)\r
+ os.symlink(srcFilename, destFilename)\r
+ print '[symlinked]'\r
+ elif todo == 'do nothing':\r
+ print '[skipped by user request]'\r
+ else:\r
+ raise Exception, 'Unknown action for templates&conf: %s' % todo\r
+\r
+ def WriteEnvironmentConfigurationScript(self):\r
+ workspace = os.path.realpath(self.Opt.workspace)\r
+ scriptFilename = os.path.join(workspace, 'Conf', 'BuildEnv.sh')\r
+ print\r
+ print 'Storing environment configuration into',\r
+ print self.RelativeToWorkspace(scriptFilename)\r
+ script = open(scriptFilename, 'w')\r
+ print >> script, 'export WORKSPACE="%s"' % workspace\r
+ print >> script, 'export TOOLCHAIN="%s"' % self.conf['compiler']\r
+ print >> script, 'export EDK_CC_PATH_PREFIX="%s"' % self.conf['compiler-prefix']\r
+ script.close()\r
+\r
+ def RelativeToWorkspace(self, path):\r
+ workspace = os.path.realpath(self.Opt.workspace)\r
+ for prefix in (workspace + os.path.sep, workspace):\r
+ if path.startswith(prefix):\r
+ return path[len(prefix):]\r
+ \r
+\r
+if __name__ == '__main__':\r
+ SetupBuildEnvironmentApp()\r
+\r