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