blob: b8db7b2aca0486f3d653f88921f027b460c0d833 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# Copyright (C) 2013 Intel Corporation
2#
3# Released under the MIT license (see COPYING.MIT)
4
5# Provides a class for automating build tests for projects
6
7import os
8import re
9import bb.utils
10import subprocess
Brad Bishop6e60e8b2018-02-01 10:27:11 -050011import tempfile
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012from abc import ABCMeta, abstractmethod
13
Patrick Williamsc0f7c042017-02-23 20:41:17 -060014class BuildProject(metaclass=ABCMeta):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015
Brad Bishop6e60e8b2018-02-01 10:27:11 -050016 def __init__(self, d, uri, foldername=None, tmpdir=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017 self.d = d
18 self.uri = uri
19 self.archive = os.path.basename(uri)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050020 if not tmpdir:
21 tmpdir = self.d.getVar('WORKDIR')
22 if not tmpdir:
Brad Bishopf86d0552018-12-04 14:18:15 -080023 self.tempdirobj = tempfile.TemporaryDirectory(prefix='buildproject-')
24 tmpdir = self.tempdirobj.name
25 self.localarchive = os.path.join(tmpdir, self.archive)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026 if foldername:
27 self.fname = foldername
Patrick Williamsc0f7c042017-02-23 20:41:17 -060028 else:
29 self.fname = re.sub(r'\.tar\.bz2$|\.tar\.gz$|\.tar\.xz$', '', self.archive)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030
31 # Download self.archive to self.localarchive
32 def _download_archive(self):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050033 dl_dir = self.d.getVar("DL_DIR")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034 if dl_dir and os.path.exists(os.path.join(dl_dir, self.archive)):
35 bb.utils.copyfile(os.path.join(dl_dir, self.archive), self.localarchive)
36 return
37
38 exportvars = ['HTTP_PROXY', 'http_proxy',
39 'HTTPS_PROXY', 'https_proxy',
40 'FTP_PROXY', 'ftp_proxy',
41 'FTPS_PROXY', 'ftps_proxy',
42 'NO_PROXY', 'no_proxy',
43 'ALL_PROXY', 'all_proxy',
44 'SOCKS5_USER', 'SOCKS5_PASSWD']
45
46 cmd = ''
47 for var in exportvars:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050048 val = self.d.getVar(var)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049 if val:
50 cmd = 'export ' + var + '=\"%s\"; %s' % (val, cmd)
51
52 cmd = cmd + "wget -O %s %s" % (self.localarchive, self.uri)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050053 subprocess.check_output(cmd, shell=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054
55 # This method should provide a way to run a command in the desired environment.
56 @abstractmethod
57 def _run(self, cmd):
58 pass
59
60 # The timeout parameter of target.run is set to 0 to make the ssh command
61 # run with no timeout.
62 def run_configure(self, configure_args='', extra_cmds=''):
63 return self._run('cd %s; %s ./configure %s' % (self.targetdir, extra_cmds, configure_args))
64
65 def run_make(self, make_args=''):
66 return self._run('cd %s; make %s' % (self.targetdir, make_args))
67
68 def run_install(self, install_args=''):
69 return self._run('cd %s; make install %s' % (self.targetdir, install_args))
70
71 def clean(self):
72 self._run('rm -rf %s' % self.targetdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050073 subprocess.check_call('rm -f %s' % self.localarchive, shell=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074 pass
75
76class TargetBuildProject(BuildProject):
77
78 def __init__(self, target, d, uri, foldername=None):
79 self.target = target
80 self.targetdir = "~/"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050081 BuildProject.__init__(self, d, uri, foldername)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082
83 def download_archive(self):
84
85 self._download_archive()
86
87 (status, output) = self.target.copy_to(self.localarchive, self.targetdir)
88 if status != 0:
89 raise Exception("Failed to copy archive to target, output: %s" % output)
90
91 (status, output) = self.target.run('tar xf %s%s -C %s' % (self.targetdir, self.archive, self.targetdir))
92 if status != 0:
93 raise Exception("Failed to extract archive, output: %s" % output)
94
95 #Change targetdir to project folder
96 self.targetdir = self.targetdir + self.fname
97
98 # The timeout parameter of target.run is set to 0 to make the ssh command
99 # run with no timeout.
100 def _run(self, cmd):
101 return self.target.run(cmd, 0)[0]
102
103
104class SDKBuildProject(BuildProject):
105
106 def __init__(self, testpath, sdkenv, d, uri, foldername=None):
107 self.sdkenv = sdkenv
108 self.testdir = testpath
109 self.targetdir = testpath
110 bb.utils.mkdirhier(testpath)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500111 self.datetime = d.getVar('DATETIME')
112 self.testlogdir = d.getVar("TEST_LOG_DIR")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113 bb.utils.mkdirhier(self.testlogdir)
114 self.logfile = os.path.join(self.testlogdir, "sdk_target_log.%s" % self.datetime)
115 BuildProject.__init__(self, d, uri, foldername, tmpdir=testpath)
116
117 def download_archive(self):
118
119 self._download_archive()
120
121 cmd = 'tar xf %s%s -C %s' % (self.targetdir, self.archive, self.targetdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500122 subprocess.check_output(cmd, shell=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500123
124 #Change targetdir to project folder
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600125 self.targetdir = os.path.join(self.targetdir, self.fname)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600127 def run_configure(self, configure_args='', extra_cmds=' gnu-configize; '):
128 return super(SDKBuildProject, self).run_configure(configure_args=(configure_args or '$CONFIGURE_FLAGS'), extra_cmds=extra_cmds)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500129
130 def run_install(self, install_args=''):
131 return super(SDKBuildProject, self).run_install(install_args=(install_args or "DESTDIR=%s/../install" % self.targetdir))
132
133 def log(self, msg):
134 if self.logfile:
135 with open(self.logfile, "a") as f:
136 f.write("%s\n" % msg)
137
138 def _run(self, cmd):
139 self.log("Running . %s; " % self.sdkenv + cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500140 return subprocess.check_call(". %s; " % self.sdkenv + cmd, shell=True)