~q3cpma/misc-tools

ref: 0695c6188d0f70c89d8e1757f2463b2e3869a60a misc-tools/build.sh -rwxr-xr-x 4.1 KiB
0695c618q3cpma htmldecode: move to root, use genhtab instead of lex 11 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
#!/bin/sh
set -eu
cd -- "$(dirname -- "$0")"
self=./$(basename -- "$0")
. ./build_util.sh


# Build all if no BIN is specified
if [ ! "${BIN:-}" ]
then
	for i in htmlencode urldecode urlencode wcswidth genhtab mbcut natsort htmldecode
	do
		BIN=$i "$self" "$@"
	done
	exit
fi

# Get configuration from the environment
CC=${CC:-c99}
STD=${STD:-c99}
CFLAGS=${CFLAGS:-}
CPPFLAGS=${CPPFLAGS:-}
append_cppflag -D_DEFAULT_SOURCE
LDFLAGS=${LDFLAGS:-}
LDLIBS=
JOBS=${JOBS:-1}
LTO=${LTO:-false}
PGO=${PGO:-false}
STATIC=${STATIC:-false}
NATIVE=${NATIVE:-false}
CONFIG=${CONFIG:-release}
DESTDIR=${DESTDIR+${DESTDIR%/}}
PREFIX=$(mkdir -p "$DESTDIR${PREFIX:-/usr/local}"; readlinkf "$DESTDIR${PREFIX:-/usr/local}")
PREFIX=${PREFIX%/}

# Target specific definitions
NAME=$(basename -- "$BIN")
gitver=$(git_version ..)
append_cppflag \
	-DPROG_NAME="$(dquote "$NAME" 2)" \
	-DPROG_VERSION="$(dquote "$gitver" 2)"

case "$BIN" in
	htmlencode|urldecode|urlencode|wcswidth|genhtab)
		SRC="$BIN.c misc.c"
		;;
	mbcut|natsort)
		SRC="$BIN.c misc.c utf8.c"
		;;
	htmldecode)
		SRC="$BIN.c htab.c misc.c utf8.c"
		;;
	*)
		die "$BIN: unknown program"
		;;
esac

# 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 UnicodeData.txt unicode_tolower.h entities.json htab.h htab.c
				;;
			install)
				pb_install -m 755 "$PREFIX"/bin/ "$BIN"
				;;
			uninstall)
				uninstall -- "$PREFIX/bin/$NAME"
				;;
			help)
				pb_usage 0
				;;
			*)
				die "$1: unknown action"
				;;
		esac
		exit
	elif [ $# -gt 1 ]
	then
		pb_usage 1
	fi
fi

# Detect compiler vendor
case "$(tolower "$CC")" in
	*gcc*)
		cctype=gcc
		;;
	*clang*)
		cctype=clang
		;;
	*)
		cctype=c99
		[ "$STD" != c99 ] && die "$STD: c99 only supports the c99 standard"
		;;
esac

# Apply PGO with workload specified by "$@"
if "$PGO"
then
	xtest_cflag -fprofile-generate
	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)
		test_append_cflag -g3 || append_cflag -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
		;;
	release)
		test_append_cflag -O3 || append_cflag -O1
		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
[ "$CC" != c99 ] && xtest_append_cflag -std="$STD"
test_append_cflag -pedantic -Wall -Wextra # -Wconversion
test_append_ldflag -Wl,-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

if match "$SRC" '(.* )?utf8.c( .*)?'
then
	! [ -s UnicodeData.txt ] &&
		pb_fetch https://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt
	! [ -s unicode_tolower.h ] &&
		sh gen_unicode_tolower_lut.sh UnicodeData.txt >unicode_tolower.h
fi

if [ "$BIN" = htmldecode ]
then
	! [ -s genhtab ] &&
		BIN=genhtab "$self"
	! [ -s entities.json ] &&
		pb_fetch https://www.w3.org/TR/html5/entities.json
	! [ -s htab.c ] &&
		./gen_html_entities_htab.sh <entities.json | \
		./genhtab -f namedref_to_codep_pair -i namedref.h -l -t codep_pair
fi

pb_make