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