-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
91 lines (73 loc) · 3.11 KB
/
Makefile
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
MODULE:=baetyl-gateway
PLATFORM_ALL:=darwin/amd64 linux/amd64 linux/arm64 linux/arm/v7 windows/amd64
export DOCKER_CLI_EXPERIMENTAL=enabled
GIT_TAG:=$(shell git tag --contains HEAD|awk 'END {print}')
GIT_REV:=git-$(shell git rev-parse --short HEAD)
VERSION:=$(if $(GIT_TAG),$(GIT_TAG),$(GIT_REV))
ifeq ($(findstring race,$(BUILD_ARGS)),race)
VERSION:=$(VERSION)-race
endif
GO_OS :=$(shell go env GOOS)
GO_ARCH:=$(shell go env GOARCH)
GO_ARM :=$(shell go env GOARM)
ifndef PLATFORMS
PLATFORMS:=$(if $(GO_ARM),$(GO_OS)/$(GO_ARCH)/$(GO_ARM),$(GO_OS)/$(GO_ARCH))
ifeq ($(GO_OS),darwin)
PLATFORMS+=linux/amd64
endif
else ifeq ($(PLATFORMS),all)
override PLATFORMS:=$(PLATFORM_ALL)
endif
GO := go
GO_MOD := $(GO) mod
GO_ENV := env GO111MODULE=on CGO_ENABLED=0
GO_FLAGS := $(BUILD_ARGS) -ldflags '-X "github.com/baetyl/baetyl-go/v2/utils.REVISION=$(GIT_REV)" -X "github.com/baetyl/baetyl-go/v2/utils.VERSION=$(VERSION)"'
ifeq ($(findstring race,$(BUILD_ARGS)),race)
GO_ENV := env GO111MODULE=on CGO_ENABLED=1
GO_FLAGS := $(BUILD_ARGS) -ldflags '-s -w -X "github.com/baetyl/baetyl-go/v2/utils.REVISION=$(GIT_REV)" -X "github.com/baetyl/baetyl-go/v2/utils.VERSION=$(VERSION)" -linkmode external -w -extldflags "-static"'
override PLATFORMS:= $(filter-out linux/arm/v7,$(PLATFORMS))
endif
GO_BUILD := $(GO_ENV) $(GO) build $(GO_FLAGS)
GOTEST := $(GO) test
GOPKGS := $$($(GO) list ./... | grep -vE "vendor")
REGISTRY :=
XFLAGS :=--load
XPLATFORMS :=$(shell echo $(filter-out darwin/amd64,$(PLATFORMS)) | sed 's: :,:g')
OUTPUT := output
OUTPUT_DIRS:=$(PLATFORMS:%=$(OUTPUT)/%/$(MODULE))
OUTPUT_BINS:=$(OUTPUT_DIRS:%=%/$(MODULE))
PKG_PLATFORMS := $(shell echo $(PLATFORMS) | sed 's:/:-:g')
OUTPUT_PKGS:=$(PKG_PLATFORMS:%=$(OUTPUT)/$(MODULE)_%_$(VERSION).zip)
.PHONY: all
all: build package
set-env:
$(GO) env -w GO111MODULE="on"
$(OUTPUT_BINS): set-env $(SRC_FILES)
@echo "BUILD $(patsubst ../../%,%,$@)"
@mkdir -p $(dir $@)
@cp program.yml $(dir $@)
@$(shell echo $(@:$(OUTPUT)/%/$(MODULE)/$(MODULE)=%) | sed 's:/v:/:g' | awk -F '/' '{print "GOOS="$$1" GOARCH="$$2" GOARM="$$3""}') $(GO_BUILD) -o $@ .
.PHONY: build
build: $(OUTPUT_BINS)
$(OUTPUT_PKGS):
@echo "PACKAGE $(patsubst ../../%,%,$@)"
@cd $(OUTPUT)/$(shell echo $(@:$(OUTPUT)/$(MODULE)_%_$(VERSION).zip=%) | sed 's:-:/:g')/$(MODULE) && zip -q -r $(notdir $@) $(MODULE) program.yml
@$(shell if [ $(shell echo $(@:$(OUTPUT)/$(MODULE)_%_$(VERSION).zip=%) | sed 's:-:/:g') == "windows/amd64" ];then \
cd $(OUTPUT)/windows/amd64/$(MODULE) && mv $(MODULE) $(MODULE).exe && zip -q -r $(MODULE)_windows-amd64_$(VERSION).zip $(MODULE).exe program.yml ; fi)
.PHONY: package
package: build $(OUTPUT_PKGS)
.PHONY: build-local
build-local: $(SRC_FILES)
@echo "BUILD $(MODULE)"
$(GO_BUILD) -o $(MODULE) .
@chmod +x $(MODULE)
.PHONY: image
image:
@echo "BUILDX: $(REGISTRY)$(MODULE):$(VERSION)"
@-docker buildx create --name baetyl
@docker buildx use baetyl
@docker run --privileged --rm tonistiigi/binfmt --install all
docker buildx build $(XFLAGS) --platform $(XPLATFORMS) -t $(REGISTRY)$(MODULE):$(VERSION) -f Dockerfile .
.PHONY: clean
clean:
@rm -rf $(OUTPUT)