From 5ac8415ac229fd319764c912f49e6a761296bcbf Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Mon, 30 Dec 2024 16:08:47 +0100 Subject: [PATCH 1/2] SONARGO-57 Setup static linking with musl for the Go binary --- sonar-go-plugin/build.gradle.kts | 2 +- sonar-go-to-slang/Makefile | 23 ----------------------- sonar-go-to-slang/build.gradle.kts | 2 ++ sonar-go-to-slang/make.sh | 16 ++++++++++++---- 4 files changed, 15 insertions(+), 28 deletions(-) delete mode 100644 sonar-go-to-slang/Makefile diff --git a/sonar-go-plugin/build.gradle.kts b/sonar-go-plugin/build.gradle.kts index d870c8f6..96bf4306 100644 --- a/sonar-go-plugin/build.gradle.kts +++ b/sonar-go-plugin/build.gradle.kts @@ -103,7 +103,7 @@ tasks.shadowJar { exclude("spotless/**") } doLast { - enforceJarSizeAndCheckContent(tasks.shadowJar.get().archiveFile.get().asFile, 9_000_000L, 9_500_000L) + enforceJarSizeAndCheckContent(tasks.shadowJar.get().archiveFile.get().asFile, 5_000_000L, 5_500_000L) } } diff --git a/sonar-go-to-slang/Makefile b/sonar-go-to-slang/Makefile deleted file mode 100644 index 5cf7a8c9..00000000 --- a/sonar-go-to-slang/Makefile +++ /dev/null @@ -1,23 +0,0 @@ -GO_VERSION=1.21.1 -GO_BINARY=go$(GO_VERSION) - -all: goparser_generated.go - -install_go: - go install golang.org/dl/go"${GO_VERSION}@latest" - $(GO_BINARY) download - - -goparser_generated.go: generate_source.go install_go - $(GO_BINARY) run generate_source.go - GOOS=darwin GOARCH=amd64 $(GO_BINARY) build -o build/sonar-go-to-slang-darwin-amd64 - GOOS=linux GOARCH=amd64 $(GO_BINARY) build -o build/sonar-go-to-slang-linux-amd64 - GOOS=windows GOARCH=amd64 $(GO_BINARY) build -o build/sonar-go-to-slang-windows-amd64.exe - -test-report.out: install_go - $(GO_BINARY) test -json > test-report.out - - -clean: - rm -f goparser_generated.go - rm -f build/sonar-go-to-slang-* diff --git a/sonar-go-to-slang/build.gradle.kts b/sonar-go-to-slang/build.gradle.kts index ea11015e..d0a39dd0 100644 --- a/sonar-go-to-slang/build.gradle.kts +++ b/sonar-go-to-slang/build.gradle.kts @@ -52,7 +52,9 @@ val compileGo by tasks.registering(Exec::class) { exclude("build/**", "*_generated.go") } ) + outputs.file("goparser_generated.go") outputs.dir("build/executable") + outputs.cacheIf { true } callMake("build") } diff --git a/sonar-go-to-slang/make.sh b/sonar-go-to-slang/make.sh index 2d11adc5..30573691 100755 --- a/sonar-go-to-slang/make.sh +++ b/sonar-go-to-slang/make.sh @@ -84,11 +84,19 @@ compile_binaries() { path_to_binary=$(install_go "${GO_VERSION}") # Build bash -c "${path_to_binary} run generate_source.go" + mkdir -p build/executable - bash -c "GOOS=darwin GOARCH=amd64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-darwin-amd64" - bash -c "GOOS=darwin GOARCH=arm64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-darwin-arm64" - bash -c "GOOS=linux GOARCH=amd64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-linux-amd64" - bash -c "GOOS=windows GOARCH=amd64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-windows-amd64.exe" + # Note: -ldflags="-s -w" is used to strip debug information from the binary and reduce its size. + GO_FLAGS=(-ldflags='-s -w' -buildmode=exe) + GOOS=darwin GOARCH=amd64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-darwin-amd64 "${GO_FLAGS[@]}" + GOOS=darwin GOARCH=arm64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-darwin-arm64 "${GO_FLAGS[@]}" + if command -v musl-gcc &> /dev/null; then + # This should be run on CI; for a dev environment, this is not essential + GOOS=linux GOARCH=amd64 CC=musl-gcc ${path_to_binary} build -o build/executable/sonar-go-to-slang-linux-amd64 "${GO_FLAGS[@]}" -ldflags '-linkmode external -extldflags "-s -w -static"' + else + GOOS=linux GOARCH=amd64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-linux-amd64 "${GO_FLAGS[@]}" + fi + GOOS=windows GOARCH=amd64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-windows-amd64.exe "${GO_FLAGS[@]}" } generate_test_report() { From 1dc4a38178932590c5c7fbd14610567cf65f622b Mon Sep 17 00:00:00 2001 From: Peter Trifanov Date: Mon, 6 Jan 2025 16:25:13 +0100 Subject: [PATCH 2/2] Remove external linking --- sonar-go-to-slang/make.sh | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/sonar-go-to-slang/make.sh b/sonar-go-to-slang/make.sh index 30573691..975feef6 100755 --- a/sonar-go-to-slang/make.sh +++ b/sonar-go-to-slang/make.sh @@ -88,15 +88,10 @@ compile_binaries() { mkdir -p build/executable # Note: -ldflags="-s -w" is used to strip debug information from the binary and reduce its size. GO_FLAGS=(-ldflags='-s -w' -buildmode=exe) - GOOS=darwin GOARCH=amd64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-darwin-amd64 "${GO_FLAGS[@]}" - GOOS=darwin GOARCH=arm64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-darwin-arm64 "${GO_FLAGS[@]}" - if command -v musl-gcc &> /dev/null; then - # This should be run on CI; for a dev environment, this is not essential - GOOS=linux GOARCH=amd64 CC=musl-gcc ${path_to_binary} build -o build/executable/sonar-go-to-slang-linux-amd64 "${GO_FLAGS[@]}" -ldflags '-linkmode external -extldflags "-s -w -static"' - else - GOOS=linux GOARCH=amd64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-linux-amd64 "${GO_FLAGS[@]}" - fi - GOOS=windows GOARCH=amd64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-windows-amd64.exe "${GO_FLAGS[@]}" + CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-darwin-amd64 "${GO_FLAGS[@]}" + CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-darwin-arm64 "${GO_FLAGS[@]}" + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-linux-amd64 "${GO_FLAGS[@]}" + CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ${path_to_binary} build -o build/executable/sonar-go-to-slang-windows-amd64.exe "${GO_FLAGS[@]}" } generate_test_report() {