Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
162 lines (140 loc) 路 4.89 KB
/
Copy pathMakefile
File metadata and controls
162 lines (140 loc) 路 4.89 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# CIDR Calculator Makefile
# Binary name
BINARY_NAME=cidr-calc
MODULE_NAME=cidr-calculator
# Build directory
BUILD_DIR=build
# Version information
VERSION?=1.0.0
BUILD_TIME=$(shell date -u '+%Y-%m-%d_%H:%M:%S')
GIT_COMMIT=$(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod
# Build flags
LDFLAGS=-ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME) -X main.GitCommit=$(GIT_COMMIT)"
# Default target
.PHONY: all
all: clean test build
# Build for current platform
.PHONY: build
build:
@echo "Building $(BINARY_NAME) for current platform..."
@mkdir -p $(BUILD_DIR)
$(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) .
@echo "Build complete: $(BUILD_DIR)/$(BINARY_NAME)"
# Build for all platforms
.PHONY: build-all
build-all: build-linux build-darwin build-windows
@echo "All platform builds complete"
# Build for Linux
.PHONY: build-linux
build-linux:
@echo "Building for Linux..."
@mkdir -p $(BUILD_DIR)
GOOS=linux GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-amd64 .
GOOS=linux GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-arm64 .
# Build for macOS
.PHONY: build-darwin
build-darwin:
@echo "Building for macOS..."
@mkdir -p $(BUILD_DIR)
GOOS=darwin GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64 .
GOOS=darwin GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 .
# Build for Windows
.PHONY: build-windows
build-windows:
@echo "Building for Windows..."
@mkdir -p $(BUILD_DIR)
GOOS=windows GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe .
GOOS=windows GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-windows-arm64.exe .
# Run tests
.PHONY: test
test:
@echo "Running tests..."
$(GOTEST) -v ./...
# Run tests with coverage
.PHONY: test-coverage
test-coverage:
@echo "Running tests with coverage..."
$(GOTEST) -v -cover ./...
$(GOTEST) -coverprofile=coverage.out ./...
$(GOCMD) tool cover -html=coverage.out -o coverage.html
@echo "Coverage report generated: coverage.html"
# Run integration tests
.PHONY: test-integration
test-integration:
@echo "Running integration tests..."
$(GOTEST) -v -tags=integration ./...
# Clean build artifacts
.PHONY: clean
clean:
@echo "Cleaning build artifacts..."
$(GOCLEAN)
rm -rf $(BUILD_DIR)
rm -f coverage.out coverage.html
rm -f *.html *.txt # Clean example output files
# Install dependencies
.PHONY: deps
deps:
@echo "Installing dependencies..."
$(GOMOD) download
$(GOMOD) tidy
# Format code
.PHONY: fmt
fmt:
@echo "Formatting code..."
$(GOCMD) fmt ./...
# Lint code (requires golangci-lint)
.PHONY: lint
lint:
@echo "Linting code..."
@which golangci-lint > /dev/null || (echo "golangci-lint not installed. Install with: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest" && exit 1)
golangci-lint run
# Run the application with example
.PHONY: run
run:
@echo "Running $(BINARY_NAME) with example..."
$(GOCMD) run . 192.168.1.0/24
# Install binary to GOPATH/bin
.PHONY: install
install:
@echo "Installing $(BINARY_NAME)..."
$(GOCMD) install $(LDFLAGS) .
# Create release archives
.PHONY: release
release: build-all
@echo "Creating release archives..."
@mkdir -p $(BUILD_DIR)/release
@cd $(BUILD_DIR) && \
tar -czf release/$(BINARY_NAME)-$(VERSION)-linux-amd64.tar.gz $(BINARY_NAME)-linux-amd64 && \
tar -czf release/$(BINARY_NAME)-$(VERSION)-linux-arm64.tar.gz $(BINARY_NAME)-linux-arm64 && \
tar -czf release/$(BINARY_NAME)-$(VERSION)-darwin-amd64.tar.gz $(BINARY_NAME)-darwin-amd64 && \
tar -czf release/$(BINARY_NAME)-$(VERSION)-darwin-arm64.tar.gz $(BINARY_NAME)-darwin-arm64 && \
zip -q release/$(BINARY_NAME)-$(VERSION)-windows-amd64.zip $(BINARY_NAME)-windows-amd64.exe && \
zip -q release/$(BINARY_NAME)-$(VERSION)-windows-arm64.zip $(BINARY_NAME)-windows-arm64.exe
@echo "Release archives created in $(BUILD_DIR)/release/"
# Show help
.PHONY: help
help:
@echo "Available targets:"
@echo " build - Build for current platform"
@echo " build-all - Build for all platforms (Linux, macOS, Windows)"
@echo " build-linux - Build for Linux (amd64, arm64)"
@echo " build-darwin - Build for macOS (amd64, arm64)"
@echo " build-windows - Build for Windows (amd64, arm64)"
@echo " test - Run tests"
@echo " test-coverage - Run tests with coverage report"
@echo " test-integration - Run integration tests"
@echo " clean - Clean build artifacts"
@echo " deps - Install dependencies"
@echo " fmt - Format code"
@echo " lint - Lint code (requires golangci-lint)"
@echo " run - Run with example"
@echo " install - Install binary to GOPATH/bin"
@echo " release - Create release archives"
@echo " help - Show this help"