sdk.py 15 KB

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