blob: 20e98fafe48cac289ecf50bb04ab408df7568f29 [file] [log] [blame]
raymes5154d7f2013-02-15 04:35:37 +00001#!/usr/bin/python2.6
2#
3# Copyright 2010 Google Inc. All Rights Reserved.
4
5"""Script to checkout the ChromeOS source.
6
7This script sets up the ChromeOS source in the given directory, matching a
8particular release of ChromeOS.
9"""
10
11__author__ = "raymes@google.com (Raymes Khoury)"
12
13import optparse
14import os
15import sys
asharife3668f12013-02-15 04:46:29 +000016import tc_enter_chroot
raymes01959ae2013-02-15 04:50:07 +000017from utils import command_executer
18from utils import logger
raymes5154d7f2013-02-15 04:35:37 +000019from utils import utils
20
raymes01959ae2013-02-15 04:50:07 +000021cmd_executer = command_executer.GetCommandExecuter()
22
asharife3668f12013-02-15 04:46:29 +000023
raymes5154d7f2013-02-15 04:35:37 +000024def Usage(parser, message):
25 print "ERROR: " + message
26 parser.print_help()
27 sys.exit(0)
28
29
30def ExecuteCommandInChroot(chromeos_root, toolchain_root, command,
raymesbfb57992013-02-15 04:35:45 +000031 return_output=False, chrome_root=""):
asharife3668f12013-02-15 04:46:29 +000032 """Executes a command in the chroot."""
raymesbfb57992013-02-15 04:35:45 +000033 chrome_mount = ""
34 if chrome_root:
35 chrome_mount = "--chrome_root=" + chromeos_root + "/" + chrome_root
raymes01959ae2013-02-15 04:50:07 +000036 argv = [os.path.dirname(os.path.abspath(__file__)) + "/tc_enter_chroot.py",
37 "--chromeos_root=" + chromeos_root,
38 "--toolchain_root=" + toolchain_root,
39 chrome_mount,
40 "--",
41 command]
asharife3668f12013-02-15 04:46:29 +000042 return tc_enter_chroot.Main(argv)
43
44
45def MakeChroot(chromeos_root, clobber_chroot=False):
46 """Make a chroot given a chromeos checkout."""
47 if (not os.path.isdir(chromeos_root + "/chroot")
48 or clobber_chroot):
49 commands = []
50 commands.append("cd " + chromeos_root + "/src/scripts")
51 clobber_chroot = ""
52 if clobber_chroot:
53 clobber_chroot = "--replace"
54 commands.append("./make_chroot --fast " + clobber_chroot)
raymes01959ae2013-02-15 04:50:07 +000055 ret = cmd_executer.RunCommands(commands)
asharife3668f12013-02-15 04:46:29 +000056 utils.AssertTrue(ret == 0, "make_chroot failed")
57 else:
raymes01959ae2013-02-15 04:50:07 +000058 logger.GetLogger().LogOutput("Did not make_chroot because it already exists")
raymes5154d7f2013-02-15 04:35:37 +000059
60
raymes5154d7f2013-02-15 04:35:37 +000061def Main():
62 """Build ChromeOS."""
63 # Common initializations
raymes5154d7f2013-02-15 04:35:37 +000064
65 parser = optparse.OptionParser()
66 parser.add_option("--chromeos_root", dest="chromeos_root",
67 help="Target directory for ChromeOS installation.")
68 parser.add_option("--toolchain_root", dest="toolchain_root",
69 help="The gcctools directory of your P4 checkout.")
70 parser.add_option("--clobber_chroot", dest="clobber_chroot",
71 action="store_true", help=
72 "Delete the chroot and start fresh", default=False)
73 parser.add_option("--clobber_board", dest="clobber_board",
74 action="store_true",
75 help="Delete the board and start fresh", default=False)
76 parser.add_option("--cflags", dest="cflags",
77 help="CFLAGS for the ChromeOS packages")
78 parser.add_option("--cxxflags", dest="cxxflags",
79 help="CXXFLAGS for the ChromeOS packages")
80 parser.add_option("--ldflags", dest="ldflags",
81 help="LDFLAGS for the ChromeOS packages")
82 parser.add_option("--board", dest="board",
83 help="ChromeOS target board, e.g. x86-generic")
84
85 options = parser.parse_args()[0]
86
87 if options.chromeos_root is None:
88 Usage(parser, "--chromeos_root must be set")
89
90 if options.toolchain_root is None:
91 Usage(parser, "--toolchain_root must be set")
92
93 if options.board is None:
94 Usage(parser, "--board must be set")
95
asharife3668f12013-02-15 04:46:29 +000096 MakeChroot(options.chromeos_root, options.clobber_chroot)
raymes5154d7f2013-02-15 04:35:37 +000097
98 # Setup board
raymes04164a12013-02-15 04:36:03 +000099 if not os.path.isdir(options.chromeos_root + "/chroot/build/"
100 + options.board) or options.clobber_board:
raymes5f35b922013-02-15 04:35:57 +0000101 force = ""
102 if options.clobber_board:
103 force = "--force"
raymes04164a12013-02-15 04:36:03 +0000104 # Run build_tc.py from binary package
raymes01959ae2013-02-15 04:50:07 +0000105 rootdir = utils.GetRoot(sys.argv[0])[0]
106 ret = cmd_executer.RunCommand(rootdir + "/build_tc.py --chromeos_root=%s "
107 "--toolchain_root=%s --board=%s -B"
108 % (options.chromeos_root,
109 options.toolchain_root, options.board))
raymes04164a12013-02-15 04:36:03 +0000110 utils.AssertTrue(ret == 0, "build_tc.py failed")
raymes5f6be5f2013-02-15 04:36:13 +0000111 version_number = utils.GetRoot(rootdir)[1]
asharifad465152013-02-15 04:46:29 +0000112 pkgdir = "/usr/local/toolchain_root/" + version_number + "/pkgs"
raymes5f35b922013-02-15 04:35:57 +0000113 ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root,
raymes5f6be5f2013-02-15 04:36:13 +0000114 "PKGDIR=%s ./setup_board --board=%s "
115 " --gcc_version=9999 "
116 "%s" % (pkgdir, options.board, force))
raymes5f35b922013-02-15 04:35:57 +0000117 utils.AssertTrue(ret == 0, "setup_board failed")
118 else:
raymes01959ae2013-02-15 04:50:07 +0000119 logger.GetLogger().LogOutput("Did not setup_board "
120 "because it already exists")
raymesbfb57992013-02-15 04:35:45 +0000121
122 # Modify make.conf to add CFLAGS/CXXFLAGS/LDFLAGS
raymesbfb57992013-02-15 04:35:45 +0000123 ret1 = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root,
124 "[ -e /build/%s/etc/make.conf.orig ] || "
125 "sudo mv /build/%s/etc/make.conf "
126 "/build/%s/etc/make.conf.orig"
127 % (options.board, options.board, options.board))
128 makeconf = ("source make.conf.orig\\\n")
129 #"CFLAGS='%s'\\\nCXXFLAGS='%s'\\\nLDFLAGS='%s'\\\n" %
130 #(options.cflags, options.cxxflags, options.ldflags))
raymese91a6e62013-02-15 04:35:51 +0000131 ret2 = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root,
raymes01959ae2013-02-15 04:50:07 +0000132 "\"if [ -e /build/%s/etc/make.conf.orig ] ; "
133 "then sudo echo -e \\\"%s\\\" | sudo tee "
raymese91a6e62013-02-15 04:35:51 +0000134 "/build/%s/etc/make.conf > /dev/null ;"
asharif0269d462013-02-15 04:46:31 +0000135 "else exit 1 ; fi\""
raymes5f6be5f2013-02-15 04:36:13 +0000136 % (options.board, makeconf, options.board))
raymes5f35b922013-02-15 04:35:57 +0000137
raymesbfb57992013-02-15 04:35:45 +0000138 utils.AssertTrue(ret1 == 0 and ret2 == 0, "Could not modify make.conf")
raymes5154d7f2013-02-15 04:35:37 +0000139
140 # Find Chrome browser version
raymes01959ae2013-02-15 04:50:07 +0000141 chrome_version = cmd_executer.RunCommand("%s/src/scripts/chromeos_version.sh "
142 "| grep CHROME_BUILD"
143 % options.chromeos_root, True)
raymesbfb57992013-02-15 04:35:45 +0000144
145 ret = chrome_version[0]
146 utils.AssertTrue(ret == 0, "Could not determine Chrome browser version")
raymes5154d7f2013-02-15 04:35:37 +0000147
148 chrome_version = chrome_version[1].strip().split("=")
149 if len(chrome_version) == 2:
150 chrome_version = chrome_version[1]
151 else:
152 chrome_version = ""
153
raymes5154d7f2013-02-15 04:35:37 +0000154 # Build packages
raymesbfb57992013-02-15 04:35:45 +0000155 ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root,
raymese91a6e62013-02-15 04:35:51 +0000156 "CHROME_ORIGIN=SERVER_SOURCE CHROME_VERSION=%s "
raymesbfb57992013-02-15 04:35:45 +0000157 "./build_packages --withdev "
158 "--board=%s --withtest --withautotest"
159 % (chrome_version, options.board),
160 chrome_root="chrome_browser")
161
162 utils.AssertTrue(ret == 0, "build_packages failed")
raymes5154d7f2013-02-15 04:35:37 +0000163
164 # Build image
raymesbfb57992013-02-15 04:35:45 +0000165 ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root,
166 "./build_image --board=%s" % options.board)
167
168 utils.AssertTrue(ret == 0, "build_image failed")
raymes5154d7f2013-02-15 04:35:37 +0000169
170 # Mod image for test
raymesbfb57992013-02-15 04:35:45 +0000171 ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root,
172 "./mod_image_for_test.sh --board=%s"
173 % options.board)
raymes5154d7f2013-02-15 04:35:37 +0000174
raymesbfb57992013-02-15 04:35:45 +0000175 utils.AssertTrue(ret == 0, "mod_image_for_test failed")
raymes5154d7f2013-02-15 04:35:37 +0000176
177if __name__ == "__main__":
178 Main()