blob: ba99fb6e8fd81763dbbecaeeec017adaf65506fc [file] [log] [blame]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001UNINATIVE_LOADER ?= "${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux/lib/${@bb.utils.contains('BUILD_ARCH', 'x86_64', 'ld-linux-x86-64.so.2', '', d)}${@bb.utils.contains('BUILD_ARCH', 'i686', 'ld-linux.so.2', '', d)}${@bb.utils.contains('BUILD_ARCH', 'aarch64', 'ld-linux-aarch64.so.1', '', d)}"
Brad Bishop37a0e4d2017-12-04 01:01:44 -05002UNINATIVE_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
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08007#UNINATIVE_CHECKSUM[aarch64] = "dead"
8#UNINATIVE_CHECKSUM[i686] = "dead"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05009#UNINATIVE_CHECKSUM[x86_64] = "dead"
10UNINATIVE_DLDIR ?= "${DL_DIR}/uninative/"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011
Brad Bishop316dfdd2018-06-25 12:45:53 -040012# Enabling uninative will change the following variables so they need to go the parsing white list to prevent multiple recipe parsing
13BB_HASHCONFIG_WHITELIST += "NATIVELSBSTRING SSTATEPOSTUNPACKFUNCS BUILD_LDFLAGS"
14
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050015addhandler uninative_event_fetchloader
16uninative_event_fetchloader[eventmask] = "bb.event.BuildStarted"
17
18addhandler uninative_event_enable
19uninative_event_enable[eventmask] = "bb.event.ConfigParsed"
20
21python uninative_event_fetchloader() {
22 """
23 This event fires on the parent and will try to fetch the tarball if the
24 loader isn't already present.
25 """
26
Brad Bishop6e60e8b2018-02-01 10:27:11 -050027 chksum = d.getVarFlag("UNINATIVE_CHECKSUM", d.getVar("BUILD_ARCH"))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050028 if not chksum:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050029 bb.fatal("Uninative selected but not configured correctly, please set UNINATIVE_CHECKSUM[%s]" % d.getVar("BUILD_ARCH"))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050030
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031 loader = d.getVar("UNINATIVE_LOADER")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050032 loaderchksum = loader + ".chksum"
33 if os.path.exists(loader) and os.path.exists(loaderchksum):
34 with open(loaderchksum, "r") as f:
35 readchksum = f.read().strip()
36 if readchksum == chksum:
37 return
38
39 import subprocess
40 try:
41 # Save and restore cwd as Fetch.download() does a chdir()
42 olddir = os.getcwd()
43
Brad Bishop6e60e8b2018-02-01 10:27:11 -050044 tarball = d.getVar("UNINATIVE_TARBALL")
45 tarballdir = os.path.join(d.getVar("UNINATIVE_DLDIR"), chksum)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050046 tarballpath = os.path.join(tarballdir, tarball)
47
48 if not os.path.exists(tarballpath):
49 bb.utils.mkdirhier(tarballdir)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050050 if d.getVar("UNINATIVE_URL") == "unset":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050051 bb.fatal("Uninative selected but not configured, please set UNINATIVE_URL")
52
53 localdata = bb.data.createCopy(d)
54 localdata.setVar('FILESPATH', "")
55 localdata.setVar('DL_DIR', tarballdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050056 # Our games with path manipulation of DL_DIR mean standard PREMIRRORS don't work
57 # and we can't easily put 'chksum' into the url path from a url parameter with
58 # the current fetcher url handling
59 ownmirror = d.getVar('SOURCE_MIRROR_URL')
60 if ownmirror:
61 localdata.appendVar("PREMIRRORS", " ${UNINATIVE_URL}${UNINATIVE_TARBALL} ${SOURCE_MIRROR_URL}/uninative/%s/${UNINATIVE_TARBALL}" % chksum)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050062
63 srcuri = d.expand("${UNINATIVE_URL}${UNINATIVE_TARBALL};sha256sum=%s" % chksum)
64 bb.note("Fetching uninative binary shim from %s" % srcuri)
65
66 fetcher = bb.fetch2.Fetch([srcuri], localdata, cache=False)
67 fetcher.download()
68 localpath = fetcher.localpath(srcuri)
69 if localpath != tarballpath and os.path.exists(localpath) and not os.path.exists(tarballpath):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050070 # Follow the symlink behavior from the bitbake fetch2.
71 # This will cover the case where an existing symlink is broken
72 # as well as if there are two processes trying to create it
73 # at the same time.
74 if os.path.islink(tarballpath):
75 # Broken symbolic link
76 os.unlink(tarballpath)
77
78 # Deal with two processes trying to make symlink at once
79 try:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050080 os.symlink(localpath, tarballpath)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050081 except FileExistsError:
82 pass
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050083
Brad Bishop00111322018-04-01 22:23:53 -040084 # ldd output is "ldd (Ubuntu GLIBC 2.23-0ubuntu10) 2.23", extract last option from first line
85 glibcver = subprocess.check_output(["ldd", "--version"]).decode('utf-8').split('\n')[0].split()[-1]
86 if bb.utils.vercmp_string(d.getVar("UNINATIVE_MAXGLIBCVERSION"), glibcver) < 0:
87 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")))
88
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089 cmd = d.expand("\
90mkdir -p ${UNINATIVE_STAGING_DIR}-uninative; \
91cd ${UNINATIVE_STAGING_DIR}-uninative; \
92tar -xjf ${UNINATIVE_DLDIR}/%s/${UNINATIVE_TARBALL}; \
93${UNINATIVE_STAGING_DIR}-uninative/relocate_sdk.py \
94 ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux \
95 ${UNINATIVE_LOADER} \
96 ${UNINATIVE_LOADER} \
97 ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux/${bindir_native}/patchelf-uninative \
98 ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux${base_libdir_native}/libc*.so" % chksum)
99 subprocess.check_output(cmd, shell=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500100
101 with open(loaderchksum, "w") as f:
102 f.write(chksum)
103
104 enable_uninative(d)
105
Brad Bishop00111322018-04-01 22:23:53 -0400106 except RuntimeError as e:
107 bb.warn(str(e))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500108 except bb.fetch2.BBFetchException as exc:
109 bb.warn("Disabling uninative as unable to fetch uninative tarball: %s" % str(exc))
110 bb.warn("To build your own uninative loader, please bitbake uninative-tarball and set UNINATIVE_TARBALL appropriately.")
111 except subprocess.CalledProcessError as exc:
112 bb.warn("Disabling uninative as unable to install uninative tarball: %s" % str(exc))
113 bb.warn("To build your own uninative loader, please bitbake uninative-tarball and set UNINATIVE_TARBALL appropriately.")
114 finally:
115 os.chdir(olddir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116}
117
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500118python uninative_event_enable() {
119 """
120 This event handler is called in the workers and is responsible for setting
121 up uninative if a loader is found.
122 """
123 enable_uninative(d)
124}
125
126def enable_uninative(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500127 loader = d.getVar("UNINATIVE_LOADER")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500128 if os.path.exists(loader):
129 bb.debug(2, "Enabling uninative")
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500130 d.setVar("NATIVELSBSTRING", "universal%s" % oe.utils.host_gcc_version(d))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500131 d.appendVar("SSTATEPOSTUNPACKFUNCS", " uninative_changeinterp")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500132 d.appendVarFlag("SSTATEPOSTUNPACKFUNCS", "vardepvalueexclude", "| uninative_changeinterp")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400133 d.appendVar("BUILD_LDFLAGS", " -Wl,--allow-shlib-undefined -Wl,--dynamic-linker=${UNINATIVE_LOADER}")
134 d.appendVarFlag("BUILD_LDFLAGS", "vardepvalueexclude", "| -Wl,--allow-shlib-undefined -Wl,--dynamic-linker=${UNINATIVE_LOADER}")
135 d.appendVarFlag("BUILD_LDFLAGS", "vardepsexclude", "UNINATIVE_LOADER")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500136 d.prependVar("PATH", "${STAGING_DIR}-uninative/${BUILD_ARCH}-linux${bindir_native}:")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137
138python uninative_changeinterp () {
139 import subprocess
140 import stat
141 import oe.qa
142
143 if not (bb.data.inherits_class('native', d) or bb.data.inherits_class('crosssdk', d) or bb.data.inherits_class('cross', d)):
144 return
145
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500146 sstateinst = d.getVar('SSTATE_INSTDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147 for walkroot, dirs, files in os.walk(sstateinst):
148 for file in files:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500149 if file.endswith(".so") or ".so." in file:
150 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151 f = os.path.join(walkroot, file)
152 if os.path.islink(f):
153 continue
154 s = os.stat(f)
155 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)):
156 continue
157 elf = oe.qa.ELFFile(f)
158 try:
159 elf.open()
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500160 except oe.qa.NotELFFileError:
161 continue
162 if not elf.isDynamic():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500163 continue
164
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500165 subprocess.check_output(("patchelf-uninative", "--set-interpreter", d.getVar("UNINATIVE_LOADER"), f), stderr=subprocess.STDOUT)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166}