# Copyright 2026 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 # shellcheck shell=bash # shellcheck disable=SC2034 # @ECLASS: cachyos-kernel-build.eclass # @MAINTAINER: # Xarblu # @AUTHOR: # Xarblu # @SUPPORTED_EAPIS: 8 # @PROVIDES: kernel-build # @BLURB: Build mechanics for CachyOS Kernels # @DESCRIPTION: # This eclass provides the common logic to fetch # and setup all required resources for building the # CachyOS kernels via kernel-build.eclass # # The build must only set the appropriate patch/config/tarball # versions and then inherit this eclass. # @ECLASS_VARIABLE: GENTOO_PATCHSET # @PRE_INHERIT # @DEFAULT_UNSET # @REQUIRED # @DESCRIPTION: # String containing a Gentoo patchset file name # without extension from # https://distfiles.gentoo.org/pub/proj/dist-kernel/patchsets/X.Y/.tar.xz # Any required URL parts are derived from it. [[ -z "${GENTOO_PATCHSET}" ]] && die "GENTOO_PATCHSET is not set" # @ECLASS_VARIABLE: GENTOO_CONFIG_VER # @PRE_INHERIT # @DEFAULT_UNSET # @REQUIRED # @DESCRIPTION: # String containing a Gentoo config release tag. # See https://github.com/gentoo/gentoo-kernel-config # for available tags. [[ -z "${GENTOO_CONFIG_VER}" ]] && die "GENTOO_CONFIG_VER is not set" # @ECLASS_VARIABLE: CACHY_CONFIG_COMMIT # @PRE_INHERIT # @DEFAULT_UNSET # @REQUIRED # @DESCRIPTION: # CachyOS kernel config commit from # https://github.com/CachyOS/linux-cachyos [[ -z "${CACHY_CONFIG_COMMIT}" ]] && die "CACHY_CONFIG_COMMIT is not set" # @ECLASS_VARIABLE: CACHY_PATCH_COMMIT # @PRE_INHERIT # @DEFAULT_UNSET # @REQUIRED # @DESCRIPTION: # CachyOS kernel patch commit from # https://github.com/CachyOS/kernel-patches [[ -z "${CACHY_PATCH_COMMIT}" ]] && die "CACHY_PATCH_COMMIT is not set" # @ECLASS_VARIABLE: CACHY_TAR_REL # @PRE_INHERIT # @DEFAULT_UNSET # @REQUIRED # @DESCRIPTION: # CachyOS kernel tarball release revision from # https://github.com/CachyOS/linux [[ -z "${CACHY_TAR_REL}" ]] && die "CACHY_TAR_REL is not set" # @ECLASS_VARIABLE: BCACHEFS_VER # @PRE_INHERIT # @DEFAULT_UNSET # @DESCRIPTION: # Bcachefs patch version from # https://github.com/xarblu/bcachefs-patches # # If empty/unset a pkg_setup() hook will die # to make users aware the requested version # doesn't have their potential root fs # (instead of just dropping the IUSE which might be missed). : "${BCACHEFS_VER=}" # @ECLASS_VARIABLE: CACHY_FLAVOURS # @PRE_INHERIT # @DEFAULT_UNSET # @REQUIRED # @DESCRIPTION: # Supported linux-cachyos flavours from CachyOS/linux-cachyos (excl. lts/rc) # # These are added to IUSE as well with 'cachyos' # marked as default ('+cachyos') if present [[ -z "${CACHY_FLAVOURS}" ]] && die "CACHY_FLAVOURS is not set" # @ECLASS_VARIABLE: CACHY_PATCH_SPECS # @PRE_INHERIT # @DESCRIPTION: # Array of patches in format # : in # the CachyOS/kernel-patches repo. # # Special use "-" always applies a patch. # Patches are applied in this order. # # Adds IUSE entries as needed. [[ -n "${CACHY_PATCH_SPECS[*]}" ]] || CACHY_PATCH_SPECS=() # @ECLASS_VARIABLE: BAD_PATCHES # @PRE_INHERIT # @DESCRIPTION: # Bash array of bad patches that don't apply # properly and should be skipped. # Usually these are genpatches that are also included # in the cachyos patchset or genpatches that are not rebased yet. [[ -n "${BAD_PATCHES[*]}" ]] || BAD_PATCHES=() # @ECLASS_VARIABLE: EXTRA_KCONF_SNIPPETS # @DESCRIPTION: # Bash array of extra kernel config snippets # to merge. Applied after all standard and USE dependent # configs. # Create snippets in src_prepare, then call cachyos-kernel-build_src_prepare. [[ -n "${EXTRA_KCONF_SNIPPETS[*]}" ]] || EXTRA_KCONF_SNIPPETS=() case ${EAPI} in 8) ;; *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;; esac if [[ ! ${_CACHYOS_KERNEL_BUILD_ECLASS} ]]; then _CACHYOS_KERNEL_BUILD_ECLASS=1 # --- setup for inherited eclasses --- # kernel-install.eclass KERNEL_IUSE_GENERIC_UKI=1 KERNEL_IUSE_MODULES_SIGN=1 # scripts/min-tool-version.sh if ver_test -lt 7.4; then # rust.eclass RUST_MIN_VER="1.85.0" BINDGEN_MIN_VER="0.71.1" else die "Check scripts/min-tool-version.sh for new tool requirements" fi # rust.eclass RUST_NEEDS_LLVM=1 RUST_OPTIONAL=1 RUST_REQ_USE="rust-src" # llvm-r1.eclass # migrate to -r2 once rust.eclass has LLVM_COMPAT=( {19..23} ) LLVM_OPTIONAL=1 inherit eapi9-pipestatus toolchain-funcs flag-o-matic inherit llvm-r1 rust kernel-build verify-sig # --- internal vars --- # @ECLASS_VARIABLE: KERNEL_BASE # @INTERNAL # @DESCRIPTION: # base linux version like 7.0 # @ECLASS_VARIABLE: KERNEL_RC # @INTERNAL # @DESCRIPTION: # release candidate number if this # is an RC # @ECLASS_VARIABLE: KERNEL_PATCH # @INTERNAL # @DESCRIPTION: # patch release version if this # is a stable version # @ECLASS_VARIABLE: KERNEL_REL # @INTERNAL # @DESCRIPTION: # additional version added by # a _p* suffix # Parse Kernel version vars from PV # KERNEL_BASE - base linux version # KERNEL_RC - release candidate patch target # KERNEL_PATCH - stable patch target # KERNEL_REL - _p* version bumped on changes to config/patch vars (0 if unset) if [[ "${PV}" == *_rc* ]]; then # release candidate KERNEL_BASE="$(ver_cut 1-2)" KERNEL_RC="${PV##*_rc}" KERNEL_RC="${KERNEL_RC%_p*}" KERNEL_PATCH="0" KERNEL_REL="${PV##*_p}" elif [[ "${PV}" == *_pre* ]]; then # transitional testing version during merge window # tracks stable releases of the last mainline KERNEL_BASE="$(ver_cut 1).$(( $(ver_cut 2) - 1 ))" KERNEL_RC="0" KERNEL_PATCH="${PV##*_pre}" KERNEL_PATCH="${KERNEL_PATCH%_p*}" KERNEL_REL="${PV##*_p}" else # stable KERNEL_BASE="$(ver_cut 1-2)" KERNEL_RC="0" KERNEL_PATCH="$(ver_cut 3)" KERNEL_REL="${PV##*_p}" fi # default 0 if unset, else whatever is in _p* [[ "${PV}" == "${KERNEL_REL}" ]] && KERNEL_REL="0" # @ECLASS_VARIABLE: CACHY_CONFIG_P # @INTERNAL # @DESCRIPTION: # P for the cachyos kernel config CACHY_CONFIG_P="${PN}-${KERNEL_BASE}-${CACHY_CONFIG_COMMIT::8}" # @ECLASS_VARIABLE: CACHY_PATCH_P # @INTERNAL # @DESCRIPTION: # P for the cachyos kernel patches CACHY_PATCH_P="${PN}-${KERNEL_BASE}-${CACHY_PATCH_COMMIT::8}" DESCRIPTION="Linux kernel built with CachyOS and Gentoo patches" HOMEPAGE=" https://cachyos.org/ https://github.com/CachyOS/linux-cachyos/ https://www.kernel.org/ " IUSE="bcachefs cfi clang debug lto rust scx ${CACHY_FLAVOURS/cachyos/+cachyos}" REQUIRED_USE=" ^^ ( ${CACHY_FLAVOURS} ) bcachefs? ( rust ) cfi? ( clang ) clang? ( ${LLVM_REQUIRED_USE} ) lto? ( clang ) rust? ( ${LLVM_REQUIRED_USE} ) " # CONFIG_RUST requires this: # (!DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE && !LTO) # CONFIG_DEBUG_INFO_BTF and CONFIG_LTO are mutually exclusive REQUIRED_USE+=" rust? ( lto? ( !debug !scx ) ) " # shellcheck disable=SC2016 # we don't want LLVM_SLOT to expand BDEPEND=" clang? ( $(llvm_gen_dep ' llvm-core/clang:${LLVM_SLOT}= llvm-core/lld:${LLVM_SLOT}= llvm-core/llvm:${LLVM_SLOT}= ') ) rust? ( $(llvm_gen_dep 'llvm-core/clang:${LLVM_SLOT}=') >=dev-util/bindgen-${BINDGEN_MIN_VER} ${RUST_DEPEND} ) debug? ( dev-util/pahole ) scx? ( dev-util/pahole ) verify-sig? ( >=sec-keys/openpgp-keys-cachyos-20260615 ) " 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 " VERIFY_SIG_OPENPGP_KEY_PATH=/usr/share/openpgp-keys/cachyos.asc # @ECLASS_VARIABLE: CACHY_BASE_TARBALL # @OUTPUT_VARIABLE # @INTERNAL # @DESCRIPTION: # Filename of the kernel source tarball from CachyOS. # # Set by cachyos-kernel-build_setup_globals. # @FUNCTION: cachyos-kernel-build_setup_globals # @INTERNAL # @DESCRIPTION: # Sets up SRC_URI, S, CACHY_BASE_TARBALL cachyos-kernel-build_setup_globals() { local archive base local spec cond patch file flavour local v # --- CachyOS base tarball --- base="https://github.com/CachyOS/linux/releases/download" if (( KERNEL_RC > 0 )); then archive="cachyos-${KERNEL_BASE}-rc${KERNEL_RC}-${CACHY_TAR_REL}.tar.gz" SRC_URI+=" ${base}/cachyos-${KERNEL_BASE}-rc${KERNEL_RC}-${CACHY_TAR_REL}/${archive} verify-sig? ( ${base}/cachyos-${KERNEL_BASE}-rc${KERNEL_RC}-${CACHY_TAR_REL}/${archive}.asc ) " S="${WORKDIR}/cachyos-${KERNEL_BASE}-rc${KERNEL_RC}-${CACHY_TAR_REL}" else archive="cachyos-${KERNEL_BASE}.${KERNEL_PATCH}-${CACHY_TAR_REL}.tar.gz" SRC_URI+=" ${base}/cachyos-${KERNEL_BASE}.${KERNEL_PATCH}-${CACHY_TAR_REL}/${archive} verify-sig? ( ${base}/cachyos-${KERNEL_BASE}.${KERNEL_PATCH}-${CACHY_TAR_REL}/${archive}.asc ) " S="${WORKDIR}/cachyos-${KERNEL_BASE}.${KERNEL_PATCH}-${CACHY_TAR_REL}" fi CACHY_BASE_TARBALL="${archive}" # --- Gentoo patchset --- if [[ "${GENTOO_PATCHSET}" =~ .*-([0-9.]+)(-r[0-9]+)? ]]; then v="${BASH_REMATCH[1]}" else die "Failed to parse version from ${GENTOO_PATCHSET}" fi SRC_URI+=" https://distfiles.gentoo.org/pub/proj/dist-kernel/patchsets/$(ver_cut 1-2 "${v}")/${GENTOO_PATCHSET}.tar.xz https://github.com/gentoo/gentoo-kernel-config/archive/${GENTOO_CONFIG_VER}.tar.gz -> gentoo-kernel-config-${GENTOO_CONFIG_VER}.tar.gz " # --- CachyOS kernel config --- base="https://raw.githubusercontent.com/CachyOS/linux-cachyos/${CACHY_CONFIG_COMMIT}" if [[ ${PV} == *_rc* ]]; then # RC has no flavours SRC_URI+=" ${base}/linux-cachyos-rc/config -> ${CACHY_CONFIG_P}-rc.config " else for flavour in ${CACHY_FLAVOURS}; do file="${CACHY_CONFIG_P}-${flavour}.config" if [[ "${flavour}" == "cachyos" ]]; then SRC_URI+=" ${flavour}? ( ${base}/linux-cachyos/config -> ${file} ) " else SRC_URI+="${flavour}? ( ${base}/linux-cachyos-${flavour}/config -> ${file} ) " fi done fi # --- CachyOS kernel patches --- base="https://raw.githubusercontent.com/CachyOS/kernel-patches/${CACHY_PATCH_COMMIT}/${KERNEL_BASE}" for spec in "${CACHY_PATCH_SPECS[@]}"; do IFS=":" read -rd '' cond patch < <(printf '%s' "${spec}") file="${CACHY_PATCH_P}-${patch##*/}" case "${cond}" in -) SRC_URI+=" ${base}/${patch} -> ${file} " ;; *) # add IUSE as needed # shellcheck disable=SC2086 has "${cond}" ${IUSE//+/} || IUSE+=" ${cond} " SRC_URI+=" ${cond}? ( ${base}/${patch} -> ${file} ) " ;; esac done # --- bcachefs patch + tools --- if [[ -n "${BCACHEFS_VER}" ]]; then if ver_test "${BCACHEFS_VER}" -le 1.39.4; then BCACHEFS_PATCH="bcachefs-v${BCACHEFS_VER}-for-v${KERNEL_BASE}.patch" else BCACHEFS_PATCH="bcachefs-v${BCACHEFS_VER}.patch" fi SRC_URI+=" bcachefs? ( https://raw.githubusercontent.com/xarblu/bcachefs-patches/refs/heads/main/${KERNEL_BASE}/${BCACHEFS_PATCH} ) " # enforce bcachefs-tools version on minor-level # to make sure there are no weird kernel/user-space # incompatibilities local bch_tools_min if [[ "${BCACHEFS_VER}" == *_pre* ]]; then bch_tools_min="$(ver_cut 1-2 "${BCACHEFS_VER}").0_pre0" else bch_tools_min="$(ver_cut 1-2 "${BCACHEFS_VER}").0" fi RDEPEND+=" bcachefs? ( >=sys-fs/bcachefs-tools-${bch_tools_min} ) " fi } # setup globals cachyos-kernel-build_setup_globals # @FUNCTION: cachy_flavour # @DESCRIPTION: # Get the selected flavour from CACHY_FLAVOURS cachy_flavour() { local flavour for flavour in ${CACHY_FLAVOURS}; do if use "${flavour}"; then printf -- "%s" "${flavour}" || die return 0 fi done die "Could not get selected flavour" } # @FUNCTION: cachy_base_config # @DESCRIPTION: # Get the base config file name cachy_base_config() { case "${PV}" in *_rc*) printf -- "%s-rc.config" "${CACHY_CONFIG_P}" || die ;; *) printf -- "%s-%s.config" "${CACHY_CONFIG_P}" "$(cachy_flavour)" || die ;; esac } # @FUNCTION: cachy_stage_patches # @DESCRIPTION: # Move required patches to ${WORKDIR}/patches # and ensure they get applied in correct order # by prefixing them accordingly. # Then clean up patches listed in BAD_PATCHES. cachy_stage_patches() { local target="${WORKDIR}/patches" local -i incr=1000 einfo "Staging patches to be applied in ${target} ..." mkdir -p "${target}" || die # CachyOS patches are always first # on collision we'll rather drop the incompatible Gentoo patch local spec cond patch file for spec in "${CACHY_PATCH_SPECS[@]}"; do IFS=":" read -r cond patch <<<"${spec}" || die file="${CACHY_PATCH_P}-${patch##*/}" if [[ "${cond}" == "-" ]]; then : elif use "${cond}"; then : else continue fi cp "${DISTDIR}/${file}" "${target}/$(( incr++ ))_${file}" || die done # Gentoo patches pushd "${WORKDIR}/${GENTOO_PATCHSET}" >/dev/null || die local file for file in *.patch; do cp "${file}" "${target}/$(( incr++ ))_${file#*-}" || die # everything after *Add-Gentoo-Linux-support-config-settings* # used to be USE=experimental in gentoo-kernel but I guess # not anymore since 7.0.1 (according to git bisect)? done popd >/dev/null || die # bcachefs patch if use bcachefs; then cp "${DISTDIR}/${BCACHEFS_PATCH}" \ "${target}/$(( incr++ ))_${BCACHEFS_PATCH}" || die fi # remove problematic patches local patch for patch in "${BAD_PATCHES[@]}"; do rm "${target}/${patch}" || die done } # @FUNCTION: cachy_processor_opt # @DESCRIPTION: # Auto-detect closest march value based on CFLAGS # and print the corresponding CONFIG_X86_64_VERSION # value to stdout. # On non-amd64 this will always print "GENERIC". # -march=native is preserved as "NATIVE". # # This is based on _qt6-build_sanitize_cpu_flags() # from qt6-build.eclass cachy_processor_opt() { # not supported but in case someone # builds on non amd64 return default if ! use amd64; then printf "GENERIC" return 0 fi # apply X86_NATIVE_CPU if we have -march=native if [[ "$(get-flag march)" == native ]]; then printf "NATIVE" return 0 fi # get closest march for others # this is mostly a shamelessly ripped from qt6-build.eclass's _qt6-build_sanitize_cpu_flags # Changes: # - C++ -> C # - -march=* -> GENERIC_V* # shellcheck disable=SC2086,SC2155 # *FLAGS should split local march=$( $(tc-getCC) -x c -E -P ${CFLAGS} ${CPPFLAGS} - <<-EOF | sed -n '/^GENERIC_V/p' | tail -n 1 /* ignore evex* for >=gcc-16 and >=clang-22 (bug #956750,#969664) */ /* TODO: drop this and v4's EVEX* when both compilers been stable for a while */ #if (!defined(__clang__) && __GNUC__ >= 16) || __clang_major__ >= 22 # ifndef __EVEX256__ # define __EVEX256__ 1 # endif # ifndef __EVEX512__ # define __EVEX512__ 1 # endif #endif GENERIC_V1 #if (__CRC32__ + __LAHF_SAHF__ + __POPCNT__ + __SSE3__ + __SSE4_1__ + __SSE4_2__ + __SSSE3__) == 7 GENERIC_V2 # if (__AVX__ + __AVX2__ + __BMI__ + __BMI2__ + __F16C__ + __FMA__ + __LZCNT__ + __MOVBE__ + __XSAVE__) == 9 GENERIC_V3 # if (__AVX512BW__ + __AVX512CD__ + __AVX512DQ__ + __AVX512F__ + __AVX512VL__ + __EVEX256__ + __EVEX512__) == 7 GENERIC_V4 # endif # endif #endif EOF pipestatus || die ) case "${march}" in GENERIC_V?) printf "%s" "${march}";; *) die "Got unknown march: ${march}";; esac } # @FUNCTION: kconf # @USAGE: <"set"|"unset"|"mod"|"val"> [value] # @DESCRIPTION: # Print formatted kernel config line to stdout # as in CONFIG_. # # [value] is required for "val" and # needs to be quoted accordingly # by the caller. # For example string values should call an equivalent of: # kconf val FOO "\"bar\"" kconf() { if (( $# < 2 )); then die "kconf needs at least 2 args" fi case "$1" in set) printf -- "CONFIG_%s=y\n" "${2}" || die ;; unset) printf -- "# CONFIG_%s is not set\n" "${2}" || die ;; mod) printf -- "CONFIG_%s=m\n" "${2}" || die ;; val) if [[ -z "${3}" ]]; then die "kconf val requires a value" fi printf -- "CONFIG_%s=%s\n" "${2}" "${3}" || die ;; *) die "invalid option $1 for kconf" ;; esac } # @FUNCTION: cachy_localversion_kconfig # @DESCRIPTION: # Print a CONFIG_LOCALVERSION variant # based on the selected flavour to stdout. cachy_localversion_kconfig() { # shellcheck disable=SC2155 local flavour="$(cachy_flavour)" case "${flavour}" in cachyos) kconf val LOCALVERSION "\"-cachyos\"" ;; *) kconf val LOCALVERSION "\"-cachyos-${flavour}\"" ;; esac } # @FUNCTION: cachy_flavour_defaults_kconfig # @DESCRIPTION: # Kernel config defaults from CachyOS PKGBUILD # based on the selected flavour. # # Users can override individual options using # package.env using the same variable names # as the CachyOS PKGBUILD. cachy_flavour_defaults_kconfig() { # cachy config vars (only those that make sense in ebuild) # advanced users can override these with package.env case "$(cachy_flavour)" in cachyos) : "${_cachy_config:=yes}" : "${_cpusched:=cachyos}" : "${_per_gov:=no}" : "${_tcp_bbr3:=no}" : "${_HZ_ticks:=1000}" : "${_tickrate:=full}" : "${_preempt:=full}" : "${_hugepage:=always}" ;; bmq) : "${_cachy_config:=yes}" : "${_cpusched:=bmq}" : "${_per_gov:=no}" : "${_tcp_bbr3:=no}" : "${_HZ_ticks:=1000}" : "${_tickrate:=full}" : "${_preempt:=full}" : "${_hugepage:=always}" ;; bore) : "${_cachy_config:=yes}" : "${_cpusched:=bore}" : "${_per_gov:=no}" : "${_tcp_bbr3:=no}" : "${_HZ_ticks:=1000}" : "${_tickrate:=full}" : "${_preempt:=full}" : "${_hugepage:=always}" ;; deckify) : "${_cachy_config:=yes}" : "${_cpusched:=cachyos}" : "${_per_gov:=no}" : "${_tcp_bbr3:=no}" : "${_HZ_ticks:=1000}" : "${_tickrate:=full}" : "${_preempt:=full}" : "${_hugepage:=always}" ;; eevdf) : "${_cachy_config:=yes}" : "${_cpusched:=eevdf}" : "${_per_gov:=no}" : "${_tcp_bbr3:=no}" : "${_HZ_ticks:=1000}" : "${_tickrate:=full}" : "${_preempt:=full}" : "${_hugepage:=always}" ;; rt-bore) : "${_cachy_config:=yes}" : "${_cpusched:=rt-bore}" : "${_per_gov:=no}" : "${_tcp_bbr3:=no}" : "${_HZ_ticks:=1000}" : "${_tickrate:=full}" : "${_preempt:=full}" : "${_hugepage:=always}" ;; server) : "${_cachy_config:=no}" : "${_cpusched:=eevdf}" : "${_per_gov:=no}" : "${_tcp_bbr3:=no}" : "${_HZ_ticks:=300}" : "${_tickrate:=idle}" : "${_preempt:=lazy}" : "${_hugepage:=always}" ;; *) die "Unknown flavour" ;; esac : "${_processor_opt:="$(cachy_processor_opt)"}" if use cfi; then : "${_use_kcfi:=yes}" else : "${_use_kcfi:=no}" fi if use lto; then : "${_use_llvm_lto:=thin}" else : "${_use_llvm_lto:=none}" fi # decide default based on CFLAGS if is-flagq '-O3'; then : "${_cc_harder:=yes}" else : "${_cc_harder:=no}" fi # print cachy config einfo "Selected cachy-config:" einfo " _cachy_config=${_cachy_config}" einfo " _cpusched=${_cpusched}" einfo " _cc_harder=${_cc_harder}" einfo " _per_gov=${_per_gov}" einfo " _tcp_bbr3=${_tcp_bbr3}" einfo " _HZ_ticks=${_HZ_ticks}" einfo " _tickrate=${_tickrate}" einfo " _preempt=${_preempt}" einfo " _hugepage=${_hugepage}" einfo " _processor_opt=${_processor_opt}" einfo " _use_kcfi=${_use_kcfi}" einfo " _use_llvm_lto=${_use_llvm_lto}" # _processor_opt local MARCH="${_processor_opt^^}" case "${MARCH}" in GENERIC) ;; GENERIC_V[1-4]) kconf set GENERIC_CPU kconf unset MZEN4 kconf unset X86_NATIVE_CPU kconf val X86_64_VERSION "${MARCH#GENERIC_V}" ;; ZEN4) kconf unset GENERIC_CPU kconf set MZEN4 kconf unset X86_NATIVE_CPU ;; NATIVE) kconf unset GENERIC_CPU kconf unset MZEN4 kconf set X86_NATIVE_CPU ;; *) die "Invalid _processor_opt value: ${_processor_opt}" ;; esac # _cachy_config case "${_cachy_config}" in yes) kconf set CACHY ;; no) ;; *) die "Invalid _cachy_config value: ${_cachy_config}" ;; esac # _cpusched case "${_cpusched}" in bore) kconf set SCHED_BORE ;; bmq) kconf set SCHED_ALT kconf set SCHED_BMQ ;; cachyos|eevdf) ;; rt) kconf set PREEMPT_RT ;; rt-bore) kconf set SCHED_BORE kconf set PREEMPT_RT ;; *) die "Invalid _cpusched value: ${_cpusched}" ;; esac # _use_kcfi case "${_use_kcfi}" in yes) kconf set CFI kconf set CFI_AUTO_DEFAULT ;; no) ;; *) die "Invalid _use_kcfi value: ${_use_kcfi}" ;; esac # _use_llvm_lto case "${_use_llvm_lto}" in thin) kconf set LTO_CLANG_THIN ;; full) kconf set LTO_CLANG_FULL ;; none) kconf set LTO_NONE ;; *) die "Invalid _use_llvm_lto value: ${_use_llvm_lto}" ;; esac # _HZ_ticks case "${_HZ_ticks}" in 100|250|500|600|750|1000) kconf unset HZ_300 kconf set "HZ_${_HZ_ticks}" kconf val HZ "${_HZ_ticks}" ;; 300) kconf set HZ_300 kconf val HZ 300 ;; *) die "Invalid _HZ_ticks value: ${_HZ_ticks}" ;; esac # _per_gov case "${_per_gov}" in yes) kconf unset CPU_FREQ_DEFAULT_GOV_SCHEDUTIL kconf set CPU_FREQ_DEFAULT_GOV_PERFORMANCE ;; no) ;; *) die "Invalid _per_gov value: ${_per_gov}" ;; esac # _tickrate case "${_tickrate}" in periodic) kconf unset NO_HZ_IDLE kconf unset NO_HZ_FULL kconf unset NO_HZ kconf unset NO_HZ_COMMON kconf set HZ_PERIODIC ;; idle) kconf unset HZ_PERIODIC kconf unset NO_HZ_FULL kconf set NO_HZ_IDLE kconf set NO_HZ kconf set NO_HZ_COMMON ;; full) kconf unset HZ_PERIODIC kconf unset NO_HZ_IDLE kconf set NO_HZ_FULL kconf set NO_HZ kconf set NO_HZ_COMMON kconf set CONTEXT_TRACKING ;; *) die "Invalid _tickrate value: ${_tickrate}" ;; esac # _preempt if [[ "${_cpusched}" != rt* ]]; then case "${_preempt}" in full) kconf set PREEMPT kconf unset PREEMPT_LAZY ;; lazy) kconf unset PREEMPT kconf set PREEMPT_LAZY ;; *) die "Invalid _preempt value: ${_preempt}" ;; esac fi # _cc_harder case "${_cc_harder}" in yes) kconf unset CC_OPTIMIZE_FOR_PERFORMANCE kconf set CC_OPTIMIZE_FOR_PERFORMANCE_O3 ;; no) ;; *) die "Invalid _cc_harder value: ${_cc_harder}" ;; esac # _tcp_bbr3 case "${_tcp_bbr3}" in yes) kconf mod TCP_CONG_CUBIC kconf unset DEFAULT_CUBIC kconf set TCP_CONG_BBR kconf val DEFAULT_TCP_CONG "\"bbr\"" if ! use server; then kconf mod NET_SCH_FQ_CODEL kconf set NET_SCH_FQ kconf unset CONFIG_DEFAULT_FQ_CODEL kconf set CONFIG_DEFAULT_FQ fi ;; no) ;; *) die "Invalid _tcp_bbr3 value: ${_tcp_bbr3}" ;; esac # _hugepage case "${_hugepage}" in always) kconf unset TRANSPARENT_HUGEPAGE_MADVISE kconf set TRANSPARENT_HUGEPAGE_ALWAYS ;; madvise) kconf unset TRANSPARENT_HUGEPAGE_ALWAYS kconf set TRANSPARENT_HUGEPAGE_MADVISE ;; *) die "Invalid _hugepage value: ${_hugepage}" ;; esac # handheld only if [[ "$(cachy_flavour)" == deckify ]]; then kconf unset RCU_LAZY_DEFAULT_OFF kconf set AMD_PRIVATE_COLOR kconf mod SENSORS_STEAMDECK kconf mod MFD_STEAMDECK kconf mod SND_SOC_AW87XXX kconf mod HID_ASUS_ALLY kconf mod HID_LENOVO_GO kconf mod HID_LENOVO_GO_S kconf mod HID_MSI_CLAW kconf mod ZOTAC_ZONE_HID kconf mod LEDS_STEAMDECK kconf mod LEDS_VALVE kconf set ACPI_CALL kconf mod ZOTAC_ZONE_PLATFORM kconf mod EXTCON_STEAMDECK kconf mod HID_MSI fi # rust if use rust; then kconf set RUST else kconf unset RUST fi # bcachefs defaults if use bcachefs; then kconf mod BCACHEFS_FS kconf set BCACHEFS_QUOTA kconf set BCACHEFS_LOCK_TIME_STATS kconf set BCACHEFS_SIX_OPTIMISTIC_SPIN fi } # @FUNCTION: scx_kconfig # @DESCRIPTION: # Print a config snippet containing # options required for sched_ext support # to stdout. # # Deps as specified in sys-kernel/scx # (along with deps needed to enable the required set) scx_kconfig() { kconf set BPF kconf set BPF_EVENTS kconf set BPF_JIT kconf set BPF_JIT_ALWAYS_ON kconf set BPF_JIT_DEFAULT_ON kconf set BPF_SYSCALL kconf set DEBUG_INFO kconf set DEBUG_INFO_BTF kconf set DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT kconf set DEBUG_KERNEL kconf set FTRACE kconf set KALLSYMS_ALL kconf set SCHED_CLASS_EXT } # @FUNCTION: cachy_verify_kconfig # @USAGE: # @DESCRIPTION: # Verify that provided config snippets exist after make # has been called. Note that this is more of a QA check # for the ebuild defaults as user configs from # /etc/kernel/config.d/ can override the defaults. cachy_verify_kconfig() { (( ${#} < 1 )) && die "requires at least 1 arg" local kconfig="${WORKDIR}/modprep/.config" if [[ ! -f "${kconfig}" ]]; then eerror "${kconfig} does not exist" die "Did you call kernel-build_src_configure?" fi einfo "Verifying config snippets..." local snippet line local bad_config="false" for snippet; do einfo "Checking ${snippet}..." while read -r line; do if ! grep -q -F "${line}" "${kconfig}"; then ewarn "'${line}' provided in ${snippet} but not in ${kconfig}!" bad_config="true" fi done < <(grep -E '^(CONFIG_|# CONFIG_)' "${snippet}") done if [[ "${bad_config}" == "true" ]]; then ewarn "Some config snippets did not apply correctly!" else einfo "All config snippets applied correctly!" fi } cachyos-kernel-build_pkg_pretend() { # die instead of just masking/dropping USE # to make extra sure users don't unknowingly lose # access to their filesystem if use bcachefs && [[ -z "${BCACHEFS_VER}" ]]; then eerror "bcachefs is currently broken on kernel $(ver_cut 1-2)" eerror "Failing early to make sure you know and don't lose access to your fs" die "broken bcachefs" fi if use bcachefs; then elog "This kernel will have support for bcachefs ${BCACHEFS_VER} built in" fi kernel-install_pkg_pretend } cachyos-kernel-build_pkg_setup() { if [[ "${MERGE_TYPE}" == binary ]]; then kernel-build_pkg_setup return fi if use clang; then # tools passed as MAKEARGS in kernel-build.eclass einfo "Forcing LLVM toolchain due to USE=clang" declare -g AS="llvm-as" declare -g CC="clang" declare -g LD="ld.lld" declare -g AR="llvm-ar" declare -g NM="llvm-nm" declare -g STRIP="llvm-strip" declare -g OBJCOPY="llvm-objcopy" declare -g OBJDUMP="llvm-objdump" declare -g READELF="llvm-readelf" # explicit prepend_path to ensure vars point to correct version llvm_prepend_path -b "${LLVM_SLOT}" fi if use clang || use rust; then llvm-r1_pkg_setup fi if use rust; then rust_pkg_setup fi einfo "Effective toolchain:" einfo "AS: ${AS}" einfo "CC: ${CC}" einfo "LD: ${LD}" einfo "AR: ${AR}" einfo "NM: ${NM}" einfo "STRIP: ${STRIP}" einfo "OBJCOPY: ${OBJCOPY}" einfo "OBJDUMP: ${OBJDUMP}" einfo "READELF: ${READELF}" einfo "RUSTC: ${RUSTC}" kernel-build_pkg_setup } cachyos-kernel-build_src_unpack() { use verify-sig && verify-sig_verify_detached \ "${DISTDIR}/${CACHY_BASE_TARBALL}" \ "${DISTDIR}/${CACHY_BASE_TARBALL}.asc" default } cachyos-kernel-build_src_prepare() { # prepare and stage patches cachy_stage_patches # apply package and user patches eapply "${WORKDIR}/patches" eapply_user local extraversion # keep existing info extraversion="$(grep '^EXTRAVERSION = ' Makefile | sed -e 's:^EXTRAVERSION = \(.*\)$:\1:' || die)" # bump everything to our testing version if [[ "${PV}" == *_pre* ]]; then sed -i -e "s:^\(VERSION =\).*:\1 $(ver_cut 1):" Makefile || die sed -i -e "s:^\(PATCHLEVEL =\).*:\1 $(ver_cut 2):" Makefile || die sed -i -e "s:^\(SUBLEVEL =\).*:\1 0:" Makefile || die extraversion+="-pre${KERNEL_PATCH}" fi # KERNEL_REL 0 doesn't need extraversion if (( KERNEL_REL > 0 )); then # dist-kernel_PV_to_KV only converts the first "_" to "-" # instead of fighting that we'll just use the # slightly uglier -(rc|pre)*_p* if [[ -z "${extraversion}" ]]; then extraversion+="-p${KERNEL_REL}" else extraversion+="_p${KERNEL_REL}" fi fi # add extraversion if [[ -n "${extraversion}" ]]; then sed -i -e "s:^\(EXTRAVERSION =\).*:\1 ${extraversion}:" Makefile || die fi # Localversion cachy_localversion_kconfig > "${T}/cachy-localversion.config" || die # CachyOS config as base cp "${DISTDIR}/$(cachy_base_config)" .config || die # Package defaults cachy_flavour_defaults_kconfig > "${T}/cachy-flavour-defaults.config" || die # Gentoo defaults local dist_conf_path="${WORKDIR}/gentoo-kernel-config-${GENTOO_CONFIG_VER}" local merge_configs=( "${T}/cachy-localversion.config" "${dist_conf_path}/base.config" "${dist_conf_path}/6.12+.config" "${T}/cachy-flavour-defaults.config" ) use debug || merge_configs+=( "${dist_conf_path}/no-debug.config" ) # partially reverts no-debug.config so must come after that one if use scx; then scx_kconfig > "${T}/scx.config" || die merge_configs+=( "${T}/scx.config" ) fi use secureboot && merge_configs+=( "${dist_conf_path}/secureboot.config" "${dist_conf_path}/zboot.config" ) merge_configs+=( "${EXTRA_KCONF_SNIPPETS[@]}" ) kernel-build_merge_configs "${merge_configs[@]}" } cachyos-kernel-build_src_configure() { kernel-build_src_configure # Only check our ebuild generated configs. # Users will likely expect these to work # because they're set via USE flags. # The others are to broad to check and throw too # many false positives. local check_configs=( "${T}/cachy-localversion.config" "${T}/cachy-flavour-defaults.config" ) use scx && check_configs+=( "${T}/scx.config" ) check_configs+=( "${EXTRA_KCONF_SNIPPETS[@]}" ) cachy_verify_kconfig "${check_configs[@]}" } cachyos-kernel-build_pkg_postinst() { kernel-build_pkg_postinst # print info for included modules if use bcachefs && has_version sys-fs/bcachefs-kmod; then elog "bcachefs ${BCACHEFS_VER} is included in ${CATEGORY}/${PN} - no need for sys-fs/bcachefs-kmod" fi if has_version media-video/v4l2loopback; then elog "v4l2loopback is included in ${CATEGORY}/${PN} - no need for media-video/v4l2loopback" fi } fi EXPORT_FUNCTIONS pkg_pretend pkg_setup src_unpack src_prepare src_configure pkg_postinst