blob: 9754669296b838533aa026b5878b026b1779765c [file] [log] [blame]
Brad Bishop37a0e4d2017-12-04 01:01:44 -05001UNINATIVE_LOADER ?= "${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux/lib/${@bb.utils.contains('BUILD_ARCH', 'x86_64', 'ld-linux-x86-64.so.2', 'ld-linux.so.2', d)}"
2UNINATIVE_STAGING_DIR ?= "${STAGING_DIR}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05004UNINATIVE_URL ?= "unset"
5UNINATIVE_TARBALL ?= "${BUILD_ARCH}-nativesdk-libc.tar.bz2"
6# Example checksums
7#UNINATIVE_CHECKSUM[i586] = "dead"
8#UNINATIVE_CHECKSUM[x86_64] = "dead"
9UNINATIVE_DLDIR ?= "${DL_DIR}/uninative/"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050011addhandler uninative_event_fetchloader
12uninative_event_fetchloader[eventmask] = "bb.event.BuildStarted"
13
14addhandler uninative_event_enable
15uninative_event_enable[eventmask] = "bb.event.ConfigParsed"
16
17python uninative_event_fetchloader() {
18 """
19 This event fires on the parent and will try to fetch the tarball if the
20 loader isn't already present.
21 """
22
23 chksum = d.getVarFlag("UNINATIVE_CHECKSUM", d.getVar("BUILD_ARCH", True), True)
24 if not chksum:
25 bb.fatal("Uninative selected but not configured correctly, please set UNINATIVE_CHECKSUM[%s]" % d.getVar("BUILD_ARCH", True))
26
27 loader = d.getVar("UNINATIVE_LOADER", True)
28 loaderchksum = loader + ".chksum"
29 if os.path.exists(loader) and os.path.exists(loaderchksum):
30 with open(loaderchksum, "r") as f:
31 readchksum = f.read().strip()
32 if readchksum == chksum:
33 return
34
35 import subprocess
36 try:
37 # Save and restore cwd as Fetch.download() does a chdir()
38 olddir = os.getcwd()
39
40 tarball = d.getVar("UNINATIVE_TARBALL", True)
41 tarballdir = os.path.join(d.getVar("UNINATIVE_DLDIR", True), chksum)
42 tarballpath = os.path.join(tarballdir, tarball)
43
44 if not os.path.exists(tarballpath):
45 bb.utils.mkdirhier(tarballdir)
46 if d.getVar("UNINATIVE_URL", True) == "unset":
47 bb.fatal("Uninative selected but not configured, please set UNINATIVE_URL")
48
49 localdata = bb.data.createCopy(d)
50 localdata.setVar('FILESPATH', "")
51 localdata.setVar('DL_DIR', tarballdir)
52
53 srcuri = d.expand("${UNINATIVE_URL}${UNINATIVE_TARBALL};sha256sum=%s" % chksum)
54 bb.note("Fetching uninative binary shim from %s" % srcuri)
55
56 fetcher = bb.fetch2.Fetch([srcuri], localdata, cache=False)
57 fetcher.download()
58 localpath = fetcher.localpath(srcuri)
59 if localpath != tarballpath and os.path.exists(localpath) and not os.path.exists(tarballpath):
60 os.symlink(localpath, tarballpath)
61
Brad Bishop37a0e4d2017-12-04 01:01:44 -050062 cmd = d.expand("mkdir -p ${UNINATIVE_STAGING_DIR}-uninative; cd ${UNINATIVE_STAGING_DIR}-uninative; tar -xjf ${UNINATIVE_DLDIR}/%s/${UNINATIVE_TARBALL}; ${UNINATIVE_STAGING_DIR}-uninative/relocate_sdk.py ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux ${UNINATIVE_LOADER} ${UNINATIVE_LOADER} ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux/${bindir_native}/patchelf-uninative ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux${base_libdir_native}/libc*.so" % chksum)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063 subprocess.check_call(cmd, shell=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050064
65 with open(loaderchksum, "w") as f:
66 f.write(chksum)
67
68 enable_uninative(d)
69
70 except bb.fetch2.BBFetchException as exc:
71 bb.warn("Disabling uninative as unable to fetch uninative tarball: %s" % str(exc))
72 bb.warn("To build your own uninative loader, please bitbake uninative-tarball and set UNINATIVE_TARBALL appropriately.")
73 except subprocess.CalledProcessError as exc:
74 bb.warn("Disabling uninative as unable to install uninative tarball: %s" % str(exc))
75 bb.warn("To build your own uninative loader, please bitbake uninative-tarball and set UNINATIVE_TARBALL appropriately.")
76 finally:
77 os.chdir(olddir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078}
79
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050080python uninative_event_enable() {
81 """
82 This event handler is called in the workers and is responsible for setting
83 up uninative if a loader is found.
84 """
85 enable_uninative(d)
86}
87
88def enable_uninative(d):
89 loader = d.getVar("UNINATIVE_LOADER", True)
90 if os.path.exists(loader):
91 bb.debug(2, "Enabling uninative")
Brad Bishop37a0e4d2017-12-04 01:01:44 -050092 d.setVar("NATIVELSBSTRING", "universal%s" % oe.utils.host_gcc_version(d))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050093 d.appendVar("SSTATEPOSTUNPACKFUNCS", " uninative_changeinterp")
Brad Bishop37a0e4d2017-12-04 01:01:44 -050094 d.prependVar("PATH", "${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux${bindir_native}:")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095
96python uninative_changeinterp () {
97 import subprocess
98 import stat
99 import oe.qa
100
101 if not (bb.data.inherits_class('native', d) or bb.data.inherits_class('crosssdk', d) or bb.data.inherits_class('cross', d)):
102 return
103
104 sstateinst = d.getVar('SSTATE_INSTDIR', True)
105 for walkroot, dirs, files in os.walk(sstateinst):
106 for file in files:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500107 if file.endswith(".so") or ".so." in file:
108 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109 f = os.path.join(walkroot, file)
110 if os.path.islink(f):
111 continue
112 s = os.stat(f)
113 if not ((s[stat.ST_MODE] & stat.S_IXUSR) or (s[stat.ST_MODE] & stat.S_IXGRP) or (s[stat.ST_MODE] & stat.S_IXOTH)):
114 continue
115 elf = oe.qa.ELFFile(f)
116 try:
117 elf.open()
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500118 except oe.qa.NotELFFileError:
119 continue
120 if not elf.isDynamic():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121 continue
122
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500123 try:
124 subprocess.check_output(("patchelf-uninative", "--set-interpreter",
125 d.getVar("UNINATIVE_LOADER", True), f),
126 stderr=subprocess.STDOUT)
127 except subprocess.CalledProcessError as e:
128 bb.fatal("'%s' failed with exit code %d and the following output:\n%s" %
129 (e.cmd, e.returncode, e.output))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500130}