blob: 670efa9f0564f9cd6034843fa82d2b7db19c525a [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 Bishop6e60e8b2018-02-01 10:27:11 -050080 cmd = d.expand("\
81mkdir -p ${UNINATIVE_STAGING_DIR}-uninative; \
82cd ${UNINATIVE_STAGING_DIR}-uninative; \
83tar -xjf ${UNINATIVE_DLDIR}/%s/${UNINATIVE_TARBALL}; \
84${UNINATIVE_STAGING_DIR}-uninative/relocate_sdk.py \
85 ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux \
86 ${UNINATIVE_LOADER} \
87 ${UNINATIVE_LOADER} \
88 ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux/${bindir_native}/patchelf-uninative \
89 ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux${base_libdir_native}/libc*.so" % chksum)
90 subprocess.check_output(cmd, shell=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050091
92 with open(loaderchksum, "w") as f:
93 f.write(chksum)
94
95 enable_uninative(d)
96
97 except bb.fetch2.BBFetchException as exc:
98 bb.warn("Disabling uninative as unable to fetch uninative tarball: %s" % str(exc))
99 bb.warn("To build your own uninative loader, please bitbake uninative-tarball and set UNINATIVE_TARBALL appropriately.")
100 except subprocess.CalledProcessError as exc:
101 bb.warn("Disabling uninative as unable to install uninative tarball: %s" % str(exc))
102 bb.warn("To build your own uninative loader, please bitbake uninative-tarball and set UNINATIVE_TARBALL appropriately.")
103 finally:
104 os.chdir(olddir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105}
106
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500107python uninative_event_enable() {
108 """
109 This event handler is called in the workers and is responsible for setting
110 up uninative if a loader is found.
111 """
112 enable_uninative(d)
113}
114
115def enable_uninative(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500116 loader = d.getVar("UNINATIVE_LOADER")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500117 if os.path.exists(loader):
118 bb.debug(2, "Enabling uninative")
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500119 d.setVar("NATIVELSBSTRING", "universal%s" % oe.utils.host_gcc_version(d))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500120 d.appendVar("SSTATEPOSTUNPACKFUNCS", " uninative_changeinterp")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500121 d.appendVarFlag("SSTATEPOSTUNPACKFUNCS", "vardepvalueexclude", "| uninative_changeinterp")
122 d.prependVar("PATH", "${STAGING_DIR}-uninative/${BUILD_ARCH}-linux${bindir_native}:")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500123
124python uninative_changeinterp () {
125 import subprocess
126 import stat
127 import oe.qa
128
129 if not (bb.data.inherits_class('native', d) or bb.data.inherits_class('crosssdk', d) or bb.data.inherits_class('cross', d)):
130 return
131
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500132 sstateinst = d.getVar('SSTATE_INSTDIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133 for walkroot, dirs, files in os.walk(sstateinst):
134 for file in files:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500135 if file.endswith(".so") or ".so." in file:
136 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137 f = os.path.join(walkroot, file)
138 if os.path.islink(f):
139 continue
140 s = os.stat(f)
141 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)):
142 continue
143 elf = oe.qa.ELFFile(f)
144 try:
145 elf.open()
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500146 except oe.qa.NotELFFileError:
147 continue
148 if not elf.isDynamic():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149 continue
150
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500151 subprocess.check_output(("patchelf-uninative", "--set-interpreter", d.getVar("UNINATIVE_LOADER"), f), stderr=subprocess.STDOUT)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152}