Repository navigation
Expand file tree
/
Copy pathsync-headers.sh
More file actions
executable file
·1676 lines (1476 loc) · 45.9 KB
/
Copy pathsync-headers.sh
File metadata and controls
executable file
·1676 lines (1476 loc) · 45.9 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (C) 2026, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
#
# Sync tools/ header copies with the kernel sources, generate beauty
# string table diffs, verify perf still builds, and create git commits.
set -euo pipefail
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
PERF_DIR=$(cd "$(dirname "$0")/.." && pwd)
KERNEL_DIR=$(cd "$PERF_DIR/../.." && pwd)
DRY_RUN=0
BUILD=1
COMMIT=1
FORCE=0
OUTPUT_DIR="${OUTPUT_DIR:-${TMPDIR:-/tmp}/build/perf-tools-$(id -u)}"
SIGN_OFF_NAME="${SIGN_OFF_NAME:-}"
SIGN_OFF_EMAIL="${SIGN_OFF_EMAIL:-}"
HEADER_FILTER=""
VERBOSE=0
# Track build successes and failures for end-of-run summary
declare -a SYNCED_OK=()
declare -a BUILD_FAILED=()
declare -a COPY_FAILED=()
declare -a COMMIT_FAILED=()
declare -a TOOLS_AHEAD=()
declare -a HAND_SYNC=()
declare -a MANUAL=()
declare -a FORCED=()
info() { echo -e "${GREEN}==>${NC} $*"; }
warn() { echo -e "${YELLOW}Warning:${NC} $*"; }
err() { echo -e "${RED}Error:${NC} $*" >&2; }
detail(){ [[ $VERBOSE -eq 1 ]] && echo -e " $*"; return 0; }
usage() {
local status="${1:-0}"
cat <<EOF
Usage: $(basename "$0") [OPTIONS]
Sync tools/ header copies with the kernel sources and create commits.
Options:
-n, --dry-run Show what would be done without making changes
-N, --no-build Skip the perf build verification step
-C, --no-commit Copy files but do not create git commits
-f, --filter PAT Only process headers matching PAT (substring match
against the kernel-side path)
-F, --force Sync also when both copies have content the other
lacks, after manual investigation; tools/ additions
dropped are recorded in the commit
-o, --output DIR Build output directory (default: $OUTPUT_DIR)
-v, --verbose Show more details
-h, --help Show this help
Environment:
SIGN_OFF_NAME Override git user.name for Signed-off-by
SIGN_OFF_EMAIL Override git user.email for Signed-off-by
OUTPUT_DIR Build output directory
Through make:
SYNC_HEADERS_ARGS Options to pass to this script, e.g.:
make -C tools/perf sync-headers SYNC_HEADERS_ARGS='-n'
The script:
1. Runs 'make -C tools/perf check-headers' to find stale copies.
2. For each stale header, identifies the kernel commits that changed it,
including the merges that brought content of their own.
3. Copies the kernel header into the tools/ tree, skipping it when only
the tools/ copy changed (it is ahead) or when it carries intentional
edits, and stopping for manual investigation when both sides have
content the other lacks.
4. For beauty headers, runs the relevant beauty script(s) before and
after, showing new/changed string table entries.
5. Builds perf and the other consumers of the header to verify nothing
breaks.
6. Creates a git commit per header (or group) with the standard commit
message format.
EOF
exit "$status"
}
while [[ $# -gt 0 ]]; do
case "$1" in
-n|--dry-run) DRY_RUN=1; shift ;;
-N|--no-build) BUILD=0; shift ;;
-C|--no-commit) COMMIT=0; shift ;;
-f|--filter)
[[ $# -ge 2 ]] || { err "$1 needs an argument"; exit 1; }
HEADER_FILTER="$2"; shift 2 ;;
-F|--force) FORCE=1; shift ;;
-o|--output)
[[ $# -ge 2 ]] || { err "$1 needs an argument"; exit 1; }
OUTPUT_DIR="$2"; shift 2 ;;
-v|--verbose) VERBOSE=1; shift ;;
-h|--help) usage ;;
*) err "Unknown option: $1"; usage 1 ;;
esac
done
# make -C resolves a relative O= from the directory it changes to, so pin
# the build directory down before main() moves into the kernel tree.
case "$OUTPUT_DIR" in
/*) ;;
*) OUTPUT_DIR="$PWD/$OUTPUT_DIR" ;;
esac
sign_off() {
local name="${SIGN_OFF_NAME:-$(git config user.name)}"
local email="${SIGN_OFF_EMAIL:-$(git config user.email)}"
echo "Signed-off-by: $name <$email>"
}
# --- Detect stale headers ------------------------------------------------
detect_stale_headers() {
local tmpfile
tmpfile=$(mktemp)
# check-headers.sh must be run from tools/perf/ (it does cd ../.. internally)
(cd "$PERF_DIR" && bash check-headers.sh 2>/dev/null) | \
grep '^ *diff -u ' | \
sed 's/^ *diff -u //' > "$tmpfile"
if [[ ! -s "$tmpfile" ]]; then
rm -f "$tmpfile"
return 1
fi
cat "$tmpfile"
rm -f "$tmpfile"
return 0
}
# --- Classify a header ---------------------------------------------------
#
# Returns via globals:
# HDR_CLASS: "files" | "beauty"
classify_header() {
local tools_path="$1"
if [[ "$tools_path" == tools/perf/trace/beauty/* ]]; then
HDR_CLASS="beauty"
else
HDR_CLASS="files"
fi
}
# --- Find kernel commits that changed a header since our copy ------------
# Copies checked with -I or hunk-ignore rules in check-headers.sh carry
# intentional edits, so a sync commit never leaves the two blobs equal.
has_ignore_rules() {
local kernel_path="$1"
local check_headers="$PERF_DIR/check-headers.sh"
grep -Eq "^[[:space:]]*(check[[:space:]]+${kernel_path}[[:space:]]+.*-I|check_ignore_some_hunks[[:space:]]+${kernel_path}([[:space:]]|$))" \
"$check_headers" 2>/dev/null && return 0
sed -n '/^declare -a SYNC_CHECK_FILES=/,/)/p' "$check_headers" 2>/dev/null | \
grep -Fq "\"$kernel_path\""
}
# The last commit that left the two copies equal: an in-tree kernel commit
# can update both files while an earlier kernel change is still missing.
find_sync_point() {
local kernel_path="$1"
local tools_path="$2"
local -a candidates blobs
local i
# Files with ignore rules carry intentional edits and will never have
# identical blobs; fall back to the last commit touching tools/.
if ! has_ignore_rules "$kernel_path"; then
mapfile -t candidates < <(git log --no-merges --format='%H' -- "$kernel_path" "$tools_path" 2>/dev/null)
mapfile -t blobs < <(
printf '%s\n' "${candidates[@]}" | \
awk -v k="$kernel_path" -v t="$tools_path" '{ print $1 ":" k ORS $1 ":" t }' | \
git cat-file --batch-check)
for ((i = 0; i < ${#candidates[@]}; i++)); do
# "<spec> missing" keeps the ':' of the spec as its first field,
# which no object id has.
if [[ "${blobs[2*i]%% *}" == "${blobs[2*i+1]%% *}" && \
"${blobs[2*i]%% *}" != *:* ]]; then
echo "${candidates[i]}"
return
fi
done
fi
git log --no-merges --format='%H' -1 -- "$tools_path" 2>/dev/null
}
# A kernel commit that changed both the header and the tools copy already
# carried its change into tools/, so it has nothing left to pick up here.
find_kernel_commits() {
local kernel_path="$1"
local tools_path="$2"
local sync_point="$3"
local copied sha
copied=$(git log --no-merges --format='%H' \
"${sync_point}..HEAD" -- "$tools_path" 2>/dev/null)
[[ -n "$copied" ]] && copied+=$'\n'
while IFS= read -r sha; do
[[ "$copied" == *"$sha"$'\n'* ]] && continue
echo "$sha"
done < <(git log --no-merges --format='%H' \
"${sync_point}..HEAD" -- "$kernel_path" 2>/dev/null)
}
# The lines the kernel copy has and the tools/ copy lacks, as "start,end"
# ranges on the kernel side.
added_line_ranges() {
local kernel_path="$1"
local tools_path="$2"
local out
out=$(diff -U0 "$tools_path" "$kernel_path" 2>/dev/null) || true
printf '%s\n' "$out" | awk '
/^@@/ {
split($3, a, ",");
start = substr(a[1], 2);
count = (a[2] == "" ? 1 : a[2]);
if (count > 0)
print start "," start + count - 1;
}'
}
# A merge that resolves a conflict brings content of its own, e.g. a
# capability renumbered to avoid clashing with another one; git log
# --no-merges does not see those, so credit the merge with the lines it
# introduced.
merge_pickup_commits() {
local kernel_path="$1"
local tools_path="$2"
local sync_point="$3"
local range sha
local -a blame_args=()
local -a shas=()
[[ -n "$sync_point" ]] || return 0
while read -r range; do
[[ -z "$range" ]] && continue
blame_args+=(-L "$range")
done < <(added_line_ranges "$kernel_path" "$tools_path")
if [[ ${#blame_args[@]} -eq 0 ]]; then
return 0
fi
# Blame only what changed since the last sync, the lines older than
# it come out as the boundary commit and are not to be picked up.
while read -r sha; do
[[ -z "$sha" ]] && continue
shas+=("$sha")
done < <(git blame --porcelain "${sync_point}..HEAD" \
"${blame_args[@]}" -- "$kernel_path" 2>/dev/null |
awk '
/^[0-9a-f]{40} / {
sha = $1;
if (sha !~ /^0+$/)
order[++n] = sha;
next;
}
/^boundary$/ {
skip[sha] = 1;
next;
}
END {
for (i = 1; i <= n; i++)
if (!(order[i] in skip))
print order[i];
}')
if [[ ${#shas[@]} -eq 0 ]]; then
return 0
fi
printf '%s\n' "${shas[@]}" | awk 'NF && !seen[$0]++' |
git rev-list --no-walk=unsorted --stdin --min-parents=2 2>/dev/null
}
# Render commit ids with FORMAT at the usual 12 chars, keeping their order.
format_commits() {
local format="$1"
local ids="$2"
[[ -n "$ids" ]] || return 0
printf '%s\n' "$ids" | git log --no-walk=unsorted --abbrev=12 \
--format="$format" --stdin 2>/dev/null
}
# "add" when only the kernel changed since the sync point, "delete" when
# only the tools/ copy did, "both" when each side did; the sync point is
# where the blobs were equal, so comparing them is exact.
sync_direction() {
local kernel_path="$1"
local tools_path="$2"
local sync_point="$3"
local tools_base kernel_base tools_now kernel_now
[[ -n "$sync_point" ]] || { echo add; return; }
tools_base=$(git -C "$KERNEL_DIR" rev-parse -q --verify "$sync_point:$tools_path") || {
echo add
return
}
kernel_base=$(git -C "$KERNEL_DIR" rev-parse -q --verify "$sync_point:$kernel_path") || {
echo add
return
}
tools_now=$(git -C "$KERNEL_DIR" hash-object --path="$tools_path" "$tools_path") || {
echo add
return
}
kernel_now=$(git -C "$KERNEL_DIR" hash-object --path="$kernel_path" "$kernel_path") || {
echo add
return
}
if [[ "$tools_now" == "$tools_base" ]]; then
echo add # only the kernel moved
elif [[ "$kernel_now" == "$kernel_base" ]]; then
echo delete # only tools/ moved, it is ahead
else
echo both
fi
}
# A clean merge that matches the kernel means the tools/ side changed only
# what the kernel already has, so a copy drops nothing unique.
merge_matches_kernel() {
local kernel_path="$1"
local tools_path="$2"
local sync_point="$3"
local base merged status
[[ -n "$sync_point" ]] || return 1
base=$(mktemp) || return 1
merged=$(mktemp) || { rm -f "$base"; return 1; }
if ! git -C "$KERNEL_DIR" show "$sync_point:$tools_path" > "$base" 2>/dev/null; then
rm -f "$base" "$merged"
return 1
fi
if ! git merge-file -p "$KERNEL_DIR/$tools_path" "$base" \
"$KERNEL_DIR/$kernel_path" > "$merged" 2>/dev/null; then
rm -f "$base" "$merged"
return 1
fi
cmp -s "$merged" "$KERNEL_DIR/$kernel_path"
status=$?
rm -f "$base" "$merged"
return $status
}
# The additions only the tools/ copy added since the sync point, what a
# forced sync drops; diffing against the sync-point blob gives it exactly.
tools_only_lines() {
local tools_path="$1"
local sync_point="$2"
local line
[[ -n "$sync_point" ]] || return 0
git -C "$KERNEL_DIR" rev-parse -q --verify "$sync_point:$tools_path" \
>/dev/null || return 0
while IFS= read -r line; do
case "$line" in
---*|+++*) continue ;;
-*) echo " $line" ;;
esac
done < <(git -C "$KERNEL_DIR" cat-file blob "$sync_point:$tools_path" | \
diff -u "$KERNEL_DIR/$tools_path" - 2>/dev/null)
}
# --- Map a kernel header path to its beauty script(s) --------------------
#
# The mapping comes from the beauty Build files: a script reads the
# headers listed as its prerequisites and the directories passed as its
# arguments. The header name still has to appear in the script so that
# one reading a whole tree, like syscalltbl.sh, doesn't match everything.
declare -A BEAUTY_MAKE_DIRS=()
declare -A BEAUTY_MAKE_SCRIPTS=()
declare -A BEAUTY_SCRIPT_ARGS=()
declare -A BEAUTY_SCRIPT_PATH=()
declare -A BEAUTY_SCRIPT_ROOTS=()
declare -A BEAUTY_SCRIPT_FILES=()
declare -a BEAUTY_SCRIPTS=()
BEAUTY_ARCH=""
beauty_add_script() {
local script="$1"
[[ -n "$script" ]] || return 0
[[ -n "${BEAUTY_SCRIPT_FILES[$script]+x}" ]] && return 0
BEAUTY_SCRIPT_FILES["$script"]=""
BEAUTY_SCRIPT_ROOTS["$script"]=""
BEAUTY_SCRIPTS+=("$script")
}
find_beauty_scripts() {
local kernel_path="$1"
local tools_path="$2"
local header_basename script script_path root matched
header_basename=$(basename "$kernel_path")
for script in "${BEAUTY_SCRIPTS[@]+"${BEAUTY_SCRIPTS[@]}"}"; do
script_path="${BEAUTY_SCRIPT_PATH[$script]:-$script}"
matched=0
case $'\n'"${BEAUTY_SCRIPT_FILES[$script]:-}"$'\n' in
*$'\n'"$tools_path"$'\n'*) matched=1 ;;
esac
if [[ $matched -eq 0 ]]; then
while IFS= read -r root; do
[[ -n "$root" ]] || continue
[[ "$tools_path" == "$root" || \
"$tools_path" == "$root"/* ]] || continue
if grep -F -q -e "/$header_basename" \
-e "\"$header_basename\"" \
"$KERNEL_DIR/$script_path" 2>/dev/null; then
matched=1
break
fi
done <<< "${BEAUTY_SCRIPT_ROOTS[$script]:-}"
fi
[[ $matched -eq 1 ]] && echo "$script_path"
done
}
# --- Find buildable targets that consume a header -------------------------
#
# Given a tools/ header path, grep tools/ source files to find which
# buildable directories include it, then return them as build commands.
# This is discovered dynamically so new consumers are picked up automatically.
# C sources include tools/ headers by their include-relative path, e.g.
# <linux/socket.h> for tools/include/uapi/linux/socket.h.
include_relative_path() {
local tools_path="$1"
case "$tools_path" in
tools/include/uapi/*)
echo "${tools_path#tools/include/uapi/}"
;;
tools/include/*)
echo "${tools_path#tools/include/}"
;;
tools/arch/*/include/uapi/*)
echo "${tools_path#tools/arch/*/include/uapi/}"
;;
tools/arch/*/include/*)
echo "${tools_path#tools/arch/*/include/}"
;;
*)
basename "$tools_path"
;;
esac
}
# Map from a source file path to its buildable target directory and make command.
# Returns empty if the file doesn't belong to a known buildable target.
buildable_target() {
local src="$1"
case "$src" in
tools/perf/*)
echo "perf:$PERF_DIR"
;;
tools/lib/bpf/*)
echo "libbpf:$KERNEL_DIR/tools/lib/bpf"
;;
tools/bpf/bpftool/*)
echo "bpftool:$KERNEL_DIR/tools/bpf/bpftool"
;;
tools/objtool/*)
echo "objtool:$KERNEL_DIR/tools/objtool"
;;
tools/lib/subcmd/*)
echo "libsubcmd:$KERNEL_DIR/tools/lib/subcmd"
;;
tools/lib/api/*)
echo "libapi:$KERNEL_DIR/tools/lib/api"
;;
tools/lib/perf/*)
echo "libperf:$KERNEL_DIR/tools/lib/perf"
;;
tools/lib/symbol/*)
echo "libsymbol:$KERNEL_DIR/tools/lib/symbol"
;;
tools/tracing/*)
echo "tracing:$KERNEL_DIR/tools/tracing"
;;
esac
}
find_build_targets() {
local tools_path="$1"
local header_class="$2"
local include_path
local targets=()
local seen=()
# Beauty copies are only on the include path of the beauty scripts.
if [[ "$header_class" == "beauty" ]]; then
echo "perf:$PERF_DIR"
return
fi
include_path=$(include_relative_path "$tools_path")
while IFS= read -r src; do
[[ -z "$src" ]] && continue
local target
target=$(buildable_target "$src")
[[ -z "$target" ]] && continue
# Deduplicate
local already=0
for s in "${seen[@]+"${seen[@]}"}"; do
if [[ "$s" == "$target" ]]; then
already=1
break
fi
done
if [[ $already -eq 0 ]]; then
seen+=("$target")
targets+=("$target")
fi
done < <(grep -rlF -e "<$include_path>" -e "\"$include_path\"" -e "/$include_path" \
--include='*.c' --include='*.h' --include='*.S' \
"$KERNEL_DIR/tools/" 2>/dev/null | \
sed "s|^$KERNEL_DIR/||" | grep -v '^tools/include/')
# Always include perf
local has_perf=0
for t in "${targets[@]+"${targets[@]}"}"; do
[[ "$t" == perf:* ]] && has_perf=1
done
if [[ $has_perf -eq 0 ]]; then
targets+=("perf:$PERF_DIR")
fi
printf '%s\n' "${targets[@]}"
}
# Print the consumers that will be built for a header or group.
show_build_targets() {
local what="$1"
local target_list="$2"
info "Build-testing consumers of $what:"
while IFS=: read -r name dir; do
[[ -z "$name" ]] && continue
echo " $name ($dir)"
done <<< "$target_list"
}
# Keep build output out of the source tree; perf, tracing and objtool take
# O=, the libraries and bpftool take OUTPUT=.
build_target() {
local name="$1"
local dir="$2"
case "$name" in
perf)
make -C "$dir" O="$OUTPUT_DIR" -j"$(nproc)"
;;
tracing|objtool)
(umask 077 && mkdir -p "$OUTPUT_DIR/$name")
make -C "$dir" O="$OUTPUT_DIR/$name" -j"$(nproc)"
;;
*)
(umask 077 && mkdir -p "$OUTPUT_DIR/$name")
make -C "$dir" OUTPUT="$OUTPUT_DIR/$name/" -j"$(nproc)"
;;
esac
}
# Run build tests for all targets that consume a header.
# Returns 0 if all builds pass, 1 if any fail.
build_test_targets() {
local tools_path="$1"
local header_class="$2"
local header_basename log_name
local target_list failed=0
header_basename=$(basename "$tools_path")
# Key the log by tools path, same-basename headers in different
# directories would otherwise share a log.
log_name="${tools_path//\//-}"
target_list=$(find_build_targets "$tools_path" "$header_class")
show_build_targets "$header_basename" "$target_list"
while IFS=: read -r name dir; do
[[ -z "$name" ]] && continue
local log="$OUTPUT_DIR/$name.$log_name.log"
if ! build_target "$name" "$dir" > "$log" 2>&1; then
err " $name build failed, see $log"
failed=1
fi
done <<< "$target_list"
return $failed
}
# --- Run a beauty script capturing output --------------------------------
#
# The arguments come from the same Build files the build uses, so a new
# script or a changed directory needs no edit here; run bare,
# arch_errno_names.sh misses the header copies.
# Resolve a Build variable token, $(srctree)/path or $(dir)/suffix, to a
# tree-relative path.
beauty_resolve_token() {
local token="$1"
local name suffix
case "$token" in
'$(srctree)'*)
suffix="${token//\$(srctree)/}"
echo "${suffix#/}"
;;
'$('*')'*)
name="${token#\$(}"
name="${name%%)*}"
suffix="${token#*\)}"
[[ -n "${BEAUTY_MAKE_DIRS[$name]:-}" ]] || return 1
printf '%s\n' "${BEAUTY_MAKE_DIRS[$name]}$suffix" | \
sed "s/\$(SRCARCH)/$BEAUTY_ARCH/g"
;;
*)
return 1
;;
esac
}
# Collect, from one Build file, the script each rule runs with its
# arguments and the headers it takes as prerequisites.
parse_beauty_build() {
local build="$1"
local -a rule_lines=()
local line last_rule="" var script args token value roots files needle
local name path re_dir re_tbl
[[ -r "$build" ]] || return 0
# _dir :=, _tbl := assignments, and the $(SHELL) rules using them.
re_dir='^([A-Za-z0-9_]+_(dir|dirs))[[:space:]]*(:=|\?=)[[:space:]]*'
re_dir+='\$\(srctree\)/(.*)$'
re_tbl='^([A-Za-z0-9_]+_(tbl|tbls))[[:space:]]*(:=|\?=)[[:space:]]*'
re_tbl+='\$\(srctree\)/((.*\.sh))[[:space:]]*$'
while IFS= read -r line; do
if [[ "$line" =~ $re_dir ]]; then
BEAUTY_MAKE_DIRS["${BASH_REMATCH[1]}"]=$(printf '%s' \
"${BASH_REMATCH[4]%/}" | \
sed "s/\$(SRCARCH)/$BEAUTY_ARCH/g")
elif [[ "$line" =~ $re_tbl ]]; then
BEAUTY_MAKE_SCRIPTS["${BASH_REMATCH[1]}"]="${BASH_REMATCH[4]}"
elif [[ "$line" == *'$(SHELL)'* && -n "$last_rule" ]]; then
rule_lines+=("$last_rule"$'\n'"$line")
elif [[ "$line" != '#'* && "$line" != $'\t'* && \
"$line" == *:* && "$line" != *':='* && \
"$line" != *'?='* && "$line" != *'+='* ]]; then
last_rule="$line"
fi
done < "$build"
local rule prereqs command
for rule in "${rule_lines[@]+"${rule_lines[@]}"}"; do
prereqs="${rule%%$'\n'*}"
command="${rule#*$'\n'}"
script=""
for var in "${!BEAUTY_MAKE_SCRIPTS[@]}"; do
needle="'\$($var)'"
if [[ "$command" == *"$needle"* ]]; then
script="${BEAUTY_MAKE_SCRIPTS[$var]##*/}"
BEAUTY_SCRIPT_PATH["$script"]="${BEAUTY_MAKE_SCRIPTS[$var]}"
args="${command#*"$needle"}"
break
fi
done
[[ -n "$script" ]] || continue
beauty_add_script "$script"
args="${args//\'}"
args=$(printf '%s\n' "$args" | \
sed 's/\$(patsubst -%,,\$(CC))/gcc/g')
args="${args%' > $@'}"
value=""
roots=""
for token in $args; do
case "$token" in
'$@')
token='@OUTFILE@'
;;
'$(srctree)'*|'$('*')'*)
path=$(beauty_resolve_token "$token") || {
value=""
break
}
case "$token" in
'$(srctree)'*)
roots+="${path}"$'\n'
;;
*)
name="${token#\$(}"
name="${name%%)*}"
roots+="${BEAUTY_MAKE_DIRS[$name]:-}"$'\n'
;;
esac
token="$KERNEL_DIR/$path"
;;
*) ;;
esac
token=$(printf '%s' "$token" | sed "s/\$(SRCARCH)/$BEAUTY_ARCH/g")
if [[ "$token" == *'$('* ]]; then
value=""
break
fi
value+="${value:+ }$token"
done
BEAUTY_SCRIPT_ROOTS["$script"]+="$roots"
[[ -n "$value" ]] && BEAUTY_SCRIPT_ARGS["$script"]="$value"
prereqs="${prereqs#*:}"
files=""
for token in $prereqs; do
path=$(beauty_resolve_token "$token") || continue
[[ "$path" == *'*'* ]] && continue
[[ -n "$path" ]] && files+="$path"$'\n'
done
BEAUTY_SCRIPT_FILES["$script"]+="$files"
done
}
setup_beauty_args() {
local build found=0
BEAUTY_ARCH=$(uname -m)
case "$BEAUTY_ARCH" in
x86_64|i[3-6]86) BEAUTY_ARCH=x86 ;;
aarch64|arm64) BEAUTY_ARCH=arm64 ;;
s390*) BEAUTY_ARCH=s390 ;;
riscv*) BEAUTY_ARCH=riscv ;;
loongarch*) BEAUTY_ARCH=loongarch ;;
ppc*) BEAUTY_ARCH=powerpc ;;
sparc*) BEAUTY_ARCH=sparc ;;
mips*) BEAUTY_ARCH=mips ;;
esac
for build in "$PERF_DIR/trace/beauty/Build" \
"$PERF_DIR/trace/beauty/tracepoints/Build"; do
[[ -r "$build" ]] || continue
parse_beauty_build "$build"
found=1
done
[[ $found -eq 1 ]] || \
detail "No beauty Build file, running the scripts without arguments"
}
run_beauty_script() {
local script="$1"
local -a cmd=()
local outfile="" i
read -ra cmd <<< "${BEAUTY_SCRIPT_ARGS["${script##*/}"]:-}"
for ((i = 0; i < ${#cmd[@]}; i++)); do
if [[ "${cmd[$i]}" == '@OUTFILE@' ]]; then
outfile=$(mktemp) || continue
cmd[$i]="$outfile"
fi
done
bash "$script" "${cmd[@]+"${cmd[@]}"}" 2>/dev/null || true
if [[ -n "$outfile" ]]; then
cat "$outfile" 2>/dev/null || true
rm -f "$outfile"
fi
}
# Run one beauty script against the kernel content swapped into the tools
# copy, capturing its output before and after; the saved copy restores the
# tools copy, also when the script is interrupted.
run_beauty_pass() {
local script="$1"
local kernel_path="$2"
local tools_path="$3"
local tmpdir="$4"
local idx="$5"
local before="$tmpdir/beauty.$idx.before"
local after="$tmpdir/beauty.$idx.after"
local saved="$tmpdir/beauty.$idx.saved"
local restore
if ! cp -p "$KERNEL_DIR/$tools_path" "$saved"; then
err "Failed to save $tools_path"
return 1
fi
restore=$(printf 'cp -p %q %q' "$saved" "$KERNEL_DIR/$tools_path")
# shellcheck disable=SC2064 # expand now, locals may be gone
trap "$restore" EXIT
# shellcheck disable=SC2064 # expand now, locals may be gone
trap "$restore; trap - EXIT; exit 130" INT
# shellcheck disable=SC2064 # expand now, locals may be gone
trap "$restore; trap - EXIT; exit 143" TERM
(cd "$KERNEL_DIR" && run_beauty_script "$script") > "$before"
if ! cp "$KERNEL_DIR/$kernel_path" "$KERNEL_DIR/$tools_path"; then
err "Failed to copy $kernel_path -> $tools_path"
cp -p "$saved" "$KERNEL_DIR/$tools_path" || \
err "Failed to restore $tools_path"
trap - INT TERM EXIT
return 1
fi
(cd "$KERNEL_DIR" && run_beauty_script "$script") > "$after"
if ! cp -p "$saved" "$KERNEL_DIR/$tools_path"; then
err "Failed to restore $tools_path"
trap - INT TERM EXIT
return 1
fi
trap - INT TERM EXIT
}
# The tools/perf/trace/beauty/ relative path of a beauty script, as shown
# in the commit messages.
beauty_script_relpath() {
echo "tools/perf/trace/beauty/${1##*/trace/beauty/}"
}
# The commit message fragment for a beauty script whose table changed:
# the recipe the pre-pass ran and the difference between its two runs.
beauty_body_fragment() {
local script="$1"
local kernel_path="$2"
local tools_path="$3"
local before="$4"
local after="$5"
local rel_script args
rel_script=$(beauty_script_relpath "$script")
# The runs are from the tree root, show the dirs relative to it.
args="${BEAUTY_SCRIPT_ARGS["${script##*/}"]:-}"
args="${args//"$KERNEL_DIR/"/}"
local before_args="$args" after_args="$args"
local before_tail=" > before.txt" after_tail=" > after.txt"
# Scripts taking the output file as an argument write it themselves.
if [[ "$args" == *@OUTFILE@* ]]; then
before_args="${args//@OUTFILE@/out.before}"
after_args="${args//@OUTFILE@/out.after}"
before_tail=""
after_tail=""
fi
printf '\n'
printf ' $ %s%s%s\n' "$rel_script" "${before_args:+ $before_args}" "$before_tail"
printf ' $ cp %s %s\n' "$kernel_path" "$tools_path"
printf ' $ %s%s%s\n' "$rel_script" "${after_args:+ $after_args}" "$after_tail"
if [[ "$args" == *@OUTFILE@* ]]; then
printf ' $ diff -u out.before out.after\n'
else
printf ' $ diff -u before.txt after.txt\n'
fi
printf '\n'
diff -u "$before" "$after" | tail -n +3 | sed 's/^/ /'
printf '\n'
}
cleanup_tmpdir() {
[[ -n "$1" ]] || return 0
rm -rf "$1"
}
# --- Copy header and create commit ---------------------------------------
process_header() {
local tools_path="$1"
local kernel_path="$2"
classify_header "$tools_path"
if [[ -n "$HEADER_FILTER" ]] && [[ "$kernel_path" != *"$HEADER_FILTER"* ]]; then
detail "Skipping $kernel_path (does not match filter)"
return
fi
info "Processing: $kernel_path"
detail " Class: $HDR_CLASS"
detail " Tools: $tools_path"
detail " Kernel: $kernel_path"
# Find kernel commits that changed this file
local sync_point pickup_commits=""
sync_point=$(find_sync_point "$kernel_path" "$tools_path")
if [[ -n "$sync_point" ]]; then
pickup_commits=$(find_kernel_commits "$kernel_path" \
"$tools_path" "$sync_point")
else
warn "No previous sync commit found for $tools_path"
fi
local kernel_commits=""
if [[ -n "$pickup_commits" ]]; then
kernel_commits=$(format_commits ' %h ("%s")' "$pickup_commits")
fi
# A merge can bring content of its own; list it too, it is not the
# author of a change, so it gets no Cc.
local merge_commits=""
merge_commits=$(merge_pickup_commits "$kernel_path" "$tools_path" \
"$sync_point")
if [[ -n "$merge_commits" ]]; then
if [[ -n "$kernel_commits" ]]; then
kernel_commits+=$'\n'
fi
kernel_commits+=$(format_commits ' %h ("%s")' "$merge_commits")
fi
if [[ -n "$kernel_commits" ]]; then
echo ""
echo " Kernel commits since last sync:"
echo "$kernel_commits"
fi
# Copies with ignore rules carry deliberate edits that a plain copy
# would drop; leave them for a hand sync, printing the Cc list for them.
if has_ignore_rules "$kernel_path"; then
local cc_lines=""
if [[ -n "$pickup_commits" ]]; then
cc_lines=$(format_commits 'Cc: %an <%ae>' "$pickup_commits" | sort -u)
if [[ -n "$cc_lines" ]]; then
echo ""
echo "$cc_lines" | sed 's/^/ /'
fi
echo ""
fi
warn "$tools_path carries intentional edits, sync by hand"
HAND_SYNC+=("$tools_path")
return
fi
# The kernel is the reference, but only copy when that doesn't drop
# content only the tools/ copy has.
local direction dropped="" forced=0
direction=$(sync_direction "$kernel_path" "$tools_path" "$sync_point")
case "$direction" in
delete)
warn "$tools_path is ahead of the kernel sources, leaving it alone"
TOOLS_AHEAD+=("$tools_path")
return
;;
both)
if merge_matches_kernel "$kernel_path" "$tools_path" "$sync_point"; then
detail "Tools/ changes are already in the kernel"
elif [[ $FORCE -eq 0 ]]; then
err "$tools_path and $kernel_path both have content the other lacks, needs manual investigation"
MANUAL+=("$tools_path")
return
else
warn "Both copies have unique content, dropping the tools/ one (--force)"
dropped=$(tools_only_lines "$tools_path" "$sync_point")
forced=1
fi
;;
esac
# For beauty files, capture the string table changes and keep the
# before/after files for the commit message.
local beauty_changed=0
local beauty_tmpdir=""
local beauty_scripts
local -a beauty_script_list=()
beauty_scripts=$(find_beauty_scripts "$kernel_path" "$tools_path")
while IFS= read -r script; do
[[ -z "$script" ]] && continue
beauty_script_list+=("$script")
done <<< "$beauty_scripts"
if [[ ${#beauty_script_list[@]} -gt 0 ]]; then
beauty_tmpdir=$(mktemp -d)
local idx=0
for script in "${beauty_script_list[@]}"; do
local script_name
script_name=$(basename "$script")
local before="$beauty_tmpdir/beauty.$idx.before"
local after="$beauty_tmpdir/beauty.$idx.after"