mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 1 | __author__ = """Copyright Martin J. Bligh, 2006""" |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 2 | |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 3 | import os,os.path,shutil,urllib,copy,pickle,re,glob,time |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 4 | from autotest_utils import * |
mbligh | 1003801 | 2006-10-19 03:32:45 +0000 | [diff] [blame] | 5 | import kernel_config, test, os_dep |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 6 | |
| 7 | class kernel: |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 8 | """ Class for compiling kernels. |
| 9 | |
| 10 | Data for the object includes the src files |
| 11 | used to create the kernel, patches applied, config (base + changes), |
| 12 | the build directory itself, and logged output |
| 13 | |
| 14 | Properties: |
| 15 | job |
| 16 | Backpointer to the job object we're part of |
| 17 | autodir |
| 18 | Path to the top level autotest dir (/usr/local/autotest) |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 19 | src_dir |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 20 | <tmp_dir>/src/ |
mbligh | c49af7b | 2006-11-24 04:02:21 +0000 | [diff] [blame] | 21 | build_dir |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 22 | <tmp_dir>/linux/ |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 23 | config_dir |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 24 | <results_dir>/config/ |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 25 | log_dir |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 26 | <results_dir>/debug/ |
| 27 | results_dir |
| 28 | <results_dir>/results/ |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 29 | """ |
| 30 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 31 | autodir = '' |
| 32 | |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 33 | def __init__(self, job, base_tree, subdir, tmp_dir, build_dir, leave = False): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 34 | """Initialize the kernel build environment |
| 35 | |
| 36 | job |
| 37 | which job this build is part of |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 38 | base_tree |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 39 | base kernel tree. Can be one of the following: |
| 40 | 1. A local tarball |
| 41 | 2. A URL to a tarball |
| 42 | 3. A local directory (will symlink it) |
| 43 | 4. A shorthand expandable (eg '2.6.11-git3') |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 44 | subdir |
| 45 | subdir in the results directory (eg "build") |
| 46 | (holds config/, debug/, results/) |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 47 | tmp_dir |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 48 | |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 49 | leave |
| 50 | Boolean, whether to leave existing tmpdir or not |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 51 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 52 | self.job = job |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 53 | self.autodir = job.autodir |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 54 | |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 55 | self.src_dir = os.path.join(tmp_dir, 'src') |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 56 | self.build_dir = os.path.join(tmp_dir, build_dir) |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 57 | # created by get_kernel_tree |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 58 | self.config_dir = os.path.join(subdir, 'config') |
| 59 | self.log_dir = os.path.join(subdir, 'debug') |
| 60 | self.results_dir = os.path.join(subdir, 'results') |
| 61 | self.subdir = os.path.basename(subdir) |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 62 | |
apw | 87c65c1 | 2007-09-27 17:19:37 +0000 | [diff] [blame^] | 63 | self.installed_as = None |
| 64 | |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 65 | if not leave: |
| 66 | if os.path.isdir(self.src_dir): |
| 67 | system('rm -rf ' + self.src_dir) |
| 68 | if os.path.isdir(self.build_dir): |
| 69 | system('rm -rf ' + self.build_dir) |
| 70 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 71 | os.mkdir(self.src_dir) |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 72 | for path in [self.config_dir, self.log_dir, self.results_dir]: |
| 73 | if os.path.exists(path): |
| 74 | system('rm -rf ' + path) |
| 75 | os.mkdir(path) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 76 | |
mbligh | 4426de0 | 2006-10-10 07:18:28 +0000 | [diff] [blame] | 77 | logpath = os.path.join(self.log_dir, 'build_log') |
| 78 | self.logfile = open(logpath, 'w+') |
| 79 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 80 | self.target_arch = None |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 81 | self.build_target = 'bzImage' |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 82 | self.build_image = None |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 83 | |
mbligh | cac347a | 2007-06-02 17:21:48 +0000 | [diff] [blame] | 84 | if get_current_kernel_arch() == 'ia64': |
| 85 | self.build_target = 'all' |
| 86 | self.build_image = 'vmlinux.gz' |
| 87 | |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 88 | if leave: |
| 89 | return |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 90 | |
mbligh | 4426de0 | 2006-10-10 07:18:28 +0000 | [diff] [blame] | 91 | self.logfile.write('BASE: %s\n' % base_tree) |
apw | 2366d99 | 2007-03-12 20:35:57 +0000 | [diff] [blame] | 92 | |
| 93 | # Where we have direct version hint record that |
| 94 | # for later configuration selection. |
| 95 | shorthand = re.compile(r'^\d+\.\d+\.\d+') |
| 96 | if shorthand.match(base_tree): |
| 97 | self.base_tree_version = base_tree |
| 98 | else: |
| 99 | self.base_tree_version = None |
| 100 | |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 101 | if os.path.exists(base_tree): |
| 102 | self.get_kernel_tree(base_tree) |
| 103 | else: |
apw | 8ad56be | 2006-11-06 17:49:54 +0000 | [diff] [blame] | 104 | args = self.job.config_get('mirror.ftp_kernel_org') |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 105 | if args: |
| 106 | args = '-l ' + args |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 107 | base_components = kernelexpand(base_tree, args) |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 108 | print 'kernelexpand: ' |
| 109 | print base_components |
| 110 | self.get_kernel_tree(base_components.pop(0)) |
| 111 | if base_components: # apply remaining patches |
| 112 | self.patch(*base_components) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 113 | |
| 114 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 115 | def __record(self, fn, name, *args, **dargs): |
| 116 | try: |
| 117 | fn(*args, **dargs) |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 118 | self.job.record('GOOD', self.subdir, name) |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 119 | except Exception, detail: |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 120 | err = detail.__str__() |
| 121 | self.job.record('FAIL', self.subdir, name, err) |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 122 | raise |
| 123 | |
| 124 | |
| 125 | def _patch(self, *patches): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 126 | """Apply a list of patches (in order)""" |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 127 | if not patches: |
| 128 | return |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 129 | # if isinstance(patches, basestring): |
| 130 | # patches = [patches] |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 131 | print 'Applying patches: ', patches |
| 132 | # self.job.stdout.redirect(os.path.join(self.log_dir, 'stdout')) |
mbligh | 0763e73 | 2007-08-30 16:35:06 +0000 | [diff] [blame] | 133 | self.apply_patches(self.get_patches(patches)) |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 134 | # self.job.stdout.restore() |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 135 | |
| 136 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 137 | def patch(self, *args, **dargs): |
| 138 | self.__record(self._patch, "kernel.patch", *args, **dargs) |
| 139 | |
| 140 | |
| 141 | def _config(self, config_file = '', config_list = None, |
apw | b449c7d | 2006-12-05 12:21:44 +0000 | [diff] [blame] | 142 | defconfig = False): |
mbligh | 9305701 | 2006-08-06 15:51:56 +0000 | [diff] [blame] | 143 | self.job.stdout.redirect(os.path.join(self.log_dir, 'stdout')) |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 144 | self.set_cross_cc() |
apw | b449c7d | 2006-12-05 12:21:44 +0000 | [diff] [blame] | 145 | config = kernel_config.kernel_config(self.job, self.build_dir, |
apw | 2366d99 | 2007-03-12 20:35:57 +0000 | [diff] [blame] | 146 | self.config_dir, config_file, config_list, |
| 147 | defconfig, self.base_tree_version) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 148 | self.job.stdout.restore() |
| 149 | |
| 150 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 151 | def config(self, *args, **dargs): |
| 152 | self.__record(self._config, "kernel.config", *args, **dargs) |
| 153 | |
| 154 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 155 | def get_patches(self, patches): |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 156 | """fetch the patches to the local src_dir""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 157 | local_patches = [] |
| 158 | for patch in patches: |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 159 | dest = os.path.join(self.src_dir, basename(patch)) |
mbligh | 0763e73 | 2007-08-30 16:35:06 +0000 | [diff] [blame] | 160 | # FIXME: this isn't unique. Append something to it |
| 161 | # like wget does if it's not there? |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 162 | print "get_file %s %s %s %s" % (patch, dest, self.src_dir, basename(patch)) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 163 | get_file(patch, dest) |
mbligh | 0763e73 | 2007-08-30 16:35:06 +0000 | [diff] [blame] | 164 | # probably safer to use the command, not python library |
| 165 | md5sum = system_output('md5sum ' + dest).split()[0] |
| 166 | local_patches.append((patch, dest, md5sum)) |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 167 | return local_patches |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 168 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 169 | |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 170 | def apply_patches(self, local_patches): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 171 | """apply the list of patches, in order""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 172 | builddir = self.build_dir |
| 173 | os.chdir(builddir) |
| 174 | |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 175 | if not local_patches: |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 176 | return None |
mbligh | 0763e73 | 2007-08-30 16:35:06 +0000 | [diff] [blame] | 177 | for (spec, local, md5sum) in local_patches: |
| 178 | if local.endswith('.bz2') or local.endswith('.gz'): |
| 179 | ref = spec |
| 180 | else: |
| 181 | ref = force_copy(local, self.results_dir) |
| 182 | ref = self.job.relative_path(ref) |
| 183 | log = 'PATCH: %s %s %s\n' % (spec, ref, md5sum) |
| 184 | print log |
| 185 | cat_file_to_cmd(local, 'patch -p1 > /dev/null') |
| 186 | self.logfile.write(log) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 187 | |
| 188 | |
| 189 | def get_kernel_tree(self, base_tree): |
mbligh | c49af7b | 2006-11-24 04:02:21 +0000 | [diff] [blame] | 190 | """Extract/link base_tree to self.build_dir""" |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 191 | |
| 192 | # if base_tree is a dir, assume uncompressed kernel |
| 193 | if os.path.isdir(base_tree): |
| 194 | print 'Symlinking existing kernel source' |
mbligh | c49af7b | 2006-11-24 04:02:21 +0000 | [diff] [blame] | 195 | os.symlink(base_tree, self.build_dir) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 196 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 197 | # otherwise, extract tarball |
| 198 | else: |
mbligh | c49af7b | 2006-11-24 04:02:21 +0000 | [diff] [blame] | 199 | os.chdir(os.path.dirname(self.src_dir)) |
| 200 | # Figure out local destination for tarball |
| 201 | tarball = os.path.join(self.src_dir, os.path.basename(base_tree)) |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 202 | get_file(base_tree, tarball) |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 203 | print 'Extracting kernel tarball:', tarball, '...' |
mbligh | c49af7b | 2006-11-24 04:02:21 +0000 | [diff] [blame] | 204 | extract_tarball_to_dir(tarball, self.build_dir) |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 205 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 206 | |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 207 | def extraversion(self, tag, append=1): |
| 208 | os.chdir(self.build_dir) |
mbligh | 4426de0 | 2006-10-10 07:18:28 +0000 | [diff] [blame] | 209 | extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = ' |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 210 | if append: |
mbligh | 4426de0 | 2006-10-10 07:18:28 +0000 | [diff] [blame] | 211 | p = extraversion_sub + '\\1-%s/' % tag |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 212 | else: |
mbligh | 4426de0 | 2006-10-10 07:18:28 +0000 | [diff] [blame] | 213 | p = extraversion_sub + '-%s/' % tag |
mbligh | 4c3fe4a | 2007-07-31 17:59:21 +0000 | [diff] [blame] | 214 | system('mv Makefile Makefile.old') |
| 215 | system('sed "%s" < Makefile.old > Makefile' % p) |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 216 | |
| 217 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 218 | def _build(self, make_opts = '', logfile = '', extraversion='autotest'): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 219 | """build the kernel |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 220 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 221 | make_opts |
| 222 | additional options to make, if any |
| 223 | """ |
mbligh | 1003801 | 2006-10-19 03:32:45 +0000 | [diff] [blame] | 224 | os_dep.commands('gcc', 'make') |
mbligh | 6d4c941 | 2006-09-13 23:08:44 +0000 | [diff] [blame] | 225 | if logfile == '': |
| 226 | logfile = os.path.join(self.log_dir, 'kernel_build') |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 227 | os.chdir(self.build_dir) |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 228 | if extraversion: |
| 229 | self.extraversion(extraversion) |
mbligh | 9305701 | 2006-08-06 15:51:56 +0000 | [diff] [blame] | 230 | print os.path.join(self.log_dir, 'stdout') |
mbligh | 6d4c941 | 2006-09-13 23:08:44 +0000 | [diff] [blame] | 231 | self.job.stdout.redirect(logfile + '.stdout') |
| 232 | self.job.stderr.redirect(logfile + '.stderr') |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 233 | self.set_cross_cc() |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 234 | # setup_config_file(config_file, config_overrides) |
apw | c784610 | 2006-04-06 18:22:13 +0000 | [diff] [blame] | 235 | |
| 236 | # Not needed on 2.6, but hard to tell -- handle failure |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 237 | system('make dep', ignorestatus=1) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 238 | threads = 2 * count_cpus() |
mbligh | 6d4c941 | 2006-09-13 23:08:44 +0000 | [diff] [blame] | 239 | build_string = 'make -j %d %s %s' % (threads, make_opts, |
| 240 | self.build_target) |
| 241 | # eg make bzImage, or make zImage |
| 242 | print build_string |
| 243 | system(build_string) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 244 | if kernel_config.modules_needed('.config'): |
apw | d470197 | 2006-10-16 09:24:56 +0000 | [diff] [blame] | 245 | system('make -j %d modules' % (threads)) |
apw | c784610 | 2006-04-06 18:22:13 +0000 | [diff] [blame] | 246 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 247 | self.job.stdout.restore() |
| 248 | self.job.stderr.restore() |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 249 | |
mbligh | 4426de0 | 2006-10-10 07:18:28 +0000 | [diff] [blame] | 250 | kernel_version = self.get_kernel_build_ver() |
| 251 | kernel_version = re.sub('-autotest', '', kernel_version) |
| 252 | self.logfile.write('BUILD VERSION: %s\n' % kernel_version) |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 253 | |
| 254 | force_copy(self.build_dir+'/System.map', self.results_dir) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 255 | |
| 256 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 257 | def build(self, *args, **dargs): |
| 258 | self.__record(self._build, "kernel.build", *args, **dargs) |
| 259 | |
| 260 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 261 | def build_timed(self, threads, timefile = '/dev/null', make_opts = ''): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 262 | """time the bulding of the kernel""" |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 263 | os.chdir(self.build_dir) |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 264 | self.set_cross_cc() |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 265 | print "make clean" |
| 266 | system('make clean') |
mbligh | a2bb9d6 | 2006-10-09 16:26:03 +0000 | [diff] [blame] | 267 | build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" % (timefile, make_opts, threads) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 268 | print build_string |
apw | c784610 | 2006-04-06 18:22:13 +0000 | [diff] [blame] | 269 | system(build_string) |
| 270 | if (not os.path.isfile('vmlinux')): |
| 271 | raise TestError("no vmlinux found, kernel build failed") |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 272 | |
| 273 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 274 | def _clean(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 275 | """make clean in the kernel tree""" |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 276 | os.chdir(self.build_dir) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 277 | print "make clean" |
apw | c784610 | 2006-04-06 18:22:13 +0000 | [diff] [blame] | 278 | system('make clean') |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 279 | |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 280 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 281 | def clean(self, *args, **dargs): |
| 282 | self.__record(self._clean, "kernel.clean", *args, **dargs) |
| 283 | |
| 284 | |
| 285 | def _mkinitrd(self, version, image, system_map, initrd): |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 286 | """Build kernel initrd image. |
| 287 | Try to use distro specific way to build initrd image. |
| 288 | Parameters: |
| 289 | version |
| 290 | new kernel version |
| 291 | image |
| 292 | new kernel image file |
| 293 | system_map |
| 294 | System.map file |
| 295 | initrd |
| 296 | initrd image file to build |
| 297 | """ |
| 298 | vendor = get_os_vendor() |
| 299 | |
| 300 | if os.path.isfile(initrd): |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 301 | print "Existing %s file, will remove it." % initrd |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 302 | os.remove(initrd) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 303 | |
apw | e43a30b | 2007-09-25 16:51:30 +0000 | [diff] [blame] | 304 | args = self.job.config_get('kernel.mkinitrd_extra_args') |
| 305 | |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 306 | if vendor in ['Red Hat', 'Fedora Core']: |
apw | e43a30b | 2007-09-25 16:51:30 +0000 | [diff] [blame] | 307 | system('mkinitrd %s %s %s' % (args, initrd, version)) |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 308 | elif vendor in ['SUSE']: |
apw | e43a30b | 2007-09-25 16:51:30 +0000 | [diff] [blame] | 309 | system('mkinitrd %s -k %s -i %s -M %s' % (args, image, initrd, system_map)) |
apw | c898a1f | 2007-02-28 15:27:22 +0000 | [diff] [blame] | 310 | elif vendor in ['Debian', 'Ubuntu']: |
| 311 | if os.path.isfile('/usr/sbin/mkinitrd'): |
| 312 | cmd = '/usr/sbin/mkinitrd' |
| 313 | elif os.path.isfile('/usr/sbin/mkinitramfs'): |
| 314 | cmd = '/usr/sbin/mkinitramfs' |
| 315 | else: |
| 316 | raise TestError('No Debian initrd builder') |
apw | e43a30b | 2007-09-25 16:51:30 +0000 | [diff] [blame] | 317 | system('%s %s -o %s %s' % (cmd, args, initrd, version)) |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 318 | else: |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 319 | raise TestError('Unsupported vendor %s' % vendor) |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 320 | |
| 321 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 322 | def mkinitrd(self, *args, **dargs): |
| 323 | self.__record(self._mkinitrd, "kernel.mkinitrd", *args, **dargs) |
| 324 | |
| 325 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 326 | def set_build_image(self, image): |
| 327 | self.build_image = image |
| 328 | |
| 329 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 330 | def _install(self, tag='autotest', prefix = '/'): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 331 | """make install in the kernel tree""" |
apw | 87c65c1 | 2007-09-27 17:19:37 +0000 | [diff] [blame^] | 332 | |
| 333 | # Record that we have installed the kernel, and |
| 334 | # the tag under which we installed it. |
| 335 | self.installed_as = tag |
| 336 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 337 | os.chdir(self.build_dir) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 338 | |
mbligh | 6a1d4db | 2006-10-06 04:30:16 +0000 | [diff] [blame] | 339 | if not os.path.isdir(prefix): |
| 340 | os.mkdir(prefix) |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 341 | self.boot_dir = os.path.join(prefix, 'boot') |
| 342 | if not os.path.isdir(self.boot_dir): |
| 343 | os.mkdir(self.boot_dir) |
mbligh | 0ad6558 | 2006-10-06 04:16:36 +0000 | [diff] [blame] | 344 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 345 | if not self.build_image: |
| 346 | images = glob.glob('arch/*/boot/' + self.build_target) |
| 347 | if len(images): |
| 348 | self.build_image = images[0] |
| 349 | else: |
| 350 | self.build_image = self.build_target |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 351 | |
| 352 | # remember installed files |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 353 | self.vmlinux = self.boot_dir + '/vmlinux-' + tag |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 354 | if (self.build_image != 'vmlinux'): |
apw | 9a61c5b | 2006-11-28 10:03:15 +0000 | [diff] [blame] | 355 | self.image = self.boot_dir + '/vmlinuz-' + tag |
| 356 | else: |
| 357 | self.image = self.vmlinux |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 358 | self.system_map = self.boot_dir + '/System.map-' + tag |
| 359 | self.config = self.boot_dir + '/config-' + tag |
| 360 | self.initrd = '' |
| 361 | |
| 362 | # copy to boot dir |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 363 | force_copy('vmlinux', self.vmlinux) |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 364 | if (self.build_image != 'vmlinux'): |
| 365 | force_copy(self.build_image, self.image) |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 366 | force_copy('System.map', self.system_map) |
| 367 | force_copy('.config', self.config) |
| 368 | |
mbligh | 6a1d4db | 2006-10-06 04:30:16 +0000 | [diff] [blame] | 369 | if not kernel_config.modules_needed('.config'): |
| 370 | return |
| 371 | |
| 372 | system('make modules_install INSTALL_MOD_PATH=%s' % prefix) |
| 373 | if prefix == '/': |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 374 | self.initrd = self.boot_dir + '/initrd-' + tag |
| 375 | self.mkinitrd(self.get_kernel_build_ver(), self.image, \ |
| 376 | self.system_map, self.initrd) |
| 377 | |
| 378 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 379 | def install(self, *args, **dargs): |
| 380 | self.__record(self._install, "kernel.install", *args, **dargs) |
| 381 | |
| 382 | |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 383 | def add_to_bootloader(self, tag='autotest', args=''): |
| 384 | """ add this kernel to bootloader, taking an |
| 385 | optional parameter of space separated parameters |
| 386 | e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off') |
| 387 | """ |
| 388 | |
| 389 | # remove existing entry if present |
| 390 | self.job.bootloader.remove_kernel(tag) |
| 391 | |
apw | cbe3257 | 2006-11-28 10:00:23 +0000 | [diff] [blame] | 392 | # pull the base argument set from the job config, |
apw | cbe3257 | 2006-11-28 10:00:23 +0000 | [diff] [blame] | 393 | baseargs = self.job.config_get('boot.default_args') |
mbligh | 78bc05e | 2006-12-25 02:29:59 +0000 | [diff] [blame] | 394 | if baseargs: |
| 395 | args = baseargs + " " + args |
| 396 | |
| 397 | # otherwise populate from /proc/cmdline |
| 398 | # if not baseargs: |
| 399 | # baseargs = open('/proc/cmdline', 'r').readline().strip() |
| 400 | # NOTE: This is unnecessary, because boottool does it. |
apw | cbe3257 | 2006-11-28 10:00:23 +0000 | [diff] [blame] | 401 | |
mbligh | 78bc05e | 2006-12-25 02:29:59 +0000 | [diff] [blame] | 402 | root = None |
| 403 | roots = [x for x in args.split() if x.startswith('root=')] |
| 404 | if roots: |
| 405 | root = re.sub('^root=', '', roots[0]) |
| 406 | arglist = [x for x in args.split() if not x.startswith('root=')] |
| 407 | args = ' '.join(arglist) |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 408 | |
mbligh | 78bc05e | 2006-12-25 02:29:59 +0000 | [diff] [blame] | 409 | # add the kernel entry |
| 410 | # add_kernel(image, title='autotest', initrd='') |
| 411 | self.job.bootloader.add_kernel(self.image, tag, self.initrd, \ |
| 412 | args = args, root = root) |
mbligh | 6a1d4db | 2006-10-06 04:30:16 +0000 | [diff] [blame] | 413 | |
mbligh | 201aa89 | 2006-10-29 04:02:05 +0000 | [diff] [blame] | 414 | |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 415 | def get_kernel_build_arch(self, arch=None): |
mbligh | 201aa89 | 2006-10-29 04:02:05 +0000 | [diff] [blame] | 416 | """ |
| 417 | Work out the current kernel architecture (as a kernel arch) |
| 418 | """ |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 419 | if not arch: |
| 420 | arch = get_current_kernel_arch() |
| 421 | if re.match('i.86', arch): |
| 422 | return 'i386' |
| 423 | elif re.match('sun4u', arch): |
| 424 | return 'sparc64' |
| 425 | elif re.match('arm.*', arch): |
| 426 | return 'arm' |
| 427 | elif re.match('sa110', arch): |
| 428 | return 'arm' |
| 429 | elif re.match('s390x', arch): |
| 430 | return 's390' |
| 431 | elif re.match('parisc64', arch): |
| 432 | return 'parisc' |
| 433 | elif re.match('ppc.*', arch): |
| 434 | return 'powerpc' |
| 435 | elif re.match('mips.*', arch): |
| 436 | return 'mips' |
| 437 | else: |
| 438 | return arch |
| 439 | |
mbligh | 6a1d4db | 2006-10-06 04:30:16 +0000 | [diff] [blame] | 440 | |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 441 | def get_kernel_build_release(self): |
| 442 | releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*'); |
| 443 | versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*'); |
| 444 | |
| 445 | release = None |
| 446 | version = None |
| 447 | |
| 448 | for file in [ self.build_dir + "/include/linux/version.h", |
| 449 | self.build_dir + "/include/linux/utsrelease.h", |
| 450 | self.build_dir + "/include/linux/compile.h" ]: |
| 451 | if os.path.exists(file): |
| 452 | fd = open(file, 'r') |
| 453 | for line in fd.readlines(): |
| 454 | m = releasem.match(line) |
| 455 | if m: |
| 456 | release = m.groups()[0] |
| 457 | m = versionm.match(line) |
| 458 | if m: |
| 459 | version = m.groups()[0] |
| 460 | fd.close() |
| 461 | |
| 462 | return (release, version) |
| 463 | |
| 464 | |
| 465 | def get_kernel_build_ident(self): |
| 466 | (release, version) = self.get_kernel_build_release() |
| 467 | |
| 468 | if not release or not version: |
| 469 | raise JobError('kernel has no identity') |
| 470 | |
| 471 | return release + '::' + version |
| 472 | |
| 473 | |
| 474 | def boot(self, args='', once=True, ident=1): |
apw | 1b5dc36 | 2006-10-31 11:24:26 +0000 | [diff] [blame] | 475 | """ install and boot this kernel, do not care how |
| 476 | just make it happen. |
| 477 | """ |
| 478 | |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 479 | # If we can check the kernel identity do so. |
| 480 | if ident: |
| 481 | when = int(time.time()) |
| 482 | ident = self.get_kernel_build_ident() |
| 483 | args += " IDENT=%d" % (when) |
| 484 | |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 485 | self.job.next_step_prepend(["job.kernel_check_ident", |
| 486 | when, ident, self.subdir]) |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 487 | |
apw | 87c65c1 | 2007-09-27 17:19:37 +0000 | [diff] [blame^] | 488 | # Check if the kernel has been installed, if not install |
| 489 | # as the default tag and boot that. |
| 490 | if not self.installed_as: |
| 491 | self.install() |
| 492 | |
| 493 | # Boot the selected tag. |
| 494 | self.add_to_bootloader(args=args, tag=self.installed_as) |
mbligh | da76b21 | 2007-07-19 16:50:41 +0000 | [diff] [blame] | 495 | if once: |
apw | 87c65c1 | 2007-09-27 17:19:37 +0000 | [diff] [blame^] | 496 | self.job.bootloader.boot_once(self.installed_as) |
apw | 1b5dc36 | 2006-10-31 11:24:26 +0000 | [diff] [blame] | 497 | |
| 498 | # Boot it. |
| 499 | self.job.reboot() |
| 500 | |
| 501 | |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 502 | def get_kernel_build_ver(self): |
mbligh | e11f5fc | 2006-10-04 04:42:22 +0000 | [diff] [blame] | 503 | """Check Makefile and .config to return kernel version""" |
| 504 | version = patchlevel = sublevel = extraversion = localversion = '' |
| 505 | |
| 506 | for line in open(self.build_dir + '/Makefile', 'r').readlines(): |
| 507 | if line.startswith('VERSION'): |
| 508 | version = line[line.index('=') + 1:].strip() |
| 509 | if line.startswith('PATCHLEVEL'): |
| 510 | patchlevel = line[line.index('=') + 1:].strip() |
| 511 | if line.startswith('SUBLEVEL'): |
| 512 | sublevel = line[line.index('=') + 1:].strip() |
| 513 | if line.startswith('EXTRAVERSION'): |
| 514 | extraversion = line[line.index('=') + 1:].strip() |
| 515 | |
| 516 | for line in open(self.build_dir + '/.config', 'r').readlines(): |
| 517 | if line.startswith('CONFIG_LOCALVERSION='): |
| 518 | localversion = line.rstrip().split('"')[1] |
| 519 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 520 | return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion) |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 521 | |
| 522 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 523 | def set_build_target(self, build_target): |
| 524 | if build_target: |
| 525 | self.build_target = build_target |
| 526 | print 'BUILD TARGET: %s' % self.build_target |
| 527 | |
| 528 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 529 | def set_cross_cc(self, target_arch=None, cross_compile=None, |
| 530 | build_target='bzImage'): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 531 | """Set up to cross-compile. |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 532 | This is broken. We need to work out what the default |
| 533 | compile produces, and if not, THEN set the cross |
| 534 | compiler. |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 535 | """ |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 536 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 537 | if self.target_arch: |
| 538 | return |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 539 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 540 | # if someone has set build_target, don't clobber in set_cross_cc |
| 541 | # run set_build_target before calling set_cross_cc |
| 542 | if not self.build_target: |
| 543 | self.set_build_target(build_target) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 544 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 545 | # If no 'target_arch' given assume native compilation |
| 546 | if target_arch == None: |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 547 | target_arch = get_current_kernel_arch() |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 548 | if target_arch == 'ppc64': |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 549 | if self.build_target == 'bzImage': |
apw | 9a61c5b | 2006-11-28 10:03:15 +0000 | [diff] [blame] | 550 | self.build_target = 'vmlinux' |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 551 | |
| 552 | if not cross_compile: |
| 553 | cross_compile = self.job.config_get('kernel.cross_cc') |
| 554 | |
| 555 | if cross_compile: |
| 556 | os.environ['CROSS_COMPILE'] = cross_compile |
| 557 | else: |
| 558 | if os.environ.has_key('CROSS_COMPILE'): |
| 559 | del os.environ['CROSS_COMPILE'] |
| 560 | |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 561 | return # HACK. Crap out for now. |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 562 | |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 563 | # At this point I know what arch I *want* to build for |
| 564 | # but have no way of working out what arch the default |
| 565 | # compiler DOES build for. |
| 566 | |
| 567 | # Oh, and BTW, install_package() doesn't exist yet. |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 568 | |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 569 | if target_arch == 'ppc64': |
| 570 | install_package('ppc64-cross') |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 571 | cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin') |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 572 | |
| 573 | elif target_arch == 'x86_64': |
| 574 | install_package('x86_64-cross') |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 575 | cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin') |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 576 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 577 | os.environ['ARCH'] = self.target_arch = target_arch |
| 578 | |
| 579 | self.cross_compile = cross_compile |
| 580 | if self.cross_compile: |
| 581 | os.environ['CROSS_COMPILE'] = self.cross_compile |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 582 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 583 | |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 584 | def pickle_dump(self, filename): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 585 | """dump a pickle of ourself out to the specified filename |
| 586 | |
| 587 | we can't pickle the backreference to job (it contains fd's), |
mbligh | 709bb9b | 2006-10-12 04:32:16 +0000 | [diff] [blame] | 588 | nor would we want to. Same for logfile (fd's). |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 589 | """ |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 590 | temp = copy.copy(self) |
| 591 | temp.job = None |
mbligh | 709bb9b | 2006-10-12 04:32:16 +0000 | [diff] [blame] | 592 | temp.logfile = None |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 593 | pickle.dump(temp, open(filename, 'w')) |