~q3cpma/mus

ref: cf29c2237d0c99e5240a55895c07564ef4635392 mus/mus_player/build_player.sh -rwxr-xr-x 5.1 KiB
cf29c223q3cpma Add a #pragma once to the new xoshiro128plus.h header 1 year, 7 months ago
                                                                                
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/sh
set -eu
cd -- "$(dirname -- "$0")"
self=./$(basename -- "$0")
. ../build_util.sh


# Get configuration from the environment
CC=${CC:-c99}
std=c99
CFLAGS=${CFLAGS:-}
CPPFLAGS=${CPPFLAGS:-}
append_cppflag -D_DEFAULT_SOURCE
LDFLAGS=${LDFLAGS:-}
LDLIBS=
MAKE=${MAKE:-make}
JOBS=${JOBS:-1}
LTO=${LTO:-false}
PGO=${PGO:-false}
STATIC=${STATIC:-false}
NATIVE=${NATIVE:-false}
CONFIG=${CONFIG:-release}
DESTDIR=${DESTDIR+${DESTDIR%/}}
PREFIX=$DESTDIR${PREFIX:-/usr/local}
PREFIX=${PREFIX%/}

# Target specific definitions
std=c11
BIN=mus_player
gitver=$(git_version ..)
append_cppflag \
	-DPROG_NAME="$(dquote "$BIN" 2)" \
	-DPROG_VERSION="$(dquote "$gitver" 2)"
LDLIBS='-pthread -lm -lao -ldl'
USE_FLAC=${USE_FLAC:-true}
USE_OGG_VORBIS=${USE_OGG_VORBIS:-false}
USE_OGG_OPUS=${USE_OGG_OPUS:-false}

# Argument parsing
if "$PGO"
then
	[ $# -eq 0 ] && die "PGO is useless without a command to run"
else
	if [ $# -eq 1 ]
	then
		case "$(tolower "$1")" in
			clean)
				cclean
				rm -f -- *.profraw default.profdata *.gcda
				;;
			install)
				[ ! -f "$BIN" ] && "$self"
				PREFIX=$(cd - >/dev/null; resolve_create_dir "$PREFIX")
				install -m 755 "$PREFIX"/bin/ "$BIN"
				install -m 644 "$PREFIX"/share/doc/mus/README_mus_player.txt README
				install -m 644 "$PREFIX"/share/doc/mus/ IPC.txt
				;;
			uninstall)
				PREFIX=$(cd - >/dev/null; resolve_existing_dir "$PREFIX")
				uninstall -- "$PREFIX/bin/$BIN" \
					"$PREFIX"/share/doc/mus/README_mus_player.txt \
					"$PREFIX"/share/doc/mus/IPC.txt
				;;
			help)
				usage 0
				;;
			*)
				die "$1: unknown action"
				;;
		esac
		exit
	elif [ $# -gt 1 ]
	then
		usage 1
	fi
fi

# Detect compiler vendor
case "$(tolower "$CC")" in
	*gcc*)
		cctype=gcc
		;;
	*clang*)
		cctype=clang
		;;
	*)
		cctype=c99
		;;
esac

# Apply PGO with workload specified by "$@"
if "$PGO"
then
	xtest_cflag -fprofile-generate
	[ $# -eq 0 ] && die "PGO is useless without a command to run"
	case "$cctype" in
		gcc)
			PGO=false CFLAGS="$CFLAGS -fprofile-generate" \
				LDFLAGS="$LDFLAGS -fprofile-generate" "$self"
			(
				cd - >/dev/null
				"$@"
			)
			cclean
			append_ldflag -fprofile-use
			append_cflag -fprofile-use
			;;
		clang)
			PGO=false CFLAGS="$CFLAGS -fprofile-generate=$(dquote "$PWD")" \
				LDFLAGS="$LDFLAGS -fprofile-generate=$(dquote "$PWD")" "$self"
			(
				cd - >/dev/null
				"$@"
			)
			cclean
			llvm-profdata merge -output=default.profdata default_*.profraw
			append_ldflag -fprofile-use="$PWD"
			append_cflag -fprofile-use="$PWD"
			;;
		*)
			die "Unknown PGO supporting compiler"
			;;
	esac
fi

# Process CONFIG
case "$(tolower "$CONFIG")" in
	debug)
		CFLAGS=-g
		test_append_cflag -Og || append_cflag -O0
		test_append_cflag -fsanitize=address &&
			append_ldflag -fsanitize=address
		test_append_cflag -fsanitize=undefined &&
			append_ldflag -fsanitize=undefined
		test_append_ldflag -fsanitize=leak
		;;
	profile)
		CFLAGS=-g
		test_append_cflag -Og || append_cflag -O0
		append_cppflag -DNDEBUG
		;;
	release)
		test_append_cflag -Ofast || append_cflag -O3
		append_cppflag -DNDEBUG
		append_ldflag -s
		test_append_ldflag -Wl,-O1
		;;
	*)
		die "$CONFIG: unknown config"
		;;
esac

# Diverse CFLAG/LDFLAG additions
"$NATIVE" && xtest_append_cflag -march=native
"$STATIC" && xtest_append_ldflag -static
xtest_append_cflag -std="$std"
test_append_cflag -pedantic -Wall -Wextra
test_append_ldflag -Wl,-z,now,-z,defs
[ "${LD:-}" ] && test_append_ldflag -fuse-ld="${LD#ld.}"

# Apply LTO
if "$LTO"
then
	xtest_cflag -flto
	case "$cctype" in
		gcc)
			append_ldflag -flto="$JOBS" "$CFLAGS"
			append_cflag -flto -fno-fat-lto-objects
			;;
		clang)
			append_cflag -flto=thin
			append_ldflag "$CFLAGS"
			;;
		*)
			die "Unknown LTO supporting compiler"
			;;
	esac
fi


# Prepare the object list
SRC=$(printf '%s\n' *.c | grep -v mus_udsend.c)

gperf=$(listfiles . -name '*.gperf' | sed 's#^\./##; s#\.gperf$#.c#')
if [ "$gperf" ]
then
	! command -v gperf >/dev/null && die "gperf: command not found"
	SRC=$(printf '%s\n%s\n' "$SRC" "$gperf")
fi

lex=$(listfiles . -name '*.lex' | sed 's#^\./##; s#\.lex$#.c#')
if [ "$lex" ]
then
	SRC=$(printf '%s\n%s\n' "$SRC" "$lex")
fi

# Remove duplicates inserted by gperf/lex detection
SRC=$(echo "$SRC" | awk '!seen[$0]++')

# Conditional features processing
SRC=$(echo "$SRC" | grep -Evx '(flac|vorbis|opus)\.c')

if "$USE_FLAC"
then
	append_cppflag -DUSE_FLAC
	LDLIBS="$LDLIBS -lFLAC"
	SRC=$(printf '%s\n%s\n' "$SRC" flac.c)
fi
if "$USE_OGG_VORBIS"
then
	append_cppflag -DUSE_OGG_VORBIS
	LDLIBS="$LDLIBS -lvorbisfile"
	SRC=$(printf '%s\n%s\n' "$SRC" vorbis.c)
fi
if "$USE_OGG_OPUS"
then
	append_cppflag -DUSE_OGG_OPUS -I/usr/include/opus
	LDLIBS="$LDLIBS -lopusfile"
	SRC=$(printf '%s\n%s\n' "$SRC" opus.c)
fi

OBJ=$(echo "$SRC" | sed 's#\.c$#.o#' | paste -sd' ')

cat <<'EOF' | "$MAKE" $([ $JOBS -ne 1 ] && echo -j$JOBS) -f - \
	CC="$CC" BIN="$BIN" OBJ="$OBJ" CFLAGS="$CFLAGS" \
	CPPFLAGS="$CPPFLAGS" LDFLAGS="$LDFLAGS" LDLIBS="$LDLIBS"
.POSIX:
.SUFFIXES:
.SUFFIXES: .c .o .gperf .lex

.gperf.c:
	gperf -- $< >$@

.lex.c:
	$(LEX) $(LFLAGS) -t -- $< >$@

.c.o:
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<

$(BIN): $(OBJ)
	$(CC) $(LDFLAGS) $(OBJ) -o $@ $(LDLIBS)

EOF