1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7UNINATIVE_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)}${@bb.utils.contains('BUILD_ARCH', 'ppc64le', 'ld64.so.2', '', d)}"
8UNINATIVE_STAGING_DIR ?= "${STAGING_DIR}"
9
10UNINATIVE_URL ?= "unset"
11UNINATIVE_TARBALL ?= "${BUILD_ARCH}-nativesdk-libc-${UNINATIVE_VERSION}.tar.xz"
12# Example checksums
13#UNINATIVE_CHECKSUM[aarch64] = "dead"
14#UNINATIVE_CHECKSUM[i686] = "dead"
15#UNINATIVE_CHECKSUM[x86_64] = "dead"
16UNINATIVE_DLDIR ?= "${DL_DIR}/uninative/"
17
18# Enabling uninative will change the following variables so they need to go the parsing ignored variables list to prevent multiple recipe parsing
19BB_HASHCONFIG_IGNORE_VARS += "NATIVELSBSTRING SSTATEPOSTUNPACKFUNCS BUILD_LDFLAGS"
20
21addhandler uninative_event_fetchloader
22uninative_event_fetchloader[eventmask] = "bb.event.BuildStarted"
23
24addhandler uninative_event_enable
25uninative_event_enable[eventmask] = "bb.event.ConfigParsed"
26
27python uninative_event_fetchloader() {
28    """
29    This event fires on the parent and will try to fetch the tarball if the
30    loader isn't already present.
31    """
32
33    chksum = d.getVarFlag("UNINATIVE_CHECKSUM", d.getVar("BUILD_ARCH"))
34    if not chksum:
35        bb.fatal("Uninative selected but not configured correctly, please set UNINATIVE_CHECKSUM[%s]" % d.getVar("BUILD_ARCH"))
36
37    loader = d.getVar("UNINATIVE_LOADER")
38    loaderchksum = loader + ".chksum"
39    if os.path.exists(loader) and os.path.exists(loaderchksum):
40        with open(loaderchksum, "r") as f:
41            readchksum = f.read().strip()
42        if readchksum == chksum:
43            if "uninative" not in d.getVar("SSTATEPOSTUNPACKFUNCS"):
44                enable_uninative(d)
45            return
46
47    import subprocess
48    try:
49        # Save and restore cwd as Fetch.download() does a chdir()
50        olddir = os.getcwd()
51
52        tarball = d.getVar("UNINATIVE_TARBALL")
53        tarballdir = os.path.join(d.getVar("UNINATIVE_DLDIR"), chksum)
54        tarballpath = os.path.join(tarballdir, tarball)
55
56        if not os.path.exists(tarballpath + ".done"):
57            bb.utils.mkdirhier(tarballdir)
58            if d.getVar("UNINATIVE_URL") == "unset":
59                bb.fatal("Uninative selected but not configured, please set UNINATIVE_URL")
60
61            localdata = bb.data.createCopy(d)
62            localdata.setVar('FILESPATH', "")
63            localdata.setVar('DL_DIR', tarballdir)
64            # Our games with path manipulation of DL_DIR mean standard PREMIRRORS don't work
65            # and we can't easily put 'chksum' into the url path from a url parameter with
66            # the current fetcher url handling
67            premirrors = bb.fetch2.mirror_from_string(localdata.getVar("PREMIRRORS"))
68            for line in premirrors:
69                try:
70                    (find, replace) = line
71                except ValueError:
72                    continue
73                if find.startswith("http"):
74                    localdata.appendVar("PREMIRRORS", " ${UNINATIVE_URL}${UNINATIVE_TARBALL} %s/uninative/%s/${UNINATIVE_TARBALL}" % (replace, chksum))
75
76            srcuri = d.expand("${UNINATIVE_URL}${UNINATIVE_TARBALL};sha256sum=%s" % chksum)
77            bb.note("Fetching uninative binary shim %s (will check PREMIRRORS first)" % srcuri)
78
79            fetcher = bb.fetch2.Fetch([srcuri], localdata, cache=False)
80            fetcher.download()
81            localpath = fetcher.localpath(srcuri)
82            if localpath != tarballpath and os.path.exists(localpath) and not os.path.exists(tarballpath):
83                # Follow the symlink behavior from the bitbake fetch2.
84                # This will cover the case where an existing symlink is broken
85                # as well as if there are two processes trying to create it
86                # at the same time.
87                if os.path.islink(tarballpath):
88                    # Broken symbolic link
89                    os.unlink(tarballpath)
90
91                # Deal with two processes trying to make symlink at once
92                try:
93                    os.symlink(localpath, tarballpath)
94                except FileExistsError:
95                    pass
96
97        # ldd output is "ldd (Ubuntu GLIBC 2.23-0ubuntu10) 2.23", extract last option from first line
98        glibcver = subprocess.check_output(["ldd", "--version"]).decode('utf-8').split('\n')[0].split()[-1]
99        if bb.utils.vercmp_string(d.getVar("UNINATIVE_MAXGLIBCVERSION"), glibcver) < 0:
100            raise RuntimeError("Your host glibc version (%s) is newer than that in uninative (%s). Disabling uninative so that sstate is not corrupted." % (glibcver, d.getVar("UNINATIVE_MAXGLIBCVERSION")))
101
102        cmd = d.expand("\
103mkdir -p ${UNINATIVE_STAGING_DIR}-uninative; \
104cd ${UNINATIVE_STAGING_DIR}-uninative; \
105tar -xJf ${UNINATIVE_DLDIR}/%s/${UNINATIVE_TARBALL}; \
106${UNINATIVE_STAGING_DIR}-uninative/relocate_sdk.py \
107  ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux \
108  ${UNINATIVE_LOADER} \
109  ${UNINATIVE_LOADER} \
110  ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux/${bindir_native}/patchelf-uninative \
111  ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux${base_libdir_native}/libc*.so*" % chksum)
112        subprocess.check_output(cmd, shell=True)
113
114        with open(loaderchksum, "w") as f:
115            f.write(chksum)
116
117        enable_uninative(d)
118
119    except RuntimeError as e:
120        bb.warn(str(e))
121    except bb.fetch2.BBFetchException as exc:
122        bb.warn("Disabling uninative as unable to fetch uninative tarball: %s" % str(exc))
123        bb.warn("To build your own uninative loader, please bitbake uninative-tarball and set UNINATIVE_TARBALL appropriately.")
124    except subprocess.CalledProcessError as exc:
125        bb.warn("Disabling uninative as unable to install uninative tarball: %s" % str(exc))
126        bb.warn("To build your own uninative loader, please bitbake uninative-tarball and set UNINATIVE_TARBALL appropriately.")
127    finally:
128        os.chdir(olddir)
129}
130
131python uninative_event_enable() {
132    """
133    This event handler is called in the workers and is responsible for setting
134    up uninative if a loader is found.
135    """
136    enable_uninative(d)
137}
138
139def enable_uninative(d):
140    loader = d.getVar("UNINATIVE_LOADER")
141    if os.path.exists(loader):
142        bb.debug(2, "Enabling uninative")
143        d.setVar("NATIVELSBSTRING", "universal%s" % oe.utils.host_gcc_version(d))
144        d.appendVar("SSTATEPOSTUNPACKFUNCS", " uninative_changeinterp")
145        d.appendVarFlag("SSTATEPOSTUNPACKFUNCS", "vardepvalueexclude", "| uninative_changeinterp")
146        d.appendVar("BUILD_LDFLAGS", " -Wl,--allow-shlib-undefined -Wl,--dynamic-linker=${UNINATIVE_LOADER}")
147        d.appendVarFlag("BUILD_LDFLAGS", "vardepvalueexclude", "| -Wl,--allow-shlib-undefined -Wl,--dynamic-linker=${UNINATIVE_LOADER}")
148        d.appendVarFlag("BUILD_LDFLAGS", "vardepsexclude", "UNINATIVE_LOADER")
149        d.prependVar("PATH", "${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux${bindir_native}:")
150
151python uninative_changeinterp () {
152    import subprocess
153    import stat
154    import oe.qa
155
156    if not (bb.data.inherits_class('native', d) or bb.data.inherits_class('crosssdk', d) or bb.data.inherits_class('cross', d)):
157        return
158
159    sstateinst = d.getVar('SSTATE_INSTDIR')
160    for walkroot, dirs, files in os.walk(sstateinst):
161        for file in files:
162            if file.endswith(".so") or ".so." in file:
163                continue
164            f = os.path.join(walkroot, file)
165            if os.path.islink(f):
166                continue
167            s = os.stat(f)
168            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)):
169                continue
170            elf = oe.qa.ELFFile(f)
171            try:
172                elf.open()
173            except oe.qa.NotELFFileError:
174                continue
175            if not elf.isDynamic():
176                continue
177
178            os.chmod(f, s[stat.ST_MODE] | stat.S_IWUSR)
179            subprocess.check_output(("patchelf-uninative", "--set-interpreter", d.getVar("UNINATIVE_LOADER"), f), stderr=subprocess.STDOUT)
180            os.chmod(f, s[stat.ST_MODE])
181}
182