-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-variants
More file actions
executable file
·162 lines (141 loc) · 3.74 KB
/
Copy pathcode-variants
File metadata and controls
executable file
·162 lines (141 loc) · 3.74 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
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
#!/bin/bash
INFO='
Generate code variants based on formatted comments, e.g.:
x = 0 ### 0 / 1 / 2
y = x + 1 ### x + 1 / x * x
Usage:
code-variants [ ... options ... ] <source file> [ <prefix> ]
Options:
--run Execute generated files in parallel
--copies <n> Create identical copies of each variant
--print Print generated file names
'
RUN=0
COPIES=
PRINT=
while [ "x${1::2}" = x-- ]; do
case "x$1" in
x--run) RUN=1 ;;
x--copies)
shift
COPIES="$(seq $1)" || exit 1
;;
x--print) PRINT=1 ;;
*)
echo "$INFO" 1>&2
exit 1
;;
esac
shift
done
INPUT_FILE="$1"
PREFIX="${2:-variants/}"
if [ "x$INPUT_FILE" = x ]; then
echo "$INFO" 1>&2
exit 1
fi
SOURCES=( "" )
LABELS=( "" )
trim() {
local v="${1#"${1%%[![:space:]]*}"}"
v="${v%"${v##*[![:space:]]}"}"
echo -n "$v"
}
add_branch() {
IFS='/' read -r -a options <<< "$2"
if [ "${#options[@]}" -lt 2 ]; then
echo "Error: invalid format: '$2'" 1>&2
exit 1
fi
REPLACE=
for option in "${options[@]}"; do
option=$(trim "$option")
if [ "x${1/$option}" != "x$1" ]; then
if [ ${#option} -gt ${#REPLACE} ]; then
REPLACE="$option"
fi
fi
done
if [ "x$REPLACE" = x ]; then
echo "Error: variants not found in line: '$2'" 1>&2
exit 1
fi
NEW_SOURCES=()
for source in "${SOURCES[@]}"; do
for option in "${options[@]}"; do
option=$(trim "$option")
NEW_SOURCES+=( "$source${1/$REPLACE/$option}"$'\n' )
done
done
SOURCES=( "${NEW_SOURCES[@]}" )
NEW_LABELS=()
for label in "${LABELS[@]}"; do
for option in "${options[@]}"; do
option="${option,,}"
NEW_LABELS+=( "${label}_$(echo ${option//[^a-z0-9_.-]/})" )
done
done
LABELS=( "${NEW_LABELS[@]/#_/}" )
}
while IFS='' read -r line; do
COMMENT="${line##*###}"
if [ "x$COMMENT" != "x$line" ]; then
if add_branch "${line%###*}" "$COMMENT"; then
continue
fi
fi
line="$line"$'\n'
SOURCES=( "${SOURCES[@]/%/$line}" )
done < "$INPUT_FILE"
if [ "x$COPIES" != x ]; then
NEW_SOURCES=()
for source in "${SOURCES[@]}"; do
for copy in $COPIES; do
NEW_SOURCES+=( "$source" )
done
done
SOURCES=( "${NEW_SOURCES[@]}" )
NEW_LABELS=()
for label in "${LABELS[@]}"; do
for copy in $COPIES; do
NEW_LABELS+=( "${label}_copy$copy" )
done
done
LABELS=( "${NEW_LABELS[@]/#_/}" )
fi
if [ "${#SOURCES[@]}" -lt 2 ]; then
echo "Error: no variants found in '$INPUT_FILE'" 1>&2
exit 1
fi
if [ "x$PRINT" != x ]; then
for label in "${LABELS[@]}"; do
echo "$label"
done
fi
if [ "x$RUN" != x1 ]; then
for id in "${!SOURCES[@]}"; do
OUTPUT_FILE="$PREFIX${LABELS[$id]}"
mkdir -p "$(dirname "$OUTPUT_FILE")" || exit 1
echo "${SOURCES[$id]}" > "$OUTPUT_FILE" || exit 1
chmod "$OUTPUT_FILE" --reference="$INPUT_FILE" || true
done
exit 0
fi
TMPDIR=$(mktemp -d) || exit 1
trap "rm -rf $TMPDIR" EXIT
for id in "${!SOURCES[@]}"; do
OUTPUT_FILE="$TMPDIR/${LABELS[$id]}.run"
echo "${SOURCES[$id]}" > "$OUTPUT_FILE" || exit 1
chmod +x "$OUTPUT_FILE"
done
echo '
all: $(patsubst %,'"$PREFIX"'%.out, '"${LABELS[@]}"')
'"$PREFIX"'%.out:
'$'\t''@echo Running variant: $*...
'$'\t''@'"$TMPDIR"'/$*.run > '"$TMPDIR"'/$*.out
'$'\t''@mkdir -p $(dir $@)
'$'\t''@cp '"$TMPDIR"'/$*.out $@
' > "$TMPDIR/makefile"
NUM_THREADS=$(grep -c ^processor /proc/cpuinfo) || exit 1
echo "Max threads: $NUM_THREADS" 1>&2
make -f "$TMPDIR/makefile" -j "$NUM_THREADS" 1>&2 || exit 1