Makefile: add STATIC=true for static PIE builds #349
Labels
No Label
kind/breaking
kind/bug
kind/build
kind/dependency
kind/deployment
kind/docs
kind
enhancement
kind
feature
kind/proposal
kind
question
kind
refactor
kind/security
kind/testing
kind/translation
priority/critical
priority/high
priority/low
priority/medium
reviewed/duplicate
reviewed/invalid
reviewed/wontfix
skip-changelog
status/blocked
status/has-backport
status/has-pull
status/needs-backport
status/needs-feedback
status/needs-reviews
status/wip
upstream/gitea
upstream/sdk
No Milestone
No Assignees
5 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: gitea/tea#349
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "noerw/tea:make-static"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
make build
+make install
now support theSTATIC=true
parameter, creating statically linked builds that are also position independent executablesCGO_ENABLED=0
is set for all make build targets, unlessSTATIC=true
is set-s -w
) for all make build targetsI also took the liberty to declutter the makefile from unused & duplicated variables.
Tested on linux-amd64 (fedora, arch, alpine with go 1.15.8 + 1.16.2), as well as windows 10.
ok, on alpine
ldd
says there's still a reference to ld-musl, not sure if there's a way around thatOk, so this works now quite well.
make build
+make install
now support theSTATIC=true
parameter, creating statically linked builds, instead of dynamically linked PIE executables-buildmode=pie
silently switches to generate dynamic executables, so it's not feasible to produce a static PIE exe. Supposedly there is a way around that but I didn't get this to work across platforms.CGO_ENABLED=0
is set for all make build targets, which seems a reasonable default fortea
-s -w
) for all make build targetsI also took the liberty to declutter the makefile from unused & duplicated variables, let me know if I dropped something important (
MAKE_VERSION
?)I tested on linux-amd64 (fedora, arch, alpine with go 1.15 + 1.16), as well as windows 10.
@ -43,1 +26,3 @@
LDFLAGS := -X "main.Version=$(TEA_VERSION)" -X "main.Tags=$(TAGS)"
TAGS ?=
TAGS_STATIC := osusergo,netgo,static_build,$(TAGS)
does anything actually use "static_build" as a build tag? There's nothing in go's source that uses that tag and nothing in gitea's vendor tree. Does tea have a dependency that uses that tag - or is this a futureproof thing?
I added this as a safety, my only reference is this though. We can drop it or replace it with
static
as suggested in that threadno it's fine but perhaps just add a comment in the makefile referencing why you've added it - so that we know where it has come from in case it causes trouble.
@ -44,0 +27,4 @@
TAGS ?=
TAGS_STATIC := osusergo,netgo,static_build,$(TAGS)
LDFLAGS := -X "main.Version=$(TEA_VERSION)" -X "main.Tags=$(TAGS)" -s -w
It's usually nicer to have something like:
as that allows people to adjust and add to the LDFLAGS at make time.
good point, will add that
I managed to add this for
GOFLAGS
, but forLDFLAGS
I didn't find a syntax that didn't eitherIf you know how, I'm happy to add that
@ -44,0 +32,4 @@
# TODO: clean this mess up when there are news on https://github.com/golang/go/issues/26492
# note: -buildmode=pie is incompatible with static builds as of go 1.16!
LDFLAGS_STATIC := $(LDFLAGS) -extldflags "-fno-PIC -static" -X "main.Tags=$(TAGS_STATIC)"
it's interesting that you've included the "-fno-PIC -static" extldflags but I see you have CGO_ENABLED=0 so I don't think these will ever actually get called.
You're right,
-extldflags
isn't used due to missing-linkmode=external
. Will check how to resolve that.But your assumption is wrong;
-linkmode=external
works withCGO_ENABLED=0
edit: actually under some conditions
go build
implicilty sets linkmode=external, so these options are used after all.Cool thanks for checking.
@ -158,3 +151,3 @@
cd /tmp && $(GO) get -u github.com/mitchellh/gox; \
fi
CGO_ENABLED=0 gox -verbose -cgo=false -tags '$(TAGS)' -ldflags '-s -w $(LDFLAGS)' -osarch='!darwin/386 !darwin/arm64 !darwin/arm' -os="windows linux darwin" -arch="386 amd64 arm arm64" -output="$(DIST)/release/tea-$(VERSION)-{{.OS}}-{{.Arch}}"
CGO_ENABLED=0 gox -verbose -cgo=false $(GOFLAGS_STATIC) -osarch='!darwin/386 !darwin/arm64 !darwin/arm' -os="windows linux darwin" -arch="386 amd64 arm arm64" -output="$(DIST)/release/tea-$(VERSION)-{{.OS}}-{{.Arch}}"
Should/could darwin/arm64 be added in (either via this PR or another)?
I created another PR #360
make release
I finally found a way to build a PIE that is statically linked 🎉 (quite an achievement, this isn't documented anywhere)
Compared to the previous solution, there are no more caveats building statically, other than
glibc-static
or equivalentmake STATIC=true
) instead of 18M (CGO_ENABLED=0 go build -ldflags '-s -w' .
)This is ready from my side now
Allow static buildsto Makefile: add STATIC=true for static PIE buildsPlease resolve the conflicts.
@ -101,3 +101,3 @@
git clone https://gitea.com/gitea/tea.git
cd tea
make
make STATIC=true
not
STATIC=true make
?static linekd disabled do still link statically & if enabled I get following compile issue: