influxdata / telegraf

Agent for collecting, processing, aggregating, and writing metrics, logs, and other arbitrary data.
https://influxdata.com/telegraf
MIT License
14.63k stars 5.58k forks source link

Cross Compiling cusotm telegraf for arm64 fails #13796

Closed araekan closed 1 year ago

araekan commented 1 year ago

I was trying to compile custom telegraf binary for ARM64 from AMD64 machine. I set the GOARCH=arm64 and ran the make build_tools and checked the file ./tools/custom_builder/custom_builder It is in ./tools/custom_builder/custom_builder: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, Go BuildID=jE8DfiMmbvow7Oma8hRi/C4vUytAQj5PEb6HQ_8k-/7QwcwPEhiYYQDVCYPQF8/LZ_rhVbFLOuoaCE298UQ, with debug_info, not stripped

So I cannot run the custom_builder from my host machine.

in the Makefile HOSTGO is set to GOARCH instead of GOHOSTARCH. Thats why this issue comes.

Please see to this and fix asap.

https://github.com/influxdata/telegraf/blame/168d9272752a81ea9df9f48998a25026debe4cb5/Makefile#L47

powersj commented 1 year ago

Please see to this and fix asap.

put up a PR :)

araekan commented 1 year ago

Sure

araekan commented 1 year ago

Sorry there was a mistake in my understanding.

env -u GOOS -u GOARCH -u GOARM go build -o ./tools/custom_builder/custom_builder ./tools/custom_builder

doesn't unset the GOARCH variable if it is set to arm64.

I am using go version go1.20.7 linux/amd64 and trying to compile telegraf for AARCH64 machine.

araekan commented 1 year ago

Any possible workaround might be helpful.

powersj commented 1 year ago

This is why I asked for a PR :) Pretty sure this works

So I cannot run the custom_builder from my host machine.

As you should have noticed running make build_tools even with GOARCH set should still produce a binary of your host arch:

❯ uname -a
Linux ryzen 6.4.10-arch1-1 #1 SMP PREEMPT_DYNAMIC Fri, 11 Aug 2023 11:03:36 +0000 x86_64 GNU/Linux
❯ GOARCH=arm64 make build_tools
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/custom_builder/custom_builder ./tools/custom_builder
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/license_checker/license_checker ./tools/license_checker
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/readme_config_includer/generator ./tools/readme_config_includer/generator.go
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/config_includer/generator ./tools/config_includer/generator.go
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/readme_linter/readme_linter ./tools/readme_linter
❯ file ./tools/custom_builder/custom_builder 
./tools/custom_builder/custom_builder: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, Go BuildID=Du4OPrOw8XwzMM2fjlAk/Icty7pstcuhvd6lGEc4i/GA-XFBHyD_ZWmOyOZr9F/QxaiQhSbS83KCgM5ZqBG, with debug_info, not stripped

ok so I built the custom builder, now let's cross build:

❯ GOARCH=arm64 ./tools/custom_builder/custom_builder --config config.toml 
2023/08/18 15:14:53 Importing configuration file(s)...
2023/08/18 15:14:53 Found 1 configuration files...
-------------------------------------------------------------------------------
Enabled plugins:
-------------------------------------------------------------------------------
aggregators (0):
-------------------------------------------------------------------------------
inputs (1):
  procstat                        plugins/inputs/procstat
-------------------------------------------------------------------------------
outputs (1):
  file                            plugins/outputs/file
-------------------------------------------------------------------------------
parsers (0):
-------------------------------------------------------------------------------
processors (0):
-------------------------------------------------------------------------------
secretstores (0):
-------------------------------------------------------------------------------
serializers (1):
  influx                          plugins/serializers/influx
-------------------------------------------------------------------------------
2023/08/18 15:14:53 Running build...
CGO_ENABLED=0 go build -tags "custom,inputs.procstat,outputs.file,serializers.influx" -ldflags " -X github.com/influxdata/telegraf/internal.Commit=168d9272 -X github.com/influxdata/telegraf/internal.Branch=master -X github.com/influxdata/telegraf/internal.Version=1.28.0-168d9272" ./cmd/telegraf

❯ file telegraf 
telegraf: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, Go BuildID=x53LJhio5_-0f4-R3IUX/iQ0Gr9yFmt0uAIb06WNT/9_UKNzj5xDiZY9fb3X9w/MZ1NevNu9cnYL_eS2XeU, with debug_info, not stripped

That looks right to me no?

araekan commented 1 year ago
jenkins@2aeb9d77b132:~/work_dir/telegraf$ uname -a                                                                                                                                                       
Linux 2aeb9d77b132 5.19.0-46-generic #47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Jun 21 15:35:31 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
jenkins@2aeb9d77b132:~/work_dir/telegraf$ go env GOARCH
arm64
jenkins@2aeb9d77b132:~/work_dir/telegraf$ make build_tools^C
jenkins@2aeb9d77b132:~/work_dir/telegraf$ git diff
jenkins@2aeb9d77b132:~/work_dir/telegraf$ make build_tools
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/custom_builder/custom_builder ./tools/custom_builder
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/license_checker/license_checker ./tools/license_checker
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/readme_config_includer/generator ./tools/readme_config_includer/generator.go
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/config_includer/generator ./tools/config_includer/generator.go
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/readme_linter/readme_linter ./tools/readme_linter
jenkins@2aeb9d77b132:~/work_dir/telegraf$ file ./tools/custom_builder/custom_builder
./tools/custom_builder/custom_builder: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, Go BuildID=8aDog-9NoVqYiwLYKlcZ/Cg0lvLpP_Ut9kPKbyfmM/7QwcwPEhiYYQDVCYPQF8/Fn_SGcxSqkcKLhskMsrn, with debug_info, not stripped
jenkins@2aeb9d77b132:~/work_dir/telegraf$ ./tools/custom_builder/custom_builder
bash: ./tools/custom_builder/custom_builder: cannot execute binary file: Exec format error
jenkins@2aeb9d77b132:~/work_dir/telegraf$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/home/jenkins/.cache/go-build"
GOENV="/home/jenkins/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/jenkins/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/jenkins/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.7"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="0"
GOMOD="/home/jenkins/work_dir/telegraf/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2727968936=/tmp/go-build -gno-record-gcc-switches"
araekan commented 1 year ago

I did the same thing but it is not working. Is there anything wrong in my setup?

araekan commented 1 year ago

Did the same thing again but in my go env I have GOARCH as arm64.

jenkins@2aeb9d77b132:~/work_dir/telegraf$ GOARCH=arm64 make build_tools
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/custom_builder/custom_builder ./tools/custom_builder
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/license_checker/license_checker ./tools/license_checker
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/readme_config_includer/generator ./tools/readme_config_includer/generator.go
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/config_includer/generator ./tools/config_includer/generator.go
env -u GOOS -u GOARCH -u GOARM -- go build -o ./tools/readme_linter/readme_linter ./tools/readme_linter
jenkins@2aeb9d77b132:~/work_dir/telegraf$ file ./tools/custom_builder/custom_builder
./tools/custom_builder/custom_builder: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, Go BuildID=8aDog-9NoVqYiwLYKlcZ/Cg0lvLpP_Ut9kPKbyfmM/7QwcwPEhiYYQDVCYPQF8/Fn_SGcxSqkcKLhskMsrn, with debug_info, not stripped
powersj commented 1 year ago

Is there anything wrong in my setup?

yes - look at the output of your go env you have goarch set to arm64 which is wrong. With that setting our build scripts will grab that value and build the custom builder as arm64. Unset that back to amd64, and use the GOARCH on the CLI to then cross build only once you have the custom builder set up correctly.

I've clearly shown in my previous comment that you can build for arm64 so I am going to close this as this is a local build issue.