Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
131 lines (109 loc) · 3.65 KB
/
Copy pathMakefile
File metadata and controls
131 lines (109 loc) · 3.65 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Makefile for hass-cli
BINARY_NAME := hass-cli
MAIN_PATH := ./cmd/hass-cli
BUILD_DIR := ./build
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
LDFLAGS := -ldflags "-X main.Version=$(VERSION) -s -w"
# Go parameters
GOCMD := go
GOBUILD := $(GOCMD) build
GOTEST := $(GOCMD) test
GOMOD := $(GOCMD) mod
GOFMT := gofmt
GOVET := $(GOCMD) vet
# Install directories
PREFIX ?= /usr/local
BINDIR := $(PREFIX)/bin
.PHONY: all build clean test fmt vet lint tidy install uninstall help
# Default target
all: help
# Build the binary
build:
@echo "Building $(BINARY_NAME)..."
@mkdir -p $(BUILD_DIR)
$(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PATH)
@echo "Binary built: $(BUILD_DIR)/$(BINARY_NAME)"
# Build for multiple platforms
build-all: build-linux build-darwin build-windows
build-linux:
@echo "Building for Linux..."
@mkdir -p $(BUILD_DIR)
GOOS=linux GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-amd64 $(MAIN_PATH)
GOOS=linux GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-arm64 $(MAIN_PATH)
build-darwin:
@echo "Building for macOS..."
@mkdir -p $(BUILD_DIR)
GOOS=darwin GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64 $(MAIN_PATH)
GOOS=darwin GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 $(MAIN_PATH)
build-windows:
@echo "Building for Windows..."
@mkdir -p $(BUILD_DIR)
GOOS=windows GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe $(MAIN_PATH)
# Clean build artifacts
clean:
@echo "Cleaning..."
@rm -rf $(BUILD_DIR)
@echo "Done"
# Run tests
test:
@echo "Running tests..."
$(GOTEST) -v -race ./...
# Run tests with coverage
test-coverage:
@echo "Running tests with coverage..."
$(GOTEST) -v -race -coverprofile=coverage.out ./...
$(GOCMD) tool cover -html=coverage.out -o coverage.html
@echo "Coverage report: coverage.html"
# Format code
fmt:
@echo "Formatting code..."
$(GOFMT) -s -w .
# Run go vet
vet:
@echo "Running go vet..."
$(GOVET) ./...
# Run linter (requires golangci-lint)
lint:
@echo "Running linter..."
@which golangci-lint > /dev/null || (echo "golangci-lint not installed. Run: go install github.com/golangci-lint/golangci-lint/cmd/golangci-lint@latest" && exit 1)
golangci-lint run
# Tidy dependencies
tidy:
@echo "Tidying dependencies..."
$(GOMOD) tidy
# Install the binary
install: build
@echo "Installing $(BINARY_NAME) to $(BINDIR)..."
@mkdir -p $(BINDIR)
@cp $(BUILD_DIR)/$(BINARY_NAME) $(BINDIR)/$(BINARY_NAME)
@chmod +x $(BINDIR)/$(BINARY_NAME)
@echo "Installed: $(BINDIR)/$(BINARY_NAME)"
# Uninstall the binary
uninstall:
@echo "Uninstalling $(BINARY_NAME)..."
@rm -f $(BINDIR)/$(BINARY_NAME)
@echo "Done"
# Run the application
run: build
$(BUILD_DIR)/$(BINARY_NAME)
# Development build (faster, no optimizations)
dev:
@echo "Building (dev mode)..."
$(GOBUILD) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PATH)
# Show help
help:
@echo "Available targets:"
@echo " build - Build the binary"
@echo " build-all - Build for all platforms (linux, darwin, windows)"
@echo " clean - Remove build artifacts"
@echo " test - Run tests"
@echo " test-coverage - Run tests with coverage report"
@echo " fmt - Format code"
@echo " vet - Run go vet"
@echo " lint - Run golangci-lint"
@echo " tidy - Tidy go.mod dependencies"
@echo " install - Install binary to $(BINDIR)"
@echo " uninstall - Remove installed binary"
@echo " run - Build and run the application"
@echo " dev - Quick development build"
@echo " help - Show this help message"