Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | # Copyright (C) 2013 Intel Corporation |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | |
| 7 | # Provides a class for automating build tests for projects |
| 8 | |
| 9 | import os |
| 10 | import re |
| 11 | import bb.utils |
| 12 | import subprocess |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 13 | import tempfile |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 14 | from abc import ABCMeta, abstractmethod |
| 15 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 16 | class BuildProject(metaclass=ABCMeta): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 18 | def __init__(self, d, uri, foldername=None, tmpdir=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | self.d = d |
| 20 | self.uri = uri |
| 21 | self.archive = os.path.basename(uri) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 22 | if not tmpdir: |
| 23 | tmpdir = self.d.getVar('WORKDIR') |
| 24 | if not tmpdir: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 25 | self.tempdirobj = tempfile.TemporaryDirectory(prefix='buildproject-') |
| 26 | tmpdir = self.tempdirobj.name |
| 27 | self.localarchive = os.path.join(tmpdir, self.archive) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 28 | if foldername: |
| 29 | self.fname = foldername |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 30 | else: |
| 31 | self.fname = re.sub(r'\.tar\.bz2$|\.tar\.gz$|\.tar\.xz$', '', self.archive) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | |
| 33 | # Download self.archive to self.localarchive |
| 34 | def _download_archive(self): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 35 | dl_dir = self.d.getVar("DL_DIR") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | if dl_dir and os.path.exists(os.path.join(dl_dir, self.archive)): |
| 37 | bb.utils.copyfile(os.path.join(dl_dir, self.archive), self.localarchive) |
| 38 | return |
| 39 | |
| 40 | exportvars = ['HTTP_PROXY', 'http_proxy', |
| 41 | 'HTTPS_PROXY', 'https_proxy', |
| 42 | 'FTP_PROXY', 'ftp_proxy', |
| 43 | 'FTPS_PROXY', 'ftps_proxy', |
| 44 | 'NO_PROXY', 'no_proxy', |
| 45 | 'ALL_PROXY', 'all_proxy', |
| 46 | 'SOCKS5_USER', 'SOCKS5_PASSWD'] |
| 47 | |
| 48 | cmd = '' |
| 49 | for var in exportvars: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 50 | val = self.d.getVar(var) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 51 | if val: |
| 52 | cmd = 'export ' + var + '=\"%s\"; %s' % (val, cmd) |
| 53 | |
| 54 | cmd = cmd + "wget -O %s %s" % (self.localarchive, self.uri) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 55 | subprocess.check_output(cmd, shell=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | |
| 57 | # This method should provide a way to run a command in the desired environment. |
| 58 | @abstractmethod |
| 59 | def _run(self, cmd): |
| 60 | pass |
| 61 | |
| 62 | # The timeout parameter of target.run is set to 0 to make the ssh command |
| 63 | # run with no timeout. |
| 64 | def run_configure(self, configure_args='', extra_cmds=''): |
| 65 | return self._run('cd %s; %s ./configure %s' % (self.targetdir, extra_cmds, configure_args)) |
| 66 | |
| 67 | def run_make(self, make_args=''): |
| 68 | return self._run('cd %s; make %s' % (self.targetdir, make_args)) |
| 69 | |
| 70 | def run_install(self, install_args=''): |
| 71 | return self._run('cd %s; make install %s' % (self.targetdir, install_args)) |
| 72 | |
| 73 | def clean(self): |
| 74 | self._run('rm -rf %s' % self.targetdir) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 75 | subprocess.check_call('rm -f %s' % self.localarchive, shell=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 76 | pass |
| 77 | |
| 78 | class TargetBuildProject(BuildProject): |
| 79 | |
| 80 | def __init__(self, target, d, uri, foldername=None): |
| 81 | self.target = target |
| 82 | self.targetdir = "~/" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 83 | BuildProject.__init__(self, d, uri, foldername) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 84 | |
| 85 | def download_archive(self): |
| 86 | |
| 87 | self._download_archive() |
| 88 | |
| 89 | (status, output) = self.target.copy_to(self.localarchive, self.targetdir) |
| 90 | if status != 0: |
| 91 | raise Exception("Failed to copy archive to target, output: %s" % output) |
| 92 | |
| 93 | (status, output) = self.target.run('tar xf %s%s -C %s' % (self.targetdir, self.archive, self.targetdir)) |
| 94 | if status != 0: |
| 95 | raise Exception("Failed to extract archive, output: %s" % output) |
| 96 | |
| 97 | #Change targetdir to project folder |
| 98 | self.targetdir = self.targetdir + self.fname |
| 99 | |
| 100 | # The timeout parameter of target.run is set to 0 to make the ssh command |
| 101 | # run with no timeout. |
| 102 | def _run(self, cmd): |
| 103 | return self.target.run(cmd, 0)[0] |
| 104 | |
| 105 | |
| 106 | class SDKBuildProject(BuildProject): |
| 107 | |
| 108 | def __init__(self, testpath, sdkenv, d, uri, foldername=None): |
| 109 | self.sdkenv = sdkenv |
| 110 | self.testdir = testpath |
| 111 | self.targetdir = testpath |
| 112 | bb.utils.mkdirhier(testpath) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 113 | self.datetime = d.getVar('DATETIME') |
| 114 | self.testlogdir = d.getVar("TEST_LOG_DIR") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 115 | bb.utils.mkdirhier(self.testlogdir) |
| 116 | self.logfile = os.path.join(self.testlogdir, "sdk_target_log.%s" % self.datetime) |
| 117 | BuildProject.__init__(self, d, uri, foldername, tmpdir=testpath) |
| 118 | |
| 119 | def download_archive(self): |
| 120 | |
| 121 | self._download_archive() |
| 122 | |
| 123 | cmd = 'tar xf %s%s -C %s' % (self.targetdir, self.archive, self.targetdir) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 124 | subprocess.check_output(cmd, shell=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | |
| 126 | #Change targetdir to project folder |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 127 | self.targetdir = os.path.join(self.targetdir, self.fname) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 129 | def run_configure(self, configure_args='', extra_cmds=' gnu-configize; '): |
| 130 | return super(SDKBuildProject, self).run_configure(configure_args=(configure_args or '$CONFIGURE_FLAGS'), extra_cmds=extra_cmds) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 131 | |
| 132 | def run_install(self, install_args=''): |
| 133 | return super(SDKBuildProject, self).run_install(install_args=(install_args or "DESTDIR=%s/../install" % self.targetdir)) |
| 134 | |
| 135 | def log(self, msg): |
| 136 | if self.logfile: |
| 137 | with open(self.logfile, "a") as f: |
| 138 | f.write("%s\n" % msg) |
| 139 | |
| 140 | def _run(self, cmd): |
| 141 | self.log("Running . %s; " % self.sdkenv + cmd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 142 | return subprocess.check_call(". %s; " % self.sdkenv + cmd, shell=True) |