1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7import os
8import shutil
9
10import oeqa.utils.ftools as ftools
11from oeqa.utils.commands import runCmd, bitbake, get_bb_var
12from oeqa.selftest.cases.sstatetests import SStateBase
13
14
15class RebuildFromSState(SStateBase):
16
17    @classmethod
18    def setUpClass(self):
19        super(RebuildFromSState, self).setUpClass()
20        self.builddir = os.path.join(os.environ.get('BUILDDIR'))
21
22    def get_dep_targets(self, primary_targets):
23        found_targets = []
24        bitbake("-g " + ' '.join(map(str, primary_targets)))
25        with open(os.path.join(self.builddir, 'pn-buildlist'), 'r') as pnfile:
26            found_targets = pnfile.read().splitlines()
27        return found_targets
28
29    def configure_builddir(self, builddir):
30        os.mkdir(builddir)
31        self.track_for_cleanup(builddir)
32        os.mkdir(os.path.join(builddir, 'conf'))
33        shutil.copyfile(os.path.join(os.environ.get('BUILDDIR'), 'conf/local.conf'), os.path.join(builddir, 'conf/local.conf'))
34        config = {}
35        config['default_sstate_dir'] = "SSTATE_DIR ?= \"${TOPDIR}/sstate-cache\""
36        config['null_sstate_mirrors'] = "SSTATE_MIRRORS = \"\""
37        config['default_tmp_dir'] = "TMPDIR = \"${TOPDIR}/tmp\""
38        for key in config:
39            ftools.append_file(os.path.join(builddir, 'conf/selftest.inc'), config[key])
40        shutil.copyfile(os.path.join(os.environ.get('BUILDDIR'), 'conf/bblayers.conf'), os.path.join(builddir, 'conf/bblayers.conf'))
41        try:
42            shutil.copyfile(os.path.join(os.environ.get('BUILDDIR'), 'conf/auto.conf'), os.path.join(builddir, 'conf/auto.conf'))
43        except:
44            pass
45
46    def hardlink_tree(self, src, dst):
47        os.mkdir(dst)
48        self.track_for_cleanup(dst)
49        for root, dirs, files in os.walk(src):
50            if root == src:
51                continue
52            os.mkdir(os.path.join(dst, root.split(src)[1][1:]))
53            for sstate_file in files:
54                os.link(os.path.join(root, sstate_file), os.path.join(dst, root.split(src)[1][1:], sstate_file))
55
56    def run_test_sstate_rebuild(self, primary_targets, relocate=False, rebuild_dependencies=False):
57        buildA = os.path.join(self.builddir, 'buildA')
58        if relocate:
59            buildB = os.path.join(self.builddir, 'buildB')
60        else:
61            buildB = buildA
62
63        if rebuild_dependencies:
64            rebuild_targets = self.get_dep_targets(primary_targets)
65        else:
66            rebuild_targets = primary_targets
67
68        self.configure_builddir(buildA)
69        runCmd((". %s/oe-init-build-env %s && " % (get_bb_var('COREBASE'), buildA)) + 'bitbake  ' + ' '.join(map(str, primary_targets)), shell=True, executable='/bin/bash')
70        self.hardlink_tree(os.path.join(buildA, 'sstate-cache'), os.path.join(self.builddir, 'sstate-cache-buildA'))
71        shutil.rmtree(buildA)
72
73        failed_rebuild = []
74        failed_cleansstate = []
75        for target in rebuild_targets:
76            self.configure_builddir(buildB)
77            self.hardlink_tree(os.path.join(self.builddir, 'sstate-cache-buildA'), os.path.join(buildB, 'sstate-cache'))
78
79            result_cleansstate = runCmd((". %s/oe-init-build-env %s && " % (get_bb_var('COREBASE'), buildB)) + 'bitbake -ccleansstate ' + target, ignore_status=True, shell=True, executable='/bin/bash')
80            if not result_cleansstate.status == 0:
81                failed_cleansstate.append(target)
82                shutil.rmtree(buildB)
83                continue
84
85            result_build = runCmd((". %s/oe-init-build-env %s && " % (get_bb_var('COREBASE'), buildB)) + 'bitbake ' + target, ignore_status=True, shell=True, executable='/bin/bash')
86            if not result_build.status == 0:
87                failed_rebuild.append(target)
88
89            shutil.rmtree(buildB)
90
91        self.assertFalse(failed_rebuild, msg="The following recipes have failed to rebuild: %s" % ' '.join(map(str, failed_rebuild)))
92        self.assertFalse(failed_cleansstate, msg="The following recipes have failed cleansstate(all others have passed both cleansstate and rebuild from sstate tests): %s" % ' '.join(map(str, failed_cleansstate)))
93
94    def test_sstate_relocation(self):
95        self.run_test_sstate_rebuild(['core-image-weston-sdk'], relocate=True, rebuild_dependencies=True)
96
97    def test_sstate_rebuild(self):
98        self.run_test_sstate_rebuild(['core-image-weston-sdk'], relocate=False, rebuild_dependencies=True)
99