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+ sudo rm -rf /usr/local/go
49+ sudo mv go /usr/local/go
50+ export PATH=" /usr/local/go/bin:$PATH "
51+ else
52+ echo " No write permission to /usr/local. Installing Go to $HOME /.local/go"
53+ mkdir -p " $HOME /.local"
54+ rm -rf " $HOME /.local/go"
55+ mv go " $HOME /.local/go"
56+ export PATH=" $HOME /.local/go/bin:$PATH "
57+ fi
58+ cd - > /dev/null
59+ rm -rf " $TEMP_DIR "
60+ echo " Go installed successfully."
61+ go version
62+ else
63+ echo " Go is already installed."
64+ fi
65+ }
66+
67+ verify_go_cgo () {
68+ echo " Verifying Go CGO support..."
69+ if ! go env CGO_ENABLED | grep -q " 1" ; then
70+ echo " Warning: CGO is not enabled. Attempting to enable CGO..."
71+ export CGO_ENABLED=1
72+ fi
73+
74+ TEMP_GO_FILE=$( mktemp --suffix=.go)
75+ cat > " $TEMP_GO_FILE " << 'EOF '
76+ package main
77+ import "C"
78+ //export TestFunc
79+ func TestFunc() {}
80+ func main() {}
81+ EOF
82+ TEMP_SO_FILE=$( mktemp --suffix=.so)
83+ if go build -buildmode=c-shared -o " $TEMP_SO_FILE " " $TEMP_GO_FILE " 2> /dev/null; then
84+ echo " CGO is enabled and working."
85+ rm -f " $TEMP_GO_FILE " " $TEMP_SO_FILE "
86+ else
87+ echo " Error: CGO is not enabled or not working properly. Please ensure you have a C compiler installed."
88+ rm -f " $TEMP_GO_FILE " " $TEMP_SO_FILE "
89+ exit 1
90+ fi
91+ }
92+
93+ build_go_plugins () {
94+ echo " Building Go test plugins..."
95+
96+ echo " Building logs output plugin..."
97+ cd " $GO_PLUGIN_DIR "
98+ CGO_ENABLED=1 GO111MODULE=on go build -buildmode=c-shared -v -ldflags=" -s -w" -o $BUILD_DIR /test_logs_go.so logs_output.go
99+ if [ $? -eq 0 ]; then
100+ echo " Go test plugins built successfully!"
101+ echo " Logs plugin: $BUILD_DIR /test_logs_go.so"
102+ else
103+ echo " Failed to build Go test plugins."
104+ exit 1
105+ fi
106+ }
107+
108+ echo " Setting up Go build environment..."
109+ install_go_if_needed
110+ verify_go_cgo
111+ build_go_plugins
0 commit comments