-
Notifications
You must be signed in to change notification settings - Fork 8
/
build.sh
executable file
·122 lines (103 loc) · 2.59 KB
/
build.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
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
#!/bin/bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
BUILD="build"
NAME="xk6-disruptor"
ARCH=$(go env GOARCH)
OS=$(go env GOOS)
function usage() {
cat << EOF
builds a binary for a target architecture and operating system from a given module version.
$0 [OPTIONS]
options:
-a, --arch: target architecture (valid option amd64, arm64. Defaults to GOARCH)
-b, --build: directory for building binaries (defaults to 'build. Created if it does not exist)
-k, --k6-version: version of k6 to use
-n, --name: package base name. Defaults to 'xk6-disruptor'
-o, --os: target operating systems (valid options linux, darwing, windows. Defaults to GOOS)
-r, --replace: module that replaces xk6-distruptor module
-v, --version: xk6-disruptor version in semver format
-y, --binary: name of the binary (default is name-os-arch)
EOF
}
# Prints an error message the usage help and exits with error
function error () {
echo $1
usage
exit 1
}
while [[ $# -gt 0 ]]; do
case $1 in
-a|--arch)
ARCH="$2"
if [[ ! $ARCH =~ amd64|arm64 ]]; then
error "supported architectures are 'amd64' and 'arm64'"
fi
shift
;;
-b|--build)
BUILD="$2"
shift
;;
-k|--k6-version)
K6_VERSION="$2"
shift
;;
-o|--os)
OS="$2"
if [[ ! $OS =~ linux|darwin|windows ]]; then
error "supported operating systems are 'linux', 'darwin' and 'windows'"
fi
shift
;;
-r|--replace)
REPLACE_MOD="$2"
shift
;;
-v|--version)
VERSION="$2"
shift
;;
-y|--binary)
BINARY="$2"
shift
;;
*)
error "Unknown option $1"
;;
esac
shift
done
if [[ ! -e $BUILD ]]; then
mkdir -p $BUILD
fi
## make all paths absolute
BUILD=$(realpath $BUILD)
if [[ -z $OS ]]; then
error "target operating system is required"
fi
if [[ -z $ARCH ]]; then
error "target architecture is required"
fi
if [[ -n $REPLACE_MOD && -z $VERSION ]]; then
error "replace module must be versioned. Version option missing"
fi
if [[ -z $BINARY ]]; then
BINARY="$NAME-$OS-$ARCH"
fi
# set disruptor version to use for build
MOD=$(go list -m)
REPLACE="."
if [[ -n $VERSION ]]; then
REPLACE_MOD=${REPLACE_MOD:-$MOD}
REPLACE=${REPLACE_MOD}@${VERSION}
fi
#start sub shell to create its own environment
(
if [[ $OS == "linux" ]]; then # disable cross-compiling for linux
export CGO_ENABLED=0
fi
export GOARCH=$ARCH
export GOOS=$OS
export XK6_BUILD_FLAGS='-ldflags "-w -s'
xk6 build $K6_VERSION --with $MOD=${REPLACE} --output $BUILD/$BINARY
)