-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcflags.sh
More file actions
executable file
·97 lines (85 loc) · 2.56 KB
/
cflags.sh
File metadata and controls
executable file
·97 lines (85 loc) · 2.56 KB
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
#!/usr/bin/env bash
# Print the concatenated, comment-stripped clang/lld flag set for a given
# (os, arch) pair, optionally including the *-bin final-link layer.
#
# Usage:
# cflags.sh # host os, host arch, compile profile
# cflags.sh <os> <arch> # explicit (os, arch), compile profile
# cflags.sh <os> <arch> --bin # explicit (os, arch), binary (final-link) profile
# cflags.sh --bin # host os, host arch, binary profile
#
# Output is whitespace-separated on a single line, suitable for:
# export CFLAGS="$(./cli/cflags.sh)"
# export LDFLAGS="$(./cli/cflags.sh --bin)"
#
# Recognized values:
# <os>: linux | darwin
# <arch>: amd64 | arm64
set -euo pipefail
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
root="$(cd -- "${script_dir}/.." && pwd -P)"
usage() {
sed -n '2,/^$/{s/^# \{0,1\}//;p;}' "${BASH_SOURCE[0]}" >&2
}
host_os() {
case "$(uname -s)" in
Linux) echo linux ;;
Darwin) echo darwin ;;
*) echo "cflags: unsupported host os: $(uname -s)" >&2; exit 2 ;;
esac
}
host_arch() {
case "$(uname -m)" in
x86_64|amd64) echo amd64 ;;
arm64|aarch64) echo arm64 ;;
*) echo "cflags: unsupported host arch: $(uname -m)" >&2; exit 2 ;;
esac
}
bin=0
positional=()
for arg in "$@"; do
case "$arg" in
--bin) bin=1 ;;
-h|--help) usage; exit 0 ;;
--) ;;
-*) echo "cflags: unknown option: $arg" >&2; exit 2 ;;
*) positional+=("$arg") ;;
esac
done
if [[ ${#positional[@]} -gt 2 ]]; then
echo "cflags: too many positional arguments (expected at most 2: <os> <arch>)" >&2
exit 2
fi
os="${positional[0]:-$(host_os)}"
arch="${positional[1]:-$(host_arch)}"
case "$os" in
linux|darwin) ;;
*) echo "cflags: unsupported os: $os (expected: linux | darwin)" >&2; exit 2 ;;
esac
case "$arch" in
amd64|arm64) ;;
*) echo "cflags: unsupported arch: $arch (expected: amd64 | arm64)" >&2; exit 2 ;;
esac
files=(
"${root}/base.txt"
"${root}/${os}.txt"
"${root}/${os}-${arch}.txt"
)
if [[ $bin -eq 1 ]]; then
files+=(
"${root}/${os}-bin.txt"
"${root}/${os}-${arch}-bin.txt"
)
fi
for f in "${files[@]}"; do
if [[ ! -f "$f" ]]; then
echo "cflags: missing profile file: $f" >&2
exit 1
fi
done
# Strip end-of-line comments + blank lines, then collapse to a single
# whitespace-separated line. xargs with no command defaults to echo,
# which is exactly the join semantics we want.
sed -e 's/#.*$//' -e 's/[[:space:]]\{1,\}$//' "${files[@]}" \
| grep -v '^[[:space:]]*$' \
| xargs