Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | from abc import ABCMeta, abstractmethod |
| 2 | from oe.utils import execute_pre_post_process |
| 3 | from oe.manifest import * |
| 4 | from oe.package_manager import * |
| 5 | import os |
| 6 | import shutil |
| 7 | import glob |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 8 | import traceback |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | |
| 10 | |
| 11 | class Sdk(object): |
| 12 | __metaclass__ = ABCMeta |
| 13 | |
| 14 | def __init__(self, d, manifest_dir): |
| 15 | self.d = d |
| 16 | self.sdk_output = self.d.getVar('SDK_OUTPUT', True) |
| 17 | self.sdk_native_path = self.d.getVar('SDKPATHNATIVE', True).strip('/') |
| 18 | self.target_path = self.d.getVar('SDKTARGETSYSROOT', True).strip('/') |
| 19 | self.sysconfdir = self.d.getVar('sysconfdir', True).strip('/') |
| 20 | |
| 21 | self.sdk_target_sysroot = os.path.join(self.sdk_output, self.target_path) |
| 22 | self.sdk_host_sysroot = self.sdk_output |
| 23 | |
| 24 | if manifest_dir is None: |
| 25 | self.manifest_dir = self.d.getVar("SDK_DIR", True) |
| 26 | else: |
| 27 | self.manifest_dir = manifest_dir |
| 28 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 29 | self.remove(self.sdk_output, True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | |
| 31 | self.install_order = Manifest.INSTALL_ORDER |
| 32 | |
| 33 | @abstractmethod |
| 34 | def _populate(self): |
| 35 | pass |
| 36 | |
| 37 | def populate(self): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 38 | self.mkdirhier(self.sdk_output) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 39 | |
| 40 | # call backend dependent implementation |
| 41 | self._populate() |
| 42 | |
| 43 | # Don't ship any libGL in the SDK |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 44 | self.remove(os.path.join(self.sdk_output, self.sdk_native_path, |
| 45 | self.d.getVar('libdir_nativesdk', True).strip('/'), |
| 46 | "libGL*")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 | |
| 48 | # Fix or remove broken .la files |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 49 | self.remove(os.path.join(self.sdk_output, self.sdk_native_path, |
| 50 | self.d.getVar('libdir_nativesdk', True).strip('/'), |
| 51 | "*.la")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | |
| 53 | # Link the ld.so.cache file into the hosts filesystem |
| 54 | link_name = os.path.join(self.sdk_output, self.sdk_native_path, |
| 55 | self.sysconfdir, "ld.so.cache") |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 56 | self.mkdirhier(os.path.dirname(link_name)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | os.symlink("/etc/ld.so.cache", link_name) |
| 58 | |
| 59 | execute_pre_post_process(self.d, self.d.getVar('SDK_POSTPROCESS_COMMAND', True)) |
| 60 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 61 | def movefile(self, sourcefile, destdir): |
| 62 | try: |
| 63 | # FIXME: this check of movefile's return code to None should be |
| 64 | # fixed within the function to use only exceptions to signal when |
| 65 | # something goes wrong |
| 66 | if (bb.utils.movefile(sourcefile, destdir) == None): |
| 67 | raise OSError("moving %s to %s failed" |
| 68 | %(sourcefile, destdir)) |
| 69 | #FIXME: using umbrella exc catching because bb.utils method raises it |
| 70 | except Exception as e: |
| 71 | bb.debug(1, "printing the stack trace\n %s" %traceback.format_exc()) |
| 72 | bb.error("unable to place %s in final SDK location" % sourcefile) |
| 73 | |
| 74 | def mkdirhier(self, dirpath): |
| 75 | try: |
| 76 | bb.utils.mkdirhier(dirpath) |
| 77 | except OSError as e: |
| 78 | bb.debug(1, "printing the stack trace\n %s" %traceback.format_exc()) |
| 79 | bb.fatal("cannot make dir for SDK: %s" % dirpath) |
| 80 | |
| 81 | def remove(self, path, recurse=False): |
| 82 | try: |
| 83 | bb.utils.remove(path, recurse) |
| 84 | #FIXME: using umbrella exc catching because bb.utils method raises it |
| 85 | except Exception as e: |
| 86 | bb.debug(1, "printing the stack trace\n %s" %traceback.format_exc()) |
| 87 | bb.warn("cannot remove SDK dir: %s" % path) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | |
| 89 | class RpmSdk(Sdk): |
| 90 | def __init__(self, d, manifest_dir=None): |
| 91 | super(RpmSdk, self).__init__(d, manifest_dir) |
| 92 | |
| 93 | self.target_manifest = RpmManifest(d, self.manifest_dir, |
| 94 | Manifest.MANIFEST_TYPE_SDK_TARGET) |
| 95 | self.host_manifest = RpmManifest(d, self.manifest_dir, |
| 96 | Manifest.MANIFEST_TYPE_SDK_HOST) |
| 97 | |
| 98 | target_providename = ['/bin/sh', |
| 99 | '/bin/bash', |
| 100 | '/usr/bin/env', |
| 101 | '/usr/bin/perl', |
| 102 | 'pkgconfig' |
| 103 | ] |
| 104 | |
| 105 | self.target_pm = RpmPM(d, |
| 106 | self.sdk_target_sysroot, |
| 107 | self.d.getVar('TARGET_VENDOR', True), |
| 108 | 'target', |
| 109 | target_providename |
| 110 | ) |
| 111 | |
| 112 | sdk_providename = ['/bin/sh', |
| 113 | '/bin/bash', |
| 114 | '/usr/bin/env', |
| 115 | '/usr/bin/perl', |
| 116 | 'pkgconfig', |
| 117 | 'libGL.so()(64bit)', |
| 118 | 'libGL.so' |
| 119 | ] |
| 120 | |
| 121 | self.host_pm = RpmPM(d, |
| 122 | self.sdk_host_sysroot, |
| 123 | self.d.getVar('SDK_VENDOR', True), |
| 124 | 'host', |
| 125 | sdk_providename, |
| 126 | "SDK_PACKAGE_ARCHS", |
| 127 | "SDK_OS" |
| 128 | ) |
| 129 | |
| 130 | def _populate_sysroot(self, pm, manifest): |
| 131 | pkgs_to_install = manifest.parse_initial_manifest() |
| 132 | |
| 133 | pm.create_configs() |
| 134 | pm.write_index() |
| 135 | pm.dump_all_available_pkgs() |
| 136 | pm.update() |
| 137 | |
| 138 | pkgs = [] |
| 139 | pkgs_attempt = [] |
| 140 | for pkg_type in pkgs_to_install: |
| 141 | if pkg_type == Manifest.PKG_TYPE_ATTEMPT_ONLY: |
| 142 | pkgs_attempt += pkgs_to_install[pkg_type] |
| 143 | else: |
| 144 | pkgs += pkgs_to_install[pkg_type] |
| 145 | |
| 146 | pm.install(pkgs) |
| 147 | |
| 148 | pm.install(pkgs_attempt, True) |
| 149 | |
| 150 | def _populate(self): |
| 151 | bb.note("Installing TARGET packages") |
| 152 | self._populate_sysroot(self.target_pm, self.target_manifest) |
| 153 | |
| 154 | self.target_pm.install_complementary(self.d.getVar('SDKIMAGE_INSTALL_COMPLEMENTARY', True)) |
| 155 | |
| 156 | execute_pre_post_process(self.d, self.d.getVar("POPULATE_SDK_POST_TARGET_COMMAND", True)) |
| 157 | |
| 158 | self.target_pm.remove_packaging_data() |
| 159 | |
| 160 | bb.note("Installing NATIVESDK packages") |
| 161 | self._populate_sysroot(self.host_pm, self.host_manifest) |
| 162 | |
| 163 | execute_pre_post_process(self.d, self.d.getVar("POPULATE_SDK_POST_HOST_COMMAND", True)) |
| 164 | |
| 165 | self.host_pm.remove_packaging_data() |
| 166 | |
| 167 | # Move host RPM library data |
| 168 | native_rpm_state_dir = os.path.join(self.sdk_output, |
| 169 | self.sdk_native_path, |
| 170 | self.d.getVar('localstatedir_nativesdk', True).strip('/'), |
| 171 | "lib", |
| 172 | "rpm" |
| 173 | ) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 174 | self.mkdirhier(native_rpm_state_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 175 | for f in glob.glob(os.path.join(self.sdk_output, |
| 176 | "var", |
| 177 | "lib", |
| 178 | "rpm", |
| 179 | "*")): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 180 | self.movefile(f, native_rpm_state_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 181 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 182 | self.remove(os.path.join(self.sdk_output, "var"), True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 183 | |
| 184 | # Move host sysconfig data |
| 185 | native_sysconf_dir = os.path.join(self.sdk_output, |
| 186 | self.sdk_native_path, |
| 187 | self.d.getVar('sysconfdir', |
| 188 | True).strip('/'), |
| 189 | ) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 190 | self.mkdirhier(native_sysconf_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 191 | for f in glob.glob(os.path.join(self.sdk_output, "etc", "*")): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 192 | self.movefile(f, native_sysconf_dir) |
| 193 | self.remove(os.path.join(self.sdk_output, "etc"), True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 194 | |
| 195 | |
| 196 | class OpkgSdk(Sdk): |
| 197 | def __init__(self, d, manifest_dir=None): |
| 198 | super(OpkgSdk, self).__init__(d, manifest_dir) |
| 199 | |
| 200 | self.target_conf = self.d.getVar("IPKGCONF_TARGET", True) |
| 201 | self.host_conf = self.d.getVar("IPKGCONF_SDK", True) |
| 202 | |
| 203 | self.target_manifest = OpkgManifest(d, self.manifest_dir, |
| 204 | Manifest.MANIFEST_TYPE_SDK_TARGET) |
| 205 | self.host_manifest = OpkgManifest(d, self.manifest_dir, |
| 206 | Manifest.MANIFEST_TYPE_SDK_HOST) |
| 207 | |
| 208 | self.target_pm = OpkgPM(d, self.sdk_target_sysroot, self.target_conf, |
| 209 | self.d.getVar("ALL_MULTILIB_PACKAGE_ARCHS", True)) |
| 210 | |
| 211 | self.host_pm = OpkgPM(d, self.sdk_host_sysroot, self.host_conf, |
| 212 | self.d.getVar("SDK_PACKAGE_ARCHS", True)) |
| 213 | |
| 214 | def _populate_sysroot(self, pm, manifest): |
| 215 | pkgs_to_install = manifest.parse_initial_manifest() |
| 216 | |
| 217 | if (self.d.getVar('BUILD_IMAGES_FROM_FEEDS', True) or "") != "1": |
| 218 | pm.write_index() |
| 219 | |
| 220 | pm.update() |
| 221 | |
| 222 | pkgs = [] |
| 223 | pkgs_attempt = [] |
| 224 | for pkg_type in pkgs_to_install: |
| 225 | if pkg_type == Manifest.PKG_TYPE_ATTEMPT_ONLY: |
| 226 | pkgs_attempt += pkgs_to_install[pkg_type] |
| 227 | else: |
| 228 | pkgs += pkgs_to_install[pkg_type] |
| 229 | |
| 230 | pm.install(pkgs) |
| 231 | |
| 232 | pm.install(pkgs_attempt, True) |
| 233 | |
| 234 | def _populate(self): |
| 235 | bb.note("Installing TARGET packages") |
| 236 | self._populate_sysroot(self.target_pm, self.target_manifest) |
| 237 | |
| 238 | self.target_pm.install_complementary(self.d.getVar('SDKIMAGE_INSTALL_COMPLEMENTARY', True)) |
| 239 | |
| 240 | execute_pre_post_process(self.d, self.d.getVar("POPULATE_SDK_POST_TARGET_COMMAND", True)) |
| 241 | |
| 242 | bb.note("Installing NATIVESDK packages") |
| 243 | self._populate_sysroot(self.host_pm, self.host_manifest) |
| 244 | |
| 245 | execute_pre_post_process(self.d, self.d.getVar("POPULATE_SDK_POST_HOST_COMMAND", True)) |
| 246 | |
| 247 | target_sysconfdir = os.path.join(self.sdk_target_sysroot, self.sysconfdir) |
| 248 | host_sysconfdir = os.path.join(self.sdk_host_sysroot, self.sysconfdir) |
| 249 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 250 | self.mkdirhier(target_sysconfdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 251 | shutil.copy(self.target_conf, target_sysconfdir) |
| 252 | os.chmod(os.path.join(target_sysconfdir, |
| 253 | os.path.basename(self.target_conf)), 0644) |
| 254 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 255 | self.mkdirhier(host_sysconfdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 256 | shutil.copy(self.host_conf, host_sysconfdir) |
| 257 | os.chmod(os.path.join(host_sysconfdir, |
| 258 | os.path.basename(self.host_conf)), 0644) |
| 259 | |
| 260 | native_opkg_state_dir = os.path.join(self.sdk_output, self.sdk_native_path, |
| 261 | self.d.getVar('localstatedir_nativesdk', True).strip('/'), |
| 262 | "lib", "opkg") |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 263 | self.mkdirhier(native_opkg_state_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 264 | for f in glob.glob(os.path.join(self.sdk_output, "var", "lib", "opkg", "*")): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 265 | self.movefile(f, native_opkg_state_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 266 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 267 | self.remove(os.path.join(self.sdk_output, "var"), True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 268 | |
| 269 | |
| 270 | class DpkgSdk(Sdk): |
| 271 | def __init__(self, d, manifest_dir=None): |
| 272 | super(DpkgSdk, self).__init__(d, manifest_dir) |
| 273 | |
| 274 | self.target_conf_dir = os.path.join(self.d.getVar("APTCONF_TARGET", True), "apt") |
| 275 | self.host_conf_dir = os.path.join(self.d.getVar("APTCONF_TARGET", True), "apt-sdk") |
| 276 | |
| 277 | self.target_manifest = DpkgManifest(d, self.manifest_dir, |
| 278 | Manifest.MANIFEST_TYPE_SDK_TARGET) |
| 279 | self.host_manifest = DpkgManifest(d, self.manifest_dir, |
| 280 | Manifest.MANIFEST_TYPE_SDK_HOST) |
| 281 | |
| 282 | self.target_pm = DpkgPM(d, self.sdk_target_sysroot, |
| 283 | self.d.getVar("PACKAGE_ARCHS", True), |
| 284 | self.d.getVar("DPKG_ARCH", True), |
| 285 | self.target_conf_dir) |
| 286 | |
| 287 | self.host_pm = DpkgPM(d, self.sdk_host_sysroot, |
| 288 | self.d.getVar("SDK_PACKAGE_ARCHS", True), |
| 289 | self.d.getVar("DEB_SDK_ARCH", True), |
| 290 | self.host_conf_dir) |
| 291 | |
| 292 | def _copy_apt_dir_to(self, dst_dir): |
| 293 | staging_etcdir_native = self.d.getVar("STAGING_ETCDIR_NATIVE", True) |
| 294 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 295 | self.remove(dst_dir, True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 296 | |
| 297 | shutil.copytree(os.path.join(staging_etcdir_native, "apt"), dst_dir) |
| 298 | |
| 299 | def _populate_sysroot(self, pm, manifest): |
| 300 | pkgs_to_install = manifest.parse_initial_manifest() |
| 301 | |
| 302 | pm.write_index() |
| 303 | pm.update() |
| 304 | |
| 305 | pkgs = [] |
| 306 | pkgs_attempt = [] |
| 307 | for pkg_type in pkgs_to_install: |
| 308 | if pkg_type == Manifest.PKG_TYPE_ATTEMPT_ONLY: |
| 309 | pkgs_attempt += pkgs_to_install[pkg_type] |
| 310 | else: |
| 311 | pkgs += pkgs_to_install[pkg_type] |
| 312 | |
| 313 | pm.install(pkgs) |
| 314 | |
| 315 | pm.install(pkgs_attempt, True) |
| 316 | |
| 317 | def _populate(self): |
| 318 | bb.note("Installing TARGET packages") |
| 319 | self._populate_sysroot(self.target_pm, self.target_manifest) |
| 320 | |
| 321 | self.target_pm.install_complementary(self.d.getVar('SDKIMAGE_INSTALL_COMPLEMENTARY', True)) |
| 322 | |
| 323 | execute_pre_post_process(self.d, self.d.getVar("POPULATE_SDK_POST_TARGET_COMMAND", True)) |
| 324 | |
| 325 | self._copy_apt_dir_to(os.path.join(self.sdk_target_sysroot, "etc", "apt")) |
| 326 | |
| 327 | bb.note("Installing NATIVESDK packages") |
| 328 | self._populate_sysroot(self.host_pm, self.host_manifest) |
| 329 | |
| 330 | execute_pre_post_process(self.d, self.d.getVar("POPULATE_SDK_POST_HOST_COMMAND", True)) |
| 331 | |
| 332 | self._copy_apt_dir_to(os.path.join(self.sdk_output, self.sdk_native_path, |
| 333 | "etc", "apt")) |
| 334 | |
| 335 | native_dpkg_state_dir = os.path.join(self.sdk_output, self.sdk_native_path, |
| 336 | "var", "lib", "dpkg") |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 337 | self.mkdirhier(native_dpkg_state_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 338 | for f in glob.glob(os.path.join(self.sdk_output, "var", "lib", "dpkg", "*")): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 339 | self.movefile(f, native_dpkg_state_dir) |
| 340 | self.remove(os.path.join(self.sdk_output, "var"), True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 341 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 342 | |
| 343 | |
| 344 | def sdk_list_installed_packages(d, target, format=None, rootfs_dir=None): |
| 345 | if rootfs_dir is None: |
| 346 | sdk_output = d.getVar('SDK_OUTPUT', True) |
| 347 | target_path = d.getVar('SDKTARGETSYSROOT', True).strip('/') |
| 348 | |
| 349 | rootfs_dir = [sdk_output, os.path.join(sdk_output, target_path)][target is True] |
| 350 | |
| 351 | img_type = d.getVar('IMAGE_PKGTYPE', True) |
| 352 | if img_type == "rpm": |
| 353 | arch_var = ["SDK_PACKAGE_ARCHS", None][target is True] |
| 354 | os_var = ["SDK_OS", None][target is True] |
| 355 | return RpmPkgsList(d, rootfs_dir, arch_var, os_var).list(format) |
| 356 | elif img_type == "ipk": |
| 357 | conf_file_var = ["IPKGCONF_SDK", "IPKGCONF_TARGET"][target is True] |
| 358 | return OpkgPkgsList(d, rootfs_dir, d.getVar(conf_file_var, True)).list(format) |
| 359 | elif img_type == "deb": |
| 360 | return DpkgPkgsList(d, rootfs_dir).list(format) |
| 361 | |
| 362 | def populate_sdk(d, manifest_dir=None): |
| 363 | env_bkp = os.environ.copy() |
| 364 | |
| 365 | img_type = d.getVar('IMAGE_PKGTYPE', True) |
| 366 | if img_type == "rpm": |
| 367 | RpmSdk(d, manifest_dir).populate() |
| 368 | elif img_type == "ipk": |
| 369 | OpkgSdk(d, manifest_dir).populate() |
| 370 | elif img_type == "deb": |
| 371 | DpkgSdk(d, manifest_dir).populate() |
| 372 | |
| 373 | os.environ.clear() |
| 374 | os.environ.update(env_bkp) |
| 375 | |
| 376 | if __name__ == "__main__": |
| 377 | pass |