main
1# Project variables
2BINARY_NAME=mysh
3PACKAGE_PATH=.
4BIN_DIR=./bin
5VERSION=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
6BUILD_TIME=$(shell date -u '+%Y-%m-%d_%H:%M:%S')
7COMMIT_HASH=$(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
8
9# Go variables
10GOCMD=go
11GOBUILD=$(GOCMD) build
12GOCLEAN=$(GOCMD) clean
13GOTEST=$(GOCMD) test
14GOGET=$(GOCMD) get
15GOMOD=$(GOCMD) mod
16GOFMT=$(GOCMD) fmt
17
18# Build flags
19LDFLAGS=-ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME) -X main.CommitHash=$(COMMIT_HASH)"
20
21# Default target
22.PHONY: all
23all: clean build
24
25# Create bin directory
26$(BIN_DIR):
27 @mkdir -p $(BIN_DIR)
28
29# Build for current platform
30.PHONY: build
31build: $(BIN_DIR)
32 @echo "Building $(BINARY_NAME) for current platform..."
33 $(GOBUILD) $(LDFLAGS) -o $(BIN_DIR)/$(BINARY_NAME) $(PACKAGE_PATH)
34 @echo "Binary built: $(BIN_DIR)/$(BINARY_NAME)"
35
36# Build for Linux AMD64
37.PHONY: build-linux
38build-linux: $(BIN_DIR)
39 @echo "Building $(BINARY_NAME) for Linux AMD64..."
40 GOOS=linux GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BIN_DIR)/$(BINARY_NAME)-linux-amd64 $(PACKAGE_PATH)
41
42# Build for Linux ARM64
43.PHONY: build-linux-arm64
44build-linux-arm64: $(BIN_DIR)
45 @echo "Building $(BINARY_NAME) for Linux ARM64..."
46 GOOS=linux GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BIN_DIR)/$(BINARY_NAME)-linux-arm64 $(PACKAGE_PATH)
47
48# Build for macOS AMD64
49.PHONY: build-darwin
50build-darwin: $(BIN_DIR)
51 @echo "Building $(BINARY_NAME) for macOS AMD64..."
52 GOOS=darwin GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BIN_DIR)/$(BINARY_NAME)-darwin-amd64 $(PACKAGE_PATH)
53
54# Build for macOS ARM64 (Apple Silicon)
55.PHONY: build-darwin-arm64
56build-darwin-arm64: $(BIN_DIR)
57 @echo "Building $(BINARY_NAME) for macOS ARM64..."
58 GOOS=darwin GOARCH=arm64 $(GOBUILD) $(LDFLAGS) -o $(BIN_DIR)/$(BINARY_NAME)-darwin-arm64 $(PACKAGE_PATH)
59
60# Build for Windows AMD64
61.PHONY: build-windows
62build-windows: $(BIN_DIR)
63 @echo "Building $(BINARY_NAME) for Windows AMD64..."
64 GOOS=windows GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BIN_DIR)/$(BINARY_NAME)-windows-amd64.exe $(PACKAGE_PATH)
65
66# Build for all platforms
67.PHONY: build-all
68build-all: build-linux build-linux-arm64 build-darwin build-darwin-arm64 build-windows
69 @echo "All platform builds completed"
70
71# Run tests
72.PHONY: test
73test:
74 @echo "Running tests..."
75 $(GOTEST) -v ./...
76
77# Run tests with coverage
78.PHONY: test-coverage
79test-coverage:
80 @echo "Running tests with coverage..."
81 $(GOTEST) -v -coverprofile=coverage.out ./...
82 $(GOCMD) tool cover -html=coverage.out -o coverage.html
83 @echo "Coverage report generated: coverage.html"
84
85# Clean build artifacts
86.PHONY: clean
87clean:
88 @echo "Cleaning..."
89 $(GOCLEAN)
90 @rm -rf $(BIN_DIR)
91 @rm -f coverage.out coverage.html
92 @echo "Clean completed"
93
94# Install dependencies
95.PHONY: deps
96deps:
97 @echo "Installing dependencies..."
98 $(GOMOD) download
99 $(GOMOD) tidy
100
101# Format code
102.PHONY: fmt
103fmt:
104 @echo "Formatting code..."
105 $(GOFMT) ./...
106
107# Run linter (if golangci-lint is installed)
108.PHONY: lint
109lint:
110 @if command -v golangci-lint >/dev/null 2>&1; then \
111 echo "Running linter..."; \
112 golangci-lint run; \
113 else \
114 echo "golangci-lint not installed, skipping..."; \
115 fi
116
117# Install binary to system
118.PHONY: install
119install: build
120 @echo "Installing $(BINARY_NAME) to /usr/local/bin..."
121 @sudo cp $(BIN_DIR)/$(BINARY_NAME) /usr/local/bin/
122 @echo "Installation completed"
123
124# Uninstall binary from system
125.PHONY: uninstall
126uninstall:
127 @echo "Removing $(BINARY_NAME) from /usr/local/bin..."
128 @sudo rm -f /usr/local/bin/$(BINARY_NAME)
129 @echo "Uninstall completed"
130
131# Development build (fast, no optimizations)
132.PHONY: dev
133dev: $(BIN_DIR)
134 @echo "Building development version..."
135 $(GOBUILD) -o $(BIN_DIR)/$(BINARY_NAME) $(PACKAGE_PATH)
136
137# Release build (optimized)
138.PHONY: release
139release: clean test build-all
140 @echo "Release build completed"
141
142# Show help
143.PHONY: help
144help:
145 @echo "Available targets:"
146 @echo " build - Build for current platform"
147 @echo " build-linux - Build for Linux AMD64"
148 @echo " build-linux-arm64 - Build for Linux ARM64"
149 @echo " build-darwin - Build for macOS AMD64"
150 @echo " build-darwin-arm64 - Build for macOS ARM64"
151 @echo " build-windows - Build for Windows AMD64"
152 @echo " build-all - Build for all platforms"
153 @echo " test - Run tests"
154 @echo " test-coverage - Run tests with coverage report"
155 @echo " clean - Clean build artifacts"
156 @echo " deps - Install dependencies"
157 @echo " fmt - Format code"
158 @echo " lint - Run linter"
159 @echo " install - Install binary to system"
160 @echo " uninstall - Remove binary from system"
161 @echo " dev - Fast development build"
162 @echo " release - Full release build (all platforms)"
163 @echo " help - Show this help"