forked from mike1808/zmk-config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
223 lines (203 loc) · 9.7 KB
/
Makefile
File metadata and controls
223 lines (203 loc) · 9.7 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
# ZMK Firmware Local Build Configuration
ZMK_ROOT ?= $(HOME)/path/to/zmk
VENV ?= $(ZMK_ROOT)/.venv
CONFIG_PATH := $(CURDIR)/config
BUILD_BASE := $(ZMK_ROOT)/app/build
BUILD_YAML := $(CURDIR)/build.yaml
UPLOAD_TIMEOUT ?= 60
# External modules directory
MODULES_DIR := $(CURDIR)/modules
WEST_YML := $(CONFIG_PATH)/west.yml
# =============================================================================
# Build targets derived from build.yaml
# Target name format: <first_shield>-<board>
# =============================================================================
BUILDS := $(shell yq -r '.include[] | ((.shield | split(" ") | .[0]) + "-" + .board)' $(BUILD_YAML) 2>/dev/null)
# Group builds by keyboard prefix
HSV_BUILDS := $(filter hillside_view_left% hillside_view_right%,$(BUILDS))
HSV_ENC_BUILDS := $(filter hillside_view_enc_%,$(BUILDS))
# yq filter: select build.yaml entry by target name (set $$target in shell first)
YQ_SELECT = .include[] | select(((.shield | split(\" \") | .[0]) + \"-\" + .board) == \"$$target\")
# =============================================================================
# Phony targets
# =============================================================================
.PHONY: all list help update clean \
hsv/all hsv/left hsv/right hsv/upload/left hsv/upload/right \
hsv-enc/all hsv-enc/left hsv-enc/right hsv-enc/upload/left hsv-enc/upload/right \
modules/setup modules/update modules/clean
WEST_STAMP := $(ZMK_ROOT)/.west-updated
all: $(addprefix build/,$(BUILDS))
# =============================================================================
# build/<first_shield>-<board>
# All parameters (board, shield, cmake-args, snippet) read from build.yaml
# =============================================================================
$(WEST_STAMP): $(WEST_YML)
@echo "west.yml changed, running west update..."
cd $(ZMK_ROOT)/app && . $(VENV)/bin/activate && west update
@touch $(WEST_STAMP)
build/%: $(WEST_STAMP)
@target="$*"; \
eval $$(yq -r "$(YQ_SELECT) | \"board=\" + (.board | @sh) + \" shield=\" + (.shield | @sh) + \" cmake_args=\" + ((.[\"cmake-args\"] // \"\") | @sh) + \" snippet=\" + ((.snippet // \"\") | @sh)" $(BUILD_YAML)); \
if [ -z "$$board" ]; then \
echo "Error: '$$target' not found in $(BUILD_YAML)"; \
echo "Run 'make list' to see available targets"; \
exit 1; \
fi; \
snippet_args=""; \
for s in $$snippet; do snippet_args="$$snippet_args -S $$s"; done; \
echo "Building $$shield ($$board)..."; \
extra_modules=$$(find $(MODULES_DIR) -mindepth 1 -maxdepth 1 -type d 2>/dev/null | paste -sd';'); \
modules="$(CURDIR)$${extra_modules:+;$$extra_modules}"; \
cd $(ZMK_ROOT)/app && \
. $(VENV)/bin/activate && \
west build -p -d "$(BUILD_BASE)/$$target" -b "$$board" $$snippet_args \
-- -DSHIELD="$$shield" -DZMK_CONFIG=$(CONFIG_PATH) \
-DZMK_EXTRA_MODULES="$$modules" $$cmake_args
# =============================================================================
# upload/<first_shield>-<board>
# Detects bootloader device and flashes firmware
# =============================================================================
upload/%:
@target="$*"; \
eval $$(yq -r "$(YQ_SELECT) | \"board=\" + (.board | @sh) + \" shield=\" + (.shield | @sh)" $(BUILD_YAML)); \
if [ -z "$$board" ]; then \
echo "Error: '$$target' not found in $(BUILD_YAML)"; \
echo "Run 'make list' to see available targets"; \
exit 1; \
fi; \
fw="$(BUILD_BASE)/$$target/zephyr/zmk.uf2"; \
if [ ! -f "$$fw" ]; then echo "Error: Firmware not found at $$fw (run build first)"; exit 1; fi; \
case "$$board" in \
nice_nano|nice_nano//zmk) pattern="NICENANO";; \
xiao_ble|xiao_ble//zmk) pattern="XIAO|SEEED";; \
*) echo "Error: No device pattern for board '$$board' - update Makefile"; exit 1;; \
esac; \
echo "Waiting for bootloader device..."; \
echo "Please connect $$shield in bootloader mode"; \
timeout=$(UPLOAD_TIMEOUT); elapsed=0; device=""; \
while [ -z "$$device" ] && [ $$elapsed -lt $$timeout ]; do \
device=$$(lsblk -no NAME,LABEL 2>/dev/null | grep -E "$$pattern" | awk '{print "/dev/"$$1}'); \
[ -z "$$device" ] && sleep 1 && elapsed=$$((elapsed + 1)); \
done; \
if [ -z "$$device" ]; then echo "Error: Device not found after $(UPLOAD_TIMEOUT)s"; exit 1; fi; \
echo "Device: $$device"; \
mount_point=$$(lsblk -no MOUNTPOINT $$device 2>/dev/null | head -1); \
if [ -z "$$mount_point" ]; then \
echo "Mounting device..."; \
mount_point=$$(udisksctl mount -b $$device 2>&1 | grep -oP 'at \K.*'); \
else \
echo "Already mounted"; \
fi; \
if [ -z "$$mount_point" ] || [ ! -d "$$mount_point" ]; then echo "Error: Mount failed"; exit 1; fi; \
echo "Mount point: $$mount_point"; \
until cp "$$fw" "$$mount_point/"; do echo "Retrying..."; sleep 1; done; \
sync; echo "$$shield uploaded successfully"
# =============================================================================
# Aliases
# =============================================================================
hsv/all: $(addprefix build/,$(HSV_BUILDS))
hsv-enc/all: $(addprefix build/,$(HSV_ENC_BUILDS))
hsv/left: build/hillside_view_left-nice_nano//zmk
hsv/right: build/hillside_view_right-nice_nano//zmk
hsv/upload/left: upload/hillside_view_left-nice_nano//zmk
hsv/upload/right: upload/hillside_view_right-nice_nano//zmk
hsv-enc/left: build/hillside_view_enc_left-nice_nano//zmk
hsv-enc/right: build/hillside_view_enc_right-nice_nano//zmk
hsv-enc/upload/left: upload/hillside_view_enc_left-nice_nano//zmk
hsv-enc/upload/right: upload/hillside_view_enc_right-nice_nano//zmk
# =============================================================================
# List available targets
# =============================================================================
list:
@echo "Available builds (from build.yaml):"
@yq -r '.include[] | " build/" + ((.shield | split(" ") | .[0]) + "-" + .board) + "\t" + .shield + " (" + .board + ")"' $(BUILD_YAML) | column -t -s ' '
@echo ""
@echo "Groups:"
@echo " hsv/all Hillside View trackpad builds"
@echo " hsv-enc/all Hillside View encoder builds"
@echo " all All builds"
@echo ""
@echo "Upload: replace 'build/' with 'upload/' (e.g. upload/cygnus_left-nice_nano)"
# =============================================================================
# Maintenance
# =============================================================================
update:
@echo "Updating west dependencies..."
cd $(ZMK_ROOT)/app && . $(VENV)/bin/activate && west update
@touch $(WEST_STAMP)
clean:
@echo "Cleaning build directories..."
@for target in $(BUILDS); do rm -rf "$(BUILD_BASE)/$$target"; done
@echo "Build directories cleaned"
# =============================================================================
# External Modules
# =============================================================================
modules/setup:
@echo "Setting up external modules from $(WEST_YML)..."
@command -v yq >/dev/null 2>&1 || { echo "Error: yq is required. Install with: brew install yq / apt install yq"; exit 1; }
@mkdir -p $(MODULES_DIR)
@yq -r '.manifest | . as $$m | .projects[] | select(.name == "zmk" | not) | . as $$p | $$m.remotes[] | select(.name == $$p.remote) | [$$p.name, .["url-base"], $$p.revision] | @tsv' $(WEST_YML) | while read -r name url_base revision; do \
if [ -d "$(MODULES_DIR)/$$name" ]; then \
echo "$$name already exists"; \
else \
echo "Cloning $$name ($$revision) from $$url_base..."; \
git clone -b $$revision $$url_base/$$name.git $(MODULES_DIR)/$$name || exit 1; \
fi; \
done
@echo "Modules setup complete"
modules/update:
@echo "Updating external modules..."
@if [ ! -d "$(MODULES_DIR)" ]; then echo "Error: Run 'make modules/setup' first"; exit 1; fi
@for dir in $(MODULES_DIR)/*; do \
if [ -d "$$dir/.git" ]; then \
echo "Updating $$(basename $$dir)..."; \
cd $$dir && git pull || echo "Warning: Failed to update $$(basename $$dir)"; \
fi; \
done
@echo "Modules updated"
modules/clean:
@echo "Removing all external modules..."
@rm -rf $(MODULES_DIR)
@echo "Modules removed"
# =============================================================================
# Help
# =============================================================================
help:
@echo "ZMK Firmware Build System"
@echo ""
@echo "Builds are derived from $(BUILD_YAML)"
@echo ""
@echo "Commands:"
@echo " list Show available build targets"
@echo " all Build all targets"
@echo " build/<name> Build a specific target"
@echo " upload/<name> Upload firmware to device"
@echo ""
@echo "Hillside View:"
@echo " hsv/all Build left + right (trackpad)"
@echo " hsv/left Build left (central)"
@echo " hsv/right Build right (peripheral)"
@echo " hsv/upload/left Upload left firmware"
@echo " hsv/upload/right Upload right firmware"
@echo ""
@echo "Hillside View Enc:"
@echo " hsv-enc/all Build left + right (encoders)"
@echo " hsv-enc/left Build left (central)"
@echo " hsv-enc/right Build right (peripheral)"
@echo " hsv-enc/upload/left Upload left firmware"
@echo " hsv-enc/upload/right Upload right firmware"
@echo ""
@echo "Modules:"
@echo " modules/setup Clone external modules from west.yml"
@echo " modules/update Update all cloned modules"
@echo " modules/clean Remove all modules"
@echo ""
@echo "Maintenance:"
@echo " update Update west dependencies"
@echo " clean Remove build artifacts"
@echo ""
@echo "Config: ZMK_ROOT=$(ZMK_ROOT)"
@echo " MODULES_DIR=$(MODULES_DIR)"
@echo ""
@echo "Example: make modules/setup ZMK_ROOT=~/zmk"
@echo " make build/hsv/left upload/hsv/left"