1+ #! /bin/bash
2+
3+ # Build script for Go test plugins
4+ set -e
5+
6+ GO_PLUGIN_DIR=" ${FLB_ROOT} /tests/runtime_shell/go_plugins"
7+ BUILD_DIR=" ${FLB_ROOT} /build"
8+
9+ install_go_if_needed () {
10+ if ! command -v go & > /dev/null; then
11+ echo " Go not found, installing Go..."
12+
13+ ARCH=$( uname -m)
14+ case $ARCH in
15+ x86_64) GO_ARCH=" amd64" ;;
16+ aarch64|arm64) GO_ARCH=" arm64" ;;
17+ * ) echo " Unsupported architecture: $ARCH " ; exit 1 ;;
18+ esac
19+
20+ OS=$( uname -s | tr ' [:upper:]' ' [:lower:]' )
21+ GO_VERSION=" 1.25.4"
22+ GO_TARBALL=" go${GO_VERSION} .${OS} -${GO_ARCH} .tar.gz"
23+ GO_URL=" https://golang.org/dl/${GO_TARBALL} "
24+
25+ echo " Downloading Go from $GO_URL ..."
26+
27+ TEMP_DIR=$( mktemp -d)
28+ cd " $TEMP_DIR "
29+
30+ if command -v curl > /dev/null 2>&1 ; then
31+ curl -L -O " $GO_URL "
32+ else
33+ echo " Neither wget nor curl is available to download Go."
34+ exit 1
35+ fi
36+
37+ echo " Extracting Go tarball..."
38+ ls -la
39+
40+ if [ ! -f " $GO_TARBALL " ]; then
41+ echo " Failed to download Go tarball."
42+ exit 1
43+ fi
44+
45+ tar -xzf " $GO_TARBALL "
46+
47+ if [ -w " /usr/local" ]; then
48+ if [ -d /usr/local/go ]; then
49+ sudo rm -rf /usr/local/go
50+ fi
51+ sudo mv go /usr/local/go
52+ export PATH=" /usr/local/go/bin:$PATH "
53+ else
54+ echo " No write permission to /usr/local. Installing Go to $HOME /.local/go"
55+ mkdir -p " $HOME /.local"
56+ rm -rf " $HOME /.local/go"
57+ mv go " $HOME /.local/go"
58+ export PATH=" $HOME /.local/go/bin:$PATH "
59+ fi
60+ cd - > /dev/null
61+ rm -rf " $TEMP_DIR "
62+ echo " Go installed successfully."
63+ go version
64+ else
65+ echo " Go is already installed."
66+ fi
67+ }
68+
69+ verify_go_cgo () {
70+ echo " Verifying Go CGO support..."
71+ if ! go env CGO_ENABLED | grep -q " 1" ; then
72+ echo " Warning: CGO is not enabled. Attempting to enable CGO..."
73+ export CGO_ENABLED=1
74+ fi
75+
76+ TEMP_GO_FILE=$( mktemp --suffix=.go)
77+ cat > " $TEMP_GO_FILE " << 'EOF '
78+ package main
79+ import "C"
80+ //export TestFunc
81+ func TestFunc() {}
82+ func main() {}
83+ EOF
84+ TEMP_SO_FILE=$( mktemp --suffix=.so)
85+ if go build -buildmode=c-shared -o " $TEMP_SO_FILE " " $TEMP_GO_FILE " 2> /dev/null; then
86+ echo " CGO is enabled and working."
87+ rm -f " $TEMP_GO_FILE " " $TEMP_SO_FILE "
88+ else
89+ echo " Error: CGO is not enabled or not working properly. Please ensure you have a C compiler installed."
90+ rm -f " $TEMP_GO_FILE " " $TEMP_SO_FILE "
91+ exit 1
92+ fi
93+ }
94+
95+ build_go_plugins () {
96+ echo " Building Go test plugins..."
97+
98+ echo " Building logs output plugin..."
99+ cd " $GO_PLUGIN_DIR "
100+ CGO_ENABLED=1 GO111MODULE=on go build -buildmode=c-shared -v -ldflags=" -s -w" -o $BUILD_DIR /test_logs_go.so logs_output.go
101+ if [ $? -eq 0 ]; then
102+ echo " Go test plugins built successfully!"
103+ echo " Logs plugin: $BUILD_DIR /test_logs_go.so"
104+ else
105+ echo " Failed to build Go test plugins."
106+ exit 1
107+ fi
108+ }
109+
110+ echo " Setting up Go build environment..."
111+ install_go_if_needed
112+ verify_go_cgo
113+ build_go_plugins
0 commit comments