blob: 4dd57dfb1f0bde2ec033222bfca5fec3e9702f48 [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
asharife3668f12013-02-15 04:46:29 +000021
raymes5154d7f2013-02-15 04:35:37 +000022def Usage(parser, message):
23 print "ERROR: " + message
24 parser.print_help()
25 sys.exit(0)
26
raymes49fd5a32013-02-15 04:55:27 +000027#TODO(raymes): move this to a common utils file.
raymes5154d7f2013-02-15 04:35:37 +000028def ExecuteCommandInChroot(chromeos_root, toolchain_root, command,
raymes81d88962013-02-15 04:56:26 +000029 return_output=False, full_mount=False):
asharife3668f12013-02-15 04:46:29 +000030 """Executes a command in the chroot."""
raymes49fd5a32013-02-15 04:55:27 +000031 global cmd_executer
32 cmd_executer = command_executer.GetCommandExecuter()
asharif01e29a52013-02-15 04:56:41 +000033 chromeos_root = os.path.expanduser(chromeos_root)
raymes49fd5a32013-02-15 04:55:27 +000034
raymes49fd5a32013-02-15 04:55:27 +000035 if toolchain_root is None:
asharif708d57c2013-02-15 04:56:23 +000036 cmd_file = "enter_chroot.cmd"
37 cmd_file_path = chromeos_root + "/src/scripts/" + cmd_file
38 f = open(cmd_file_path, "w")
39 f.write(command)
40 logger.GetLogger().LogCmd(command)
41 f.close()
42 retval = cmd_executer.RunCommand("chmod +x " + cmd_file_path)
43 utils.AssertTrue(retval == 0, "chmod +x failed!")
raymes49fd5a32013-02-15 04:55:27 +000044 return cmd_executer.RunCommand(chromeos_root +
asharif708d57c2013-02-15 04:56:23 +000045 "/src/scripts/enter_chroot.sh -- ./%s"
46 % cmd_file)
raymes49fd5a32013-02-15 04:55:27 +000047 else:
asharif01e29a52013-02-15 04:56:41 +000048 toolchain_root = os.path.expanduser(toolchain_root)
raymes49fd5a32013-02-15 04:55:27 +000049 argv = [os.path.dirname(os.path.abspath(__file__)) + "/tc_enter_chroot.py",
50 "--chromeos_root=" + chromeos_root,
51 "--toolchain_root=" + toolchain_root,
raymes49fd5a32013-02-15 04:55:27 +000052 "\n" + command]
raymes81d88962013-02-15 04:56:26 +000053 if not full_mount:
54 argv.append("-s")
raymes9b8305c2013-02-15 04:56:27 +000055 return tc_enter_chroot.Main(argv, return_output)
asharife3668f12013-02-15 04:46:29 +000056
57
58def MakeChroot(chromeos_root, clobber_chroot=False):
59 """Make a chroot given a chromeos checkout."""
60 if (not os.path.isdir(chromeos_root + "/chroot")
61 or clobber_chroot):
62 commands = []
63 commands.append("cd " + chromeos_root + "/src/scripts")
64 clobber_chroot = ""
65 if clobber_chroot:
66 clobber_chroot = "--replace"
67 commands.append("./make_chroot --fast " + clobber_chroot)
asharif967d7002013-02-15 04:51:00 +000068 ret = command_executer.GetCommandExecuter().RunCommands(commands)
asharife3668f12013-02-15 04:46:29 +000069 utils.AssertTrue(ret == 0, "make_chroot failed")
70 else:
raymes01959ae2013-02-15 04:50:07 +000071 logger.GetLogger().LogOutput("Did not make_chroot because it already exists")
raymes5154d7f2013-02-15 04:35:37 +000072
73
bjanakiraman6496e5f2013-02-15 04:50:58 +000074def Main(argv):
raymes5154d7f2013-02-15 04:35:37 +000075 """Build ChromeOS."""
76 # Common initializations
asharif5a9bb462013-02-15 04:50:57 +000077 cmd_executer = command_executer.GetCommandExecuter()
raymes5154d7f2013-02-15 04:35:37 +000078
79 parser = optparse.OptionParser()
80 parser.add_option("--chromeos_root", dest="chromeos_root",
81 help="Target directory for ChromeOS installation.")
raymes5154d7f2013-02-15 04:35:37 +000082 parser.add_option("--clobber_chroot", dest="clobber_chroot",
83 action="store_true", help=
84 "Delete the chroot and start fresh", default=False)
85 parser.add_option("--clobber_board", dest="clobber_board",
86 action="store_true",
87 help="Delete the board and start fresh", default=False)
asharif80b47dc2013-02-15 06:31:19 +000088 parser.add_option("--rebuild", dest="rebuild",
89 action="store_true",
90 help="Rebuild all board packages except the toolchain.",
91 default=False)
raymes9b8305c2013-02-15 04:56:27 +000092 parser.add_option("--cflags", dest="cflags", default="",
raymes5154d7f2013-02-15 04:35:37 +000093 help="CFLAGS for the ChromeOS packages")
raymes9b8305c2013-02-15 04:56:27 +000094 parser.add_option("--cxxflags", dest="cxxflags", default="",
raymes5154d7f2013-02-15 04:35:37 +000095 help="CXXFLAGS for the ChromeOS packages")
raymes9b8305c2013-02-15 04:56:27 +000096 parser.add_option("--ldflags", dest="ldflags", default="",
raymes5154d7f2013-02-15 04:35:37 +000097 help="LDFLAGS for the ChromeOS packages")
98 parser.add_option("--board", dest="board",
99 help="ChromeOS target board, e.g. x86-generic")
asharif0341d302013-02-15 04:56:38 +0000100 parser.add_option("--vanilla", dest="vanilla",
asharifb1752c82013-02-15 04:56:37 +0000101 default=False,
102 action="store_true",
asharif0341d302013-02-15 04:56:38 +0000103 help="Use default ChromeOS toolchain.")
raymes5154d7f2013-02-15 04:35:37 +0000104
bjanakiraman6496e5f2013-02-15 04:50:58 +0000105 options = parser.parse_args(argv[1:])[0]
raymes5154d7f2013-02-15 04:35:37 +0000106
107 if options.chromeos_root is None:
108 Usage(parser, "--chromeos_root must be set")
109
raymes5154d7f2013-02-15 04:35:37 +0000110 if options.board is None:
111 Usage(parser, "--board must be set")
112
asharif80b47dc2013-02-15 06:31:19 +0000113 build_packages_env = ""
114 if options.rebuild == True:
115 build_packages_env = "EXTRA_BOARD_FLAGS=-e"
116
asharif01e29a52013-02-15 04:56:41 +0000117 options.chromeos_root = os.path.expanduser(options.chromeos_root)
asharif01e29a52013-02-15 04:56:41 +0000118
asharife3668f12013-02-15 04:46:29 +0000119 MakeChroot(options.chromeos_root, options.clobber_chroot)
raymes5154d7f2013-02-15 04:35:37 +0000120
asharife0cc3052013-02-15 05:20:48 +0000121 build_packages_command = utils.GetBuildPackagesCommand(options.board)
122 build_image_command = utils.GetBuildImageCommand(options.board)
123 mod_image_command = utils.GetModImageForTestCommand(options.board)
asharifca8c5ef2013-02-15 04:57:02 +0000124
asharif0341d302013-02-15 04:56:38 +0000125 if options.vanilla == True:
asharife0cc3052013-02-15 05:20:48 +0000126 command = utils.GetSetupBoardCommand(options.board,
127 usepkg=False,
128 force=options.clobber_board)
asharifc380f612013-02-15 09:13:07 +0000129 command += "; " + build_packages_env + " " + build_packages_command
asharifca8c5ef2013-02-15 04:57:02 +0000130 command += "&& " + build_image_command
131 command += "&& " + mod_image_command
asharifb1752c82013-02-15 04:56:37 +0000132 ret = ExecuteCommandInChroot(options.chromeos_root, None, command)
133 return ret
134
raymes5154d7f2013-02-15 04:35:37 +0000135 # Setup board
raymes04164a12013-02-15 04:36:03 +0000136 if not os.path.isdir(options.chromeos_root + "/chroot/build/"
137 + options.board) or options.clobber_board:
raymes04164a12013-02-15 04:36:03 +0000138 # Run build_tc.py from binary package
bjanakiraman6496e5f2013-02-15 04:50:58 +0000139 rootdir = utils.GetRoot(argv[0])[0]
raymes5f6be5f2013-02-15 04:36:13 +0000140 version_number = utils.GetRoot(rootdir)[1]
asharif977cd6b2013-02-15 04:56:49 +0000141 ret = ExecuteCommandInChroot(options.chromeos_root, None,
asharife0cc3052013-02-15 05:20:48 +0000142 utils.GetSetupBoardCommand(options.board,
143 gcc_version="9999",
144 binutils_version="9999",
145 force=options.clobber_board))
raymes5f35b922013-02-15 04:35:57 +0000146 utils.AssertTrue(ret == 0, "setup_board failed")
147 else:
raymes01959ae2013-02-15 04:50:07 +0000148 logger.GetLogger().LogOutput("Did not setup_board "
149 "because it already exists")
raymesbfb57992013-02-15 04:35:45 +0000150
raymes9b8305c2013-02-15 04:56:27 +0000151 # Build packages
asharif977cd6b2013-02-15 04:56:49 +0000152 ret = ExecuteCommandInChroot(options.chromeos_root, None,
raymesaa351762013-02-15 04:56:51 +0000153 "CFLAGS=\"$(portageq-%s envvar CFLAGS) %s\" "
154 "LDFLAGS=\"$(portageq-%s envvar LDFLAGS) %s\" "
155 "CXXFLAGS=\"$(portageq-%s envvar CXXFLAGS) %s\" "
asharifc0f71932013-02-15 04:56:18 +0000156 "CHROME_ORIGIN=SERVER_SOURCE "
asharif80b47dc2013-02-15 06:31:19 +0000157 "%s "
asharifca8c5ef2013-02-15 04:57:02 +0000158 "%s"
asharif253e88b2013-02-15 05:15:34 +0000159 % (options.board, options.cflags,
160 options.board, options.cxxflags,
161 options.board, options.ldflags,
asharif80b47dc2013-02-15 06:31:19 +0000162 build_packages_env,
asharif0e0e2682013-02-15 05:15:29 +0000163 build_packages_command))
raymesbfb57992013-02-15 04:35:45 +0000164
165 utils.AssertTrue(ret == 0, "build_packages failed")
raymes5154d7f2013-02-15 04:35:37 +0000166
167 # Build image
asharif977cd6b2013-02-15 04:56:49 +0000168 ret = ExecuteCommandInChroot(options.chromeos_root, None,
asharifca8c5ef2013-02-15 04:57:02 +0000169 build_image_command)
raymesbfb57992013-02-15 04:35:45 +0000170
171 utils.AssertTrue(ret == 0, "build_image failed")
raymes5154d7f2013-02-15 04:35:37 +0000172
173 # Mod image for test
asharif977cd6b2013-02-15 04:56:49 +0000174 ret = ExecuteCommandInChroot(options.chromeos_root, None,
asharifca8c5ef2013-02-15 04:57:02 +0000175 mod_image_command)
raymes5154d7f2013-02-15 04:35:37 +0000176
raymesbfb57992013-02-15 04:35:45 +0000177 utils.AssertTrue(ret == 0, "mod_image_for_test failed")
asharif253e88b2013-02-15 05:15:34 +0000178 return 0
raymes5154d7f2013-02-15 04:35:37 +0000179
180if __name__ == "__main__":
bjanakiraman6496e5f2013-02-15 04:50:58 +0000181 Main(sys.argv)