-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
75 lines (62 loc) · 1.92 KB
/
Makefile
File metadata and controls
75 lines (62 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Binary name
BINARY_NAME=gitid
VERSION ?= $(shell git describe --tags --always --dirty)
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
# Build flags
LDFLAGS=-ldflags "-X main.version=${VERSION} -s -w"
# Strip 'v' prefix from version for package naming
VERSION_CLEAN=$(shell echo $(VERSION) | sed 's/^v//')
CGO_ENABLED=0
# Supported platforms
PLATFORMS=linux darwin windows
ARCHITECTURES=amd64 arm64
# Output directories
RELEASE_DIR=release
BUILD_DIR=build
.PHONY: all build clean test release
all: clean build
build:
$(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME) $(LDFLAGS)
build-static:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
$(GOBUILD) -tags netgo -ldflags '-extldflags "-static" $(LDFLAGS)' \
-o $(BUILD_DIR)/$(BINARY_NAME)-static
clean:
$(GOCLEAN)
rm -rf $(BUILD_DIR)
rm -rf $(RELEASE_DIR)
test:
$(GOTEST) -v ./...
release: clean
mkdir -p $(RELEASE_DIR) $(BUILD_DIR)
# Build for each platform/architecture
$(foreach GOOS, $(PLATFORMS), \
$(foreach GOARCH, $(ARCHITECTURES), \
$(eval EXTENSION := $(if $(filter windows,$(GOOS)),.exe,)) \
GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) \
$(GOBUILD) $(LDFLAGS) \
-o $(BUILD_DIR)/$(BINARY_NAME)_$(GOOS)_$(GOARCH)$(EXTENSION); \
) \
)
# Build musl version for linux_amd64
CC=musl-gcc CGO_ENABLED=1 GOOS=linux GOARCH=amd64 \
$(GOBUILD) -tags musl \
-ldflags "-linkmode external -extldflags '-static' -X main.version=${VERSION} -s -w" \
-o $(BUILD_DIR)/$(BINARY_NAME)_linux_amd64_musl
# Create Linux packages for each architecture
# Package for amd64 using musl binary
$(foreach ARCH, $(ARCHITECTURES), \
export ARCH=$(ARCH) && export VERSION=$(shell echo $(VERSION) | sed 's/^v//') && nfpm package \
-f nfpm.yaml \
-p deb \
-t $(RELEASE_DIR) && \
export ARCH=$(ARCH) && export VERSION=$(shell echo $(VERSION) | sed 's/^v//') && nfpm package \
-f nfpm.yaml \
-p rpm \
-t $(RELEASE_DIR); \
)
.DEFAULT_GOAL := all