-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
92 lines (81 loc) · 2.68 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
PROJECT = file-organizer
OUTPUT_DIR = build
INSTALL_DIR = /usr/local/bin
# List of OS and architecture pairs to build for
OS_ARCH_PAIRS = \
linux/amd64 \
linux/arm64 \
darwin/amd64 \
darwin/arm64 \
windows/amd64 \
windows/arm64
# List of unsupported OS/ARCH pairs for UPX compression
UNSUPPORTED_UPX_PAIRS = \
darwin/amd64 \
darwin/arm64 \
windows/arm64
.PHONY: all build clean install uninstall help
# Default target
all: clean build
# Determine output name based on OS and architecture
define build_target
OS=$(1); \
ARCH=$(2); \
OUTPUT_NAME=$(OUTPUT_DIR)/$(PROJECT)-$$OS-$$ARCH; \
if [ "$$OS" = "windows" ]; then OUTPUT_NAME=$$OUTPUT_NAME.exe; fi; \
echo "Building $$OUTPUT_NAME..."; \
GOOS=$$OS GOARCH=$$ARCH go build -o $$OUTPUT_NAME; \
$(call compress_target,$$OUTPUT_NAME,$$OS,$$ARCH)
endef
# Compress the binary if supported
define compress_target
OUTPUT_NAME=$(1); \
OS=$(2); \
ARCH=$(3); \
if echo "$(UNSUPPORTED_UPX_PAIRS)" | grep -q "$$OS/$$ARCH"; then \
echo "Skipping UPX compression for $$OUTPUT_NAME (unsupported platform)"; \
else \
echo "Compressing $$OUTPUT_NAME with UPX..."; \
upx --best $$OUTPUT_NAME || echo "UPX compression failed for $$OUTPUT_NAME"; \
fi
endef
# Build the project for all architectures
build:
@echo "Building ${PROJECT} for multiple platforms..."
@mkdir -p $(OUTPUT_DIR)
@for os_arch in $(OS_ARCH_PAIRS); do \
OS=$$(echo $$os_arch | cut -d'/' -f1); \
ARCH=$$(echo $$os_arch | cut -d'/' -f2); \
$(call build_target,$$OS,$$ARCH); \
done
@echo "All builds and compressions completed."
# Clean the built files
clean:
@echo "Cleaning built files..."
@rm -rf $(OUTPUT_DIR)
@echo "Done"
# Install the binary/config locally
install: clean
@echo "Building ${PROJECT} for local installation..."
GOOS=$(shell go env GOOS) GOARCH=$(shell go env GOARCH) go build -o $(OUTPUT_DIR)/$(PROJECT)
@echo "Installing ${PROJECT} to $(INSTALL_DIR)..."
@install -m 755 $(OUTPUT_DIR)/$(PROJECT) $(INSTALL_DIR)/
@mkdir -p ~/.config/${PROJECT}/
@cp ./config/config.json ~/.config/${PROJECT}/
@echo "Installation completed."
# Uninstall the binary/config locally
uninstall:
@echo "Uninstalling ${PROJECT} from $(INSTALL_DIR)..."
@rm -f $(INSTALL_DIR)/$(PROJECT)
@rm -rf ~/.config/${PROJECT}/
@echo "Uninstallation completed."
# Display help screen
help:
@echo "\nUsage: make [target]\n"
@echo "Targets:\n"
@echo " all : Clean and build the project"
@echo " build : Build the project for all architectures and compress with UPX"
@echo " clean : Remove the built executables"
@echo " install : Build and install the project locally"
@echo " uninstall : Uninstall the project locally"
@echo " help : Show this help message\n"