-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMakefile
More file actions
274 lines (247 loc) · 9.88 KB
/
Copy pathMakefile
File metadata and controls
274 lines (247 loc) · 9.88 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# SwiftDependencyAudit Makefile
# Inspired by SwiftLint's build system
# Variables
PRODUCT_NAME = swift-dependency-audit
EXECUTABLE_NAME = swift-dependency-audit
SWIFT_BUILD_FLAGS = -c release -Xswiftc -Osize
MACOS_BUILD_DIR = .build/apple/Products/Release
LINUX_X86_64_BUILD_DIR = .build/x86_64-unknown-linux-gnu/release
LINUX_AARCH64_BUILD_DIR = .build/aarch64-unknown-linux-gnu/release
# Platform detection
UNAME := $(shell uname)
ifeq ($(UNAME), Darwin)
PLATFORM = macos
else
PLATFORM = linux
endif
# Version handling - get from git tags
VERSION := $(shell git describe --tags --always 2>/dev/null | sed 's/^v//' || echo "1.0.0-dev")
# Default target
.PHONY: all
all: build
# Clean all build artifacts
.PHONY: clean
clean:
rm -rf .build
rm -rf *.artifactbundle.zip
rm -rf *.checksum
rm -f Package.swift.bak
# Build for current platform
.PHONY: build
build:
VERSION=$(VERSION) swift build $(SWIFT_BUILD_FLAGS)
# Build universal macOS binary
.PHONY: build_macos
build_macos:
@echo "Building universal macOS binary..."
VERSION=$(VERSION) swift build $(SWIFT_BUILD_FLAGS) --arch arm64 --arch x86_64
strip -rSTx $(MACOS_BUILD_DIR)/$(EXECUTABLE_NAME)
@echo "✅ macOS universal binary built at $(MACOS_BUILD_DIR)/$(EXECUTABLE_NAME)"
# Build Linux binaries
.PHONY: build_linux
build_linux: build_linux_x86_64 build_linux_aarch64
# Native Linux builds (when running on appropriate architecture)
.PHONY: build_linux_native
build_linux_native:
ifeq ($(shell uname -m), x86_64)
@echo "Building Linux x86_64 binary natively..."
mkdir -p $(LINUX_X86_64_BUILD_DIR)
VERSION=$(VERSION) swift build $(SWIFT_BUILD_FLAGS) --triple x86_64-unknown-linux-gnu --static-swift-stdlib
strip .build/x86_64-unknown-linux-gnu/release/$(EXECUTABLE_NAME)
@echo "✅ Linux x86_64 binary built natively"
else ifeq ($(shell uname -m), aarch64)
@echo "Building Linux ARM64 binary natively..."
mkdir -p $(LINUX_AARCH64_BUILD_DIR)
VERSION=$(VERSION) swift build $(SWIFT_BUILD_FLAGS) --triple aarch64-unknown-linux-gnu --static-swift-stdlib
strip .build/aarch64-unknown-linux-gnu/release/$(EXECUTABLE_NAME)
@echo "✅ Linux ARM64 binary built natively"
else
@echo "❌ Unsupported architecture for native Linux build: $(shell uname -m)"
@echo "Use 'make build_linux' for cross-compilation with development tools"
exit 1
endif
# Legacy cross-compilation targets using containerized Swift toolchain
# NOTE: CI/CD uses native builds on appropriate runners for better performance
.PHONY: build_linux_x86_64
build_linux_x86_64:
@echo "Building Linux x86_64 binary using containerized toolchain..."
mkdir -p $(LINUX_X86_64_BUILD_DIR)
docker run --rm \
--platform linux/amd64 \
-v $(PWD):/workspace \
-w /workspace \
-e HOME=/tmp \
swift:6.2 \
bash -c " \
VERSION=$(VERSION) swift build $(SWIFT_BUILD_FLAGS) --triple x86_64-unknown-linux-gnu --static-swift-stdlib && \
strip .build/x86_64-unknown-linux-gnu/release/$(EXECUTABLE_NAME) \
"
@echo "✅ Linux x86_64 binary built at $(LINUX_X86_64_BUILD_DIR)/$(EXECUTABLE_NAME)"
.PHONY: build_linux_aarch64
build_linux_aarch64:
@echo "Building Linux ARM64 binary using containerized cross-compilation..."
mkdir -p $(LINUX_AARCH64_BUILD_DIR)
docker run --rm \
--platform linux/amd64 \
-v $(PWD):/workspace \
-w /workspace \
-e HOME=/tmp \
swift:6.2 \
bash -c " \
echo 'Installing cross-compilation tools...' && \
apt-get update && apt-get install -y gcc-aarch64-linux-gnu && \
echo 'Cross-compiling for ARM64...' && \
VERSION=$(VERSION) swift build $(SWIFT_BUILD_FLAGS) --triple aarch64-unknown-linux-gnu -Xcc -target -Xcc aarch64-unknown-linux-gnu --static-swift-stdlib -Xswiftc -resource-dir -Xswiftc /usr/lib/swift_static && \
aarch64-linux-gnu-strip .build/aarch64-unknown-linux-gnu/release/$(EXECUTABLE_NAME) \
"
@echo "✅ Linux ARM64 binary built at $(LINUX_AARCH64_BUILD_DIR)/$(EXECUTABLE_NAME)"
# Build all platform binaries
.PHONY: build_all
build_all: build_macos build_linux
@echo "✅ All platform binaries built successfully"
# Test current platform build
.PHONY: test
test:
swift test
# Test functionality of built binary
.PHONY: test_binary
test_binary:
ifeq ($(PLATFORM), macos)
@echo "Testing macOS binary..."
$(MACOS_BUILD_DIR)/$(EXECUTABLE_NAME) --version
$(MACOS_BUILD_DIR)/$(EXECUTABLE_NAME) --help
$(MACOS_BUILD_DIR)/$(EXECUTABLE_NAME) . --verbose --exclude-tests
else
@echo "Testing local binary..."
.build/release/$(EXECUTABLE_NAME) --version
.build/release/$(EXECUTABLE_NAME) --help
.build/release/$(EXECUTABLE_NAME) . --verbose --exclude-tests
endif
# Create SPM artifact bundle
.PHONY: spm_artifactbundle
spm_artifactbundle: build_all
@echo "Creating SPM artifact bundle for version $(VERSION)..."
chmod +x Scripts/spm-artifact-bundle.sh
./Scripts/spm-artifact-bundle.sh "$(VERSION)"
@echo "✅ Artifact bundle created: $(PRODUCT_NAME).artifactbundle.zip"
# Update Package.swift with new version and checksum
.PHONY: update_package
update_package:
@echo "Updating Package.swift with version v$(VERSION)..."
chmod +x Scripts/update-artifact-bundle.sh
./Scripts/update-artifact-bundle.sh "v$(VERSION)"
@echo "✅ Package.swift updated"
# Create portable binary archives
.PHONY: package
package: build_all
@echo "Creating portable binary packages for version $(VERSION)..."
mkdir -p release
# macOS universal binary
mkdir -p "$(PRODUCT_NAME)-$(VERSION)-macos-universal"
cp $(MACOS_BUILD_DIR)/$(EXECUTABLE_NAME) "$(PRODUCT_NAME)-$(VERSION)-macos-universal/"
cp LICENSE "$(PRODUCT_NAME)-$(VERSION)-macos-universal/" 2>/dev/null || true
cp README.md "$(PRODUCT_NAME)-$(VERSION)-macos-universal/" 2>/dev/null || true
tar -czf "release/$(PRODUCT_NAME)-$(VERSION)-macos-universal.tar.gz" "$(PRODUCT_NAME)-$(VERSION)-macos-universal"
rm -rf "$(PRODUCT_NAME)-$(VERSION)-macos-universal"
# Linux x86_64 binary
mkdir -p "$(PRODUCT_NAME)-$(VERSION)-linux-x86_64"
cp $(LINUX_X86_64_BUILD_DIR)/$(EXECUTABLE_NAME) "$(PRODUCT_NAME)-$(VERSION)-linux-x86_64/"
cp LICENSE "$(PRODUCT_NAME)-$(VERSION)-linux-x86_64/" 2>/dev/null || true
cp README.md "$(PRODUCT_NAME)-$(VERSION)-linux-x86_64/" 2>/dev/null || true
tar -czf "release/$(PRODUCT_NAME)-$(VERSION)-linux-x86_64.tar.gz" "$(PRODUCT_NAME)-$(VERSION)-linux-x86_64"
rm -rf "$(PRODUCT_NAME)-$(VERSION)-linux-x86_64"
# Linux ARM64 binary
mkdir -p "$(PRODUCT_NAME)-$(VERSION)-linux-aarch64"
cp $(LINUX_AARCH64_BUILD_DIR)/$(EXECUTABLE_NAME) "$(PRODUCT_NAME)-$(VERSION)-linux-aarch64/"
cp LICENSE "$(PRODUCT_NAME)-$(VERSION)-linux-aarch64/" 2>/dev/null || true
cp README.md "$(PRODUCT_NAME)-$(VERSION)-linux-aarch64/" 2>/dev/null || true
tar -czf "release/$(PRODUCT_NAME)-$(VERSION)-linux-aarch64.tar.gz" "$(PRODUCT_NAME)-$(VERSION)-linux-aarch64"
rm -rf "$(PRODUCT_NAME)-$(VERSION)-linux-aarch64"
# Generate checksums
cd release && shasum -a 256 *.tar.gz > checksums.txt
@echo "✅ Portable packages created in release/ directory"
# Install binary to system (macOS only)
.PHONY: install
install: build_macos
@echo "Installing $(EXECUTABLE_NAME) to /usr/local/bin..."
sudo cp $(MACOS_BUILD_DIR)/$(EXECUTABLE_NAME) /usr/local/bin/
@echo "✅ $(EXECUTABLE_NAME) installed to /usr/local/bin"
# Uninstall binary from system
.PHONY: uninstall
uninstall:
@echo "Removing $(EXECUTABLE_NAME) from /usr/local/bin..."
sudo rm -f /usr/local/bin/$(EXECUTABLE_NAME)
@echo "✅ $(EXECUTABLE_NAME) removed from /usr/local/bin"
# Development helpers
.PHONY: format
format:
@if command -v swift-format >/dev/null 2>&1; then \
echo "Formatting Swift code..."; \
find Sources Tests -name "*.swift" | xargs swift-format --in-place; \
echo "✅ Code formatted"; \
else \
echo "⚠️ swift-format not available"; \
fi
.PHONY: lint
lint:
@echo "Checking for build warnings..."
swift build $(SWIFT_BUILD_FLAGS) 2>&1 | tee build.log
@if grep -q "warning:" build.log; then \
echo "⚠️ Build warnings found:"; \
grep "warning:" build.log; \
rm -f build.log; \
exit 1; \
else \
echo "✅ No build warnings"; \
rm -f build.log; \
fi
# Release workflow
.PHONY: release
release: clean test lint build_all spm_artifactbundle package
@echo "✅ Release build complete for version $(VERSION)"
@echo "Artifacts created:"
@echo " - SPM artifact bundle: $(PRODUCT_NAME).artifactbundle.zip"
@echo " - Portable packages: release/*.tar.gz"
@echo " - Checksums: release/checksums.txt"
# Help
.PHONY: help
help:
@echo "SwiftDependencyAudit Build System"
@echo ""
@echo "Usage: make [target]"
@echo ""
@echo "Build Targets:"
@echo " build Build for current platform"
@echo " build_macos Build universal macOS binary"
@echo " build_linux Build Linux binaries using development tools (both architectures)"
@echo " build_linux_native Build Linux binary natively (current architecture only)"
@echo " build_all Build all platform binaries"
@echo ""
@echo "Test Targets:"
@echo " test Run Swift tests"
@echo " test_binary Test built binary functionality"
@echo " lint Check for build warnings"
@echo ""
@echo "Package Targets:"
@echo " spm_artifactbundle Create SPM artifact bundle"
@echo " package Create portable binary packages"
@echo " update_package Update Package.swift with new version"
@echo ""
@echo "Install Targets:"
@echo " install Install binary to /usr/local/bin (macOS only)"
@echo " uninstall Remove binary from /usr/local/bin"
@echo ""
@echo "Utility Targets:"
@echo " clean Remove all build artifacts"
@echo " format Format Swift code (requires swift-format)"
@echo " release Complete release build workflow"
@echo " help Show this help message"
@echo ""
@echo "Environment:"
@echo " Platform: $(PLATFORM)"
@echo " Version: $(VERSION)"
@echo ""
@echo "Notes:"
@echo " - CI/CD uses native Linux builds on appropriate runners"
@echo " - For local development, 'build_linux' provides cross-compilation with development tools"
@echo " - 'build_linux_native' requires running on target architecture"