-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
140 lines (114 loc) · 3.95 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
PACKAGE := chip8
# Go defintions
GOCMD ?= go
GOBUILD := $(GOCMD) build
GOINSTALL := $(GOCMD) install
ifndef $(GOPATH)
GOPATH=$(shell go env GOPATH)
export GOPATH
endif
GOARCH := amd64
DLV_PORT := 44571
# Build definitions
BUILD_ENTRY := $(PWD)
BIN_DIR := $(PWD)/bin
# Determine the file extension based on the platform
ifeq ($(OS),Windows_NT)
EXTENSION := .exe
else
EXTENSION :=
endif
# Different platform support
PLATFORMS := linux windows darwin
BINARIES := $(addprefix $(BIN_DIR)/,$(addsuffix /$(PACKAGE)$(EXTENSION),$(PLATFORMS)))
# Fancy colors
BOLD := $(shell tput bold)
ITALIC := \e[3m
YELLOW := $(shell tput setaf 222)
GREEN := $(shell tput setaf 114)
BLUE := $(shell tput setaf 111)
PURPLE := $(shell tput setaf 183)
END := $(shell tput sgr0)
# Function to colorize a command help string
command-style = $(BLUE)$(BOLD)$1$(END) $(ITALIC)$(YELLOW)$2$(END)
define help_text
$(PURPLE)$(BOLD)Targets:$(END)
- $(call command-style,all, Build $(PACKAGE) for all targets (Linux, Windows, Mac, 64-bit))
- $(call command-style,build, Build $(PACKAGE) for current host architecture)
- $(call command-style,run, Build and run $(PACKAGE) for current host)
- $(call command-style,install, Build and install $(PACKAGE) for current host)
- $(call command-style,debug, Run a dlv debug headless session on :$(DLV_PORT))
- $(call command-style,test, Run all Go tests)
- $(call command-style,test-#, Run a numbered ROM test)
- $(call command-style,clean, Delete built artifacts)
- $(call command-style,[help], Print this help)
endef
export help_text
.PHONY: test clean help build all install run debug
help:
@echo -e "$$help_text"
# Select the right binary for the current host
ifeq ($(OS),Windows_NT)
BIN := $(BIN_DIR)/windows/$(PACKAGE)$(EXTENSION)
else
UNAME := $(shell uname -s)
ifeq ($(UNAME),Linux)
BIN := $(BIN_DIR)/linux/$(PACKAGE)
endif
ifeq ($(UNAME),Darwin)
BIN := $(BIN_DIR)/darwin/$(PACKAGE)
endif
endif
SOURCES := $(shell find . -name "*.go")
SOURCES += go.mod go.sum
all: $(BINARIES)
@echo -e "$(GREEN)📦️ Builds are complete: $(END)$(PURPLE)$(BIN_DIR)$(END)"
$(BIN_DIR)/%/$(PACKAGE)$(EXTENSION): $(SOURCES)
@echo -e "$(YELLOW)🚧 Building $@...$(END)"
@CGO_ENABLED=1 GOARCH=$(GOARCH) GOOS=$* $(GOBUILD) -o $@ $(BUILD_ENTRY)
build: $(BIN)
@echo -e "$(GREEN)📦️ Build is complete: $(END)$(PURPLE)$(BIN)$(END)"
clean:
@rm -rf $(BIN_DIR)
@echo -e "$(GREEN)Cleaned!$(END)"
TEST_FILES = $(PWD)/internal/
test:
@echo -e "$(YELLOW)Testing...$(END)"
@go test $(TEST_FILES)
@echo -e "$(GREEN)✅ Test is complete!$(END)"
run: $(BIN)
@exec $? $(LOGO_TEST_FILE)
debug:
@dlv debug --listen ":44571" --headless $(BUILD_ENTRY)
install: $(BIN)
@echo -e "$(YELLOW)🚀 Installing $(BIN) to $(GOPATH)/bin...$(END)"
@$(GOINSTALL) $(BUILD_ENTRY)
@mv $(GOPATH)/bin/chip-8 $(GOPATH)/bin/$(PACKAGE)
@echo -e "$(GREEN)✅ Installation complete!$(END)"
#
# Create targets test-# for each supported ROM test file
#
# Define the list of test files
ROM_FILES := 2-ibm-logo.ch8 3-corax+.ch8 4-flags.ch8 5-quirks.ch8 6-keypad.ch8 7-beep.ch8
# Define the URL to download the file
DOWNLOAD_URL := https://github.com/Timendus/chip8-test-suite/releases/download/v4.1
# Rule to download a test file if it doesn't exist locally
define download_file
$(1):
@if [ ! -f $$@ ]; then \
echo "Downloading $$@"; \
wget -q $(DOWNLOAD_URL)/$$@; \
fi
endef
# Extra flags for the BIN command when testing "5-quirks.ch8"
EXTRA_FLAGS_5_QUIRKS := --cosmac
# Rule to run a test for a specific file
define test_rule
.PHONY: test-$(shell echo $(1) | cut -d'-' -f1)
test-$(shell echo $(1) | cut -d'-' -f1): $(BIN) $(1)
@echo "Running test-$(shell echo $(1) | cut -d'-' -f1) for $(1)"; \
[ "$(1)" = "5-quirks.ch8" ] && CHIP8_THROTTLE_SPEED=60 $$^ $(EXTRA_FLAGS_5_QUIRKS) || $$^
endef
# Create test rules for each test file
$(foreach file,$(ROM_FILES),$(eval $(call download_file,$(file))))
$(foreach file,$(ROM_FILES),$(eval $(call test_rule,$(file))))