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