-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path15-rust.sh
126 lines (113 loc) · 3.88 KB
/
15-rust.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
##15-rust.sh: Builds Rust + Cargo projects
##@copyright GPL-2.0+
DEFAULT_CARGO_CONFIG=(
--config 'profile.release.lto = true'
--config 'profile.release.incremental = false'
--config 'profile.release.codegen-units = 1'
--config 'profile.release.strip = false'
)
build_rust_prepare_registry() {
local REGISTRY_URL='https://github.com/rust-lang/crates.io-index'
local REGISTRY_DIR='github.heygears.com-1ecc6299db9ec823'
local THIS_REGISTRY_DIR="$HOME/.cargo/registry/index/${REGISTRY_DIR}/.git"
if [ ! -d "${THIS_REGISTRY_DIR}" ]; then
git clone --bare "${REGISTRY_URL}" "${THIS_REGISTRY_DIR}"
fi
}
build_rust_get_installed_rust_version() {
rustc --version | perl -ne '/^rustc\s+(\d+\.\d+\.\d+)\s+\(/ && print"$1\n"'
}
build_rust_probe() {
[ -f "$SRCDIR"/Cargo.toml ]
}
build_rust_inject_lto() {
bool "${USECLANG}" \
|| abdie 'Please set "USECLANG=1" in your defines to enable proper LTO.'
command -v ld.lld > /dev/null \
|| abdie 'ld.lld is unavailable. Please add "llvm" to your $BUILDDEP or disable LTO for this architecture.'
}
build_rust_audit() {
# FIXME: cargo-audit >= 0.18 uses rustls, which breaks non-amd64/arm64 architectures.
if ab_match_arch "!(amd64|arm64)" || bool "$NOCARGOAUDIT"; then
return 0
fi
abinfo 'Auditing dependencies...'
if ! command -v cargo-audit > /dev/null; then
abdie "cargo-audit not found: $?."
elif [ -f "$SRCDIR"/Cargo.lock ]; then
if ! cargo audit; then
abinfo 'Vulnerabilities detected! Attempting automatic fix ...'
cargo audit fix \
|| abdie 'Unable to fix vulnerability! Refusing to continue.'
fi
else
abdie 'No lock file found -- Dependency information unreliable. Unable to conduct audit.'
fi
}
build_rust_configure() {
abinfo 'Setting up build environment: $PKG_CONFIG_SYSROOT_DIR= hack ...'
export PKG_CONFIG_SYSROOT_DIR=/
BUILD_START
if bool AB_FLAGS_O3; then
DEFAULT_CARGO_CONFIG+=(--config 'profile.release.opt-level = 3')
fi
if ! bool ABSPLITDBG; then
DEFAULT_CARGO_CONFIG+=(--config 'profile.release.debug = 0')
fi
[ -f "$SRCDIR"/Cargo.lock ] \
|| abwarn "This project is lacking the lock file. Please report this issue to the upstream."
if ! dpkg --compare-versions "$(build_rust_get_installed_rust_version)" ge '1.70.0'; then
build_rust_prepare_registry
fi
if ab_match_arch "ppc64" && \
! bool "$NOLTO"; then
build_rust_inject_lto
fi
}
build_rust_build() {
BUILD_READY
abinfo 'Building Cargo package ...'
install -vd "$PKGDIR/usr/bin/"
ab_tostringarray CARGO_AFTER
local manifest
manifest="$(cargo read-manifest --manifest-path "$SRCDIR"/Cargo.toml || true)"
abjson_get_item "${manifest}" "['targets'][0]['kind'][0]" _JSON_OUTPUT || true
if [ "${_JSON_OUTPUT}" = 'bin' ]; then
cargo install --locked -f --path "$SRCDIR" \
"${DEFAULT_CARGO_CONFIG[@]}" \
--root="$PKGDIR/usr/" \
"${CARGO_AFTER[@]}" \
|| abdie "Compilation failed: $?."
else
abinfo 'Using fallback build method ...'
parsed_flags="$(getopt -o ':p' -- "${CARGO_AFTER[@]}" || true)"
parsed_flags=(${parsed_flags})
if [[ "${parsed_flags}" = '-p' ]]; then
DEFAULT_CARGO_CONFIG+=('--workspace')
fi
cargo build "${DEFAULT_CARGO_CONFIG[@]}" \
--release --locked \
"${CARGO_AFTER[@]}" \
|| abdie "Compilation failed: $?."
abinfo "Installing binaries in the workspace ..."
find "$SRCDIR"/target/release \
-maxdepth 1 \
-executable \
-type f ! \
-name "*.so" \
-exec 'install' '-Dvm755' '{}' "$PKGDIR/usr/bin/" ';'
fi
}
build_rust_install() {
abinfo 'Installing exported shared libraries in the workspace ...'
for i in "$SRCDIR"/target/release/*.so*; do
# filter out the compiler plugins
readelf --wide --dyn-syms "$i" | grep -q '__rustc_proc_macro_decls_[0-9a-f]*__' ||\
install -Dvm755 "$SRCDIR"/target/release/"$i" -t "$PKGDIR"/usr/lib/
done
abinfo 'Dropping lingering files ...'
rm -v "$PKGDIR"/usr/.crates{.toml,2.json}
BUILD_FINAL
}
ab_register_template -l rust -- rustc cargo readelf