Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame^] | 1 | inherit rust |
| 2 | |
| 3 | RDEPENDS:${PN}:append:class-target = " ${RUSTLIB_DEP}" |
| 4 | |
| 5 | RUSTC_ARCHFLAGS += "-C opt-level=3 -g -L ${STAGING_DIR_HOST}/${rustlibdir} -C linker=${RUST_TARGET_CCLD}" |
| 6 | EXTRA_OEMAKE += 'RUSTC_ARCHFLAGS="${RUSTC_ARCHFLAGS}"' |
| 7 | |
| 8 | # Some libraries alias with the standard library but libstd is configured to |
| 9 | # make it difficult or imposisble to use its version. Unfortunately libstd |
| 10 | # must be explicitly overridden using extern. |
| 11 | OVERLAP_LIBS = "\ |
| 12 | libc \ |
| 13 | log \ |
| 14 | getopts \ |
| 15 | rand \ |
| 16 | " |
| 17 | def get_overlap_deps(d): |
| 18 | deps = d.getVar("DEPENDS").split() |
| 19 | overlap_deps = [] |
| 20 | for o in d.getVar("OVERLAP_LIBS").split(): |
| 21 | l = len([o for dep in deps if (o + '-rs' in dep)]) |
| 22 | if l > 0: |
| 23 | overlap_deps.append(o) |
| 24 | return " ".join(overlap_deps) |
| 25 | OVERLAP_DEPS = "${@get_overlap_deps(d)}" |
| 26 | |
| 27 | # Prevents multiple static copies of standard library modules |
| 28 | # See https://github.com/rust-lang/rust/issues/19680 |
| 29 | RUSTC_PREFER_DYNAMIC = "-C prefer-dynamic" |
| 30 | RUSTC_FLAGS += "${RUSTC_PREFER_DYNAMIC}" |
| 31 | |
| 32 | CRATE_NAME ?= "${@d.getVar('BPN').replace('-rs', '').replace('-', '_')}" |
| 33 | BINNAME ?= "${BPN}" |
| 34 | LIBNAME ?= "lib${CRATE_NAME}-rs" |
| 35 | CRATE_TYPE ?= "dylib" |
| 36 | BIN_SRC ?= "${S}/src/main.rs" |
| 37 | LIB_SRC ?= "${S}/src/lib.rs" |
| 38 | |
| 39 | rustbindest ?= "${bindir}" |
| 40 | rustlibdest ?= "${rustlibdir}" |
| 41 | RUST_RPATH_ABS ?= "${rustlibdir}:${rustlib}" |
| 42 | |
| 43 | def relative_rpaths(paths, base): |
| 44 | relpaths = set() |
| 45 | for p in paths.split(':'): |
| 46 | if p == base: |
| 47 | relpaths.add('$ORIGIN') |
| 48 | continue |
| 49 | relpaths.add(os.path.join('$ORIGIN', os.path.relpath(p, base))) |
| 50 | return '-rpath=' + ':'.join(relpaths) if len(relpaths) else '' |
| 51 | |
| 52 | RUST_LIB_RPATH_FLAGS ?= "${@relative_rpaths(d.getVar('RUST_RPATH_ABS', True), d.getVar('rustlibdest', True))}" |
| 53 | RUST_BIN_RPATH_FLAGS ?= "${@relative_rpaths(d.getVar('RUST_RPATH_ABS', True), d.getVar('rustbindest', True))}" |
| 54 | |
| 55 | def libfilename(d): |
| 56 | if d.getVar('CRATE_TYPE', True) == 'dylib': |
| 57 | return d.getVar('LIBNAME', True) + '.so' |
| 58 | else: |
| 59 | return d.getVar('LIBNAME', True) + '.rlib' |
| 60 | |
| 61 | def link_args(d, bin): |
| 62 | linkargs = [] |
| 63 | if bin: |
| 64 | rpaths = d.getVar('RUST_BIN_RPATH_FLAGS', False) |
| 65 | else: |
| 66 | rpaths = d.getVar('RUST_LIB_RPATH_FLAGS', False) |
| 67 | if d.getVar('CRATE_TYPE', True) == 'dylib': |
| 68 | linkargs.append('-soname') |
| 69 | linkargs.append(libfilename(d)) |
| 70 | if len(rpaths): |
| 71 | linkargs.append(rpaths) |
| 72 | if len(linkargs): |
| 73 | return ' '.join(['-Wl,' + arg for arg in linkargs]) |
| 74 | else: |
| 75 | return '' |
| 76 | |
| 77 | get_overlap_externs () { |
| 78 | externs= |
| 79 | for dep in ${OVERLAP_DEPS}; do |
| 80 | extern=$(ls ${STAGING_DIR_HOST}/${rustlibdir}/lib$dep-rs.{so,rlib} 2>/dev/null \ |
| 81 | | awk '{print $1}'); |
| 82 | if [ -n "$extern" ]; then |
| 83 | externs="$externs --extern $dep=$extern" |
| 84 | else |
| 85 | echo "$dep in depends but no such library found in ${rustlibdir}!" >&2 |
| 86 | exit 1 |
| 87 | fi |
| 88 | done |
| 89 | echo "$externs" |
| 90 | } |
| 91 | |
| 92 | do_configure () { |
| 93 | } |
| 94 | |
| 95 | oe_runrustc () { |
| 96 | export RUST_TARGET_PATH="${RUST_TARGET_PATH}" |
| 97 | bbnote ${RUSTC} ${RUSTC_ARCHFLAGS} ${RUSTC_FLAGS} "$@" |
| 98 | "${RUSTC}" ${RUSTC_ARCHFLAGS} ${RUSTC_FLAGS} "$@" |
| 99 | } |
| 100 | |
| 101 | oe_compile_rust_lib () { |
| 102 | rm -rf ${LIBNAME}.{rlib,so} |
| 103 | local -a link_args |
| 104 | if [ -n '${@link_args(d, False)}' ]; then |
| 105 | link_args[0]='-C' |
| 106 | link_args[1]='link-args=${@link_args(d, False)}' |
| 107 | fi |
| 108 | oe_runrustc $(get_overlap_externs) \ |
| 109 | "${link_args[@]}" \ |
| 110 | ${LIB_SRC} \ |
| 111 | -o ${@libfilename(d)} \ |
| 112 | --crate-name=${CRATE_NAME} --crate-type=${CRATE_TYPE} \ |
| 113 | "$@" |
| 114 | } |
| 115 | oe_compile_rust_lib[vardeps] += "get_overlap_externs" |
| 116 | |
| 117 | oe_compile_rust_bin () { |
| 118 | rm -rf ${BINNAME} |
| 119 | local -a link_args |
| 120 | if [ -n '${@link_args(d, True)}' ]; then |
| 121 | link_args[0]='-C' |
| 122 | link_args[1]='link-args=${@link_args(d, True)}' |
| 123 | fi |
| 124 | oe_runrustc $(get_overlap_externs) \ |
| 125 | "${link_args[@]}" \ |
| 126 | ${BIN_SRC} -o ${BINNAME} "$@" |
| 127 | } |
| 128 | oe_compile_rust_bin[vardeps] += "get_overlap_externs" |
| 129 | |
| 130 | oe_install_rust_lib () { |
| 131 | for lib in $(ls ${LIBNAME}.{so,rlib} 2>/dev/null); do |
| 132 | echo Installing $lib |
| 133 | install -D -m 755 $lib ${D}/${rustlibdest}/$lib |
| 134 | done |
| 135 | } |
| 136 | |
| 137 | oe_install_rust_bin () { |
| 138 | echo Installing ${BINNAME} |
| 139 | install -D -m 755 ${BINNAME} ${D}/${rustbindest}/${BINNAME} |
| 140 | } |
| 141 | |
| 142 | do_rust_bin_fixups() { |
| 143 | for f in `find ${PKGD} -name '*.so*'`; do |
| 144 | echo "Strip rust note: $f" |
| 145 | ${OBJCOPY} -R .note.rustc $f $f |
| 146 | done |
| 147 | } |
| 148 | PACKAGE_PREPROCESS_FUNCS += "do_rust_bin_fixups" |
| 149 | |