@@ -61,6 +61,41 @@ GetBuildTagsForTarget() {
6161 esac
6262}
6363
64+ # Keep musl static link flags centralized for all musl build paths.
65+ GetMuslStaticLdflags () {
66+ echo " -linkmode external -extldflags '-static -fpic' $ldflags "
67+ }
68+
69+ # Fail fast if a musl build artifact is not fully static.
70+ AssertStaticBinary () {
71+ local binary=" $1 "
72+ if [ ! -f " $binary " ]; then
73+ echo " Error: binary not found: $binary "
74+ return 1
75+ fi
76+
77+ if command -v readelf > /dev/null 2>&1 ; then
78+ if readelf -l " $binary " 2> /dev/null | grep -q " Requesting program interpreter" ; then
79+ echo " Error: binary is not fully static: $binary "
80+ readelf -l " $binary " | grep " Requesting program interpreter" || true
81+ return 1
82+ fi
83+ return 0
84+ fi
85+
86+ if command -v file > /dev/null 2>&1 ; then
87+ if file " $binary " | grep -qi " dynamically linked" ; then
88+ echo " Error: binary is dynamically linked: $binary "
89+ file " $binary "
90+ return 1
91+ fi
92+ return 0
93+ fi
94+
95+ echo " Warning: readelf/file not found, skip static verification for $binary "
96+ return 0
97+ }
98+
6499FetchWebRolling () {
65100 pre_release_json=$( eval " curl -fsSL --max-time 2 $githubAuthArgs -H \" Accept: application/vnd.github.v3+json\" \" https://api.github.com/repos/$frontendRepo /releases/tags/rolling\" " )
66101 pre_release_assets=$( echo " $pre_release_json " | jq -r ' .assets[].browser_download_url' )
@@ -145,7 +180,7 @@ BuildWin7() {
145180BuildDev () {
146181 rm -rf .git/
147182 mkdir -p " dist"
148- muslflags=" --extldflags '-static -fpic' $ldflags "
183+ muslflags=" $( GetMuslStaticLdflags ) "
149184 BASE=" https://github.com/OpenListTeam/musl-compilers/releases/latest/download/"
150185 FILES=(x86_64-linux-musl-cross aarch64-linux-musl-cross)
151186 for i in " ${FILES[@]} " ; do
@@ -163,7 +198,8 @@ BuildDev() {
163198 export GOARCH=${os_arch##* -}
164199 export CC=${cgo_cc}
165200 export CGO_ENABLED=1
166- go build -o ./dist/$appName -$os_arch -ldflags=" $muslflags " -tags=jsoniter .
201+ CGO_LDFLAGS=" -static" go build -o ./dist/$appName -$os_arch -ldflags=" $muslflags " -tags=jsoniter .
202+ AssertStaticBinary " ./dist/$appName -$os_arch "
167203 done
168204 xgo -targets=windows/amd64,darwin/amd64,darwin/arm64 -out " $appName " -ldflags=" $ldflags " -tags=jsoniter .
169205 mv " $appName " -* dist
@@ -197,7 +233,7 @@ BuildDockerMultiplatform() {
197233 # run PrepareBuildDockerMusl before build
198234 export PATH=$PATH :$PWD /build/musl-libs/bin
199235
200- docker_lflags=" --extldflags '-static -fpic' $ldflags "
236+ docker_lflags=" $( GetMuslStaticLdflags ) "
201237 export CGO_ENABLED=1
202238
203239 OS_ARCHES=(linux-amd64 linux-arm64 linux-386 linux-riscv64 linux-ppc64le linux-loong64) # # Disable linux-s390x builds
@@ -212,7 +248,8 @@ BuildDockerMultiplatform() {
212248 export GOARCH=$arch
213249 export CC=${cgo_cc}
214250 echo " building for $os_arch "
215- go build -o build/$os /$arch /" $appName " -ldflags=" $docker_lflags " -tags=" $build_tags " .
251+ CGO_LDFLAGS=" -static" go build -o build/$os /$arch /" $appName " -ldflags=" $docker_lflags " -tags=" $build_tags " .
252+ AssertStaticBinary " build/$os /$arch /$appName "
216253 done
217254
218255 DOCKER_ARM_ARCHES=(linux-arm/v6 linux-arm/v7)
@@ -226,7 +263,8 @@ BuildDockerMultiplatform() {
226263 export GOARM=${GO_ARM[$i]}
227264 export CC=${cgo_cc}
228265 echo " building for $docker_arch "
229- go build -o build/${docker_arch%% -* } /${docker_arch##* -} /" $appName " -ldflags=" $docker_lflags " -tags=jsoniter .
266+ CGO_LDFLAGS=" -static" go build -o build/${docker_arch%% -* } /${docker_arch##* -} /" $appName " -ldflags=" $docker_lflags " -tags=jsoniter .
267+ AssertStaticBinary " build/${docker_arch%% -* } /${docker_arch##* -} /$appName "
230268 done
231269}
232270
@@ -406,7 +444,7 @@ BuildLoongGLIBC() {
406444BuildReleaseLinuxMusl () {
407445 rm -rf .git/
408446 mkdir -p " build"
409- muslflags=" --extldflags '-static -fpic' $ldflags "
447+ muslflags=" $( GetMuslStaticLdflags ) "
410448 BASE=" https://github.com/OpenListTeam/musl-compilers/releases/latest/download/"
411449 # Keep mips-family targets enabled; sqlite driver selection is handled by Go build tags.
412450 FILES=(x86_64-linux-musl-cross aarch64-linux-musl-cross mips-linux-musl-cross mips64-linux-musl-cross mips64el-linux-musl-cross mipsel-linux-musl-cross powerpc64le-linux-musl-cross s390x-linux-musl-cross loongarch64-linux-musl-cross)
@@ -427,14 +465,15 @@ BuildReleaseLinuxMusl() {
427465 export GOARCH=${os_arch##* -}
428466 export CC=${cgo_cc}
429467 export CGO_ENABLED=1
430- go build -o ./build/$appName -$os_arch -ldflags=" $muslflags " -tags=" $build_tags " .
468+ CGO_LDFLAGS=" -static" go build -o ./build/$appName -$os_arch -ldflags=" $muslflags " -tags=" $build_tags " .
469+ AssertStaticBinary " ./build/$appName -$os_arch "
431470 done
432471}
433472
434473BuildReleaseLinuxMuslArm () {
435474 rm -rf .git/
436475 mkdir -p " build"
437- muslflags=" --extldflags '-static -fpic' $ldflags "
476+ muslflags=" $( GetMuslStaticLdflags ) "
438477 BASE=" https://github.com/OpenListTeam/musl-compilers/releases/latest/download/"
439478 FILES=(arm-linux-musleabi-cross arm-linux-musleabihf-cross armel-linux-musleabi-cross armel-linux-musleabihf-cross armv5l-linux-musleabi-cross armv5l-linux-musleabihf-cross armv6-linux-musleabi-cross armv6-linux-musleabihf-cross armv7l-linux-musleabihf-cross armv7m-linux-musleabi-cross armv7r-linux-musleabihf-cross)
440479 for i in " ${FILES[@]} " ; do
@@ -456,7 +495,8 @@ BuildReleaseLinuxMuslArm() {
456495 export CC=${cgo_cc}
457496 export CGO_ENABLED=1
458497 export GOARM=${arm}
459- go build -o ./build/$appName -$os_arch -ldflags=" $muslflags " -tags=jsoniter .
498+ CGO_LDFLAGS=" -static" go build -o ./build/$appName -$os_arch -ldflags=" $muslflags " -tags=jsoniter .
499+ AssertStaticBinary " ./build/$appName -$os_arch "
460500 done
461501}
462502
0 commit comments