SUKISU-ULTRA

grep -r SU_VERSION .

#############################################

drivers/kernelsu/Kbuild

ifeq ($(KSU_GITHUB_VERSION_COMMIT),) ifeq ($(LOCAL_GIT_EXISTS),1) $(shell cd $(KSU_SRC); [ -f ../.git/shallow ] && $(GIT_BIN) fetch --unshallow) KSU_LOCAL_VERSION := $(shell cd $(KSU_SRC); $(GIT_BIN) rev-list --count $(REPO_BRANCH)) KSU_VERSION := $(shell expr 40000 + $(KSU_LOCAL_VERSION) - 2815) $(info -- $(REPO_NAME) version (local .git): $(KSU_VERSION)) 
else KSU_VERSION := 13000 $(warning -- Could not fetch version online or via local .git! Using fallback version: $(KSU_VERSION))
endif
else KSU_VERSION := $(shell expr 40000 + $(KSU_GITHUB_VERSION_COMMIT) - 2815) $(info -- $(REPO_NAME) version (GitHub): $(KSU_VERSION))
git switch -c sukisu


git rm --cached drivers/kernelsu 2>/dev/null || true

# 删除工作树中的 symlink(谨慎)
rm -rf drivers/kernelsu

# 复制顶层 KernelSU/kernel 到 drivers/kernelsu(物理目录)
cp -a KernelSU/kernel drivers/kernelsu

# 确认复制内容
ls -la drivers/kernelsu | sed -n '1,120p'

find drivers/kernelsu -name .git -type d -exec rm -rf {} +

# 删除可能的 .gitmodules
find drivers/kernelsu -name .gitmodules -type f -exec rm -f {} +

echo "KernelSU (vendored) imported on $(date)" > drivers/kernelsu/VENDOR

# 确保 drivers/Makefile 有 kernelsu 入口(若无则追加)
grep -q '^obj-$(CONFIG_KSU) += kernelsu/' drivers/Makefile || printf '\nobj-$(CONFIG_KSU) += kernelsu/\n' >> drivers/Makefile

# 确保 drivers/Kconfig 引入 kernelsu/Kconfig
grep -q 'source "drivers/kernelsu/Kconfig"' drivers/Kconfig || sed -i '/endmenu/i\source "drivers/kernelsu/Kconfig"' drivers/Kconfig


git add drivers/kernelsu drivers/kernelsu/VENDOR drivers/kernelsu/Makefile drivers/Makefile drivers/Kconfig
git commit -m "Vendor KernelSU into drivers/kernelsu (physical copy); set KSU_VERSION=${KSU_VERSION}"

############################################

KernelSU-Next


明白了 — 你用的是 KernelSU-Next 仓库(owner = KernelSU-Next),脚本也是针对这个仓库。下面给出一套简洁、可靠、可复现的步骤(命令逐行可复制),把 KernelSU-Next 的 v1.1.1 物理化到 drivers/kernelsu,并保证内核里报告的 KSU 版本与 v1.1.1 对应(你期望的那一串数字,例如 12851)。同时说明常见坑和如何验证。

先检验 tag 是否存在(一定要先做)


# 检查远端是否有 v1.1.1
git ls-remote --tags https://github.com/KernelSU-Next/KernelSU-Next.git | grep "refs/tags/v1.1.1" || true


如果这条命令有输出,说明 tag 存在;没有输出说明 tag 名不对(请贴 ls-remote 输出或列出 tags 我帮你确认)。

若 tag 存在,计算对应的 KSU_VERSION(推荐做法:在 /tmp 做一个完整 clone,保证 rev-list 计数正确)

# 完整 clone(不要 --depth 1,保证 rev-list 计数准确)
git clone https://github.com/KernelSU-Next/KernelSU-Next.git /tmp/KernelSU_next_full
cd /tmp/KernelSU_next_full
git checkout v1.1.1

# 计算 commit count 与 KSU_VERSION
GIT_COUNT=$(git rev-list --count HEAD)
echo "git_count=$GIT_COUNT"
KSU_VERSION=$((10000 + GIT_COUNT + 200))
echo "KSU_VERSION=$KSU_VERSION"

# 记下 KSU_VERSION(例如这里会输出类似 12851),然后退出
cd -


说明:KSU_VERSION 的计算方式就是 10000 + (git rev-list --count HEAD) + 200(Makefile 注释中写的公式)。

把 KernelSU-Next 的 kernel 物理化到当前内核树的 drivers/kernelsu,并写入 KSU_VERSION (你在 next 分支上,下面在 integrate 分支做改动以便后续合并/替换)



# 在内核树根目录(当前为 next)
git checkout next
git switch -c integrate/kernelsunext-v1.1.1
# 备份当前 next(可选)
git branch backup-next-$(date +%s) next

# 移除旧 symlink / gitlink(如果已存在)
git rm --cached drivers/kernelsu 2>/dev/null || true
rm -rf drivers/kernelsu

# 复制物理目录
cp -a KernelSU-Next/kernel drivers/kernelsu

# 删除任何嵌套 .git
find drivers/kernelsu -name .git -type d -exec rm -rf {} +
find drivers/kernelsu -name .gitmodules -type f -exec rm -f {} +


把计算得到的 KSU_VERSION 写入 drivers/kernelsu/Makefile(将 fallback 的 16 替换为正确值) (假设上面计算出的变量在 shell 中是 $KSU_VERSION)



# 如果你已经在 /tmp/KernelSU_next_full 计算出 KSU_VERSION=12851,
# 可以直接在这里用那个值替换下面的 12851。
KSU_VERSION=12851

# 先备份 Makefile
cp drivers/kernelsu/Makefile drivers/kernelsu/Makefile.bak

# 用 sed 替换 fallback(如果 Makefile 中确实包含 'ccflags-y += -DKSU_VERSION=16')
sed -i "s/ccflags-y += -DKSU_VERSION=16/ccflags-y += -DKSU_VERSION=${KSU_VERSION}/" drivers/kernelsu/Makefile

# 如果 Makefile 没有那一行(或结构不同),也可以直接追加确保宏被定义:
# echo "ccflags-y += -DKSU_VERSION=${KSU_VERSION}" >> drivers/kernelsu/Makefile


写入 VENDOR 并确保 drivers 的 Makefile/Kconfig 包含 kernelsu




echo "KernelSU v1.1.1 (imported from KernelSU-Next), imported at $(date)" > drivers/kernelsu/VENDOR

# 确保 drivers 的 Makefile/Kconfig 引入 kernelsu
grep -q '^obj-$(CONFIG_KSU) += kernelsu/' drivers/Makefile || printf '\nobj-$(CONFIG_KSU) += kernelsu/\n' >> drivers/Makefile
grep -q 'source "drivers/kernelsu/Kconfig"' drivers/Kconfig || sed -i '/endmenu/i\source "drivers/kernelsu/Kconfig"' drivers/Kconfig


提交更改



git add drivers/kernelsu drivers/kernelsu/VENDOR drivers/Makefile drivers/Kconfig
git commit -m "Vendor KernelSU-Next v1.1.1 into drivers/kernelsu; set KSU_VERSION=${KSU_VERSION}"


构建并验证


# 按你的构建流程
rm -rf "$OUT_DIR" && mkdir -p "$OUT_DIR"
make -C "$KERNEL" O="$OUT_DIR" $COMMON_ARGS CC="ccache clang" cannon_defconfig
make -C "$KERNEL" O="$OUT_DIR" $COMMON_ARGS CC="ccache clang" olddefconfig
make -C "$KERNEL" O="$OUT_DIR" $COMMON_ARGS CC="ccache clang" $BUILD_ARGS -j$(nproc --all) Image.gz


验证点:

- 编译输出里(drivers/kernelsu/Makefile 会输出版本信息)应显示 KSU_VERSION 值或相关信息。
- 刷机并启动后,Manager/UI 显示的内核端版本应与 KSU_VERSION 一致(例如 12851)。

可选:让 Makefile 支持由外部环境传入 KSU_VERSION(长期推荐)

- 你可以把 drivers/kernelsu/Makefile 的 fallback 修改为优先使用环境变量 KSU_VERSION,当没有 .git 时直接用 env 值,这样以后不必改源码:
- 我之前给了完整的 Makefile snippet;需要我现在生成并 git apply 一份补丁吗?

常见坑与注意

- tag 名必须正确(ls-remote 可确认);如果你之前对 tiann/KernelSU 做 clone,会找不到 v1.1.1 是因为 tag 属于 KernelSU-Next 仓库而非 tiann 的。
- 若使用浅克隆(--depth 1),git rev-list --count 可能不准确;为了准确值请完整 clone(或至少确保你有完整历史用于计数)。
- 若你把 upstream 的 .git 放进 drivers/kernelsu(不建议),Makefile 会自动计算版本,但通常我们把 .git 移除并写入 KSU_VERSION。
- 若你更喜欢我直接把计算/替换命令包装成一行脚本,或帮你生成 Makefile 环境变量补丁,我可以把补丁文本/脚本贴出来。

如果你要我现在做的具体事,选其一回复:

1. 我把核对 tag 存在并计算 KSU_VERSION 的命令给出(我已给上面);你告诉我 ls-remote 输出或 KSU_VERSION 值,我帮你生成 sed+commit 的一行命令。
2. 我生成并给出可直接 git apply 的 Makefile 环境变量补丁。
3. 或者你已经照着做完了,只想我确认某一条输出 —— 把你运行 git ls-remote 的结果或 drivers/kernelsu/Makefile 的当前前几行贴来,我检查并确认没问题。

###########################################

KernelSU

mv KernelSU /tmp/KernelSU_src || true
git rm --cached KernelSU 2>/dev/null || true
git rm --cached drivers/kernelsu 2>/dev/null || true
# 同时删掉工作树中的 symlink(我们将用真实目录替代)
rm -rf KernelSU drivers/kernelsu
cp -a /tmp/KernelSU_src/kernel drivers/kernelsu
# 确保目录实际复制成功
ls -la drivers/kernelsu | sed -n '1,200p'
# 删除任何嵌套的 git 元数据,防止再次被当作子模块或带来嵌套仓库问题
find drivers/kernelsu -name .git -type d -exec rm -rf {} +
# 也删除 gitlink 文件(如果有)
find drivers/kernelsu -name .gitmodules -type f -exec rm -f {} +

UPSTREAM_TAG="v0.9.5"
UPSTREAM_REF="ksu_work1"   # 或你想记录的来源
echo "KernelSU ${UPSTREAM_TAG} (imported from ${UPSTREAM_REF}), imported at $(date)" > drivers/kernelsu/VENDOR


grep -q '^obj-$(CONFIG_KSU) += kernelsu/' drivers/Makefile || printf '\nobj-$(CONFIG_KSU) += kernelsu/\n' >> drivers/Makefile
grep -q 'source "drivers/kernelsu/Kconfig"' drivers/Kconfig || sed -i '/endmenu/i\source "drivers/kernelsu/Kconfig"' drivers/Kconfig

git add drivers/kernelsu drivers/kernelsu/VENDOR drivers/Makefile drivers/Kconfig

if [ -f .gitmodules ]; then
    git rm --cached .gitmodules 2>/dev/null || true
    rm -f .gitmodules
    git commit -m "Remove .gitmodules (KernelSU vendored directly)" || true
fi







  方法 1(最快、最直接)—— 直接把正确的数字写到 Makefile(推荐临时/快速验证)

1. 在另一处克隆 upstream 并计算 v0.9.5 对应的数字:
    
    

    git clone https://github.com/tiann/KernelSU.git /tmp/KernelSU_full
    cd /tmp/KernelSU_full
    git checkout v0.9.5
    GIT_COUNT=$(git rev-list --count HEAD)
    echo "git_count=$GIT_COUNT"
    KSU_VERSION=$((10000 + GIT_COUNT + 200))
    echo "KSU_VERSION=$KSU_VERSION"
    
2. 把该值写到 drivers/kernelsu/Makefile 的 fallback 中(把 16 替换为上面算出的值):

sed -i "s/ccflags-y += -DKSU_VERSION=16/ccflags-y += -DKSU_VERSION=${KSU_VERSION}/" drivers/kernelsu/Makefile