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