forked from Mu2e/Offline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildopts
executable file
·172 lines (150 loc) · 4.61 KB
/
buildopts
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
#!/bin/bash
#
# A script to query and manipulate Mu2e Offline build options.
# Andrei Gaponenko, 2015
if [[ $(basename $0) != buildopts ]]; then
echo "ERROR: The buildopts script should be executed, not sourced" >&2
return 1
fi
#================================================================
# known keywords
kkw=(build g4vis g4mt trigger)
# a list of allowed values, the first one is the default
opts_build() {
echo prof debug
}
# a list of allowed values, the first one is the default
opts_g4vis() {
echo none ogl qt
}
# a list of allowed values, the first one is the default
opts_g4mt() {
echo on off
}
# a list of allowed values, the first one is the default
opts_trigger(){
echo off on
}
#================================================================
# the --help option overrides everything else on the command line
help=0
for arg in "${@}"; do
case "$arg" in
( -h | --help ) help=1 ;;
esac
done
if [[ $help == 1 ]]; then
cat <<EOF
Usage:
$0 prints the current config
$0 --<option> prints the current value for the option
$0 --<option>=<value> sets the new value
$0 --<option>=default resets the option
$0 --reset resets all options
$0 --help prints this message
The recognized options are:
EOF
for opt in "${kkw[@]}"; do
echo " --$opt, allowed values are: "$(opts_$opt)
echo ""
done
cat <<EOF
The first value on each list of option values is the default.
EOF
exit 0
fi
#================================================================
optdb=$(dirname $0)/.buildopts
## We want to allow modifications of build config only when buildopts
## is called from the base release directory and not from a satellite.
assert_writes_allowed() {
# Get the physical path to .buildopts
CONFDIR=`dirname $(readlink -f $optdb)`
if [[ $(/bin/pwd) != $CONFDIR ]]; then
(
echo "Error: modifying build options must be done from the top of the base Offline directory."
echo " Base release: $CONFDIR"
echo " You are in: $(/bin/pwd)"
) >& 2
exit 2
fi
}
## mu2e_getopt <keyword> <default_value>
mu2e_getopt() {
keyword="$1"
default="$2"
##echo "# verbose: querying $keyword, default is $default" >&2
val=$(awk -F= '/^'$keyword'/{print $2}' $optdb 2>/dev/null)
if [[ x$val == x ]]; then val="$default";fi
echo $val
}
# synopsis: mu2e_setopt <keyword> <new_value>
mu2e_setopt() {
keyword="$1"
value="$2"
##echo "# verbose: setting $keyword to $value" >&2
assert_writes_allowed
grep -v ^"$keyword" $optdb > $optdb.$$ 2>/dev/null
echo "$keyword=$value" >> $optdb.$$
/bin/mv -f $optdb.$$ $optdb
}
# synopsis: mu2e_resetopt <keyword>
mu2e_resetopt() {
keyword="$1"
##echo "# verbose: resetting $keyword" >&2
assert_writes_allowed
grep -v ^"$keyword" $optdb > $optdb.$$ 2>/dev/null
/bin/mv -f $optdb.$$ $optdb
}
noargs=1
while [[ x"$1" != x ]]; do
noargs=0
matched=0
for kw in "${kkw[@]}"; do
if [[ "$1" =~ --${kw}.* ]]; then
if [[ "$1" == --$kw ]]; then
matched=1
mu2e_getopt $kw $(opts_$kw)
else
if [[ "$1" =~ --$kw=(.*) ]]; then
matched=1
val="${BASH_REMATCH[1]}"
if [[ $val == default ]]; then
mu2e_resetopt $kw
else
allowed=0
for opt in $(opts_$kw); do
#echo "# verbose: kw = $kw testing allowed val = $opt" >&2
if [[ $opt == $val ]]; then
allowed=1
fi
done
if [[ $allowed = 0 ]]; then
echo "ERROR: the value of '$val' is not allowed for the --$kw option" >&2
exit 1
fi
mu2e_setopt $kw "$val"
fi
fi
fi
fi
done
if [[ $matched == 0 ]]; then
if [[ $1 == --reset ]]; then
assert_writes_allowed
/bin/rm -f $optdb
else
echo "ERROR: unrecognized option $1. Try $0 --help" >&2
exit 1
fi
fi
shift
done
#================================================================
if [[ $noargs == 1 ]]; then
#echo "# The current settings are:"
for i in "${kkw[@]}"; do
echo "$i=$(mu2e_getopt $i $(opts_$i) )"
#echo "$i ${!i[0]}"
done
fi