# Copyright 2023-2026 Gentoo Authors # Distributed under the terms of the GNU General Public License v3 EAPI=8 # LLVM support for Clang/LTO builds LLVM_COMPAT=( {17..23} ) LLVM_OPTIONAL=1 inherit kernel-build toolchain-funcs llvm-r2 optfeature # Pin patch and config inputs so Manifest checks cover exact upstream bytes. CACHYOS_PATCHES_COMMIT="f3770b16d069a1bce8dfa63841042137f8b43434" CACHYOS_CONFIGS_COMMIT="099f92d8c38de495015370490290213d72becf1a" CACHYOS_PR="$(( ${PR#r} + 1 ))" # CachyOS pre-patched tarball MY_P="cachyos-$(ver_cut 1-3)-${CACHYOS_PR}" # Gentoo genpatches for the 7.3 series. GENPATCHES_VER=2 # ZFS commit for kernel-builtin-zfs support ZFS_COMMIT="fb48c76e17d3e0092fbba430a9eec1960e1b9961" CACHYOS_SERIES="$(ver_cut 1-2)" CACHYOS_PATCH_URI="https://github.com/CachyOS/kernel-patches/raw/${CACHYOS_PATCHES_COMMIT}/${CACHYOS_SERIES}" CACHYOS_CONFIG_URI="https://github.com/CachyOS/linux-cachyos/raw/${CACHYOS_CONFIGS_COMMIT}" CACHYOS_PATCH_PREFIX="cachyos-kernel-patches-${CACHYOS_PATCHES_COMMIT}-${CACHYOS_SERIES}" CACHYOS_CONFIG_PREFIX="linux-cachyos-${CACHYOS_CONFIGS_COMMIT}" DESCRIPTION="Linux kernel built with CachyOS patches (BORE, LTO, AutoFDO, BBR3 and more)" HOMEPAGE=" https://github.com/CachyOS/linux-cachyos https://github.com/Szowisz/CachyOS-kernels " SRC_URI=" https://github.com/CachyOS/linux/releases/download/${MY_P}/${MY_P}.tar.gz https://dev.gentoo.org/~mpagano/dist/genpatches/genpatches-$(ver_cut 1-2)-${GENPATCHES_VER}.base.tar.xz https://dev.gentoo.org/~mpagano/dist/genpatches/genpatches-$(ver_cut 1-2)-${GENPATCHES_VER}.extras.tar.xz ${CACHYOS_CONFIG_URI}/linux-cachyos/config -> ${CACHYOS_CONFIG_PREFIX}-config bore? ( ${CACHYOS_PATCH_URI}/sched/0001-bore-cachy.patch -> ${CACHYOS_PATCH_PREFIX}-bore.patch ) bmq? ( ${CACHYOS_PATCH_URI}/sched/0001-prjc-cachy.patch -> ${CACHYOS_PATCH_PREFIX}-prjc.patch ) pds? ( ${CACHYOS_PATCH_URI}/sched/0001-prjc-cachy.patch -> ${CACHYOS_PATCH_PREFIX}-prjc.patch ) muqss? ( https://github.com/ckolivas/linux/commit/4ceaa4d07af63d9d495f4cbb570547feb39d1236.patch -> linux-ck-4ceaa4d07af63d9d495f4cbb570547feb39d1236-muqss.patch ) rt? ( ${CACHYOS_PATCH_URI}/misc/0001-rt-i915.patch -> ${CACHYOS_PATCH_PREFIX}-rt-i915.patch ) rt-bore? ( ${CACHYOS_PATCH_URI}/sched/0001-bore-cachy.patch -> ${CACHYOS_PATCH_PREFIX}-bore.patch ${CACHYOS_PATCH_URI}/misc/0001-rt-i915.patch -> ${CACHYOS_PATCH_PREFIX}-rt-i915.patch ) deckify? ( ${CACHYOS_PATCH_URI}/sched/0001-bore-cachy.patch -> ${CACHYOS_PATCH_PREFIX}-bore.patch ${CACHYOS_PATCH_URI}/misc/0001-acpi-call.patch -> ${CACHYOS_PATCH_PREFIX}-acpi-call.patch ${CACHYOS_PATCH_URI}/misc/0001-handheld.patch -> ${CACHYOS_PATCH_PREFIX}-handheld.patch ${CACHYOS_CONFIG_URI}/linux-cachyos-deckify/config -> ${CACHYOS_CONFIG_PREFIX}-deckify-config ) ${CACHYOS_PATCH_URI}/misc/dkms-clang.patch -> ${CACHYOS_PATCH_PREFIX}-dkms-clang.patch aufs? ( ${CACHYOS_PATCH_URI}/misc/0001-aufs-7.3-merge-v20260914.patch -> ${CACHYOS_PATCH_PREFIX}-aufs.patch ) kernel-builtin-zfs? ( https://github.com/cachyos/zfs/archive/${ZFS_COMMIT}.tar.gz -> zfs-${ZFS_COMMIT}.tar.gz ) " S="${WORKDIR}/${MY_P}" LICENSE="GPL-3" KEYWORDS="~amd64" IUSE=" +bore bmq pds muqss rt rt-bore eevdf server deckify aufs kcfi +clang +autofdo +propeller +llvm-lto-thin llvm-lto-full llvm-lto-thin-dist llvm-lto-none kernel-builtin-zfs hz_ticks_100 hz_ticks_250 hz_ticks_300 hz_ticks_500 hz_ticks_600 hz_ticks_750 +hz_ticks_1000 +per-gov tickrate_periodic tickrate_idle +tickrate_full +preempt_full preempt_lazy +o3 os debug +bbr3 hugepage_always hugepage_madvise mgeneric mgeneric_v1 mgeneric_v2 mgeneric_v3 mgeneric_v4 +mnative mzen4 " # PRJC-LFBMQ has no 7.3 patch family # upstream hardened remains on 7.2.11; no 7.3 patch/config REQUIRED_USE=" ^^ ( bore bmq pds muqss rt rt-bore eevdf ) deckify? ( bore ) propeller? ( clang !llvm-lto-full !llvm-lto-none ) clang? ( ${LLVM_REQUIRED_USE} ) autofdo? ( || ( llvm-lto-thin llvm-lto-full llvm-lto-thin-dist ) ) kernel-builtin-zfs? ( !kcfi ) ^^ ( llvm-lto-thin llvm-lto-full llvm-lto-thin-dist llvm-lto-none ) llvm-lto-thin? ( clang ) llvm-lto-full? ( clang ) llvm-lto-thin-dist? ( clang ) kcfi? ( clang ) ^^ ( hz_ticks_100 hz_ticks_250 hz_ticks_300 hz_ticks_500 hz_ticks_600 hz_ticks_750 hz_ticks_1000 ) ^^ ( tickrate_periodic tickrate_idle tickrate_full ) ^^ ( preempt_full preempt_lazy ) ?? ( o3 os debug ) ?? ( hugepage_always hugepage_madvise ) ?? ( mgeneric mgeneric_v1 mgeneric_v2 mgeneric_v3 mgeneric_v4 mnative mzen4 ) " RDEPEND=" !sys-kernel/cachyos-sources:${SLOT} autofdo? ( dev-util/perf[libpfm] ) " BDEPEND=" clang? ( $(llvm_gen_dep ' llvm-core/llvm:${LLVM_SLOT} llvm-core/clang:${LLVM_SLOT} llvm-core/lld:${LLVM_SLOT} ') ) llvm-lto-none? ( dev-util/pahole ) !llvm-lto-none? ( dev-util/pahole-cachyos ) >=dev-util/bindgen-0.72.1 || ( >=dev-lang/rust-bin-1.85.0[rust-src] >=dev-lang/rust-1.85.0[rust-src] ) " PDEPEND=" >=virtual/dist-kernel-${PV} " QA_FLAGS_IGNORED=" usr/src/linux-.*/scripts/gcc-plugins/.*.so usr/src/linux-.*/vmlinux usr/src/linux-.*/arch/powerpc/kernel/vdso.*/vdso.*.so.dbg " _set_hztick_rate() { local _HZ_ticks=$1 if [[ $_HZ_ticks == 300 ]]; then scripts/config -e HZ_300 --set-val HZ 300 || die else scripts/config -d HZ_300 -e "HZ_${_HZ_ticks}" --set-val HZ "${_HZ_ticks}" || die fi } pkg_setup() { if use clang; then llvm-r2_pkg_setup local llvm_bindir llvm_bindir="$(get_llvm_prefix -b)/bin" export LLVM=1 export LLVM_IAS=1 export CC="${llvm_bindir}/clang" export CXX="${llvm_bindir}/clang++" export LD="${llvm_bindir}/ld.lld" export AR="${llvm_bindir}/llvm-ar" export NM="${llvm_bindir}/llvm-nm" export OBJCOPY="${llvm_bindir}/llvm-objcopy" export OBJDUMP="${llvm_bindir}/llvm-objdump" export READELF="${llvm_bindir}/llvm-readelf" export STRIP="${llvm_bindir}/llvm-strip" else tc-export CC CXX fi kernel-build_pkg_setup } src_unpack() { default # Unpack genpatches # (kernel-build does not use kernel-2's UNIPATCH mechanism) # Unpack ZFS if requested if use kernel-builtin-zfs; then unpack "zfs-${ZFS_COMMIT}.tar.gz" mv "zfs-${ZFS_COMMIT}" "${S}/zfs" || die cp "${FILESDIR}/kernel-build.sh" "${S}/" || die fi } src_prepare() { local patches_prefix="${DISTDIR}/${CACHYOS_PATCH_PREFIX}" local configs_prefix="${DISTDIR}/${CACHYOS_CONFIG_PREFIX}" # --- Apply genpatches (base + extras) --- # Genpatches extract into ${WORKDIR}/ as numbered .patch files # Exclude kernel version upgrade patches (10xx_linux-*.patch) since # the CachyOS tarball already includes the latest point release local genpatch genpatch_name genpatch_num local genpatch_exclude="" # The CachyOS pre-patched tarball already contains genpatch 2700. genpatch_exclude="2700" for genpatch in "${WORKDIR}"/*.patch; do [[ -f "${genpatch}" ]] || continue genpatch_name=$(basename "${genpatch}") genpatch_num=${genpatch_name%%_*} local skip=false # Skip kernel upgrade patches (10xx series) [[ ${genpatch_num} == 10* ]] && skip=true # Skip excluded genpatches local exclude for exclude in ${genpatch_exclude}; do [[ ${genpatch_name} == ${exclude}* ]] && skip=true done if ! ${skip}; then eapply "${genpatch}" fi done # --- Apply CachyOS-specific patches --- # Fix AutoFDO/Propeller support for LTO_CLANG_THIN_DIST # The distributed ThinLTO patch by Rong Xu (xur@google.com) did not update # Makefile.autofdo and Makefile.propeller for CONFIG_LTO_CLANG_THIN_DIST. # RFC: https://discourse.llvm.org/t/rfc-distributed-thinlto-build-for-kernel/85934 # Original patch: https://github.com/xur-llvm/linux/commit/d970eaf7d90863e7f2ea7bd0c8fe44d4602c2e86 # Upstream mail: https://lore.kernel.org/linux-kbuild/20250420010214.1963979-1-xur@google.com/ # Fix suggested by marioroy: https://discourse.llvm.org/t/rfc-distributed-thinlto-build-for-kernel/85934/5 # https://github.com/Szowisz/CachyOS-kernels/issues/35 eapply "${FILESDIR}/6.19.0/misc/0002-fix-autofdo-propeller-lto-thin-dist.patch" # Backport: SND_SOC_TAS2783_SDW needs SND_SOC_SDCA && EFI, but # SND_SOC_AMD_LEGACY_SDW_MACH selected it unconditionally. # Upstream: https://git.kernel.org/broonie/sound/c/d06607bafb36 eapply "${FILESDIR}/cachyos-sources-7.3.0-tas2783-sdca-deps.patch" # Point the kernel build at dev-util/pahole-cachyos (merged-CU # --lang_exclude fixes for Rust + LTO + BTF). eapply "${FILESDIR}/cachyos-sources-pahole-cachyos.patch" eapply --fuzz=0 -- "${FILESDIR}/cachyos-sources-7.3.1-rust-btf-lto.patch" if use bore || use rt-bore || use deckify; then eapply --fuzz=0 -- "${patches_prefix}-bore.patch" elif use muqss; then cp "${DISTDIR}/linux-ck-4ceaa4d07af63d9d495f4cbb570547feb39d1236-muqss.patch" "${T}/scheduler.patch" || die eapply --fuzz=0 -d "${T}" -- "${FILESDIR}/cachyos-sources-7.3.0-muqss-rebase.patch" eapply --fuzz=0 -- "${T}/scheduler.patch" elif use bmq || use pds; then eapply --fuzz=0 -- "${patches_prefix}-prjc.patch" fi if use rt || use rt-bore; then eapply "${patches_prefix}-rt-i915.patch" fi if use deckify; then eapply "${patches_prefix}-acpi-call.patch" eapply --fuzz=0 -- "${patches_prefix}-handheld.patch" fi if use aufs; then eapply "${patches_prefix}-aufs.patch" fi if use deckify; then cp "${configs_prefix}-deckify-config" .config || die else cp "${configs_prefix}-config" .config || die fi if use clang; then eapply "${patches_prefix}-dkms-clang.patch" fi # Apply user patches (from /etc/portage/patches/) eapply_user # CachyOS ships a prebuilt BPF object in its source tarball. rm -f tools/testing/selftests/tc-testing/action-ebpf || die # --- Kernel config modifications --- if use server; then scripts/config -d CACHY || die else scripts/config -e CACHY || die fi if use bore; then scripts/config -e SCHED_BORE || die elif use bmq; then scripts/config -e SCHED_ALT -e SCHED_BMQ || die elif use pds; then scripts/config -e SCHED_ALT -d SCHED_BMQ -e SCHED_PDS || die elif use muqss; then # POC is included by fair.c, which MuQSS does not build. scripts/config -e SCHED_MUQSS -d SCHED_POC_SELECTOR || die elif use rt; then scripts/config -e PREEMPT_RT || die elif use rt-bore; then scripts/config -e SCHED_BORE -e PREEMPT_RT || die fi if use deckify; then scripts/config -e ACPI_CALL || die fi if use aufs; then scripts/config -m AUFS_FS || die fi if use deckify; then # Upstream handheld config knobs from linux-cachyos-deckify prepare() scripts/config \ -d RCU_LAZY_DEFAULT_OFF \ -e AMD_PRIVATE_COLOR \ -m SENSORS_STEAMDECK \ -m MFD_STEAMDECK \ -m SND_SOC_AW87XXX \ -m HID_ASUS_ALLY \ -m HID_LENOVO_GO \ -m HID_LENOVO_GO_S \ -m ZOTAC_ZONE_HID \ -m LEDS_STEAMDECK \ -m LEDS_VALVE \ -m ZOTAC_ZONE_PLATFORM \ -m EXTCON_STEAMDECK \ -m HID_MSI || die fi ### Enable KCFI if use kcfi; then scripts/config -e CFI -e CFI_AUTO_DEFAULT || die else # https://github.com/openzfs/zfs/issues/15911 scripts/config -d CFI -e CFI_PERMISSIVE || die fi ### Select LLVM level if use llvm-lto-thin; then scripts/config -d LTO_NONE -d LTO_CLANG_FULL -d LTO_CLANG_THIN_DIST \ -e LTO_CLANG_THIN || die elif use llvm-lto-thin-dist; then scripts/config -d LTO_NONE -d LTO_CLANG_FULL -d LTO_CLANG_THIN \ -e LTO_CLANG_THIN_DIST || die elif use llvm-lto-full; then scripts/config -d LTO_NONE -d LTO_CLANG_THIN -d LTO_CLANG_THIN_DIST \ -e LTO_CLANG_FULL || die elif use llvm-lto-none; then scripts/config -d LTO_CLANG_FULL -d LTO_CLANG_THIN -d LTO_CLANG_THIN_DIST \ -e LTO_NONE || die fi scripts/config --set-str DRM_PANIC_SCREEN qr_code -e DRM_PANIC_SCREEN_QR_CODE \ --set-str DRM_PANIC_SCREEN_QR_CODE_URL "https://panic.archlinux.org/panic_report#" \ --set-val DRM_PANIC_SCREEN_QR_VERSION 40 || die ## LLVM patch is applied with the other source patches above. ### Select tick rate if use hz_ticks_100; then _set_hztick_rate 100 elif use hz_ticks_250; then _set_hztick_rate 250 elif use hz_ticks_300; then _set_hztick_rate 300 elif use hz_ticks_500; then _set_hztick_rate 500 elif use hz_ticks_600; then _set_hztick_rate 600 elif use hz_ticks_750; then _set_hztick_rate 750 elif use hz_ticks_1000; then _set_hztick_rate 1000 else die "Invalid HZ_TICKS use flag. Please select a valid option." fi ### Select performance governor if use per-gov; then scripts/config -d CPU_FREQ_DEFAULT_GOV_SCHEDUTIL -e CPU_FREQ_DEFAULT_GOV_PERFORMANCE || die fi ### Select tick type if use tickrate_periodic; then scripts/config -d NO_HZ_IDLE -d NO_HZ_FULL -d NO_HZ -d NO_HZ_COMMON -e HZ_PERIODIC || die elif use tickrate_idle; then scripts/config -d HZ_PERIODIC -d NO_HZ_FULL -e NO_HZ_IDLE -e NO_HZ -e NO_HZ_COMMON || die elif use tickrate_full; then scripts/config \ -d HZ_PERIODIC -d NO_HZ_IDLE -d CONTEXT_TRACKING_FORCE \ -e NO_HZ_FULL -e NO_HZ -e NO_HZ_COMMON \ -e CONTEXT_TRACKING || die fi ### Select preempt type if ! use rt && ! use rt-bore; then scripts/config -e PREEMPT_DYNAMIC || die if use preempt_full; then scripts/config -e PREEMPT -d PREEMPT_LAZY || die elif use preempt_lazy; then scripts/config -d PREEMPT -e PREEMPT_LAZY || die fi fi ### Select compiler optimization if use o3; then scripts/config -d CC_OPTIMIZE_FOR_PERFORMANCE -e CC_OPTIMIZE_FOR_PERFORMANCE_O3 || die elif use os; then scripts/config -d CC_OPTIMIZE_FOR_PERFORMANCE -e CC_OPTIMIZE_FOR_SIZE || die elif use debug; then scripts/config -d CC_OPTIMIZE_FOR_PERFORMANCE \ -d CC_OPTIMIZE_FOR_PERFORMANCE_O3 \ -e CC_OPTIMIZE_FOR_SIZE \ -d SLUB_DEBUG \ -d PM_DEBUG \ -d PM_ADVANCED_DEBUG \ -d PM_SLEEP_DEBUG \ -d ACPI_DEBUG \ -d LATENCYTOP \ -d SCHED_DEBUG \ -d DEBUG_PREEMPT || die fi ### Enable BBR3 if use bbr3; then # Upstream linux-cachyos `_tcp_bbr3` still enables vanilla BBR. # Enable real BBR3 as default, keep vanilla BBR as a module so both # are not built into vmlinux (duplicate tcp_bbr_check_kfunc_ids # BTF set, Szowisz/CachyOS-kernels#53). scripts/config -m TCP_CONG_CUBIC \ -d DEFAULT_CUBIC \ -m TCP_CONG_BBR \ -d DEFAULT_BBR \ -e TCP_CONG_BBR3 \ -e DEFAULT_BBR3 \ --set-str DEFAULT_TCP_CONG bbr3 \ -m NET_SCH_FQ_CODEL \ -e NET_SCH_FQ \ -d DEFAULT_FQ_CODEL \ -e DEFAULT_FQ || die fi # Follow the upstream variant default unless a THP mode is selected. if use hugepage_madvise || { use server && ! use hugepage_always; }; then scripts/config -d TRANSPARENT_HUGEPAGE_ALWAYS -e TRANSPARENT_HUGEPAGE_MADVISE || die else scripts/config -d TRANSPARENT_HUGEPAGE_MADVISE -e TRANSPARENT_HUGEPAGE_ALWAYS || die fi ### Select CPU optimization march_list=(mgeneric mgeneric_v1 mgeneric_v2 mgeneric_v3 mgeneric_v4 mnative mzen4) march_found=false for MMARCH in "${march_list[@]}"; do if use "${MMARCH}"; then MARCH_TRIMMED=${MMARCH:1} MARCH=$(echo "$MARCH_TRIMMED" | tr '[:lower:]' '[:upper:]') case "$MARCH" in GENERIC_V[1-4]) scripts/config -e GENERIC_CPU -d MZEN4 -d X86_NATIVE_CPU \ --set-val X86_64_VERSION "${MARCH//GENERIC_V/}" || die ;; ZEN4) scripts/config -d GENERIC_CPU -e MZEN4 -d X86_NATIVE_CPU || die ;; NATIVE) scripts/config -d GENERIC_CPU -d MZEN4 -e X86_NATIVE_CPU || die ;; esac march_found=true break fi done if [ "$march_found" = false ]; then scripts/config -d GENERIC_CPU -d MZEN4 -e X86_NATIVE_CPU || die fi ### Enable Clang AutoFDO if use autofdo; then scripts/config -e AUTOFDO_CLANG || die fi ### Propeller Optimization if use propeller; then scripts/config -e PROPELLER_CLANG || die fi ### Change hostname scripts/config --set-str DEFAULT_HOSTNAME "gentoo" || die # Gentoo/OpenRC: restore upstream default console loglevel (CachyOS defaults to 4 for silent systemd boot) #41 scripts/config --set-val CONSOLE_LOGLEVEL_DEFAULT 7 || die ### Set LOCALVERSION for dist-kernel identification local myversion="-cachyos-dist" echo "CONFIG_LOCALVERSION=\"${myversion}\"" > "${T}"/version.config || die # Ensure modprobe path is correct echo 'CONFIG_MODPROBE_PATH="/sbin/modprobe"' > "${T}"/modprobe.config || die # --- Finalize config via kernel-build merge --- local merge_configs=( "${T}"/version.config "${T}"/modprobe.config ) kernel-build_merge_configs "${merge_configs[@]}" } pkg_postinst() { kernel-build_pkg_postinst ewarn "" ewarn "${PN} is *not* supported by the Gentoo Kernel Project in any way." ewarn "Report ebuild and kernel problems to https://github.com/Szowisz/CachyOS-kernels." ewarn "Report kernel problems to the CachyOS project, if you sure it's due to upstream." ewarn "Do *not* open bugs in Gentoo's bugzilla. Thank you." ewarn "" if use mnative; then ewarn "USE=mnative builds the kernel with -march=native, which optimizes for your" ewarn "specific CPU. Binary packages built this way are NOT portable to other machines." ewarn "Use USE=mgeneric_v3 or similar for portable builds." fi optfeature "userspace KSM helper" sys-process/uksmd optfeature "NVIDIA opensource module" "x11-drivers/nvidia-drivers[kernel-open]" optfeature "NVIDIA module" x11-drivers/nvidia-drivers optfeature "Realtek RTL8125 2.5GbE driver" net-misc/r8125 optfeature "ZFS support" sys-fs/zfs optfeature "sched_ext schedulers" sys-kernel/scx-loader if use kernel-builtin-zfs; then ewarn "kernel-builtin-zfs only stages ZFS sources and a build helper." ewarn "It does not build or install ZFS modules for this distribution kernel." ewarn "Use sys-fs/zfs and sys-fs/zfs-kmod for managed ZFS support." fi if use autofdo || use propeller; then ewarn "AutoFDO/Propeller are enabled in Kconfig, but they only apply profile-guided" ewarn "optimization when you pass a profile at build time:" ewarn " AutoFDO: CLANG_AUTOFDO_PROFILE=/path/to/profile.afdo" ewarn " Propeller: CLANG_PROPELLER_PROFILE_PREFIX=/path/to/propeller" ewarn "Without a profile, CONFIG_PROPELLER_CLANG still adds" ewarn "-fbasic-block-address-map / --lto-basic-block-address-map (codegen change, no gain)." ewarn "Guide: https://cachyos.org/blog/2411-kernel-autofdo" ewarn "Example: https://github.com/xz-dev/kernel-autofdo-container" fi }