1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: GPL-2.0-only
5#
6
7import glob
8import shutil
9from oe.utils import execute_pre_post_process
10from oe.sdk import Sdk
11from oe.manifest import Manifest
12from oe.package_manager.deb import DpkgPM
13from oe.package_manager.deb.manifest import PkgManifest
14
15class PkgSdk(Sdk):
16    def __init__(self, d, manifest_dir=None):
17        super(PkgSdk, self).__init__(d, manifest_dir)
18
19        self.target_conf_dir = os.path.join(self.d.getVar("APTCONF_TARGET"), "apt")
20        self.host_conf_dir = os.path.join(self.d.getVar("APTCONF_TARGET"), "apt-sdk")
21
22
23        self.target_manifest = PkgManifest(d, self.manifest_dir,
24                                            Manifest.MANIFEST_TYPE_SDK_TARGET)
25        self.host_manifest = PkgManifest(d, self.manifest_dir,
26                                          Manifest.MANIFEST_TYPE_SDK_HOST)
27
28        deb_repo_workdir = "oe-sdk-repo"
29        if "sdk_ext" in d.getVar("BB_RUNTASK"):
30            deb_repo_workdir = "oe-sdk-ext-repo"
31
32        self.target_pm = DpkgPM(d, self.sdk_target_sysroot,
33                                self.d.getVar("PACKAGE_ARCHS"),
34                                self.d.getVar("DPKG_ARCH"),
35                                self.target_conf_dir,
36                                deb_repo_workdir=deb_repo_workdir)
37
38        self.host_pm = DpkgPM(d, self.sdk_host_sysroot,
39                              self.d.getVar("SDK_PACKAGE_ARCHS"),
40                              self.d.getVar("DEB_SDK_ARCH"),
41                              self.host_conf_dir,
42                              deb_repo_workdir=deb_repo_workdir)
43
44    def _copy_apt_dir_to(self, dst_dir):
45        staging_etcdir_native = self.d.getVar("STAGING_ETCDIR_NATIVE")
46
47        self.remove(dst_dir, True)
48
49        shutil.copytree(os.path.join(staging_etcdir_native, "apt"), dst_dir)
50
51    def _populate_sysroot(self, pm, manifest):
52        pkgs_to_install = manifest.parse_initial_manifest()
53
54        pm.write_index()
55        pm.update()
56
57        for pkg_type in self.install_order:
58            if pkg_type in pkgs_to_install:
59                pm.install(pkgs_to_install[pkg_type],
60                           [False, True][pkg_type == Manifest.PKG_TYPE_ATTEMPT_ONLY])
61
62    def _populate(self):
63        execute_pre_post_process(self.d, self.d.getVar("POPULATE_SDK_PRE_TARGET_COMMAND"))
64
65        bb.note("Installing TARGET packages")
66        self._populate_sysroot(self.target_pm, self.target_manifest)
67
68        self.target_pm.install_complementary(self.d.getVar('SDKIMAGE_INSTALL_COMPLEMENTARY'))
69
70        self.target_pm.run_pre_post_installs()
71
72        env_bkp = os.environ.copy()
73        os.environ['PATH'] = self.d.expand("${COREBASE}/scripts/nativesdk-intercept") + \
74                             os.pathsep + os.environ["PATH"]
75
76        self.target_pm.run_intercepts(populate_sdk='target')
77        os.environ.update(env_bkp)
78
79        execute_pre_post_process(self.d, self.d.getVar("POPULATE_SDK_POST_TARGET_COMMAND"))
80
81        self._copy_apt_dir_to(os.path.join(self.sdk_target_sysroot, "etc", "apt"))
82
83        if not bb.utils.contains("SDKIMAGE_FEATURES", "package-management", True, False, self.d):
84            self.target_pm.remove_packaging_data()
85
86        bb.note("Installing NATIVESDK packages")
87        self._populate_sysroot(self.host_pm, self.host_manifest)
88        self.install_locales(self.host_pm)
89
90        self.host_pm.run_pre_post_installs()
91
92        self.host_pm.run_intercepts(populate_sdk='host')
93
94        execute_pre_post_process(self.d, self.d.getVar("POPULATE_SDK_POST_HOST_COMMAND"))
95
96        self._copy_apt_dir_to(os.path.join(self.sdk_output, self.sdk_native_path,
97                                           "etc", "apt"))
98
99        if not bb.utils.contains("SDKIMAGE_FEATURES", "package-management", True, False, self.d):
100            self.host_pm.remove_packaging_data()
101
102        native_dpkg_state_dir = os.path.join(self.sdk_output, self.sdk_native_path,
103                                             "var", "lib", "dpkg")
104        self.mkdirhier(native_dpkg_state_dir)
105        for f in glob.glob(os.path.join(self.sdk_output, "var", "lib", "dpkg", "*")):
106            self.movefile(f, native_dpkg_state_dir)
107        self.remove(os.path.join(self.sdk_output, "var"), True)
108