summaryrefslogtreecommitdiffstats
path: root/meta/classes/rust-bin.bbclass
diff options
context:
space:
mode:
authorRandy MacLeod <Randy.MacLeod@windriver.com>2021-08-10 13:52:19 -0400
committerRichard Purdie <richard.purdie@linuxfoundation.org>2021-08-26 22:09:43 +0100
commit61e1570c6a09c1984e919e8c0a82a74c1a08d821 (patch)
treeaccab4b08aa3c62f098c9bb19399efb46906256a /meta/classes/rust-bin.bbclass
parent705b1d757fa221614f4f72cabf0fac5884cb6bfd (diff)
downloadpoky-61e1570c6a09c1984e919e8c0a82a74c1a08d821.tar.gz
rust: initial merge of most of meta-rust
In the meta-rust repo at commit: 448047c Upgrade to 1.54.0 (#359) Make the required directories: mkdir ../oe-core/meta/recipes-devtools/rust mkdir ../oe-core/meta/recipes-devtools/cargo mkdir ../oe-core/meta/recipes-example and then: cp recipes-devtools/rust/* ../oe-core/meta/recipes-devtools/rust cp recipes-devtools/cargo/* ../oe-core/meta/recipes-devtools/cargo cp lib/crate.py ../oe-core/meta/lib cp recipes-example/* ../oe-core/meta/recipes-example cp conf/distro/include/rust_* ../oe-core/meta/conf/distro/include/ cp classes/* ../oe-core/meta/classes/ cp recipes-core/packagegroups/packagegroup-rust-cross-canadian.bb ../oe-core/meta/recipes-core/packagegroups (From OE-Core rev: 3ed57578cca93ff1ba4e0bf3f25566e10659a2f9) Signed-off-by: Randy MacLeod <Randy.MacLeod@windriver.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/classes/rust-bin.bbclass')
-rw-r--r--meta/classes/rust-bin.bbclass149
1 files changed, 149 insertions, 0 deletions
diff --git a/meta/classes/rust-bin.bbclass b/meta/classes/rust-bin.bbclass
new file mode 100644
index 0000000000..c87343b3cf
--- /dev/null
+++ b/meta/classes/rust-bin.bbclass
@@ -0,0 +1,149 @@
1inherit 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