Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1 | UNINATIVE_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)}" |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 2 | UNINATIVE_STAGING_DIR ?= "${STAGING_DIR}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 3 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 4 | UNINATIVE_URL ?= "unset" |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 5 | UNINATIVE_TARBALL ?= "${BUILD_ARCH}-nativesdk-libc-${UNINATIVE_VERSION}.tar.xz" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 6 | # Example checksums |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 7 | #UNINATIVE_CHECKSUM[aarch64] = "dead" |
| 8 | #UNINATIVE_CHECKSUM[i686] = "dead" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 9 | #UNINATIVE_CHECKSUM[x86_64] = "dead" |
| 10 | UNINATIVE_DLDIR ?= "${DL_DIR}/uninative/" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 12 | # Enabling uninative will change the following variables so they need to go the parsing white list to prevent multiple recipe parsing |
| 13 | BB_HASHCONFIG_WHITELIST += "NATIVELSBSTRING SSTATEPOSTUNPACKFUNCS BUILD_LDFLAGS" |
| 14 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 15 | addhandler uninative_event_fetchloader |
| 16 | uninative_event_fetchloader[eventmask] = "bb.event.BuildStarted" |
| 17 | |
| 18 | addhandler uninative_event_enable |
| 19 | uninative_event_enable[eventmask] = "bb.event.ConfigParsed" |
| 20 | |
| 21 | python 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 27 | chksum = d.getVarFlag("UNINATIVE_CHECKSUM", d.getVar("BUILD_ARCH")) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 28 | if not chksum: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 29 | bb.fatal("Uninative selected but not configured correctly, please set UNINATIVE_CHECKSUM[%s]" % d.getVar("BUILD_ARCH")) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 30 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 31 | loader = d.getVar("UNINATIVE_LOADER") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 32 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 44 | tarball = d.getVar("UNINATIVE_TARBALL") |
| 45 | tarballdir = os.path.join(d.getVar("UNINATIVE_DLDIR"), chksum) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 46 | tarballpath = os.path.join(tarballdir, tarball) |
| 47 | |
Brad Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 48 | if not os.path.exists(tarballpath + ".done"): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 49 | bb.utils.mkdirhier(tarballdir) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 50 | if d.getVar("UNINATIVE_URL") == "unset": |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 51 | 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 56 | # 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 |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 59 | premirrors = bb.fetch2.mirror_from_string(localdata.getVar("PREMIRRORS")) |
| 60 | for line in premirrors: |
| 61 | try: |
| 62 | (find, replace) = line |
| 63 | except ValueError: |
| 64 | continue |
| 65 | if find.startswith("http"): |
| 66 | localdata.appendVar("PREMIRRORS", " ${UNINATIVE_URL}${UNINATIVE_TARBALL} %s/uninative/%s/${UNINATIVE_TARBALL}" % (replace, chksum)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 67 | |
| 68 | srcuri = d.expand("${UNINATIVE_URL}${UNINATIVE_TARBALL};sha256sum=%s" % chksum) |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 69 | bb.note("Fetching uninative binary shim %s (will check PREMIRRORS first)" % srcuri) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 70 | |
| 71 | fetcher = bb.fetch2.Fetch([srcuri], localdata, cache=False) |
| 72 | fetcher.download() |
| 73 | localpath = fetcher.localpath(srcuri) |
| 74 | if localpath != tarballpath and os.path.exists(localpath) and not os.path.exists(tarballpath): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 75 | # Follow the symlink behavior from the bitbake fetch2. |
| 76 | # This will cover the case where an existing symlink is broken |
| 77 | # as well as if there are two processes trying to create it |
| 78 | # at the same time. |
| 79 | if os.path.islink(tarballpath): |
| 80 | # Broken symbolic link |
| 81 | os.unlink(tarballpath) |
| 82 | |
| 83 | # Deal with two processes trying to make symlink at once |
| 84 | try: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 85 | os.symlink(localpath, tarballpath) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 86 | except FileExistsError: |
| 87 | pass |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 88 | |
Brad Bishop | 0011132 | 2018-04-01 22:23:53 -0400 | [diff] [blame] | 89 | # ldd output is "ldd (Ubuntu GLIBC 2.23-0ubuntu10) 2.23", extract last option from first line |
| 90 | glibcver = subprocess.check_output(["ldd", "--version"]).decode('utf-8').split('\n')[0].split()[-1] |
| 91 | if bb.utils.vercmp_string(d.getVar("UNINATIVE_MAXGLIBCVERSION"), glibcver) < 0: |
Andrew Geissler | f034379 | 2020-11-18 10:42:21 -0600 | [diff] [blame] | 92 | 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"))) |
Brad Bishop | 0011132 | 2018-04-01 22:23:53 -0400 | [diff] [blame] | 93 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 94 | cmd = d.expand("\ |
| 95 | mkdir -p ${UNINATIVE_STAGING_DIR}-uninative; \ |
| 96 | cd ${UNINATIVE_STAGING_DIR}-uninative; \ |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 97 | tar -xJf ${UNINATIVE_DLDIR}/%s/${UNINATIVE_TARBALL}; \ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 98 | ${UNINATIVE_STAGING_DIR}-uninative/relocate_sdk.py \ |
| 99 | ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux \ |
| 100 | ${UNINATIVE_LOADER} \ |
| 101 | ${UNINATIVE_LOADER} \ |
| 102 | ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux/${bindir_native}/patchelf-uninative \ |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 103 | ${UNINATIVE_STAGING_DIR}-uninative/${BUILD_ARCH}-linux${base_libdir_native}/libc*.so*" % chksum) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 104 | subprocess.check_output(cmd, shell=True) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 105 | |
| 106 | with open(loaderchksum, "w") as f: |
| 107 | f.write(chksum) |
| 108 | |
| 109 | enable_uninative(d) |
| 110 | |
Brad Bishop | 0011132 | 2018-04-01 22:23:53 -0400 | [diff] [blame] | 111 | except RuntimeError as e: |
| 112 | bb.warn(str(e)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 113 | except bb.fetch2.BBFetchException as exc: |
| 114 | bb.warn("Disabling uninative as unable to fetch uninative tarball: %s" % str(exc)) |
| 115 | bb.warn("To build your own uninative loader, please bitbake uninative-tarball and set UNINATIVE_TARBALL appropriately.") |
| 116 | except subprocess.CalledProcessError as exc: |
| 117 | bb.warn("Disabling uninative as unable to install uninative tarball: %s" % str(exc)) |
| 118 | bb.warn("To build your own uninative loader, please bitbake uninative-tarball and set UNINATIVE_TARBALL appropriately.") |
| 119 | finally: |
| 120 | os.chdir(olddir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 121 | } |
| 122 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 123 | python uninative_event_enable() { |
| 124 | """ |
| 125 | This event handler is called in the workers and is responsible for setting |
| 126 | up uninative if a loader is found. |
| 127 | """ |
| 128 | enable_uninative(d) |
| 129 | } |
| 130 | |
| 131 | def enable_uninative(d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 132 | loader = d.getVar("UNINATIVE_LOADER") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 133 | if os.path.exists(loader): |
| 134 | bb.debug(2, "Enabling uninative") |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 135 | d.setVar("NATIVELSBSTRING", "universal%s" % oe.utils.host_gcc_version(d)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 136 | d.appendVar("SSTATEPOSTUNPACKFUNCS", " uninative_changeinterp") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 137 | d.appendVarFlag("SSTATEPOSTUNPACKFUNCS", "vardepvalueexclude", "| uninative_changeinterp") |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 138 | d.appendVar("BUILD_LDFLAGS", " -Wl,--allow-shlib-undefined -Wl,--dynamic-linker=${UNINATIVE_LOADER}") |
| 139 | d.appendVarFlag("BUILD_LDFLAGS", "vardepvalueexclude", "| -Wl,--allow-shlib-undefined -Wl,--dynamic-linker=${UNINATIVE_LOADER}") |
| 140 | d.appendVarFlag("BUILD_LDFLAGS", "vardepsexclude", "UNINATIVE_LOADER") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 141 | d.prependVar("PATH", "${STAGING_DIR}-uninative/${BUILD_ARCH}-linux${bindir_native}:") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 142 | |
| 143 | python uninative_changeinterp () { |
| 144 | import subprocess |
| 145 | import stat |
| 146 | import oe.qa |
| 147 | |
| 148 | if not (bb.data.inherits_class('native', d) or bb.data.inherits_class('crosssdk', d) or bb.data.inherits_class('cross', d)): |
| 149 | return |
| 150 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 151 | sstateinst = d.getVar('SSTATE_INSTDIR') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 152 | for walkroot, dirs, files in os.walk(sstateinst): |
| 153 | for file in files: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 154 | if file.endswith(".so") or ".so." in file: |
| 155 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 156 | f = os.path.join(walkroot, file) |
| 157 | if os.path.islink(f): |
| 158 | continue |
| 159 | s = os.stat(f) |
| 160 | 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 | continue |
| 162 | elf = oe.qa.ELFFile(f) |
| 163 | try: |
| 164 | elf.open() |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 165 | except oe.qa.NotELFFileError: |
| 166 | continue |
| 167 | if not elf.isDynamic(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 168 | continue |
| 169 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 170 | subprocess.check_output(("patchelf-uninative", "--set-interpreter", d.getVar("UNINATIVE_LOADER"), f), stderr=subprocess.STDOUT) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 171 | } |