-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·107 lines (95 loc) · 2.24 KB
/
build
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
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2021 - 2023 Robin Vobruba <hoijui.quaero@gmail.com>
#
# SPDX-License-Identifier: Unlicense
# Exit immediately on each error and unset variable;
# see: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -Eeuo pipefail
#set -Eeu
script_dir=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
# shellcheck source=./env
source "$script_dir/env"
# initial default values
APP_NAME="Rust project build script"
TARGET_FLAG_MUSL="--target=x86_64-unknown-linux-musl"
musl=true
strip=true
function print_help() {
local script_name="$(basename "$0")"
echo "$APP_NAME - Simply builds the current project."
echo "By default, it tries to create a MUSL build,"
echo "which is compatible with all Linux 64bit systems."
echo "NOTE: This script depends on the 'env' script in the same directory."
echo
echo "Usage:"
echo " $script_name [OPTION...]"
echo "Options:"
echo " -h, --help"
echo " Show this help message and exit"
echo " --skip-musl"
echo " Do not build for MUSL, but for the native target"
echo " --skip-strip"
echo " Do not strip the debug-symbols from the resulting binary"
echo "Examples:"
echo " $script_name"
echo " $script_name --skip-musl"
}
# read command-line args
POSITIONAL=()
while [[ $# -gt 0 ]]
do
arg="$1"
shift # $2 -> $1, $3 -> $2, ...
case "$arg" in
-h|--help)
print_help
exit 0
;;
--skip-musl)
musl=false
;;
--skip-strip)
strip=false
;;
*) # non-/unknown option
POSITIONAL+=("$arg") # save it in an array for later
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
uname_out="$(uname -s)"
case "$uname_out" in
Linux*)
linux=true;;
*)
linux=false
esac
if $linux && $musl
then
if [ -z "${TARGET_FLAG:-}" ] || [ "${TARGET_FLAG}" = "$TARGET_FLAG_MUSL" ]
then
TARGET_FLAG="$TARGET_FLAG_MUSL"
else
>&2 echo "\
ERROR: Trying to set target to MUSL (--skip-musl was not given), \
but TARGET_FLAG is already set!"
exit 3
fi
fi
CARGO_FLAGS=()
if [ -n "$TARGET_FLAG" ]
then
CARGO_FLAGS+=("$TARGET_FLAG")
fi
$CARGO build --verbose --release "${CARGO_FLAGS[@]}"
if $strip
then
if [ -f "$BINARY_PATH_MUSL" ]
then
strip "$BINARY_PATH_MUSL"
fi
if [ -f "$BINARY_PATH" ]
then
strip "$BINARY_PATH"
fi
fi