-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
65 lines (57 loc) · 1.81 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
OUTPUT_NAME = gitlab-autoscaler
DOCKER_IMAGE = golang:1.23
GOARM =
GOOS =
GOARCH =
VERSION = $(shell git describe --tags --abbrev=0)
COMMIT_HASH = $(shell git rev-parse --short HEAD)
# Rules
.PHONY: all clean docker-build
all: check-params build
check-params:
@if [ -z "$(OS)" ] || [ -z "$(ARCH)" ]; then \
echo "Error: OS and ARCH must be specified."; \
echo "Usage: make OS=<os> ARCH=<arch>"; \
echo "Supported OS: darwin (macOS), linux"; \
echo "Supported Arch: amd64, arm, arm64"; \
exit 1; \
fi
build: set-params
@echo "Building for $(OS) ($(ARCH))..."
GOOS=$(GOOS) GOARCH=$(GOARCH) GOARM=$(GOARM) go build -ldflags "-X main.Version=$(VERSION) -X main.CommitHash=$(COMMIT_HASH)" -o "${OUTPUT_NAME}"
# build in docker
docker-build: check-params set-params
@echo "Building $(OUTPUT_NAME) in Docker for $(OS) ($(ARCH))..."
docker run --rm -e OUTPUT_NAME="$(OUTPUT_NAME)" -v "$$(pwd)":/app -w /app $(DOCKER_IMAGE) /bin/bash -c "GOOS=$(GOOS) GOARCH=$(GOARCH) GOARM=$(GOARM) go build -ldflags '-X main.Version=$(VERSION) -X main.CommitHash=$(COMMIT_HASH)' -o ${OUTPUT_NAME}"
set-params:
@if [ "$(OS)" == "darwin" ]; then \
if [ "$(ARCH)" == "amd64" ]; then \
GOOS="darwin"; \
GOARCH="amd64"; \
elif [ "$(ARCH)" == "arm64" ]; then \
GOOS="darwin"; \
GOARCH="arm64"; \
else \
echo "Unsupported architecture for macOS: $(ARCH)"; \
exit 1; \
fi; \
elif [ "$(OS)" == "linux" ]; then \
if [ "$(ARCH)" == "amd64" ]; then \
GOOS="linux"; \
GOARCH="amd64"; \
elif [ "$(ARCH)" == "arm" ]; then \
GOOS="linux"; \
GOARCH="arm"; \
elif [ "$(ARCH)" == "arm64" ]; then \
GOOS="linux"; \
GOARCH="arm64"; \
else \
echo "Unsupported architecture for Linux: $(ARCH)"; \
exit 1; \
fi; \
else \
echo "Unsupported operating system: $(OS)"; \
exit 1; \
fi
clean:
rm -f $(OUTPUT_NAME)