Closed xplshn closed 6 months ago
I already use a variation of those flags, as described here in the docs (may not be uptodate): https://github.com/Azathothas/Toolpacks/blob/main/Docs/BUILD_NOTES.md
If a binary doesn't need cgo, then building using pie is actually not better, as it increases the binary size and breaks incompatibility with older systems. so the default in such case is
GOOS="linux" GOARCH="amd64" CGO_ENABLED="0" go build -v -ldflags="-buildid= -s -w -extldflags '-static'"
and if it requires cgo, but doesn't link to external libs, then pie mode is used with zig
GOOS="linux" GOARCH="amd64" CGO_ENABLED="1" CGO_CFLAGS="-O2 -flto=auto -fPIE -fpie -static -w -pipe" CC="zig cc -target x86_64-linux-musl" CXX="zig c++ -target x86_64-linux-musl" go build -v -trimpath -buildmode="pie" -ldflags="-s -w -buildid= -linkmode=external -extldflags '-s -w -static-pie -Wl,--build-id=none'"
and if it rquires cgo + also links with external libs, then I use an alpine container with static libs and build in piemode
docker stop "alpine-builder" 2>/dev/null ; docker rm "alpine-builder" 2>/dev/null
docker run --privileged --net="host" --name "alpine-builder" "azathothas/alpine-builder:latest" \
sh -c '
#Setup ENV
tempdir="$(mktemp -d)" ; mkdir -p "$tempdir" && cd "$tempdir"
mkdir -p "/build-bins"
#Build
git clone --quiet --filter "blob:none" "https://github.com/etix/mirrorbits" && cd "./mirrorbits"
GOOS="linux" GOARCH="amd64" CGO_ENABLED="1" CGO_CFLAGS="-O2 -flto=auto -fPIE -fpie -static -w -pipe" go build -v -trimpath -buildmode="pie" -ldflags="-s -w -buildid= -linkmode=external -extldflags '\''-s -w -static-pie -Wl,--build-id=none'\''"
#strip & info
strip "./mirrorbits"
cp "./mirrorbits" "/build-bins/mirrorbits"
'
likewise, for rust, it's similar. That is to say, it highly depends on each package and it's configuration + deps I recommend you look at the specific package you think isn't being built/compiled with the correct flags and point it out.
Oh, sorry to bother then! I hadn't realized you had already taken this into consideration
I propose using the following GO env vars:
And using the following CFLAG also helps:
If GOBIN were to be used, programs installed using
go install source.com/username/reponame@branch_or_tag
will be installed to $GOBIN, getting rid of themv
command.There are also equivalents for Rust which would alleviate the amount of work that has to be done for each platform, like for example:
RUSTFLAGS=-C link-arg=-s