Skip to content

Commit b11d420

Browse files
committed
Improve build script for clean builds
1 parent 4c4a4ad commit b11d420

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

ax/Makefile

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
1-
# Makefile for ax Swift utility
1+
# Makefile for ax helper
22

3-
# Variables
4-
SWIFT_BUILD_DIR := .build/apple/Products/Release
5-
UNIVERSAL_BINARY_NAME := ax
6-
UNIVERSAL_BINARY_PATH := $(SWIFT_BUILD_DIR)/$(UNIVERSAL_BINARY_NAME)
7-
FINAL_BINARY_PATH := $(CURDIR)/$(UNIVERSAL_BINARY_NAME)
3+
# Define the output binary name
4+
BINARY_NAME = ax
5+
UNIVERSAL_BINARY_PATH = ./$(BINARY_NAME)
6+
RELEASE_BUILD_DIR := ./.build/arm64-apple-macosx/release
7+
RELEASE_BUILD_DIR_X86 := ./.build/x86_64-apple-macosx/release
88

9-
# Default target
10-
all: $(FINAL_BINARY_PATH)
11-
12-
# Build the universal binary, strip it, and place it in the ax/ directory
13-
$(FINAL_BINARY_PATH): $(UNIVERSAL_BINARY_PATH)
14-
@echo "Copying stripped universal binary to $(FINAL_BINARY_PATH)"
15-
@cp $(UNIVERSAL_BINARY_PATH) $(FINAL_BINARY_PATH)
16-
@echo "Final binary ready at $(FINAL_BINARY_PATH)"
9+
# Build for arm64 and x86_64, then lipo them together
10+
# -Xswiftc -Osize: Optimize for size
11+
# -Xlinker -Wl,-dead_strip: Remove dead code
12+
# strip -x: Strip symbol table and debug info
13+
# Ensure old binary is removed first
14+
all:
15+
@echo "Cleaning old binary and build artifacts..."
16+
rm -f $(UNIVERSAL_BINARY_PATH)
17+
swift package clean
18+
@echo "Building for arm64..."
19+
swift build --arch arm64 -c release -Xswiftc -Osize -Xlinker -dead_strip
20+
@echo "Building for x86_64..."
21+
swift build --arch x86_64 -c release -Xswiftc -Osize -Xlinker -dead_strip
22+
@echo "Creating universal binary..."
23+
lipo -create -output $(UNIVERSAL_BINARY_PATH) $(RELEASE_BUILD_DIR)/$(BINARY_NAME) $(RELEASE_BUILD_DIR_X86)/$(BINARY_NAME)
24+
@echo "Stripping symbols from universal binary..."
25+
strip -x $(UNIVERSAL_BINARY_PATH)
26+
@echo "Build complete: $(UNIVERSAL_BINARY_PATH)"
27+
@ls -l $(UNIVERSAL_BINARY_PATH)
28+
@codesign -s - $(UNIVERSAL_BINARY_PATH)
29+
@echo "Codesigned $(UNIVERSAL_BINARY_PATH)"
1730

18-
$(UNIVERSAL_BINARY_PATH):
19-
@echo "Building universal binary for $(UNIVERSAL_BINARY_NAME) (arm64 + x86_64) with size optimization (Osize, dead_strip)..."
20-
@swift build -c release --arch arm64 --arch x86_64 -Xswiftc -Osize -Xlinker -Wl,-dead_strip
21-
@echo "Aggressively stripping symbols (strip -x) from $(UNIVERSAL_BINARY_PATH)..."
22-
@strip -x $(UNIVERSAL_BINARY_PATH)
23-
@echo "Universal binary built and stripped: $(UNIVERSAL_BINARY_PATH)"
2431

2532
clean:
2633
@echo "Cleaning build artifacts..."
27-
@rm -rf $(SWIFT_BUILD_DIR)
28-
@rm -f $(FINAL_BINARY_PATH)
29-
@rm -f $(UNIVERSAL_BINARY_PATH) # Also remove the intermediate universal binary if it exists
34+
swift package clean
35+
rm -f $(UNIVERSAL_BINARY_PATH)
3036
@echo "Clean complete."
3137

32-
.PHONY: all clean
38+
# Default target
39+
.DEFAULT_GOAL := all

0 commit comments

Comments
 (0)