forked from urob/zmk-config
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJustfile
More file actions
211 lines (176 loc) · 6.86 KB
/
Justfile
File metadata and controls
211 lines (176 loc) · 6.86 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
default:
@just --list --unsorted
build := absolute_path('.build')
out := absolute_path('firmware')
zmk_config_root := absolute_path(`
if [ -f .west/config ]; then
path=$(awk -F ' *= *' '/^ *path/ {print $2}' .west/config)
file=$(awk -F ' *= *' '/^ *file/ {print $2}' .west/config)
west_yml_path="${path:-.}/${file}"
echo "$(dirname $west_yml_path)/.."
else
echo "."
fi
`)
# parse build.yaml and filter targets by expression
_parse_targets $expr:
#!/usr/bin/env bash
attrs="[.board, .shield, .snippet, .\"artifact-name\"]"
filter="(($attrs | map(. // [.]) | combinations), ((.include // {})[] | $attrs)) | join(\",\")"
echo "$(yq -r "$filter" "{{ zmk_config_root }}/build.yaml" | grep -v "^," | grep -i "${expr/#all/.*}")"
# build firmware for single board & shield combination
_build_single $board $shield $snippet $artifact *west_args:
#!/usr/bin/env bash
set -euo pipefail
artifact="${artifact:-${shield:+${shield// /+}-}${board}}"
# Board ids may contain '/' (e.g. xiao_ble//zmk). Slashes break cp paths and mkdir.
artifact_fs="${artifact//\//-}"
build_dir="{{ build / '$artifact_fs' }}"
echo "Building firmware for $artifact..."
# Check if zephyr/module.yml exists to determine whether to include DZMK_EXTRA_MODULES
if [[ -f "{{ zmk_config_root }}/zephyr/module.yml" ]]; then
west build -s zmk/app -d "$build_dir" -b $board {{ west_args }} ${snippet:+-S "$snippet"} -- \
-DZMK_CONFIG=""{{ zmk_config_root }}/config"" -DZMK_EXTRA_MODULES="{{ zmk_config_root }}" ${shield:+-DSHIELD="$shield"}
else
west build -s zmk/app -d "$build_dir" -b $board {{ west_args }} ${snippet:+-S "$snippet"} -- \
-DZMK_CONFIG=""{{ zmk_config_root }}/config"" ${shield:+-DSHIELD="$shield"}
fi
if [[ -f "$build_dir/zephyr/zmk.uf2" ]]; then
mkdir -p "{{ out }}" && cp "$build_dir/zephyr/zmk.uf2" "{{ out }}/$artifact_fs.uf2"
else
mkdir -p "{{ out }}" && cp "$build_dir/zephyr/zmk.bin" "{{ out }}/$artifact_fs.bin"
fi
# build firmware for matching targets
build expr *west_args:
#!/usr/bin/env bash
set -euo pipefail
targets=$(just _parse_targets {{ expr }})
[[ -z $targets ]] && echo "No matching targets found. Aborting..." >&2 && exit 1
echo "$targets" | while IFS=, read -r board shield snippet artifact; do
just _build_single "$board" "$shield" "$snippet" "$artifact" {{ west_args }}
done
# clear build cache and artifacts
clean:
rm -rf {{ build }} {{ out }}
# clear all automatically generated files
clean-all: clean
rm -rf .west zmk
# clear nix cache
clean-nix:
nix-collect-garbage --delete-old
# initialize west
init *config_path:
#!/usr/bin/env bash
set -euo pipefail
config_path="{{ config_path }}"
# If config_path is provided as argument, use fzf to select it
if [[ -z "$config_path" ]]; then
# Use fzf to select config from config/ and its subdirectories
subdirs=$(find config -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | sort)
candidates=$(printf "config\n"; printf "%s\n" "$subdirs" | sed 's#^#config/#')
config_path=$(echo "$candidates" | fzf \
--prompt="Select ZMK config: " \
--header="Choose a configuration to initialize" \
--preview="ls -1a config/{}")
if [[ -z "$config_path" ]]; then
echo "No config selected. Exiting..."
exit 0
fi
fi
# Determine west.yml path
if [[ -f "$config_path/west.yml" ]]; then
west_yml_abs="$config_path/west.yml"
else
west_yml_abs="$config_path/config/west.yml"
fi
# Convert to path relative to config
west_yml_rel=$(realpath --relative-to=config "$west_yml_abs")
rm -rf .west
west init -l config --mf "$west_yml_rel"
west update --fetch-opt=--filter=blob:none
west zephyr-export
# list build targets
list:
@just _parse_targets all | sed 's/,*$//' | sort | column
# update west
update:
west update --fetch-opt=--filter=blob:none
# upgrade zephyr-sdk and python dependencies
upgrade-sdk:
nix flake update --flake .
# flash firmware for matching targets
flash expr *args:
#!/usr/bin/env bash
set -euo pipefail
# Check if -r option is provided
rebuild=false
build_args=()
for arg in {{ args }}; do
if [[ "$arg" == "-r" ]]; then
rebuild=true
else
build_args+=("$arg")
fi
done
# Rebuild if -r option was provided
if [[ "$rebuild" == "true" ]]; then
echo "Rebuilding before flashing..."
just build "{{ expr }}" "${build_args[@]}"
fi
target=$(just _parse_targets {{ expr }} | head -n 1)
if [[ -z "$target" ]]; then
echo "No matching targets found for expression '{{ expr }}'. Aborting..." >&2
exit 1
fi
IFS=, read -r board shield snippet artifact <<< "$target"
# Use artifact-name if specified, otherwise construct from shield and board
if [[ -n "$artifact" ]]; then
artifact_name="$artifact"
else
artifact_name="${shield:+${shield// /+}-}${board}"
fi
artifact_fs="${artifact_name//\//-}"
uf2_file="$artifact_fs.uf2"
uf2_path="{{ out }}/$uf2_file"
if [[ ! -f "$uf2_path" ]]; then
echo "Firmware file '$uf2_path' not found. Please build it first with 'just build \"{{ expr }}\"'." >&2
exit 1
fi
# macOS
if [[ "$OSTYPE" == "darwin"* ]]; then
echo "Flashing '$uf2_path'..."
./flash.sh "$uf2_path"
# WSL
elif grep -q -i "Microsoft" /proc/version; then
echo "Flashing '$uf2_path'..."
powershell.exe -ExecutionPolicy Bypass -File flash.ps1 -Uf2File "$(wslpath -w $uf2_path)"
# Other: Not supported
else
echo "Flashing '$uf2_path' is not supported on this platform." >&2
exit 1
fi
[no-cd]
test $testpath *FLAGS:
#!/usr/bin/env bash
set -euo pipefail
testcase=$(basename "$testpath")
build_dir="{{ build / "tests" / '$testcase' }}"
config_dir="{{ '$(pwd)' / '$testpath' }}"
cd {{ justfile_directory() }}
if [[ "{{ FLAGS }}" != *"--no-build"* ]]; then
echo "Running $testcase..."
rm -rf "$build_dir"
west build -s zmk/app -d "$build_dir" -b native_posix_64 -- \
-DCONFIG_ASSERT=y -DZMK_CONFIG="$config_dir" \
${ZMK_EXTRA_MODULES:+-DZMK_EXTRA_MODULES="$(realpath ${ZMK_EXTRA_MODULES})"}
fi
${build_dir}/zephyr/zmk.exe | sed -e "s/.*> //" |
tee ${build_dir}/keycode_events.full.log |
sed -n -f ${config_dir}/events.patterns > ${build_dir}/keycode_events.log
if [[ "{{ FLAGS }}" == *"--verbose"* ]]; then
cat ${build_dir}/keycode_events.log
fi
if [[ "{{ FLAGS }}" == *"--auto-accept"* ]]; then
cp ${build_dir}/keycode_events.log ${config_dir}/keycode_events.snapshot
fi
diff -auZ ${config_dir}/keycode_events.snapshot ${build_dir}/keycode_events.log