blob: c87343b3cffcbaa59ff9ede09537cbb329879278 [file] [log] [blame]
Andrew Geisslerd159c7f2021-09-02 21:05:58 -05001inherit rust
2
3RDEPENDS:${PN}:append:class-target = " ${RUSTLIB_DEP}"
4
5RUSTC_ARCHFLAGS += "-C opt-level=3 -g -L ${STAGING_DIR_HOST}/${rustlibdir} -C linker=${RUST_TARGET_CCLD}"
6EXTRA_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.
11OVERLAP_LIBS = "\
12 libc \
13 log \
14 getopts \
15 rand \
16"
17def 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)
25OVERLAP_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
29RUSTC_PREFER_DYNAMIC = "-C prefer-dynamic"
30RUSTC_FLAGS += "${RUSTC_PREFER_DYNAMIC}"
31
32CRATE_NAME ?= "${@d.getVar('BPN').replace('-rs', '').replace('-', '_')}"
33BINNAME ?= "${BPN}"
34LIBNAME ?= "lib${CRATE_NAME}-rs"
35CRATE_TYPE ?= "dylib"
36BIN_SRC ?= "${S}/src/main.rs"
37LIB_SRC ?= "${S}/src/lib.rs"
38
39rustbindest ?= "${bindir}"
40rustlibdest ?= "${rustlibdir}"
41RUST_RPATH_ABS ?= "${rustlibdir}:${rustlib}"
42
43def 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
52RUST_LIB_RPATH_FLAGS ?= "${@relative_rpaths(d.getVar('RUST_RPATH_ABS', True), d.getVar('rustlibdest', True))}"
53RUST_BIN_RPATH_FLAGS ?= "${@relative_rpaths(d.getVar('RUST_RPATH_ABS', True), d.getVar('rustbindest', True))}"
54
55def 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
61def 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
77get_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
92do_configure () {
93}
94
95oe_runrustc () {
96 export RUST_TARGET_PATH="${RUST_TARGET_PATH}"
97 bbnote ${RUSTC} ${RUSTC_ARCHFLAGS} ${RUSTC_FLAGS} "$@"
98 "${RUSTC}" ${RUSTC_ARCHFLAGS} ${RUSTC_FLAGS} "$@"
99}
100
101oe_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}
115oe_compile_rust_lib[vardeps] += "get_overlap_externs"
116
117oe_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}
128oe_compile_rust_bin[vardeps] += "get_overlap_externs"
129
130oe_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
137oe_install_rust_bin () {
138 echo Installing ${BINNAME}
139 install -D -m 755 ${BINNAME} ${D}/${rustbindest}/${BINNAME}
140}
141
142do_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}
148PACKAGE_PREPROCESS_FUNCS += "do_rust_bin_fixups"
149