WIP: improve Makefile #187

Closed
noerw wants to merge 3 commits from noerw/tea:static-build into main
4 changed files with 51 additions and 31 deletions

View File

@ -81,6 +81,7 @@ steps:
event:
- push
- tag
- pull_request
- name: gpg-sign
pull: always

View File

@ -21,9 +21,6 @@ endif
GOFILES := $(shell find . -name "*.go" -type f ! -path "./vendor/*" ! -path "*/bindata.go")
GOFMT ?= gofmt -s
GOFLAGS := -i -v
EXTRA_GOFLAGS ?=
MAKE_VERSION := $(shell make -v | head -n 1)
ifneq ($(DRONE_TAG),)
@ -38,13 +35,20 @@ else
TEA_VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
endif
TAGS ?=
TAGS_STATIC := osusergo,netgo,static_build,$(TAGS)
LDFLAGS := -X "main.Version=$(TEA_VERSION)" -X "main.Tags=$(TAGS)"
LDFLAGS_STATIC := -X "main.Version=$(TEA_VERSION)" -X "main.Tags=$(TAGS_STATIC)" -linkmode external -extldflags "-fno-PIC -static"
GOFLAGS := -mod=vendor -v -tags '$(TAGS)' -ldflags '$(LDFLAGS)' # includes flags for native builds not needed / supported by xgo
XGOFLAGS := -tags '$(TAGS)' -ldflags '$(LDFLAGS) -s -w' # smaller non-debug binaries
XGOFLAGS_STATIC := -tags '$(TAGS_STATIC)' -ldflags '$(LDFLAGS_STATIC) -s -w' -buildmode=pie
GOFLAGS_STATIC := $(GOFLAGS) $(XGOFLAGS_STATIC)
PACKAGES ?= $(shell $(GO) list ./... | grep -v /vendor/)
SOURCES ?= $(shell find . -name "*.go" -type f)
TAGS ?=
ifeq ($(OS), Windows_NT)
EXECUTABLE := tea.exe
else
@ -129,15 +133,17 @@ test-vendor: vendor
.PHONY: check
check: test
.PHONY: install
install: $(wildcard *.go)
$(GO) install -v -tags '$(TAGS)' -ldflags '-s -w $(LDFLAGS)'
.PHONY: build
build: $(EXECUTABLE)
$(EXECUTABLE): $(SOURCES)
$(GO) build -mod=vendor $(GOFLAGS) $(EXTRA_GOFLAGS) -tags '$(TAGS)' -ldflags '-s -w $(LDFLAGS)' -o $@
@echo "building development executable '$@'"
$(GO) build $(GOFLAGS) -o $@
.PHONY: install
install: $(SOURCES)
@echo "installing static executable to $(GOPATH)/bin/$(EXECUTABLE)"
$(GO) install $(GOFLAGS_STATIC)
.PHONY: release
release: release-dirs release-windows release-linux release-darwin release-copy release-compress release-check
@ -151,7 +157,9 @@ release-windows:
@hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
cd /tmp && $(GO) get -u src.techknowlogick.com/xgo; \
fi
GO111MODULE=off xgo -dest $(DIST)/binaries -tags 'netgo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'windows/*' -out tea-$(VERSION) .
# go modules are turned off due to https://github.com/techknowlogick/xgo/issues/16
# xgo autodetects --mod=vendor, so no need to set it here.
GO111MODULE=off xgo -dest $(DIST)/binaries $(XGOFLAGS_STATIC) -targets 'windows/*' -out tea-$(VERSION) .
ifeq ($(CI),drone)
cp /build/* $(DIST)/binaries
endif
@ -161,7 +169,9 @@ release-linux:
@hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
cd /tmp && $(GO) get -u src.techknowlogick.com/xgo; \
fi
GO111MODULE=off xgo -dest $(DIST)/binaries -tags 'netgo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'linux/amd64,linux/386,linux/arm-5,linux/arm-6,linux/arm64,linux/mips64le,linux/mips,linux/mipsle' -out tea-$(VERSION) .
# go modules are turned off due to https://github.com/techknowlogick/xgo/issues/16
# xgo autodetects --mod=vendor, so no need to set it here.
GO111MODULE=off xgo -dest $(DIST)/binaries $(XGOFLAGS_STATIC) -targets 'linux/amd64,linux/386,linux/arm-5,linux/arm-6,linux/arm64,linux/mips64le,linux/mips,linux/mipsle' -out tea-$(VERSION) .
ifeq ($(CI),drone)
cp /build/* $(DIST)/binaries
endif
@ -171,7 +181,10 @@ release-darwin:
@hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
cd /tmp && $(GO) get -u src.techknowlogick.com/xgo; \
fi
GO111MODULE=off xgo -dest $(DIST)/binaries -tags 'netgo $(TAGS)' -ldflags '$(LDFLAGS)' -targets 'darwin/*' -out tea-$(VERSION) .
# go modules are turned off due to https://github.com/techknowlogick/xgo/issues/16
# xgo autodetects --mod=vendor, so no need to set it here.
# osx requires dynamic linking to system libraries, so don't build a static binary.
GO111MODULE=off xgo -dest $(DIST)/binaries $(XGOFLAGS) -targets 'darwin/*' -out tea-$(VERSION) .
ifeq ($(CI),drone)
cp /build/* $(DIST)/binaries
endif
@ -182,6 +195,7 @@ release-copy:
.PHONY: release-compress
release-compress:
# go modules are turned off due to https://github.com/techknowlogick/xgo/issues/16
@hash gxz > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
GO111MODULE=off $(GO) get -u github.com/ulikunitz/xz/cmd/gxz; \
fi

View File

@ -13,23 +13,28 @@ It uses [code.gitea.io/sdk](https://code.gitea.io/sdk) and interacts with the Gi
You can use the prebuilt binaries from [dl.gitea.io](https://dl.gitea.io/tea/)
To install from source, go 1.13 or newer is required:
```sh
go get code.gitea.io/tea
go install code.gitea.io/tea
```
If you have `brew` installed, you can install `tea` via:
```sh
brew tap gitea/tap https://gitea.com/gitea/homebrew-gitea
brew install tea
```
Distribution packages exist for: **alpinelinux ([tea](https://pkgs.alpinelinux.org/packages?name=tea&branch=edge))** and **archlinux ([gitea-tea](https://aur.archlinux.org/packages/gitea-tea))**
To install from source, go 1.13 or newer is required.
Outdated
Review

go >= 1.13 (sdk dependency)

go >= 1.13 (sdk dependency)
- Build static binary using git & make:
```sh
git clone https://gitea.com/gitea/tea
make install
```
- If you have `brew` installed, you can install `tea` via:
```sh
brew tap gitea/tap https://gitea.com/gitea/homebrew-gitea
brew install tea
```
- The quick & dirty way, (only go installation needed, no version injected, dynamically linked):
```sh
go get code.gitea.io/tea
go install code.gitea.io/tea
```
## Usage
First of all, you have to create a token on your `personal settings -> application` page of your gitea instance.

View File

@ -16,7 +16,7 @@ import (
"github.com/urfave/cli/v2"
)
// Version holds the current tea version
// Version holds the current tea version, injected by make via ldflags
var Version = "development"
// Tags holds the build tags used
@ -32,7 +32,7 @@ func main() {
app.Name = "tea"
app.Usage = "Command line tool to interact with Gitea"
app.Description = ``
app.Version = Version + formatBuiltWith(Tags)
app.Version = setting.AppVer + setting.AppBuiltWith
app.Commands = []*cli.Command{
&cmd.CmdLogin,
&cmd.CmdLogout,
@ -58,5 +58,5 @@ func formatBuiltWith(Tags string) string {
return ""
}
return " built with: " + strings.Replace(Tags, " ", ", ", -1)
return " built with " + strings.Replace(Tags, " ", ", ", -1)
}