Skip to content

Commit 4132adc

Browse files
authored
More robust parsing and arg validation for build.sh (#1342)
1 parent c380bc8 commit 4132adc

File tree

1 file changed

+72
-57
lines changed

1 file changed

+72
-57
lines changed

build.sh

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
usage()
44
{
5-
echo "Usage: build.sh [clean] [arm64|x86_64|universal] [CUSTOM_CMAKE_CXX_FLAGS=x] [noroot] [release] [-h|-?] [-l (static|shared)] [-D CMAKE_OPTION] [-v]"
5+
echo "Usage: build.sh [clean] [arm64|x86_64|universal] [CUSTOM_BUILD_FLAGS=x] [noroot] [release|debug] [-h|-?] [-l (static|shared)] [-D CMAKE_OPTION] [-v]"
66
echo " "
77
echo "options: "
88
echo " "
9-
echo "Positional options (1st three arguments): "
9+
echo "Positional options: "
1010
echo "[clean] - perform clean build "
1111
echo "[arm64|x86_64|universal] - Apple platform build type. Not applicable to other OS. "
12-
echo "[CUSTOM_CMAKE_CXX_FLAGS] - custom CXX compiler flags "
12+
echo "[CUSTOM_BUILD_FLAGS] - custom CXX compiler flags "
1313
echo "[noroot] - custom CXX compiler flags "
14-
echo "[release] - build for Release "
14+
echo "[release|debug] - Specify build type (defaults to Debug) "
1515
echo " "
1616
echo "Additional parameters: "
1717
echo " -h | -? - this help. "
@@ -35,68 +35,68 @@ cd $DIR
3535

3636
export NOROOT=$NOROOT
3737

38-
PARAM1="$1"
39-
PARAM2="$2"
40-
PARAM3="$3"
41-
42-
if [ "$PARAM1" == "clean" ]; then
43-
echo "Cleaning previous build artifacts"
44-
rm -f CMakeCache.txt *.cmake
45-
rm -rf out
46-
rm -rf .buildtools
47-
# make clean
48-
shift
49-
fi
50-
51-
if [ "$PARAM1" == "noroot" ] || [ "$PARAM2" == "noroot" ] || [ "$PARAM3" == "noroot" ]; then
52-
export NOROOT=true
53-
echo "NOROOT = true"
54-
shift
55-
fi
38+
# Evaluate arguments that are not switches
39+
while [[ $# -gt 0 ]]; do
40+
ARG="$1"
41+
case "$ARG" in
42+
clean|noroot|release|debug|arm64|x86_64|universal|CUSTOM_BUILD_FLAGS*)
43+
case "$ARG" in
44+
clean)
45+
CLEAN=true
46+
echo "CLEAN = true"
47+
;;
48+
noroot)
49+
export NOROOT=true
50+
echo "NOROOT = true"
51+
;;
52+
release|debug)
53+
if [[ -n "$BUILD_TYPE" ]]; then
54+
echo "Error: BUILD_TYPE is already set to '$BUILD_TYPE'. Cannot overwrite with $ARG." 1>&2
55+
exit 1
56+
elif [[ "$ARG" == "release" ]]; then
57+
BUILD_TYPE="Release"
58+
elif [[ "$ARG" == "debug" ]]; then
59+
BUILD_TYPE="Debug"
60+
fi
61+
echo "BUILD_TYPE = $BUILD_TYPE"
62+
;;
63+
arm64|x86_64|universal)
64+
if [[ -n "$MAC_ARCH" ]]; then
65+
echo "Error: MAC_ARCH is already set to '$MAC_ARCH'. Cannot overwrite with $ARG." 1>&2
66+
exit 1
67+
else
68+
MAC_ARCH="$ARG"
69+
fi
70+
echo "MAC_ARCH = $MAC_ARCH"
71+
;;
72+
CUSTOM_BUILD_FLAGS*)
73+
CUSTOM_CMAKE_CXX_FLAG="\"${ARG:19:999}\""
74+
echo "custom compiler flags = $CUSTOM_CMAKE_CXX_FLAG"
75+
;;
76+
*)
77+
echo "Error: case not added: $ARG" 1>&2
78+
exit 1
79+
;;
80+
esac
81+
shift
82+
;;
83+
*)
84+
break
85+
;;
86+
esac
87+
done
5688

57-
if [ "$PARAM1" == "release" ] || [ "$PARAM2" == "release" ] || [ "$PARAM3" == "release" ]; then
58-
BUILD_TYPE="Release"
59-
echo "BUILD_TYPE = Release"
60-
shift
61-
elif [ "$PARAM1" == "debug" ] || [ "$PARAM2" == "debug" ] || [ "$PARAM3" == "debug" ]; then
62-
BUILD_TYPE="Debug"
63-
echo "BUILD_TYPE = Debug"
64-
shift
65-
else
89+
if [[ -z "$BUILD_TYPE" ]]; then
6690
BUILD_TYPE="Debug"
6791
echo "Assuming default BUILD_TYPE = Debug"
6892
fi
6993

70-
if [ "$PARAM1" == "arm64" ] || [ "$PARAM2" == "arm64" ] || [ "$PARAM3" == "arm64" ]; then
71-
MAC_ARCH="arm64"
72-
echo "MAC_ARCH = arm64"
73-
shift
74-
elif [ "$PARAM1" == "universal" ] || [ "$PARAM2" == "universal" ] || [ "$PARAM3" == "universal" ]; then
75-
MAC_ARCH="universal"
76-
echo "MAC_ARCH = universal"
77-
shift
78-
elif [ "$PARAM1" == "x86_64" ] || [ "$PARAM2" == "x86_64" ] || [ "$PARAM3" == "x86_64" ]; then
79-
MAC_ARCH="x86_64"
80-
echo "MAC_ARCH = x86_64"
81-
shift
82-
else
94+
if [[ -z "$MAC_ARCH" ]]; then
8395
MAC_ARCH=$(/usr/bin/uname -m)
8496
echo "Using current machine MAC_ARCH = $MAC_ARCH"
8597
fi
8698

87-
CUSTOM_CMAKE_CXX_FLAG=""
88-
if [[ $PARAM1 == CUSTOM_BUILD_FLAGS* ]] || [[ $PARAM2 == CUSTOM_BUILD_FLAGS* ]] || [[ $PARAM3 == CUSTOM_BUILD_FLAGS* ]]; then
89-
if [[ $PARAM1 == CUSTOM_BUILD_FLAGS* ]]; then
90-
CUSTOM_CMAKE_CXX_FLAG="\"${1:19:999}\""
91-
elif [[ $PARAM2 == CUSTOM_BUILD_FLAGS* ]]; then
92-
CUSTOM_CMAKE_CXX_FLAG="\"${2:19:999}\""
93-
elif [[ $PARAM3 == CUSTOM_BUILD_FLAGS* ]]; then
94-
CUSTOM_CMAKE_CXX_FLAG="\"${3:19:999}\""
95-
fi
96-
shift
97-
echo "custom build flags = $CUSTOM_CMAKE_CXX_FLAG"
98-
fi
99-
99+
# Evaluate switches
100100
LINK_TYPE=
101101
CMAKE_OPTS="${CMAKE_OPTS:--DBUILD_SHARED_LIBS=OFF}"
102102
while getopts "h?vl:D:" opt; do
@@ -114,7 +114,21 @@ while getopts "h?vl:D:" opt; do
114114
;;
115115
esac
116116
done
117+
118+
# Detect args accidentally passed after the last switch argument
117119
shift $((OPTIND -1))
120+
if [[ $# -gt 0 ]]; then
121+
echo "Error: There are arguments remaining after parsing all positional arguments and switches: $@" 1>&2
122+
exit 1
123+
fi
124+
125+
if [[ "$CLEAN" == "true" ]]; then
126+
echo "Cleaning previous build artifacts"
127+
rm -f CMakeCache.txt *.cmake
128+
rm -rf out
129+
rm -rf .buildtools
130+
# make clean
131+
fi
118132

119133
echo "CMAKE_OPTS from caller: $CMAKE_OPTS"
120134

@@ -175,6 +189,7 @@ set -e
175189
cmake_cmd="cmake -DMAC_ARCH=$MAC_ARCH -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_PACKAGE_TYPE=$CMAKE_PACKAGE_TYPE -DCMAKE_CXX_FLAGS="${CUSTOM_CMAKE_CXX_FLAG}" $CMAKE_OPTS .."
176190
echo $cmake_cmd
177191
eval $cmake_cmd
192+
178193
# TODO: strip symbols to minimize (release-only)
179194

180195
# Build all

0 commit comments

Comments
 (0)