build_tc.py now accepts an output directory for where portage output
 gets stored.

PRESUBMIT=passed
R=raymes,bjanakiraman
DELTA=75  (49 added, 2 deleted, 24 changed)
OCL=45554-p2
RCL=45556-p2
RDATE=2010/12/01 19:34:17


P4 change: 42606747
diff --git a/v14/build_tc.py b/v14/build_tc.py
index d38e9a4..2953312 100755
--- a/v14/build_tc.py
+++ b/v14/build_tc.py
@@ -15,21 +15,23 @@
 import sys
 import tc_enter_chroot
 import build_chromeos
+import setup_chromeos
 from utils import command_executer
 from utils import utils
 from utils import logger
 
 # Common initializations
-logger.InitLogger(sys.argv[0])
 cmd_executer = command_executer.GetCommandExecuter()
 
 
-def Main():
+def Main(argv):
   """The main function."""
+  rootdir = utils.GetRoot(sys.argv[0])[0]
+
   parser = optparse.OptionParser()
   parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
-                    default="../..",
-                    help="ChromeOS root checkout directory.")
+                    help=("ChromeOS root checkout directory" +
+                           " uses ../.. if none given."))
   parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
                     help="Toolchain root directory.")
   parser.add_option("-b", "--board", dest="board", default="x86-generic",
@@ -47,15 +49,52 @@
                     action="store_true", default=False,
                     help="The toolchain should use binaries stored in "
                     "the install/ directory.")
+  parser.add_option("-s", "--setup-chromeos-options",
+                    dest="setup_chromeos_options",
+                    help="Additional options that should be passed on to"
+                    "the setup_chromeos script.")
+  parser.add_option("-o", "--output", dest="output",
+                    default=rootdir + "/output",
+                    help="The output directory where logs,pkgs, etc. go.")
 
-  options = parser.parse_args()[0]
-
-  options.chromeos_root = os.path.expanduser(options.chromeos_root)
+  options = parser.parse_args(argv)[0]
 
   if options.toolchain_root is None or options.board is None:
     parser.print_help()
     sys.exit()
 
+  if options.chromeos_root is None:
+    if os.path.exists("enter_chroot.sh"):
+      options.chromeos_root = "../.."
+    else:
+      logger.GetLogger().LogError("--chromeos_root not given")
+      parser.print_help()
+      sys.exit()
+  else:
+    options.chromeos_root = os.path.expanduser(options.chromeos_root)
+
+  if not os.path.exists(options.chromeos_root):
+    logger.GetLogger().LogOutput("Creating a chromeos checkout at: %s" %
+                                 options.chromeos_root)
+    sc_args = ("--dir=\"%s\" %s" % (options.chromeos_root,
+                                   options.setup_chromeos_options))
+    setup_chromeos.Main(sc_args)
+
+  output = options.output
+
+  if output.startswith("/") == False:
+    output = os.getcwd() + "/" + output
+  else:
+    output = os.path.expanduser(output)
+
+  chroot_mount = "/usr/local/toolchain_root/"
+  chroot_base = utils.GetRoot(output)[1]
+  chroot_output = chroot_mount + chroot_base
+
+  tc_enter_chroot_options = []
+  output_mount = ("-o %s:%s/%s" % (output, chroot_mount, chroot_base))
+  tc_enter_chroot_options.append(output_mount)
+
   build_chromeos.MakeChroot(options.chromeos_root)
 
   portage_flags = ""
@@ -73,26 +112,33 @@
     features += " keepwork"
   env = CreateEnvVarString(" FEATURES", features)
   env += CreateEnvVarString(" PORTAGE_USERNAME", getpass.getuser())
-  rootdir = utils.GetRoot(sys.argv[0])[0]
-  version_number = utils.GetRoot(rootdir)[1]
-  version_dir = "/usr/local/toolchain_root/" + version_number
-  env += CreateEnvVarString(" PORT_LOGDIR", version_dir + "/logs")
-  env += CreateEnvVarString(" PKGDIR", version_dir + "/pkgs")
-  env += CreateEnvVarString(" PORTAGE_BINHOST", version_dir + "/pkgs")
-  env += CreateEnvVarString(" PORTAGE_TMPDIR", version_dir + "/objects")
+  logdir = "/logs"
+  pkgdir = "/pkgs"
+  tmpdir = "/objects"
+  installdir = "/install"
+  package_dir = output + pkgdir
+  portage_logdir = chroot_output + logdir
+  portage_pkgdir = chroot_output + pkgdir
+  portage_tmpdir = chroot_output + tmpdir
+  env += CreateEnvVarString(" PORT_LOGDIR", portage_logdir)
+  env += CreateEnvVarString(" PKGDIR", portage_pkgdir)
+  env += CreateEnvVarString(" PORTAGE_BINHOST", portage_pkgdir)
+  env += CreateEnvVarString(" PORTAGE_TMPDIR", portage_tmpdir)
   env += CreateEnvVarString(" USE", "mounted_sources")
 
   retval = 0
   if options.force == True:
     retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
-                     target, True, options.incremental, portage_flags)
+                     target, True, options.incremental, portage_flags,
+                     tc_enter_chroot_options)
   retval = BuildTC(options.chromeos_root, options.toolchain_root, env,
-                   target, options.clean, options.incremental, portage_flags)
+                   target, options.clean, options.incremental, portage_flags,
+                   tc_enter_chroot_options)
   utils.AssertTrue(retval == 0, "Build toolchain failed!")
+  command = "sudo chown -R " + getpass.getuser() + " " + package_dir
 
   if options.incremental is None and not options.clean:
-    install_dir = rootdir + "/install"
-    package_dir = (rootdir + "/pkgs/")
+    install_dir = output + installdir
     retval = InstallTC(package_dir, install_dir)
     utils.AssertTrue(retval == 0, "Installation of the toolchain failed!")
 
@@ -126,7 +172,7 @@
 
 
 def BuildTC(chromeos_root, toolchain_root, env, target, uninstall,
-            incremental_component, portage_flags):
+            incremental_component, portage_flags, tc_enter_chroot_options):
   """Build the toolchain."""
   portage_flags = portage_flags.strip()
   portage_flags += " -b "
@@ -137,9 +183,10 @@
   kernel_version = "2.6.30-r1"
 
   rootdir = utils.GetRoot(sys.argv[0])[0]
-  argv = [rootdir + "tc_enter_chroot.py",
+  argv = [rootdir + "/tc_enter_chroot.py",
           "--chromeos_root=" + chromeos_root,
           "--toolchain_root=" + toolchain_root]
+  argv += tc_enter_chroot_options
 
   env += " "
 
@@ -179,5 +226,5 @@
   return retval
 
 if __name__ == "__main__":
-  Main()
+  Main(sys.argv)