blob: 172336428419fc61b6affa5740ba41af77fd4a68 [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
Brad Bishop6e60e8b2018-02-01 10:27:11 -050023 chksum = d.getVarFlag("UNINATIVE_CHECKSUM", d.getVar("BUILD_ARCH"))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050024 if not chksum:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025 bb.fatal("Uninative selected but not configured correctly, please set UNINATIVE_CHECKSUM[%s]" % d.getVar("BUILD_ARCH"))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050026
Brad Bishop6e60e8b2018-02-01 10:27:11 -050027 loader = d.getVar("UNINATIVE_LOADER")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050028 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
Brad Bishop6e60e8b2018-02-01 10:27:11 -050040 tarball = d.getVar("UNINATIVE_TARBALL")
41 tarballdir = os.path.join(d.getVar("UNINATIVE_DLDIR"), chksum)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050042 tarballpath = os.path.join(tarballdir, tarball)
43
44 if not os.path.exists(tarballpath):
45 bb.utils.mkdirhier(tarballdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046 if d.getVar("UNINATIVE_URL") == "unset":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050047 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)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050052 # Our games with path manipulation of DL_DIR mean standard PREMIRRORS don't work
53 # and we can't easily put 'chksum' into the url path from a url parameter with
54 # the current fetcher url handling
55 ownmirror = d.getVar('SOURCE_MIRROR_URL')
56 if ownmirror:
57 localdata.appendVar("PREMIRRORS", " ${UNINATIVE_URL}${UNINATIVE_TARBALL} ${SOURCE_MIRROR_URL}/uninative/%s/${UNINATIVE_TARBALL}" % chksum)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050058
59 srcuri = d.expand("${UNINATIVE_URL}${UNINATIVE_TARBALL};sha256sum=%s" % chksum)
60 bb.note("Fetching uninative binary shim from %s" % srcuri)
61
62 fetcher = bb.fetch2.Fetch([srcuri], localdata, cache=False)
63 fetcher.download()
64 localpath = fetcher.localpath(srcuri)
65 if localpath != tarballpath and os.path.exists(localpath) and not os.path.exists(tarballpath):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050066 # Follow the symlink behavior from the bitbake fetch2.
67 # This will cover the case where an existing symlink is broken
68 # as well as if there are two processes trying to create it
69 # at the same time.
70 if os.path.islink(tarballpath):
71 # Broken symbolic link
72 os.unlink(tarballpath)
73
74 # Deal with two processes trying to make symlink at once
75 try:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050076 os.symlink(localpath, tarballpath)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050077 except FileExistsError:
78 pass
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050079
Brad Bishop00111322018-04-01 22:23:53 -040080 # ldd output is "ldd (Ubuntu GLIBC 2.23-0ubuntu10) 2.23", extract last option from first line
81 glibcver = subprocess.check_output(["ldd", "--version"]).decode('utf-8').split('\n')[0].split()[-1]
82 if bb.utils.vercmp_string(d.getVar("UNINATIVE_MAXGLIBCVERSION"), glibcver) < 0:
83 raise RuntimeError("Your host glibc verson (%s) is newer than that in uninative (%s). Disabling uninative so that sstate is not corrupted." % (glibcver, d.getVar("UNINATIVE_MAXGLIBCVERSION")))
84
Brad Bishop6e60e8b2018-02-01 10:27:11 -050085 cmd = d.expand("\
86mkdir -p ${UNINATIVE_STAGING_DIR}-uninative; \
87cd ${UNINATIVE_STAGING_DIR}-uninative; \
88tar -xjf ${UNINATIVE_DLDIR}/%s/${UNINATIVE_TARBALL}; \
89${UNINATIVE_STAGING_DIR}-uninative/relocate_sdk.py \
90 ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux \
91 ${UNINATIVE_LOADER} \
92 ${UNINATIVE_LOADER} \
93 ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux/${bindir_native}/patchelf-uninative \
94 ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux${base_libdir_native}/libc*.so" % chksum)
95 subprocess.check_output(cmd, shell=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050096
97 with open(loaderchksum, "w") as f:
98 f.write(chksum)
99
100 enable_uninative(d)
101
Brad Bishop00111322018-04-01 22:23:53 -0400102 except RuntimeError as e:
103 bb.warn(str(e))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500104 except bb.fetch2.BBFetchException as exc:
105 bb.warn("Disabling uninative as unable to fetch uninative tarball: %s" % str(exc))
106 bb.warn("To build your own uninative loader, please bitbake uninative-tarball and set UNINATIVE_TARBALL appropriately.")
107 except subprocess.CalledProcessError as exc:
108 bb.warn("Disabling uninative as unable to install uninative tarball: %s" % str(exc))
109 bb.warn("To build your own uninative loader, please bitbake uninative-tarball and set UNINATIVE_TARBALL appropriately.")
110 finally:
111 os.chdir(olddir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112}
113
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500114python uninative_event_enable() {
115 """
116 This event handler is called in the workers and is responsible for setting
117 up uninative if a loader is found.
118 """
119 enable_uninative(d)
120}
121
122def enable_uninative(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500123 loader = d.getVar("UNINATIVE_LOADER")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500124 if os.path.exists(loader):
125 bb.debug(2, "Enabling uninative")
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500126 d.setVar("NATIVELSBSTRING", "universal%s" % oe.utils.host_gcc_version(d))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500127 d.appendVar("SSTATEPOSTUNPACKFUNCS", " uninative_changeinterp")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500128 d.appendVarFlag("SSTATEPOSTUNPACKFUNCS", "vardepvalueexclude", "| uninative_changeinterp")
129 d.prependVar("PATH", "${STAGING_DIR}-uninative/${BUILD_ARCH}-linux${bindir_native}:")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500130
131python uninative_changeinterp () {
132 import subprocess
133 import stat
134 import oe.qa
135
136 if not (bb.data.inherits_class('native', d) or bb.data.inherits_class('crosssdk', d) or bb.data.inherits_class('cross', d)):
137 return
138
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500139 sstateinst = d.getVar('SSTATE_INSTDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500140 for walkroot, dirs, files in os.walk(sstateinst):
141 for file in files:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500142 if file.endswith(".so") or ".so." in file:
143 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144 f = os.path.join(walkroot, file)
145 if os.path.islink(f):
146 continue
147 s = os.stat(f)
148 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)):
149 continue
150 elf = oe.qa.ELFFile(f)
151 try:
152 elf.open()
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500153 except oe.qa.NotELFFileError:
154 continue
155 if not elf.isDynamic():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156 continue
157
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500158 subprocess.check_output(("patchelf-uninative", "--set-interpreter", d.getVar("UNINATIVE_LOADER"), f), stderr=subprocess.STDOUT)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159}