xref: /aosp_15_r20/external/libbpf/scripts/sync-kernel.sh (revision f7c14bbac8cf49633f2740db462ea43457973ec4)
1*f7c14bbaSAndroid Build Coastguard Worker#!/bin/bash
2*f7c14bbaSAndroid Build Coastguard Worker
3*f7c14bbaSAndroid Build Coastguard Workerusage () {
4*f7c14bbaSAndroid Build Coastguard Worker	echo "USAGE: ./sync-kernel.sh <libbpf-repo> <kernel-repo> <bpf-branch>"
5*f7c14bbaSAndroid Build Coastguard Worker	echo ""
6*f7c14bbaSAndroid Build Coastguard Worker	echo "Set BPF_NEXT_BASELINE to override bpf-next tree commit, otherwise read from <libbpf-repo>/CHECKPOINT-COMMIT."
7*f7c14bbaSAndroid Build Coastguard Worker	echo "Set BPF_BASELINE to override bpf tree commit, otherwise read from <libbpf-repo>/BPF-CHECKPOINT-COMMIT."
8*f7c14bbaSAndroid Build Coastguard Worker	echo "Set MANUAL_MODE to 1 to manually control every cherry-picked commits."
9*f7c14bbaSAndroid Build Coastguard Worker	exit 1
10*f7c14bbaSAndroid Build Coastguard Worker}
11*f7c14bbaSAndroid Build Coastguard Worker
12*f7c14bbaSAndroid Build Coastguard Workerset -eu
13*f7c14bbaSAndroid Build Coastguard Worker
14*f7c14bbaSAndroid Build Coastguard WorkerLIBBPF_REPO=${1-""}
15*f7c14bbaSAndroid Build Coastguard WorkerLINUX_REPO=${2-""}
16*f7c14bbaSAndroid Build Coastguard WorkerBPF_BRANCH=${3-""}
17*f7c14bbaSAndroid Build Coastguard WorkerBASELINE_COMMIT=${BPF_NEXT_BASELINE:-$(cat ${LIBBPF_REPO}/CHECKPOINT-COMMIT)}
18*f7c14bbaSAndroid Build Coastguard WorkerBPF_BASELINE_COMMIT=${BPF_BASELINE:-$(cat ${LIBBPF_REPO}/BPF-CHECKPOINT-COMMIT)}
19*f7c14bbaSAndroid Build Coastguard Worker
20*f7c14bbaSAndroid Build Coastguard Workerif [ -z "${LIBBPF_REPO}" ] || [ -z "${LINUX_REPO}" ]; then
21*f7c14bbaSAndroid Build Coastguard Worker	echo "Error: libbpf or linux repos are not specified"
22*f7c14bbaSAndroid Build Coastguard Worker	usage
23*f7c14bbaSAndroid Build Coastguard Workerfi
24*f7c14bbaSAndroid Build Coastguard Workerif [ -z "${BPF_BRANCH}" ]; then
25*f7c14bbaSAndroid Build Coastguard Worker	echo "Error: linux's bpf tree branch is not specified"
26*f7c14bbaSAndroid Build Coastguard Worker	usage
27*f7c14bbaSAndroid Build Coastguard Workerfi
28*f7c14bbaSAndroid Build Coastguard Workerif [ -z "${BASELINE_COMMIT}" ] || [ -z "${BPF_BASELINE_COMMIT}" ]; then
29*f7c14bbaSAndroid Build Coastguard Worker	echo "Error: bpf or bpf-next baseline commits are not provided"
30*f7c14bbaSAndroid Build Coastguard Worker	usage
31*f7c14bbaSAndroid Build Coastguard Workerfi
32*f7c14bbaSAndroid Build Coastguard Worker
33*f7c14bbaSAndroid Build Coastguard WorkerSUFFIX=$(date --utc +%Y-%m-%dT%H-%M-%S.%3NZ)
34*f7c14bbaSAndroid Build Coastguard WorkerWORKDIR=$(pwd)
35*f7c14bbaSAndroid Build Coastguard WorkerTMP_DIR=$(mktemp -d)
36*f7c14bbaSAndroid Build Coastguard Worker
37*f7c14bbaSAndroid Build Coastguard Workertrap "cd ${WORKDIR}; exit" INT TERM EXIT
38*f7c14bbaSAndroid Build Coastguard Worker
39*f7c14bbaSAndroid Build Coastguard Workerdeclare -A PATH_MAP
40*f7c14bbaSAndroid Build Coastguard WorkerPATH_MAP=(									\
41*f7c14bbaSAndroid Build Coastguard Worker	[tools/lib/bpf]=src							\
42*f7c14bbaSAndroid Build Coastguard Worker	[tools/include/uapi/linux/bpf_common.h]=include/uapi/linux/bpf_common.h	\
43*f7c14bbaSAndroid Build Coastguard Worker	[tools/include/uapi/linux/bpf.h]=include/uapi/linux/bpf.h		\
44*f7c14bbaSAndroid Build Coastguard Worker	[tools/include/uapi/linux/btf.h]=include/uapi/linux/btf.h		\
45*f7c14bbaSAndroid Build Coastguard Worker	[tools/include/uapi/linux/fcntl.h]=include/uapi/linux/fcntl.h		\
46*f7c14bbaSAndroid Build Coastguard Worker	[tools/include/uapi/linux/openat2.h]=include/uapi/linux/openat2.h	\
47*f7c14bbaSAndroid Build Coastguard Worker	[tools/include/uapi/linux/if_link.h]=include/uapi/linux/if_link.h	\
48*f7c14bbaSAndroid Build Coastguard Worker	[tools/include/uapi/linux/if_xdp.h]=include/uapi/linux/if_xdp.h		\
49*f7c14bbaSAndroid Build Coastguard Worker	[tools/include/uapi/linux/netdev.h]=include/uapi/linux/netdev.h		\
50*f7c14bbaSAndroid Build Coastguard Worker	[tools/include/uapi/linux/netlink.h]=include/uapi/linux/netlink.h	\
51*f7c14bbaSAndroid Build Coastguard Worker	[tools/include/uapi/linux/pkt_cls.h]=include/uapi/linux/pkt_cls.h	\
52*f7c14bbaSAndroid Build Coastguard Worker	[tools/include/uapi/linux/pkt_sched.h]=include/uapi/linux/pkt_sched.h	\
53*f7c14bbaSAndroid Build Coastguard Worker	[include/uapi/linux/perf_event.h]=include/uapi/linux/perf_event.h	\
54*f7c14bbaSAndroid Build Coastguard Worker	[Documentation/bpf/libbpf]=docs						\
55*f7c14bbaSAndroid Build Coastguard Worker)
56*f7c14bbaSAndroid Build Coastguard Worker
57*f7c14bbaSAndroid Build Coastguard WorkerLIBBPF_PATHS=("${!PATH_MAP[@]}" ":^tools/lib/bpf/Makefile" ":^tools/lib/bpf/Build" ":^tools/lib/bpf/.gitignore" ":^tools/include/tools/libc_compat.h")
58*f7c14bbaSAndroid Build Coastguard WorkerLIBBPF_VIEW_PATHS=("${PATH_MAP[@]}")
59*f7c14bbaSAndroid Build Coastguard WorkerLIBBPF_VIEW_EXCLUDE_REGEX='^src/(Makefile|Build|test_libbpf\.c|bpf_helper_defs\.h|\.gitignore)$|^docs/(\.gitignore|api\.rst|conf\.py)$|^docs/sphinx/.*'
60*f7c14bbaSAndroid Build Coastguard WorkerLINUX_VIEW_EXCLUDE_REGEX='^include/tools/libc_compat.h$'
61*f7c14bbaSAndroid Build Coastguard Worker
62*f7c14bbaSAndroid Build Coastguard WorkerLIBBPF_TREE_FILTER="mkdir -p __libbpf/include/uapi/linux __libbpf/include/tools && "$'\\\n'
63*f7c14bbaSAndroid Build Coastguard Workerfor p in "${!PATH_MAP[@]}"; do
64*f7c14bbaSAndroid Build Coastguard Worker	LIBBPF_TREE_FILTER+="git mv -kf ${p} __libbpf/${PATH_MAP[${p}]} && "$'\\\n'
65*f7c14bbaSAndroid Build Coastguard Workerdone
66*f7c14bbaSAndroid Build Coastguard WorkerLIBBPF_TREE_FILTER+="git rm --ignore-unmatch -f __libbpf/src/{Makefile,Build,test_libbpf.c,.gitignore} >/dev/null"
67*f7c14bbaSAndroid Build Coastguard Worker
68*f7c14bbaSAndroid Build Coastguard Workercd_to()
69*f7c14bbaSAndroid Build Coastguard Worker{
70*f7c14bbaSAndroid Build Coastguard Worker	cd ${WORKDIR} && cd "$1"
71*f7c14bbaSAndroid Build Coastguard Worker}
72*f7c14bbaSAndroid Build Coastguard Worker
73*f7c14bbaSAndroid Build Coastguard Worker# Output brief single-line commit description
74*f7c14bbaSAndroid Build Coastguard Worker# $1 - commit ref
75*f7c14bbaSAndroid Build Coastguard Workercommit_desc()
76*f7c14bbaSAndroid Build Coastguard Worker{
77*f7c14bbaSAndroid Build Coastguard Worker	git log -n1 --pretty='%h ("%s")' $1
78*f7c14bbaSAndroid Build Coastguard Worker}
79*f7c14bbaSAndroid Build Coastguard Worker
80*f7c14bbaSAndroid Build Coastguard Worker# Create commit single-line signature, which consists of:
81*f7c14bbaSAndroid Build Coastguard Worker# - full commit subject
82*f7c14bbaSAndroid Build Coastguard Worker# - author date in ISO8601 format
83*f7c14bbaSAndroid Build Coastguard Worker# - full commit body with newlines replaced with vertical bars (|)
84*f7c14bbaSAndroid Build Coastguard Worker# - shortstat appended at the end
85*f7c14bbaSAndroid Build Coastguard Worker# The idea is that this single-line signature is good enough to make final
86*f7c14bbaSAndroid Build Coastguard Worker# decision about whether two commits are the same, across different repos.
87*f7c14bbaSAndroid Build Coastguard Worker# $1 - commit ref
88*f7c14bbaSAndroid Build Coastguard Worker# $2 - paths filter
89*f7c14bbaSAndroid Build Coastguard Workercommit_signature()
90*f7c14bbaSAndroid Build Coastguard Worker{
91*f7c14bbaSAndroid Build Coastguard Worker	local ref=$1
92*f7c14bbaSAndroid Build Coastguard Worker	shift
93*f7c14bbaSAndroid Build Coastguard Worker	git show --pretty='("%s")|%aI|%b' --shortstat $ref -- "${@-.}" | tr '\n' '|'
94*f7c14bbaSAndroid Build Coastguard Worker}
95*f7c14bbaSAndroid Build Coastguard Worker
96*f7c14bbaSAndroid Build Coastguard Worker# Cherry-pick commits touching libbpf-related files
97*f7c14bbaSAndroid Build Coastguard Worker# $1 - baseline_tag
98*f7c14bbaSAndroid Build Coastguard Worker# $2 - tip_tag
99*f7c14bbaSAndroid Build Coastguard Workercherry_pick_commits()
100*f7c14bbaSAndroid Build Coastguard Worker{
101*f7c14bbaSAndroid Build Coastguard Worker	local manual_mode=${MANUAL_MODE:-0}
102*f7c14bbaSAndroid Build Coastguard Worker	local baseline_tag=$1
103*f7c14bbaSAndroid Build Coastguard Worker	local tip_tag=$2
104*f7c14bbaSAndroid Build Coastguard Worker	local new_commits
105*f7c14bbaSAndroid Build Coastguard Worker	local signature
106*f7c14bbaSAndroid Build Coastguard Worker	local should_skip
107*f7c14bbaSAndroid Build Coastguard Worker	local synced_cnt
108*f7c14bbaSAndroid Build Coastguard Worker	local manual_check
109*f7c14bbaSAndroid Build Coastguard Worker	local libbpf_conflict_cnt
110*f7c14bbaSAndroid Build Coastguard Worker	local desc
111*f7c14bbaSAndroid Build Coastguard Worker
112*f7c14bbaSAndroid Build Coastguard Worker	new_commits=$(git rev-list --no-merges --topo-order --reverse ${baseline_tag}..${tip_tag} -- "${LIBBPF_PATHS[@]}")
113*f7c14bbaSAndroid Build Coastguard Worker	for new_commit in ${new_commits}; do
114*f7c14bbaSAndroid Build Coastguard Worker		desc="$(commit_desc ${new_commit})"
115*f7c14bbaSAndroid Build Coastguard Worker		signature="$(commit_signature ${new_commit} "${LIBBPF_PATHS[@]}")"
116*f7c14bbaSAndroid Build Coastguard Worker		synced_cnt=$(grep -F "${signature}" ${TMP_DIR}/libbpf_commits.txt | wc -l)
117*f7c14bbaSAndroid Build Coastguard Worker		manual_check=0
118*f7c14bbaSAndroid Build Coastguard Worker		if ((${synced_cnt} > 0)); then
119*f7c14bbaSAndroid Build Coastguard Worker			# commit with the same subject is already in libbpf, but it's
120*f7c14bbaSAndroid Build Coastguard Worker			# not 100% the same commit, so check with user
121*f7c14bbaSAndroid Build Coastguard Worker			echo "Commit '${desc}' is synced into libbpf as:"
122*f7c14bbaSAndroid Build Coastguard Worker			grep -F "${signature}" ${TMP_DIR}/libbpf_commits.txt | \
123*f7c14bbaSAndroid Build Coastguard Worker				cut -d'|' -f1 | sed -e 's/^/- /'
124*f7c14bbaSAndroid Build Coastguard Worker			if ((${manual_mode} != 1 && ${synced_cnt} == 1)); then
125*f7c14bbaSAndroid Build Coastguard Worker				echo "Skipping '${desc}' due to unique match..."
126*f7c14bbaSAndroid Build Coastguard Worker				continue
127*f7c14bbaSAndroid Build Coastguard Worker			fi
128*f7c14bbaSAndroid Build Coastguard Worker			if ((${synced_cnt} > 1)); then
129*f7c14bbaSAndroid Build Coastguard Worker				echo "'${desc} matches multiple commits, please, double-check!"
130*f7c14bbaSAndroid Build Coastguard Worker				manual_check=1
131*f7c14bbaSAndroid Build Coastguard Worker			fi
132*f7c14bbaSAndroid Build Coastguard Worker		fi
133*f7c14bbaSAndroid Build Coastguard Worker		if ((${manual_mode} == 1 || ${manual_check} == 1)); then
134*f7c14bbaSAndroid Build Coastguard Worker			read -p "Do you want to skip '${desc}'? [y/N]: " should_skip
135*f7c14bbaSAndroid Build Coastguard Worker			case "${should_skip}" in
136*f7c14bbaSAndroid Build Coastguard Worker				"y" | "Y")
137*f7c14bbaSAndroid Build Coastguard Worker					echo "Skipping '${desc}'..."
138*f7c14bbaSAndroid Build Coastguard Worker					continue
139*f7c14bbaSAndroid Build Coastguard Worker					;;
140*f7c14bbaSAndroid Build Coastguard Worker			esac
141*f7c14bbaSAndroid Build Coastguard Worker		fi
142*f7c14bbaSAndroid Build Coastguard Worker		# commit hasn't been synced into libbpf yet
143*f7c14bbaSAndroid Build Coastguard Worker		echo "Picking '${desc}'..."
144*f7c14bbaSAndroid Build Coastguard Worker		if ! git cherry-pick ${new_commit} &>/dev/null; then
145*f7c14bbaSAndroid Build Coastguard Worker			echo "Warning! Cherry-picking '${desc} failed, checking if it's non-libbpf files causing problems..."
146*f7c14bbaSAndroid Build Coastguard Worker			libbpf_conflict_cnt=$(git diff --name-only --diff-filter=U -- "${LIBBPF_PATHS[@]}" | wc -l)
147*f7c14bbaSAndroid Build Coastguard Worker			conflict_cnt=$(git diff --name-only | wc -l)
148*f7c14bbaSAndroid Build Coastguard Worker			prompt_resolution=1
149*f7c14bbaSAndroid Build Coastguard Worker
150*f7c14bbaSAndroid Build Coastguard Worker			if ((${libbpf_conflict_cnt} == 0)); then
151*f7c14bbaSAndroid Build Coastguard Worker				echo "Looks like only non-libbpf files have conflicts, ignoring..."
152*f7c14bbaSAndroid Build Coastguard Worker				if ((${conflict_cnt} == 0)); then
153*f7c14bbaSAndroid Build Coastguard Worker					echo "Empty cherry-pick, skipping it..."
154*f7c14bbaSAndroid Build Coastguard Worker					git cherry-pick --abort
155*f7c14bbaSAndroid Build Coastguard Worker					continue
156*f7c14bbaSAndroid Build Coastguard Worker				fi
157*f7c14bbaSAndroid Build Coastguard Worker
158*f7c14bbaSAndroid Build Coastguard Worker				git add .
159*f7c14bbaSAndroid Build Coastguard Worker				# GIT_EDITOR=true to avoid editor popping up to edit commit message
160*f7c14bbaSAndroid Build Coastguard Worker				if ! GIT_EDITOR=true git cherry-pick --continue &>/dev/null; then
161*f7c14bbaSAndroid Build Coastguard Worker					echo "Error! That still failed! Please resolve manually."
162*f7c14bbaSAndroid Build Coastguard Worker				else
163*f7c14bbaSAndroid Build Coastguard Worker					echo "Success! All cherry-pick conflicts were resolved for '${desc}'!"
164*f7c14bbaSAndroid Build Coastguard Worker					prompt_resolution=0
165*f7c14bbaSAndroid Build Coastguard Worker				fi
166*f7c14bbaSAndroid Build Coastguard Worker			fi
167*f7c14bbaSAndroid Build Coastguard Worker
168*f7c14bbaSAndroid Build Coastguard Worker			if ((${prompt_resolution} == 1)); then
169*f7c14bbaSAndroid Build Coastguard Worker				read -p "Error! Cherry-picking '${desc}' failed, please fix manually and press <return> to proceed..."
170*f7c14bbaSAndroid Build Coastguard Worker			fi
171*f7c14bbaSAndroid Build Coastguard Worker		fi
172*f7c14bbaSAndroid Build Coastguard Worker		# Append signature of just cherry-picked commit to avoid
173*f7c14bbaSAndroid Build Coastguard Worker		# potentially cherry-picking the same commit twice later when
174*f7c14bbaSAndroid Build Coastguard Worker		# processing bpf tree commits. At this point we don't know yet
175*f7c14bbaSAndroid Build Coastguard Worker		# the final commit sha in libbpf repo, so we record Linux SHA
176*f7c14bbaSAndroid Build Coastguard Worker		# instead as LINUX_<sha>.
177*f7c14bbaSAndroid Build Coastguard Worker		echo LINUX_$(git log --pretty='%h' -n1) "${signature}" >> ${TMP_DIR}/libbpf_commits.txt
178*f7c14bbaSAndroid Build Coastguard Worker	done
179*f7c14bbaSAndroid Build Coastguard Worker}
180*f7c14bbaSAndroid Build Coastguard Worker
181*f7c14bbaSAndroid Build Coastguard Workercleanup()
182*f7c14bbaSAndroid Build Coastguard Worker{
183*f7c14bbaSAndroid Build Coastguard Worker	echo "Cleaning up..."
184*f7c14bbaSAndroid Build Coastguard Worker	rm -r ${TMP_DIR}
185*f7c14bbaSAndroid Build Coastguard Worker	cd_to ${LINUX_REPO}
186*f7c14bbaSAndroid Build Coastguard Worker	git checkout ${TIP_SYM_REF}
187*f7c14bbaSAndroid Build Coastguard Worker	git branch -D ${BASELINE_TAG} ${TIP_TAG} ${BPF_BASELINE_TAG} ${BPF_TIP_TAG} \
188*f7c14bbaSAndroid Build Coastguard Worker		      ${SQUASH_BASE_TAG} ${SQUASH_TIP_TAG} ${VIEW_TAG} || true
189*f7c14bbaSAndroid Build Coastguard Worker
190*f7c14bbaSAndroid Build Coastguard Worker	cd_to .
191*f7c14bbaSAndroid Build Coastguard Worker	echo "DONE."
192*f7c14bbaSAndroid Build Coastguard Worker}
193*f7c14bbaSAndroid Build Coastguard Worker
194*f7c14bbaSAndroid Build Coastguard Worker
195*f7c14bbaSAndroid Build Coastguard Workercd_to ${LIBBPF_REPO}
196*f7c14bbaSAndroid Build Coastguard WorkerGITHUB_ABS_DIR=$(pwd)
197*f7c14bbaSAndroid Build Coastguard Workerecho "Dumping existing libbpf commit signatures..."
198*f7c14bbaSAndroid Build Coastguard Workerfor h in $(git log --pretty='%h' -n500); do
199*f7c14bbaSAndroid Build Coastguard Worker	echo $h "$(commit_signature $h)" >> ${TMP_DIR}/libbpf_commits.txt
200*f7c14bbaSAndroid Build Coastguard Workerdone
201*f7c14bbaSAndroid Build Coastguard Worker
202*f7c14bbaSAndroid Build Coastguard Worker# Use current kernel repo HEAD as a source of patches
203*f7c14bbaSAndroid Build Coastguard Workercd_to ${LINUX_REPO}
204*f7c14bbaSAndroid Build Coastguard WorkerLINUX_ABS_DIR=$(pwd)
205*f7c14bbaSAndroid Build Coastguard WorkerTIP_SYM_REF=$(git symbolic-ref -q --short HEAD || git rev-parse HEAD)
206*f7c14bbaSAndroid Build Coastguard WorkerTIP_COMMIT=$(git rev-parse HEAD)
207*f7c14bbaSAndroid Build Coastguard WorkerBPF_TIP_COMMIT=$(git rev-parse ${BPF_BRANCH})
208*f7c14bbaSAndroid Build Coastguard WorkerBASELINE_TAG=libbpf-baseline-${SUFFIX}
209*f7c14bbaSAndroid Build Coastguard WorkerTIP_TAG=libbpf-tip-${SUFFIX}
210*f7c14bbaSAndroid Build Coastguard WorkerBPF_BASELINE_TAG=libbpf-bpf-baseline-${SUFFIX}
211*f7c14bbaSAndroid Build Coastguard WorkerBPF_TIP_TAG=libbpf-bpf-tip-${SUFFIX}
212*f7c14bbaSAndroid Build Coastguard WorkerVIEW_TAG=libbpf-view-${SUFFIX}
213*f7c14bbaSAndroid Build Coastguard WorkerLIBBPF_SYNC_TAG=libbpf-sync-${SUFFIX}
214*f7c14bbaSAndroid Build Coastguard Worker
215*f7c14bbaSAndroid Build Coastguard Worker# Squash state of kernel repo at baseline into single commit
216*f7c14bbaSAndroid Build Coastguard WorkerSQUASH_BASE_TAG=libbpf-squash-base-${SUFFIX}
217*f7c14bbaSAndroid Build Coastguard WorkerSQUASH_TIP_TAG=libbpf-squash-tip-${SUFFIX}
218*f7c14bbaSAndroid Build Coastguard WorkerSQUASH_COMMIT=$(git commit-tree ${BASELINE_COMMIT}^{tree} -m "BASELINE SQUASH ${BASELINE_COMMIT}")
219*f7c14bbaSAndroid Build Coastguard Worker
220*f7c14bbaSAndroid Build Coastguard Workerecho "WORKDIR:          ${WORKDIR}"
221*f7c14bbaSAndroid Build Coastguard Workerecho "LINUX REPO:       ${LINUX_REPO}"
222*f7c14bbaSAndroid Build Coastguard Workerecho "LIBBPF REPO:      ${LIBBPF_REPO}"
223*f7c14bbaSAndroid Build Coastguard Workerecho "TEMP DIR:         ${TMP_DIR}"
224*f7c14bbaSAndroid Build Coastguard Workerecho "SUFFIX:           ${SUFFIX}"
225*f7c14bbaSAndroid Build Coastguard Workerecho "BASE COMMIT:      '$(commit_desc ${BASELINE_COMMIT})'"
226*f7c14bbaSAndroid Build Coastguard Workerecho "TIP COMMIT:       '$(commit_desc ${TIP_COMMIT})'"
227*f7c14bbaSAndroid Build Coastguard Workerecho "BPF BASE COMMIT:  '$(commit_desc ${BPF_BASELINE_COMMIT})'"
228*f7c14bbaSAndroid Build Coastguard Workerecho "BPF TIP COMMIT:   '$(commit_desc ${BPF_TIP_COMMIT})'"
229*f7c14bbaSAndroid Build Coastguard Workerecho "SQUASH COMMIT:    ${SQUASH_COMMIT}"
230*f7c14bbaSAndroid Build Coastguard Workerecho "BASELINE TAG:     ${BASELINE_TAG}"
231*f7c14bbaSAndroid Build Coastguard Workerecho "TIP TAG:          ${TIP_TAG}"
232*f7c14bbaSAndroid Build Coastguard Workerecho "BPF BASELINE TAG: ${BPF_BASELINE_TAG}"
233*f7c14bbaSAndroid Build Coastguard Workerecho "BPF TIP TAG:      ${BPF_TIP_TAG}"
234*f7c14bbaSAndroid Build Coastguard Workerecho "SQUASH BASE TAG:  ${SQUASH_BASE_TAG}"
235*f7c14bbaSAndroid Build Coastguard Workerecho "SQUASH TIP TAG:   ${SQUASH_TIP_TAG}"
236*f7c14bbaSAndroid Build Coastguard Workerecho "VIEW TAG:         ${VIEW_TAG}"
237*f7c14bbaSAndroid Build Coastguard Workerecho "LIBBPF SYNC TAG:  ${LIBBPF_SYNC_TAG}"
238*f7c14bbaSAndroid Build Coastguard Workerecho "PATCHES:          ${TMP_DIR}/patches"
239*f7c14bbaSAndroid Build Coastguard Worker
240*f7c14bbaSAndroid Build Coastguard Workergit branch ${BASELINE_TAG} ${BASELINE_COMMIT}
241*f7c14bbaSAndroid Build Coastguard Workergit branch ${TIP_TAG} ${TIP_COMMIT}
242*f7c14bbaSAndroid Build Coastguard Workergit branch ${BPF_BASELINE_TAG} ${BPF_BASELINE_COMMIT}
243*f7c14bbaSAndroid Build Coastguard Workergit branch ${BPF_TIP_TAG} ${BPF_TIP_COMMIT}
244*f7c14bbaSAndroid Build Coastguard Workergit branch ${SQUASH_BASE_TAG} ${SQUASH_COMMIT}
245*f7c14bbaSAndroid Build Coastguard Workergit checkout -b ${SQUASH_TIP_TAG} ${SQUASH_COMMIT}
246*f7c14bbaSAndroid Build Coastguard Worker
247*f7c14bbaSAndroid Build Coastguard Worker# Cherry-pick new commits onto squashed baseline commit
248*f7c14bbaSAndroid Build Coastguard Workercherry_pick_commits ${BASELINE_TAG} ${TIP_TAG}
249*f7c14bbaSAndroid Build Coastguard Workercherry_pick_commits ${BPF_BASELINE_TAG} ${BPF_TIP_TAG}
250*f7c14bbaSAndroid Build Coastguard Worker
251*f7c14bbaSAndroid Build Coastguard Worker# Move all libbpf files into __libbpf directory.
252*f7c14bbaSAndroid Build Coastguard WorkerFILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --prune-empty -f --tree-filter "${LIBBPF_TREE_FILTER}" ${SQUASH_TIP_TAG} ${SQUASH_BASE_TAG}
253*f7c14bbaSAndroid Build Coastguard Worker# Make __libbpf a new root directory
254*f7c14bbaSAndroid Build Coastguard WorkerFILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --prune-empty -f --subdirectory-filter __libbpf ${SQUASH_TIP_TAG} ${SQUASH_BASE_TAG}
255*f7c14bbaSAndroid Build Coastguard Worker
256*f7c14bbaSAndroid Build Coastguard Worker# If there are no new commits with  libbpf-related changes, bail out
257*f7c14bbaSAndroid Build Coastguard WorkerCOMMIT_CNT=$(git rev-list --count ${SQUASH_BASE_TAG}..${SQUASH_TIP_TAG})
258*f7c14bbaSAndroid Build Coastguard Workerif ((${COMMIT_CNT} <= 0)); then
259*f7c14bbaSAndroid Build Coastguard Worker    echo "No new changes to apply, we are done!"
260*f7c14bbaSAndroid Build Coastguard Worker    cleanup
261*f7c14bbaSAndroid Build Coastguard Worker    exit 2
262*f7c14bbaSAndroid Build Coastguard Workerfi
263*f7c14bbaSAndroid Build Coastguard Worker
264*f7c14bbaSAndroid Build Coastguard Worker# Exclude baseline commit and generate nice cover letter with summary
265*f7c14bbaSAndroid Build Coastguard Workergit format-patch --no-signature ${SQUASH_BASE_TAG}..${SQUASH_TIP_TAG} --cover-letter -o ${TMP_DIR}/patches
266*f7c14bbaSAndroid Build Coastguard Worker
267*f7c14bbaSAndroid Build Coastguard Worker# Now is time to re-apply libbpf-related linux patches to libbpf repo
268*f7c14bbaSAndroid Build Coastguard Workercd_to ${LIBBPF_REPO}
269*f7c14bbaSAndroid Build Coastguard Workergit checkout -b ${LIBBPF_SYNC_TAG}
270*f7c14bbaSAndroid Build Coastguard Worker
271*f7c14bbaSAndroid Build Coastguard Workerfor patch in $(ls -1 ${TMP_DIR}/patches | tail -n +2); do
272*f7c14bbaSAndroid Build Coastguard Worker	if ! git am -3 --committer-date-is-author-date "${TMP_DIR}/patches/${patch}"; then
273*f7c14bbaSAndroid Build Coastguard Worker		if ! patch -p1 --merge < "${TMP_DIR}/patches/${patch}"; then
274*f7c14bbaSAndroid Build Coastguard Worker			read -p "Applying ${TMP_DIR}/patches/${patch} failed, please resolve manually and press <return> to proceed..."
275*f7c14bbaSAndroid Build Coastguard Worker		fi
276*f7c14bbaSAndroid Build Coastguard Worker		git am --continue
277*f7c14bbaSAndroid Build Coastguard Worker	fi
278*f7c14bbaSAndroid Build Coastguard Workerdone
279*f7c14bbaSAndroid Build Coastguard Worker
280*f7c14bbaSAndroid Build Coastguard Worker# Generate bpf_helper_defs.h and commit, if anything changed
281*f7c14bbaSAndroid Build Coastguard Worker# restore Linux tip to use bpf_doc.py
282*f7c14bbaSAndroid Build Coastguard Workercd_to ${LINUX_REPO}
283*f7c14bbaSAndroid Build Coastguard Workergit checkout ${TIP_TAG}
284*f7c14bbaSAndroid Build Coastguard Worker# re-generate bpf_helper_defs.h
285*f7c14bbaSAndroid Build Coastguard Workercd_to ${LIBBPF_REPO}
286*f7c14bbaSAndroid Build Coastguard Worker"${LINUX_ABS_DIR}/scripts/bpf_doc.py" --header					      \
287*f7c14bbaSAndroid Build Coastguard Worker	--file include/uapi/linux/bpf.h > src/bpf_helper_defs.h
288*f7c14bbaSAndroid Build Coastguard Worker# if anything changed, commit it
289*f7c14bbaSAndroid Build Coastguard Workerhelpers_changes=$(git status --porcelain src/bpf_helper_defs.h | wc -l)
290*f7c14bbaSAndroid Build Coastguard Workerif ((${helpers_changes} == 1)); then
291*f7c14bbaSAndroid Build Coastguard Worker	git add src/bpf_helper_defs.h
292*f7c14bbaSAndroid Build Coastguard Worker	git commit -s -m "sync: auto-generate latest BPF helpers
293*f7c14bbaSAndroid Build Coastguard Worker
294*f7c14bbaSAndroid Build Coastguard WorkerLatest changes to BPF helper definitions.
295*f7c14bbaSAndroid Build Coastguard Worker" -- src/bpf_helper_defs.h
296*f7c14bbaSAndroid Build Coastguard Workerfi
297*f7c14bbaSAndroid Build Coastguard Worker
298*f7c14bbaSAndroid Build Coastguard Worker# Use generated cover-letter as a template for "sync commit" with
299*f7c14bbaSAndroid Build Coastguard Worker# baseline and checkpoint commits from kernel repo (and leave summary
300*f7c14bbaSAndroid Build Coastguard Worker# from cover letter intact, of course)
301*f7c14bbaSAndroid Build Coastguard Workerecho ${TIP_COMMIT} > CHECKPOINT-COMMIT &&					      \
302*f7c14bbaSAndroid Build Coastguard Workerecho ${BPF_TIP_COMMIT} > BPF-CHECKPOINT-COMMIT &&				      \
303*f7c14bbaSAndroid Build Coastguard Workergit add CHECKPOINT-COMMIT &&							      \
304*f7c14bbaSAndroid Build Coastguard Workergit add BPF-CHECKPOINT-COMMIT &&						      \
305*f7c14bbaSAndroid Build Coastguard Workerawk '/\*\*\* BLURB HERE \*\*\*/ {p=1} p' ${TMP_DIR}/patches/0000-cover-letter.patch | \
306*f7c14bbaSAndroid Build Coastguard Workersed "s/\*\*\* BLURB HERE \*\*\*/\
307*f7c14bbaSAndroid Build Coastguard Workersync: latest libbpf changes from kernel\n\
308*f7c14bbaSAndroid Build Coastguard Worker\n\
309*f7c14bbaSAndroid Build Coastguard WorkerSyncing latest libbpf commits from kernel repository.\n\
310*f7c14bbaSAndroid Build Coastguard WorkerBaseline bpf-next commit:   ${BASELINE_COMMIT}\n\
311*f7c14bbaSAndroid Build Coastguard WorkerCheckpoint bpf-next commit: ${TIP_COMMIT}\n\
312*f7c14bbaSAndroid Build Coastguard WorkerBaseline bpf commit:        ${BPF_BASELINE_COMMIT}\n\
313*f7c14bbaSAndroid Build Coastguard WorkerCheckpoint bpf commit:      ${BPF_TIP_COMMIT}/" |				      \
314*f7c14bbaSAndroid Build Coastguard Workergit commit -s --file=-
315*f7c14bbaSAndroid Build Coastguard Worker
316*f7c14bbaSAndroid Build Coastguard Workerecho "SUCCESS! ${COMMIT_CNT} commits synced."
317*f7c14bbaSAndroid Build Coastguard Worker
318*f7c14bbaSAndroid Build Coastguard Workerecho "Verifying Linux's and Github's libbpf state"
319*f7c14bbaSAndroid Build Coastguard Worker
320*f7c14bbaSAndroid Build Coastguard Workercd_to ${LINUX_REPO}
321*f7c14bbaSAndroid Build Coastguard Workergit checkout -b ${VIEW_TAG} ${TIP_COMMIT}
322*f7c14bbaSAndroid Build Coastguard WorkerFILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --tree-filter "${LIBBPF_TREE_FILTER}" ${VIEW_TAG}^..${VIEW_TAG}
323*f7c14bbaSAndroid Build Coastguard WorkerFILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --subdirectory-filter __libbpf ${VIEW_TAG}^..${VIEW_TAG}
324*f7c14bbaSAndroid Build Coastguard Workergit ls-files -- "${LIBBPF_VIEW_PATHS[@]}" | grep -v -E "${LINUX_VIEW_EXCLUDE_REGEX}" > ${TMP_DIR}/linux-view.ls
325*f7c14bbaSAndroid Build Coastguard Worker
326*f7c14bbaSAndroid Build Coastguard Workercd_to ${LIBBPF_REPO}
327*f7c14bbaSAndroid Build Coastguard Workergit ls-files -- "${LIBBPF_VIEW_PATHS[@]}" | grep -v -E "${LIBBPF_VIEW_EXCLUDE_REGEX}" > ${TMP_DIR}/github-view.ls
328*f7c14bbaSAndroid Build Coastguard Worker
329*f7c14bbaSAndroid Build Coastguard Workerecho "Comparing list of files..."
330*f7c14bbaSAndroid Build Coastguard Workerdiff -u ${TMP_DIR}/linux-view.ls ${TMP_DIR}/github-view.ls
331*f7c14bbaSAndroid Build Coastguard Workerecho "Comparing file contents..."
332*f7c14bbaSAndroid Build Coastguard WorkerCONSISTENT=1
333*f7c14bbaSAndroid Build Coastguard Workerfor F in $(cat ${TMP_DIR}/linux-view.ls); do
334*f7c14bbaSAndroid Build Coastguard Worker	if ! diff -u "${LINUX_ABS_DIR}/${F}" "${GITHUB_ABS_DIR}/${F}"; then
335*f7c14bbaSAndroid Build Coastguard Worker		echo "${LINUX_ABS_DIR}/${F} and ${GITHUB_ABS_DIR}/${F} are different!"
336*f7c14bbaSAndroid Build Coastguard Worker		CONSISTENT=0
337*f7c14bbaSAndroid Build Coastguard Worker	fi
338*f7c14bbaSAndroid Build Coastguard Workerdone
339*f7c14bbaSAndroid Build Coastguard Workerif ((${CONSISTENT} == 1)); then
340*f7c14bbaSAndroid Build Coastguard Worker	echo "Great! Content is identical!"
341*f7c14bbaSAndroid Build Coastguard Workerelse
342*f7c14bbaSAndroid Build Coastguard Worker	ignore_inconsistency=n
343*f7c14bbaSAndroid Build Coastguard Worker	echo "Unfortunately, there are some inconsistencies, please double check."
344*f7c14bbaSAndroid Build Coastguard Worker	read -p "Does everything look good? [y/N]: " ignore_inconsistency
345*f7c14bbaSAndroid Build Coastguard Worker	case "${ignore_inconsistency}" in
346*f7c14bbaSAndroid Build Coastguard Worker		"y" | "Y")
347*f7c14bbaSAndroid Build Coastguard Worker			echo "Ok, proceeding..."
348*f7c14bbaSAndroid Build Coastguard Worker			;;
349*f7c14bbaSAndroid Build Coastguard Worker		*)
350*f7c14bbaSAndroid Build Coastguard Worker			echo "Oops, exiting with error..."
351*f7c14bbaSAndroid Build Coastguard Worker			exit 4
352*f7c14bbaSAndroid Build Coastguard Worker	esac
353*f7c14bbaSAndroid Build Coastguard Workerfi
354*f7c14bbaSAndroid Build Coastguard Worker
355*f7c14bbaSAndroid Build Coastguard Workercleanup
356