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