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