-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
178 lines (150 loc) · 4.96 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Makefile for hardware_report - System Hardware Information Tool
# Define the name of the binary (must match your [[bin]] name in Cargo.toml)
BINARY_NAME := hardware_report
# Define the Rust compiler and Cargo
RUSTC := rustc
CARGO := cargo
# Define target architectures
LINUX_TARGET := x86_64-unknown-linux-gnu
MACOS_TARGET := aarch64-apple-darwin
# Define output directories
BUILD_DIR := build
RELEASE_DIR := $(BUILD_DIR)/release
# Define version
VERSION := 0.1.0
# Define flags for release builds
RELEASE_FLAGS := --release
# Define common cargo build command
CARGO_BUILD := $(CARGO) build $(RELEASE_FLAGS)
# Define docker image for Linux builds
DOCKER_IMAGE := rust:latest
# Detect the host system
UNAME_S := $(shell uname -s)
UNAME_M := $(shell uname -m)
# Check if Docker is installed (non-empty if found)
DOCKER_CMD := $(shell command -v docker 2> /dev/null)
# Check if Docker is running (if installed). We'll store '1' if running, '0' otherwise
DOCKER_RUNNING := $(shell if [ -n "$(DOCKER_CMD)" ]; then docker info >/dev/null 2>&1 && echo 1 || echo 0; else echo 0; fi)
# Default target: build for both Linux and macOS on whichever platform we are on
.PHONY: all
all:
ifeq ($(UNAME_S),Darwin)
@$(MAKE) linux macos
else ifeq ($(UNAME_S),Linux)
@$(MAKE) linux macos
else
@echo "Unsupported operating system: $(UNAME_S)"
@exit 1
endif
# Target for building Linux x86_64 binary
.PHONY: linux
linux: install-tools
@echo "Building for Linux (x86_64)..."
@mkdir -p $(RELEASE_DIR)
ifeq ($(UNAME_S),Darwin)
# On macOS → only cross-compile with Docker if Docker is running
ifeq ($(DOCKER_RUNNING),1)
@echo "Cross-compiling for Linux using Docker..."
docker run --rm \
-v "$(PWD)":/usr/src/myapp \
-w /usr/src/myapp \
--platform linux/amd64 \
$(DOCKER_IMAGE) \
bash -c "rustup target add $(LINUX_TARGET) && \
cargo build --release --target=$(LINUX_TARGET)"
@cp target/$(LINUX_TARGET)/release/$(BINARY_NAME) \
$(RELEASE_DIR)/$(BINARY_NAME)-linux-x86_64
@chmod +x $(RELEASE_DIR)/$(BINARY_NAME)-linux-x86_64
@echo "Linux binary built (docker cross-compile): $(RELEASE_DIR)/$(BINARY_NAME)-linux-x86_64"
else
@echo "WARNING: Docker is not running or not available on macOS. Skipping Linux build."
endif
else ifeq ($(UNAME_S),Linux)
# On Linux → build natively, no Docker
$(CARGO_BUILD) --target=$(LINUX_TARGET)
@cp target/$(LINUX_TARGET)/release/$(BINARY_NAME) \
$(RELEASE_DIR)/$(BINARY_NAME)-linux-x86_64
@echo "Linux binary built (native): $(RELEASE_DIR)/$(BINARY_NAME)-linux-x86_64"
else
@echo "Unsupported operating system for Linux build: $(UNAME_S)"
@exit 1
endif
# Target for building macOS binary
.PHONY: macos
macos: install-tools
ifeq ($(UNAME_S),Darwin)
@echo "Building for macOS ($(UNAME_M))..."
@mkdir -p $(RELEASE_DIR)
ifeq ($(UNAME_M),arm64)
# Apple Silicon
$(CARGO_BUILD) --target=$(MACOS_TARGET)
@cp target/$(MACOS_TARGET)/release/$(BINARY_NAME) \
$(RELEASE_DIR)/$(BINARY_NAME)-macos-arm64
@echo "macOS binary built: $(RELEASE_DIR)/$(BINARY_NAME)-macos-arm64"
else ifeq ($(UNAME_M),x86_64)
# Intel Mac
$(CARGO_BUILD) --target=x86_64-apple-darwin
@cp target/x86_64-apple-darwin/release/$(BINARY_NAME) \
$(RELEASE_DIR)/$(BINARY_NAME)-macos-x86_64
@echo "macOS binary built: $(RELEASE_DIR)/$(BINARY_NAME)-macos-x86_64"
else
@echo "Unknown Mac architecture: $(UNAME_M)."
endif
else
@echo "Skipping macOS build because we're not on macOS."
endif
# Target for installing required tools
.PHONY: install-tools
install-tools:
@echo "Installing required tools..."
rustup target add $(LINUX_TARGET)
ifeq ($(UNAME_S),Darwin)
rustup target add $(MACOS_TARGET)
# Only pull Docker image if Docker is installed & running
ifeq ($(DOCKER_RUNNING),1)
@echo "Docker is running on macOS...pulling image for cross-builds."
docker pull --platform linux/amd64 $(DOCKER_IMAGE) || true
else
@echo "Docker not running (or not installed). Will skip Docker-based Linux builds."
endif
else ifeq ($(UNAME_S),Linux)
@echo "Building on Linux natively. No Docker usage needed."
else
@echo "Unsupported operating system: $(UNAME_S)"
@exit 1
endif
# Test target
.PHONY: test
test:
$(CARGO) test
# Format check target
.PHONY: fmt
fmt:
$(CARGO) fmt --all -- --check
# Lint target
.PHONY: lint
lint:
$(CARGO) clippy -- -D warnings
# Documentation target
.PHONY: doc
doc:
$(CARGO) doc --no-deps
# Clean target
.PHONY: clean
clean:
$(CARGO) clean
rm -rf $(BUILD_DIR)
# Help target
.PHONY: help
help:
@echo "Available targets:"
@echo " all - Build for both Linux and macOS on your current platform"
@echo " linux - Build for Linux x86_64 (Docker on macOS if running, native on Linux)"
@echo " macos - Build for macOS (only works on an actual Mac)"
@echo " test - Run tests"
@echo " fmt - Check code format"
@echo " lint - Run clippy linter"
@echo " doc - Generate documentation"
@echo " clean - Clean build artifacts"
@echo " install-tools- Install required build tools"
@echo " help - Show this help message"