-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile_resource_to_raw_string.sh
executable file
·71 lines (55 loc) · 1.62 KB
/
compile_resource_to_raw_string.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
#!/bin/bash
# the following are file types we strip C-style comments for:
strip_comment_suffixes=(
"wgsl"
"glsl"
)
infile="$1"
if [ -z "$infile" ]; then
echo "Usage: $0 <filename> [namespace]" 2>&1
exit 1
fi
if [ ! -f "$infile" ]; then
echo "Resource compiler: File $infile not found." 2>&1
exit 1
fi
outfile="$infile.h"
# don't update if outfile is newer than infile
if [ ! "$infile" -nt "$outfile" ]; then
echo "Resource compiler: $outfile up to date"
exit
fi
suffix=${infile##*.} # get the suffix
suffix=${suffix,,} # make it lowercase
# generate a fairly unique hash to act as a rawstring prefix/suffix
shorthash=$(md5sum "$infile" | cut -c 1-16)
if [ -z "$2" ]; then
namespace=$(dirname "$infile" | sed 's/\//::/g')
else
namespace="$2"
fi
resourcename=$(basename "$infile" | sed 's/\./_/g')
# truncate the destination file
> "$outfile"
# comments
echo "#pragma once"$'\n' >> "$outfile"
echo "// This file is automatically generated from $infile by $0"$'\n' >> "$outfile"
# optional namespace
if [ ! -z "$namespace" ]; then
echo "namespace $namespace {"$'\n' >> "$outfile"
fi
# header
echo -n "inline constexpr char const *${resourcename}{R\"${shorthash}(" >> "$outfile"
# content
if printf '%s\0' "${strip_comment_suffixes[@]}" | grep -Fxzq -- "$suffix"; then
# strip comments and whitespace-only lines
sed "s/ *\/\/.*//;/^[ ]*$/d" "$infile" >> "$outfile"
else
cat "$infile" >> "$outfile"
fi
# footer
echo ")${shorthash}\"};" >> "$outfile"
if [ ! -z "$namespace" ]; then
echo $'\n'"} // namespace $namespace" >> "$outfile"
fi
echo "Resource compiler: $infile compiled to $outfile: $namespace::$resourcename"