blob: a92c31cc46da3ea8cc4b1cba01bddf0c733e563e [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
raymes5154d7f2013-02-15 04:35:37 +000017from utils import utils
18
asharife3668f12013-02-15 04:46:29 +000019
raymes5154d7f2013-02-15 04:35:37 +000020def Usage(parser, message):
21 print "ERROR: " + message
22 parser.print_help()
23 sys.exit(0)
24
25
26def ExecuteCommandInChroot(chromeos_root, toolchain_root, command,
raymesbfb57992013-02-15 04:35:45 +000027 return_output=False, chrome_root=""):
asharife3668f12013-02-15 04:46:29 +000028 """Executes a command in the chroot."""
raymesbfb57992013-02-15 04:35:45 +000029 chrome_mount = ""
30 if chrome_root:
31 chrome_mount = "--chrome_root=" + chromeos_root + "/" + chrome_root
asharife3668f12013-02-15 04:46:29 +000032 argv=[os.path.dirname(os.path.abspath(__file__)) + "/tc_enter_chroot.py",
33 "--chromeos_root=" + chromeos_root,
34 "--toolchain_root=" + toolchain_root,
35 chrome_mount,
36 "--",
37 command]
38 return tc_enter_chroot.Main(argv)
39
40
41def MakeChroot(chromeos_root, clobber_chroot=False):
42 """Make a chroot given a chromeos checkout."""
43 if (not os.path.isdir(chromeos_root + "/chroot")
44 or clobber_chroot):
45 commands = []
46 commands.append("cd " + chromeos_root + "/src/scripts")
47 clobber_chroot = ""
48 if clobber_chroot:
49 clobber_chroot = "--replace"
50 commands.append("./make_chroot --fast " + clobber_chroot)
51 ret = utils.RunCommands(commands)
52 utils.AssertTrue(ret == 0, "make_chroot failed")
53 else:
54 utils.main_logger.LogOutput("Did not make_chroot because it already exists")
raymes5154d7f2013-02-15 04:35:37 +000055
56
raymes5154d7f2013-02-15 04:35:37 +000057def Main():
58 """Build ChromeOS."""
59 # Common initializations
60 (rootdir, basename) = utils.GetRoot(sys.argv[0])
61 utils.InitLogger(rootdir, basename)
62
63 parser = optparse.OptionParser()
64 parser.add_option("--chromeos_root", dest="chromeos_root",
65 help="Target directory for ChromeOS installation.")
66 parser.add_option("--toolchain_root", dest="toolchain_root",
67 help="The gcctools directory of your P4 checkout.")
68 parser.add_option("--clobber_chroot", dest="clobber_chroot",
69 action="store_true", help=
70 "Delete the chroot and start fresh", default=False)
71 parser.add_option("--clobber_board", dest="clobber_board",
72 action="store_true",
73 help="Delete the board and start fresh", default=False)
74 parser.add_option("--cflags", dest="cflags",
75 help="CFLAGS for the ChromeOS packages")
76 parser.add_option("--cxxflags", dest="cxxflags",
77 help="CXXFLAGS for the ChromeOS packages")
78 parser.add_option("--ldflags", dest="ldflags",
79 help="LDFLAGS for the ChromeOS packages")
80 parser.add_option("--board", dest="board",
81 help="ChromeOS target board, e.g. x86-generic")
82
83 options = parser.parse_args()[0]
84
85 if options.chromeos_root is None:
86 Usage(parser, "--chromeos_root must be set")
87
88 if options.toolchain_root is None:
89 Usage(parser, "--toolchain_root must be set")
90
91 if options.board is None:
92 Usage(parser, "--board must be set")
93
asharife3668f12013-02-15 04:46:29 +000094 MakeChroot(options.chromeos_root, options.clobber_chroot)
raymes5154d7f2013-02-15 04:35:37 +000095
96 # Setup board
raymes04164a12013-02-15 04:36:03 +000097 if not os.path.isdir(options.chromeos_root + "/chroot/build/"
98 + options.board) or options.clobber_board:
raymes5f35b922013-02-15 04:35:57 +000099 force = ""
100 if options.clobber_board:
101 force = "--force"
raymes04164a12013-02-15 04:36:03 +0000102 # Run build_tc.py from binary package
103 ret = utils.RunCommand("./build_tc.py --chromeos_root=%s "
104 "--toolchain_root=%s --board=%s -B"
105 % (options.chromeos_root, options.toolchain_root,
106 options.board))
107 utils.AssertTrue(ret == 0, "build_tc.py failed")
raymes5f6be5f2013-02-15 04:36:13 +0000108 version_number = utils.GetRoot(rootdir)[1]
109 pkgdir = "/home/${USER}/toolchain_root/" + version_number + "/pkgs"
raymes5f35b922013-02-15 04:35:57 +0000110 ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root,
raymes5f6be5f2013-02-15 04:36:13 +0000111 "PKGDIR=%s ./setup_board --board=%s "
112 " --gcc_version=9999 "
113 "%s" % (pkgdir, options.board, force))
raymes5f35b922013-02-15 04:35:57 +0000114 utils.AssertTrue(ret == 0, "setup_board failed")
115 else:
116 utils.main_logger.LogOutput("Did not setup_board because it already exists")
raymesbfb57992013-02-15 04:35:45 +0000117
118 # Modify make.conf to add CFLAGS/CXXFLAGS/LDFLAGS
raymesbfb57992013-02-15 04:35:45 +0000119 ret1 = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root,
120 "[ -e /build/%s/etc/make.conf.orig ] || "
121 "sudo mv /build/%s/etc/make.conf "
122 "/build/%s/etc/make.conf.orig"
123 % (options.board, options.board, options.board))
124 makeconf = ("source make.conf.orig\\\n")
125 #"CFLAGS='%s'\\\nCXXFLAGS='%s'\\\nLDFLAGS='%s'\\\n" %
126 #(options.cflags, options.cxxflags, options.ldflags))
raymese91a6e62013-02-15 04:35:51 +0000127 ret2 = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root,
raymes5f6be5f2013-02-15 04:36:13 +0000128 "if [ -e /build/%s/etc/make.conf.orig ] ; then "
raymese91a6e62013-02-15 04:35:51 +0000129 "sudo echo -e \\\"%s\\\" | sudo tee "
130 "/build/%s/etc/make.conf > /dev/null ;"
raymes5f6be5f2013-02-15 04:36:13 +0000131 "else exit 1 ; fi"
132 % (options.board, makeconf, options.board))
raymes5f35b922013-02-15 04:35:57 +0000133
raymesbfb57992013-02-15 04:35:45 +0000134 utils.AssertTrue(ret1 == 0 and ret2 == 0, "Could not modify make.conf")
raymes5154d7f2013-02-15 04:35:37 +0000135
136 # Find Chrome browser version
raymesbfb57992013-02-15 04:35:45 +0000137 chrome_version = utils.RunCommand("%s/src/scripts/chromeos_version.sh | "
138 "grep CHROME_BUILD"
139 % options.chromeos_root, True)
140
141 ret = chrome_version[0]
142 utils.AssertTrue(ret == 0, "Could not determine Chrome browser version")
raymes5154d7f2013-02-15 04:35:37 +0000143
144 chrome_version = chrome_version[1].strip().split("=")
145 if len(chrome_version) == 2:
146 chrome_version = chrome_version[1]
147 else:
148 chrome_version = ""
149
raymes5154d7f2013-02-15 04:35:37 +0000150 # Build packages
raymesbfb57992013-02-15 04:35:45 +0000151 ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root,
raymese91a6e62013-02-15 04:35:51 +0000152 "CHROME_ORIGIN=SERVER_SOURCE CHROME_VERSION=%s "
raymesbfb57992013-02-15 04:35:45 +0000153 "./build_packages --withdev "
154 "--board=%s --withtest --withautotest"
155 % (chrome_version, options.board),
156 chrome_root="chrome_browser")
157
158 utils.AssertTrue(ret == 0, "build_packages failed")
raymes5154d7f2013-02-15 04:35:37 +0000159
160 # Build image
raymesbfb57992013-02-15 04:35:45 +0000161 ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root,
162 "./build_image --board=%s" % options.board)
163
164 utils.AssertTrue(ret == 0, "build_image failed")
raymes5154d7f2013-02-15 04:35:37 +0000165
166 # Mod image for test
raymesbfb57992013-02-15 04:35:45 +0000167 ret = ExecuteCommandInChroot(options.chromeos_root, options.toolchain_root,
168 "./mod_image_for_test.sh --board=%s"
169 % options.board)
raymes5154d7f2013-02-15 04:35:37 +0000170
raymesbfb57992013-02-15 04:35:45 +0000171 utils.AssertTrue(ret == 0, "mod_image_for_test failed")
raymes5154d7f2013-02-15 04:35:37 +0000172
173if __name__ == "__main__":
174 Main()