From e276c0c68204bb6c115b6f75bf45491b34a84841 Mon Sep 17 00:00:00 2001 From: Nathan Chancellor Date: Mon, 4 Mar 2024 16:26:01 -0700 Subject: [PATCH] env: Make building Arch Linux packages like other distributions Signed-off-by: Nathan Chancellor --- configs/kernel/.gitignore | 1 + .../kernel/linux-mainline-llvm.config | 2 +- .../kernel/linux-next-llvm.config | 9 +- configs/tmux/next.yml | 2 +- fish/functions/cbl_bld_krnl_pkg.fish | 44 +-- fish/functions/cbl_bld_lnx_dbg.fish | 115 ------ fish/functions/cbl_gen_archconfig.fish | 101 ----- fish/functions/cbl_rb_fd.fish | 51 +-- fish/functions/cbl_rb_pi.fish | 48 +-- fish/functions/cbl_setup_linux_repos.fish | 12 +- fish/functions/cbl_upd_krnl.fish | 5 +- fish/functions/cbl_upd_krnl_pkg.fish | 17 - fish/functions/cbl_upd_krnl_pkgver.fish | 24 -- fish/functions/gen_lnx_pkgver.fish | 7 - pkgbuilds/linux-mainline-llvm/.gitignore | 4 - pkgbuilds/linux-mainline-llvm/PKGBUILD | 124 ------ pkgbuilds/linux-next-llvm/.gitignore | 3 - pkgbuilds/linux-next-llvm/PKGBUILD | 106 ------ ...295f599cab2ab3b3df53a9098adba4a6002b.patch | 180 --------- python/lib/kernel.py | 95 ++++- python/scripts/cbl_bld_krnl_pkg.py | 360 ++++++++++++++++++ python/scripts/gen_lnx_pkgver.py | 67 ---- 22 files changed, 474 insertions(+), 903 deletions(-) create mode 100644 configs/kernel/.gitignore rename pkgbuilds/linux-mainline-llvm/config => configs/kernel/linux-mainline-llvm.config (99%) rename pkgbuilds/linux-next-llvm/config => configs/kernel/linux-next-llvm.config (99%) delete mode 100644 fish/functions/cbl_bld_lnx_dbg.fish delete mode 100644 fish/functions/cbl_gen_archconfig.fish delete mode 100644 fish/functions/cbl_upd_krnl_pkg.fish delete mode 100644 fish/functions/cbl_upd_krnl_pkgver.fish delete mode 100644 fish/functions/gen_lnx_pkgver.fish delete mode 100644 pkgbuilds/linux-mainline-llvm/.gitignore delete mode 100644 pkgbuilds/linux-mainline-llvm/PKGBUILD delete mode 100644 pkgbuilds/linux-next-llvm/.gitignore delete mode 100644 pkgbuilds/linux-next-llvm/PKGBUILD delete mode 100644 pkgbuilds/linux-next-llvm/revert-d87c295f599cab2ab3b3df53a9098adba4a6002b.patch create mode 100755 python/scripts/cbl_bld_krnl_pkg.py delete mode 100755 python/scripts/gen_lnx_pkgver.py diff --git a/configs/kernel/.gitignore b/configs/kernel/.gitignore new file mode 100644 index 000000000..645e6a20c --- /dev/null +++ b/configs/kernel/.gitignore @@ -0,0 +1 @@ +*.old diff --git a/pkgbuilds/linux-mainline-llvm/config b/configs/kernel/linux-mainline-llvm.config similarity index 99% rename from pkgbuilds/linux-mainline-llvm/config rename to configs/kernel/linux-mainline-llvm.config index ca7e9e477..83104d51d 100644 --- a/pkgbuilds/linux-mainline-llvm/config +++ b/configs/kernel/linux-mainline-llvm.config @@ -1,6 +1,6 @@ # # Automatically generated file; DO NOT EDIT. -# Linux/x86 6.8.0-rc7 Kernel Configuration +# Linux/x86_64 6.8.0-rc7 Kernel Configuration # CONFIG_CC_VERSION_TEXT="ClangBuiltLinux clang version 19.0.0git (https://github.com/llvm/llvm-project e6e53ca8470d719882539359ebe3ad8b442a8cb0)" CONFIG_GCC_VERSION=0 diff --git a/pkgbuilds/linux-next-llvm/config b/configs/kernel/linux-next-llvm.config similarity index 99% rename from pkgbuilds/linux-next-llvm/config rename to configs/kernel/linux-next-llvm.config index 2e03e0c6b..75e496839 100644 --- a/pkgbuilds/linux-next-llvm/config +++ b/configs/kernel/linux-next-llvm.config @@ -1,8 +1,8 @@ # # Automatically generated file; DO NOT EDIT. -# Linux/x86 6.8.0-rc6 Kernel Configuration +# Linux/x86_64 6.8.0-rc7 Kernel Configuration # -CONFIG_CC_VERSION_TEXT="ClangBuiltLinux clang version 19.0.0git (https://github.com/llvm/llvm-project 1a2960bab6381f2b288328e2371829b460ac020c)" +CONFIG_CC_VERSION_TEXT="ClangBuiltLinux clang version 19.0.0git (https://github.com/llvm/llvm-project e6e53ca8470d719882539359ebe3ad8b442a8cb0)" CONFIG_GCC_VERSION=0 CONFIG_CC_IS_CLANG=y CONFIG_CLANG_VERSION=190000 @@ -18,7 +18,7 @@ CONFIG_CC_HAS_ASM_GOTO_TIED_OUTPUT=y CONFIG_TOOLS_SUPPORT_RELR=y CONFIG_CC_HAS_ASM_INLINE=y CONFIG_CC_HAS_NO_PROFILE_FN_ATTR=y -CONFIG_PAHOLE_VERSION=125 +CONFIG_PAHOLE_VERSION=126 CONFIG_IRQ_WORK=y CONFIG_BUILDTIME_TABLE_SORT=y CONFIG_THREAD_INFO_IN_TASK=y @@ -11091,7 +11091,6 @@ CONFIG_CRYPTO_USER_API_RNG=m # CONFIG_CRYPTO_USER_API_RNG_CAVP is not set CONFIG_CRYPTO_USER_API_AEAD=m # CONFIG_CRYPTO_USER_API_ENABLE_OBSOLETE is not set -CONFIG_CRYPTO_STATS=y # end of Userspace interface CONFIG_CRYPTO_HASH_INFO=y @@ -11749,6 +11748,8 @@ CONFIG_RUNTIME_TESTING_MENU=y # CONFIG_ATOMIC64_SELFTEST is not set CONFIG_ASYNC_RAID6_TEST=m # CONFIG_TEST_HEXDUMP is not set +# CONFIG_STRING_SELFTEST is not set +# CONFIG_TEST_STRING_HELPERS is not set # CONFIG_TEST_KSTRTOX is not set # CONFIG_TEST_PRINTF is not set # CONFIG_TEST_SCANF is not set diff --git a/configs/tmux/next.yml b/configs/tmux/next.yml index e21c078bb..115ef87bf 100644 --- a/configs/tmux/next.yml +++ b/configs/tmux/next.yml @@ -6,7 +6,7 @@ windows: panes: - shell: dbxe shell_command: cbl_upd_krnl next-llvm - start_directory: $ENV_FOLDER/pkgbuilds/linux-next-llvm + start_directory: $CBL_SRC_P/linux-next-llvm enter: false focus: true - shell: dbxe diff --git a/fish/functions/cbl_bld_krnl_pkg.fish b/fish/functions/cbl_bld_krnl_pkg.fish index 29a202b27..cd3259bab 100644 --- a/fish/functions/cbl_bld_krnl_pkg.fish +++ b/fish/functions/cbl_bld_krnl_pkg.fish @@ -1,45 +1,7 @@ #!/usr/bin/env fish # SPDX-License-Identifier: MIT -# Copyright (C) 2021-2023 Nathan Chancellor +# Copyright (C) 2024 Nathan Chancellor -function cbl_bld_krnl_pkg -d "Build ClangBuiltLinux Arch Linux kernel package" - in_container_msg -c; or return - - for arg in $argv - switch $arg - case --cfi --cfi-permissive -f --full -l --local --lto -m --menuconfig -u --ubsan-bounds - set -a config_args $arg - case -p --permissive - set -a config_args --cfi-permissive - case '*'debug '*'mainline'*' '*'next'*' - set pkg linux-(string replace 'linux-' '' $arg) - end - end - if not set -q pkg - set pkg linux-mainline-llvm - end - - set pkgroot $ENV_FOLDER/pkgbuilds/$pkg - - pushd $pkgroot; or return - - # Prerequisite: Clean up old kernels - rm -- *.tar.zst - - # Generate .config - cbl_gen_archconfig $config_args $pkg; or return - - # Update the pkgver if using a local tree - if grep -q "file://" PKGBUILD - cbl_upd_krnl_pkgver (basename $PWD) - end - - # Build the kernel - makepkg -C; or return - - echo Run - printf '\n\t$ sudo fish -c "pacman -U %s; and bootctl set-oneshot %s.conf; and reboot"\n\n' (realpath -- *.tar.zst | string replace $ENV_FOLDER \$ENV_FOLDER) $pkg - echo "to install and use new kernel." - - popd +function cbl_bld_krnl_pkg -d "Wrapper for cbl_bld_krnl_pkg.py" + $PYTHON_SCRIPTS_FOLDER/cbl_bld_krnl_pkg.py $argv end diff --git a/fish/functions/cbl_bld_lnx_dbg.fish b/fish/functions/cbl_bld_lnx_dbg.fish deleted file mode 100644 index 9a3378c36..000000000 --- a/fish/functions/cbl_bld_lnx_dbg.fish +++ /dev/null @@ -1,115 +0,0 @@ -#!/usr/bin/env fish -# SPDX-License-Identifier: MIT -# Copyright (C) 2022-2023 Nathan Chancellor - -function cbl_bld_lnx_dbg -d "Build linux-debug Arch Linux package" - in_kernel_tree; or return - - set bld (tbf) - - for arg in $argv - switch $arg - case --cfi - set -a scripts_cfg_args -e CFI_CLANG - case --cfi-permissive - set -a scripts_cfg_args \ - -e CFI_CLANG \ - -e CFI_PERMISSIVE - case -g --gcc - set gcc true - case -l --localmodconfig - set localmodconfig true - case -m --menuconfig - set menuconfig true - case -z --zero-call-used-regs - set -a scripts_cfg_args -e ZERO_CALL_USED_REGS - case '*' - set -a kmake_args $arg - end - end - - if test "$gcc" = true - if not string match -qr CROSS_COMPILE= $kmake_args - set kmake_args (korg_gcc var x86_64) - end - else - if not string match -qr LLVM= $kmake_args - set kmake_args LLVM=1 - end - end - set -a kmake_args O=$bld - set make_args -s O=$bld - - set pkg linux-debug - set pkgroot $bld/pkgbuild - set pkgdir $pkgroot/pkg-prepared/$pkg - - ############# - # prepare() # - ############# - git cl -q - - echo -debug >localversion.10-pkgname - - prep_config https://gitlab.archlinux.org/archlinux/packaging/packages/linux/-/raw/main/config $bld;or return - - # Keep in sync with cbl_gen_archconfig, step 2 - scripts/config \ - --file $bld/.config \ - $scripts_cfg_args \ - -m DRM - - kmake $kmake_args olddefconfig; or return - - if test "$localmodconfig" = true - yes "" | kmake $kmake_args LSMOD=/tmp/modprobed.db localmodconfig; or return - end - - if test "$menuconfig" = true - kmake $kmake_args menuconfig; or return - end - - make $make_args kernelrelease >version - - ########### - # build() # - ########### - kmake $kmake_args all; or return - - ############# - # package() # - ############# - set kernver (cat version) - set modulesdir $pkgdir/usr/lib/modules/$kernver - - install -Dm644 $bld/(make $make_args image_name) $modulesdir/vmlinuz; or return - echo "$pkg" | install -Dm644 /dev/stdin $modulesdir/pkgbase; or return - ZSTD_CLEVEL=19 kmake $kmake_args DEPMOD=/doesnt/exist INSTALL_MOD_PATH=$pkgdir/usr INSTALL_MOD_STRIP=1 modules_install; or return - rm -f $modulesdir/{source,build} - - # Call makepkg with a dynamically generated PKGBUILD - echo 'pkgname='$pkg' -pkgver='(git describe | string replace -a - .)' -pkgrel=1 -arch=(x86_64) -license=(GPL2) -options=(\'!strip\') - -package() { - pkgdesc="The Linux kernel and modules" - depends=(coreutils kmod initramfs) - optdepends=(\'crda: to set the correct wireless channels of your country\' - \'linux-firmware: firmware images needed for some devices\') - provides=(VIRTUALBOX-GUEST-MODULES WIREGUARD-MODULE) - replaces=(virtualbox-guest-modules-arch wireguard-arch) - - local pkgroot="${pkgdir//\\/pkg\\/$pkgname/}" - rm -fr "$pkgroot"/pkg - mv -v "$pkgroot"/pkg-prepared "$pkgroot"/pkg -}' >$pkgroot/PKGBUILD - fish -c "cd $pkgroot; and makepkg -R" - set ret $status - - printf '\a' - return $ret -end diff --git a/fish/functions/cbl_gen_archconfig.fish b/fish/functions/cbl_gen_archconfig.fish deleted file mode 100644 index 97ae9b723..000000000 --- a/fish/functions/cbl_gen_archconfig.fish +++ /dev/null @@ -1,101 +0,0 @@ -#!/usr/bin/env fish -# SPDX-License-Identifier: MIT -# Copyright (C) 2021-2023 Nathan Chancellor - -function cbl_gen_archconfig -d "Generate a configuration file for Arch Linux" - in_container_msg -c; or return - - for arg in $argv - switch $arg - case --cfi --cfi-permissive - set -a config_args -e CFI_CLANG - if test $arg = --cfi-permissive - set -a config_args -e CFI_PERMISSIVE - end - case -f --full - set config full - case -l --local - set config local - case --lto - set -a config_args \ - -d LTO_NONE \ - -e LTO_CLANG_THIN - case -m --menuconfig - set menuconfig true - case -u --ubsan-bounds - set -a config_args \ - -e UBSAN \ - -e UBSAN_BOUNDS \ - -d UBSAN_ALIGNMENT \ - -d UBSAN_BOOL \ - -d UBSAN_DIV_ZERO \ - -d UBSAN_ENUM \ - -d UBSAN_SHIFT \ - -d UBSAN_UNREACHABLE - case linux-debug linux-mainline-'*' linux-next-'*' - set pkg $arg - end - end - - switch $pkg - case linux-mainline-'*' - set gpg_key 79BE3E4300411886 - case linux-next-'*' - set gpg_key 89F91C0A41D5C07A - end - if not gpg -k $gpg_key &>/dev/null - gpg --receive-keys $gpg_key - end - - set linux $ENV_FOLDER/pkgbuilds/$pkg - set cfg $linux/config - set src $linux/src/$pkg - set src_cfg $src/.config - - pushd $linux; or return - - # Step 1: Download and extract files - touch $cfg - makepkg -Cdo --skipchecksums; or return - rm $src_cfg - - # Step 2: Copy default Arch configuration and set a few options - crl https://gitlab.archlinux.org/archlinux/packaging/packages/linux/-/raw/main/config >$cfg - $src/scripts/config \ - --file $cfg \ - -d LOCALVERSION_AUTO \ - -e DEBUG_INFO_DWARF5 \ - -e WERROR \ - -m DRM - - # Step 3: Run olddefconfig - kmake -C $src KCONFIG_CONFIG=$cfg olddefconfig - - # Step 4: Run localmodconfig if requested - if test "$config" = local - cp $cfg $src_cfg - yes "" | kmake -C $src localmodconfig - cp $src_cfg $cfg - end - - # Step 5: Run through olddefconfig with Clang - kmake -C $src KCONFIG_CONFIG=$cfg LLVM=1 LLVM_IAS=1 olddefconfig - - # Step 6: Enable ThinLTO, CFI, or UBSAN - if test -n "$config_args" - $src/scripts/config \ - --file $cfg \ - $config_args - kmake -C $src KCONFIG_CONFIG=$cfg LLVM=1 LLVM_IAS=1 olddefconfig - end - - # Step 7: Run menuconfig if additional options are needed - if test "$menuconfig" = true - kmake -C $src KCONFIG_CONFIG=$cfg LLVM=1 LLVM_IAS=1 menuconfig - end - - # Step 8: Update checksums - updpkgsums - - popd -end diff --git a/fish/functions/cbl_rb_fd.fish b/fish/functions/cbl_rb_fd.fish index 8336ee348..014c27a29 100644 --- a/fish/functions/cbl_rb_fd.fish +++ b/fish/functions/cbl_rb_fd.fish @@ -6,56 +6,11 @@ function cbl_rb_fd -d "Rebase generic Fedora kernel on latest linux-next" in_container_msg -c or return - set fd_src $CBL_SRC_P/fedora - pushd $fd_src - or return - - # Update kernel - git ru --prune origin - or return - git rh origin/master - - # Patching - for revert in $reverts - git revert --mainline 1 --no-edit $revert - or return - end - set -a b4_patches https://lore.kernel.org/all/20240301-pci-imx6-fix-clang-implicit-fallthrough-v1-1-db78c7cbb384@kernel.org/ # PCI: imx6: Fix clang -Wimplicit-fallthrough in imx6_pcie_probe() - for patch in $b4_patches - b4 shazam -l -P _ -s $patch - or begin - set ret $status - git ama - return $ret - end - end - for patch in $crl_patches - crl $patch | git am -3 - or begin - set ret $status - git ama - return $ret - end - end - for hash in $ln_commits - git -C $CBL_SRC_P/linux-next fp -1 --stdout $hash | git am - or begin - set ret $status - git ama - return $ret - end - end - for patch in $am_patches - git am -3 $patch - or begin - set ret $status - git ama - return $ret - end - end + # Prepare kernel source + PYTHONPATH=$PYTHON_FOLDER/lib python3 -c "import kernel; kernel.prepare_source('fedora')" # Build kernel - cbl_bld_krnl_rpm --cfi --lto arm64 + fish -c "cd $CBL_SRC_P/fedora; and cbl_bld_krnl_rpm --cfi --lto arm64" or return popd diff --git a/fish/functions/cbl_rb_pi.fish b/fish/functions/cbl_rb_pi.fish index 22b5d4d1e..60ce3cb0f 100644 --- a/fish/functions/cbl_rb_pi.fish +++ b/fish/functions/cbl_rb_pi.fish @@ -19,52 +19,10 @@ function cbl_rb_pi -d "Rebase Raspberry Pi kernel on latest linux-next" end end - begin - pushd $pi_src - and git ru -p origin - end - or return - - git rh origin/master - - # Patching - for revert in $reverts - git revert --mainline 1 --no-edit $revert; or return - end - for patch in $b4_patches - b4 shazam -l -P _ -s $patch - or begin - set ret $status - git ama - return $ret - end - end - for patch in $crl_patches - crl $patch | git am -3 - or begin - set ret $status - git ama - return $ret - end - end - for hash in $ln_commits - git -C $CBL_SRC_P/linux-next fp -1 --stdout $hash | git am - or begin - set ret $status - git ama - return $ret - end - end - for patch in $am_patches - git am -3 $patch - or begin - set ret $status - git ama - return $ret - end - - end + # Prepare kernel source + PYTHONPATH=$PYTHON_FOLDER/lib python3 -c "import kernel; kernel.prepare_source('rpi')" + pushd $pi_src for arch in $pi_arches switch $arch case arm diff --git a/fish/functions/cbl_setup_linux_repos.fish b/fish/functions/cbl_setup_linux_repos.fish index 3c87c3472..27dd73748 100644 --- a/fish/functions/cbl_setup_linux_repos.fish +++ b/fish/functions/cbl_setup_linux_repos.fish @@ -55,10 +55,16 @@ function cbl_setup_linux_repos -d "Clone ClangBuiltLinux Linux repos into their end rm -rf $tmp_dir - # Set up Fedora and Raspberry Pi source worktrees - for worktree in $CBL_SRC_P/{fedora,rpi} + # Set up Arch Linux, Fedora, and Raspberry Pi source worktrees + for worktree in $CBL_SRC_P/{fedora,linux-{mainline,next}-llvm,rpi} if not test -d $worktree - git -C $CBL_SRC_D/linux-next worktree add -B (basename $worktree) --no-track $worktree + switch (basename $worktree) + case linux-mainline-llvm + set src_tree $CBL_SRC_D/linux + case '*' + set src_tree $CBL_SRC_D/linux-next + end + git -C $src_tree worktree add -B (basename $worktree) --no-track $worktree or return end end diff --git a/fish/functions/cbl_upd_krnl.fish b/fish/functions/cbl_upd_krnl.fish index 4ab3f104d..03044de0f 100644 --- a/fish/functions/cbl_upd_krnl.fish +++ b/fish/functions/cbl_upd_krnl.fish @@ -52,7 +52,7 @@ function cbl_upd_krnl -d "Update machine's kernel" end case hetzner-server workstation - cbl_upd_krnl_pkg $argv + cbl_bld_krnl_pkg --cfi --lto $argv case pi3 in_container_msg -h; or return @@ -109,7 +109,8 @@ function cbl_upd_krnl -d "Update machine's kernel" cd /tmp; or return - scp $remote_user@$remote_host:(string replace $MAIN_FOLDER $remote_main_folder $ENV_FOLDER)/pkgbuilds/$krnl/'*'.tar.zst .; or return + set remote_krnl_bld (tbf $krnl | string replace $TMP_BUILD_FOLDER $remote_tmp_build_folder) + scp $remote_user@$remote_host:$remote_krnl_bld/pkgbuild/$krnl-'*'.tar.zst .; or return sudo pacman -U --noconfirm *$krnl*.tar.zst diff --git a/fish/functions/cbl_upd_krnl_pkg.fish b/fish/functions/cbl_upd_krnl_pkg.fish deleted file mode 100644 index c4a9bcddc..000000000 --- a/fish/functions/cbl_upd_krnl_pkg.fish +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env fish -# SPDX-License-Identifier: MIT -# Copyright (C) 2021-2023 Nathan Chancellor - -function cbl_upd_krnl_pkg -d "Update Arch Linux ClangBuiltLinux kernels" - for arg in $argv - set krnl linux-(string replace 'linux-' '' $arg) - set pkgbuild $ENV_FOLDER/pkgbuilds/$krnl/PKGBUILD - if not test -f $pkgbuild - print_error "$pkgbuild does not exist!" - return 1 - end - vim $pkgbuild; or return - - cbl_bld_krnl_pkg --cfi --lto $krnl - end -end diff --git a/fish/functions/cbl_upd_krnl_pkgver.fish b/fish/functions/cbl_upd_krnl_pkgver.fish deleted file mode 100644 index 2ce05caed..000000000 --- a/fish/functions/cbl_upd_krnl_pkgver.fish +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env fish -# SPDX-License-Identifier: MIT -# Copyright (C) 2021-2023 Nathan Chancellor - -function cbl_upd_krnl_pkgver -d "Update the pkgver variable in a kernel PKGBUILD" - for arg in $argv - switch $arg - case -b --bisect - set bisect true - case '*' - set krnl linux-(string replace 'linux-' '' $arg) - end - end - - set pkgbuild $ENV_FOLDER/pkgbuilds/$krnl - - if test "$bisect" = true - set src $pkgbuild/src/$krnl - else - set src $CBL_SRC_D/$krnl - end - - sed -i 's/pkgver=.*/pkgver='(git -C $src describe | string replace -a '-' '.')'/g' $pkgbuild/PKGBUILD -end diff --git a/fish/functions/gen_lnx_pkgver.fish b/fish/functions/gen_lnx_pkgver.fish deleted file mode 100644 index c4d526f0a..000000000 --- a/fish/functions/gen_lnx_pkgver.fish +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env fish -# SPDX-License-Identifier: MIT -# Copyright (C) 2024 Nathan Chancellor - -function gen_lnx_pkgver -d "Wrapper for gen_lnx_pkgver.py" - $PYTHON_SCRIPTS_FOLDER/gen_lnx_pkgver.py $argv -end diff --git a/pkgbuilds/linux-mainline-llvm/.gitignore b/pkgbuilds/linux-mainline-llvm/.gitignore deleted file mode 100644 index 58da41200..000000000 --- a/pkgbuilds/linux-mainline-llvm/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -config.old -linux-mainline-llvm/ -*.c -!*.patch diff --git a/pkgbuilds/linux-mainline-llvm/PKGBUILD b/pkgbuilds/linux-mainline-llvm/PKGBUILD deleted file mode 100644 index 27f8158eb..000000000 --- a/pkgbuilds/linux-mainline-llvm/PKGBUILD +++ /dev/null @@ -1,124 +0,0 @@ -# Maintainer: Nathan Chancellor -# Contributor: Jan Alexander Steffens (heftig) - -_localversion=-llvm -pkgname=linux-mainline$_localversion -_srctag=v6.8-rc7 -pkgver=${_srctag//-/.} -pkgrel=1 -pkgdesc='Linux' -url="https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/log/?h=$_srctag" -arch=(x86_64) -license=(GPL2) -makedepends=( - bc - cpio - libelf - pahole - perl - tar - xz -) -options=('!strip') -source=( - "$pkgname::git+https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git?signed#tag=$_srctag" - config - https://github.com/pop-os/system76-io-dkms/raw/master/system76-io.c - https://github.com/pop-os/system76-io-dkms/raw/master/system76-io_dev.c - https://github.com/pop-os/system76-io-dkms/raw/master/system76-io_hwmon.c -) -validpgpkeys=( - 'ABAF11C65A2970B130ABE3C479BE3E4300411886' # Linus Torvalds -) -sha256sums=('SKIP' - '62ea67e070ee38032614e30134e2ba83ca8a8b1f3989c6b4f342d5981fcc7c58' - 'd569ee2fbd072a09e89196779ccc0dfd008d5a79a0cec5fa80cc7ed99a3027fa' - '7276b7fb345f6a5156c594969a01e1ab63e864e0b3ed646bf5132b8301fa4975' - '26b42e2a48cc4666ad6f85eb4d0fe87f30ea0097d6c1cff4f45e0e3330382916') - -# This obviously ties this PKGBUILD to my machine but that is kind of the point :) -_kmake() { - set -x - make \ - -skj"$(nproc)" \ - HOSTLDFLAGS=-fuse-ld=lld \ - LLVM=${LLVM:-$CBL_TC_LLVM/} \ - LOCALVERSION= \ - "$@" - set +x -} - -prepare() { - cd $pkgname - - echo "Setting version..." - echo "${_localversion}" > localversion.10-pkgname - # Future versions of scripts/setlocalversion mess with this, so - # just do it here - if head=$(git rev-parse --verify HEAD 2>/dev/null); then - if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then - if atag="$(git describe 2>/dev/null)"; then - echo "$atag" | awk -F- '{printf("-%05d", $(NF-1))}' - fi - # Add -g and exactly 12 hex chars. - printf '%s%s' -g "$(echo $head | cut -c1-12)" - fi >localversion.20-git - fi - - for file in system76-io{,_dev,_hwmon}.c; do - cp -v "$(readlink -f ../$file)" drivers/hwmon - done - echo "obj-m += system76-io.o" >> drivers/hwmon/Makefile - - local src - for src in "${source[@]}"; do - src="${src%%::*}" - src="${src##*/}" - [[ $src = *.patch ]] || continue - echo "Applying patch $src..." - patch -Np1 < "../$src" - done - - echo "Setting config..." - cp ../config .config - _kmake olddefconfig - diff -u ../config .config || : - - _kmake -s kernelrelease > version - echo "Prepared $pkgbase version $( -# Contributor: Jan Alexander Steffens (heftig) - -_localversion=-llvm -pkgname=linux-next$_localversion -_srctag=next-20240301 -pkgver=${_srctag//-/.} -pkgrel=1 -pkgdesc='linux-next' -url="https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/log/?h=$_srctag" -arch=(x86_64) -license=(GPL2) -makedepends=( - bc - cpio - libelf - pahole - perl - tar - xz -) -options=('!strip') -source=( - "$pkgname::git+https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git?signed#tag=$_srctag" - config - revert-d87c295f599cab2ab3b3df53a9098adba4a6002b.patch -) - -validpgpkeys=( - '985B681FA45919699753A26489F91C0A41D5C07A' # Stephen Rothwell - '3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB' # Mark Brown -) -sha256sums=('SKIP' - '7ae38060e7a4df5276c5229945e9b76568dd366dab50f738f7ae40feb751c4a0' - '2c5d1f7180465e1db5d09bb70d70b00f2142f1f18b529066843d67a0be8e3dff') - -# This obviously ties this PKGBUILD to my machine but that is kind of the point :) -_kmake() { - set -x - make \ - -skj"$(nproc)" \ - HOSTLDFLAGS=-fuse-ld=lld \ - LLVM=${LLVM:-$CBL_TC_LLVM/} \ - LOCALVERSION= \ - "$@" - set +x -} - -prepare() { - cd $pkgname - - echo "Setting version..." - echo "${_localversion}" > localversion.10-pkgname - - local src - for src in "${source[@]}"; do - src="${src%%::*}" - src="${src##*/}" - [[ $src = *.patch ]] || continue - echo "Applying patch $src..." - patch -Np1 < "../$src" - done - - echo "Setting config..." - cp ../config .config - _kmake olddefconfig prepare - diff -u ../config .config || : - - _kmake -s kernelrelease > version - echo "Prepared $pkgbase version $(attr.name); - } - --static umode_t __first_visible(const struct attribute_group *grp, struct kobject *kobj) --{ -- if (grp->attrs && grp->is_visible) -- return grp->is_visible(kobj, grp->attrs[0], 0); -- -- if (grp->bin_attrs && grp->is_bin_visible) -- return grp->is_bin_visible(kobj, grp->bin_attrs[0], 0); -- -- return 0; --} -- - static int create_files(struct kernfs_node *parent, struct kobject *kobj, - kuid_t uid, kgid_t gid, - const struct attribute_group *grp, int update) -@@ -63,7 +52,6 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj, - kernfs_remove_by_name(parent, (*attr)->name); - if (grp->is_visible) { - mode = grp->is_visible(kobj, *attr, i); -- mode &= ~SYSFS_GROUP_INVISIBLE; - if (!mode) - continue; - } -@@ -93,7 +81,6 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj, - (*bin_attr)->attr.name); - if (grp->is_bin_visible) { - mode = grp->is_bin_visible(kobj, *bin_attr, i); -- mode &= ~SYSFS_GROUP_INVISIBLE; - if (!mode) - continue; - } -@@ -140,31 +127,16 @@ static int internal_create_group(struct kobject *kobj, int update, - - kobject_get_ownership(kobj, &uid, &gid); - if (grp->name) { -- umode_t mode = __first_visible(grp, kobj); -- -- if (mode & SYSFS_GROUP_INVISIBLE) -- mode = 0; -- else -- mode = S_IRWXU | S_IRUGO | S_IXUGO; -- - if (update) { - kn = kernfs_find_and_get(kobj->sd, grp->name); - if (!kn) { -- pr_debug("attr grp %s/%s not created yet\n", -- kobj->name, grp->name); -- /* may have been invisible prior to this update */ -- update = 0; -- } else if (!mode) { -- sysfs_remove_group(kobj, grp); -- kernfs_put(kn); -- return 0; -+ pr_warn("Can't update unknown attr grp name: %s/%s\n", -+ kobj->name, grp->name); -+ return -EINVAL; - } -- } -- -- if (!update) { -- if (!mode) -- return 0; -- kn = kernfs_create_dir_ns(kobj->sd, grp->name, mode, -+ } else { -+ kn = kernfs_create_dir_ns(kobj->sd, grp->name, -+ S_IRWXU | S_IRUGO | S_IXUGO, - uid, gid, kobj, NULL); - if (IS_ERR(kn)) { - if (PTR_ERR(kn) == -EEXIST) -@@ -307,8 +279,9 @@ void sysfs_remove_group(struct kobject *kobj, - if (grp->name) { - kn = kernfs_find_and_get(parent, grp->name); - if (!kn) { -- pr_debug("sysfs group '%s' not found for kobject '%s'\n", -- grp->name, kobject_name(kobj)); -+ WARN(!kn, KERN_WARNING -+ "sysfs group '%s' not found for kobject '%s'\n", -+ grp->name, kobject_name(kobj)); - return; - } - } else { -diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h -index a42642b277dd..b717a70219f6 100644 ---- a/include/linux/sysfs.h -+++ b/include/linux/sysfs.h -@@ -61,32 +61,22 @@ do { \ - /** - * struct attribute_group - data structure used to declare an attribute group. - * @name: Optional: Attribute group name -- * If specified, the attribute group will be created in a -- * new subdirectory with this name. Additionally when a -- * group is named, @is_visible and @is_bin_visible may -- * return SYSFS_GROUP_INVISIBLE to control visibility of -- * the directory itself. -+ * If specified, the attribute group will be created in -+ * a new subdirectory with this name. - * @is_visible: Optional: Function to return permissions associated with an -- * attribute of the group. Will be called repeatedly for -- * each non-binary attribute in the group. Only read/write -+ * attribute of the group. Will be called repeatedly for each -+ * non-binary attribute in the group. Only read/write - * permissions as well as SYSFS_PREALLOC are accepted. Must -- * return 0 if an attribute is not visible. The returned -- * value will replace static permissions defined in struct -- * attribute. Use SYSFS_GROUP_VISIBLE() when assigning this -- * callback to specify separate _group_visible() and -- * _attr_visible() handlers. -+ * return 0 if an attribute is not visible. The returned value -+ * will replace static permissions defined in struct attribute. - * @is_bin_visible: - * Optional: Function to return permissions associated with a - * binary attribute of the group. Will be called repeatedly - * for each binary attribute in the group. Only read/write -- * permissions as well as SYSFS_PREALLOC (and the -- * visibility flags for named groups) are accepted. Must -- * return 0 if a binary attribute is not visible. The -- * returned value will replace static permissions defined -- * in struct bin_attribute. If @is_visible is not set, Use -- * SYSFS_GROUP_VISIBLE() when assigning this callback to -- * specify separate _group_visible() and _attr_visible() -- * handlers. -+ * permissions as well as SYSFS_PREALLOC are accepted. Must -+ * return 0 if a binary attribute is not visible. The returned -+ * value will replace static permissions defined in -+ * struct bin_attribute. - * @attrs: Pointer to NULL terminated list of attributes. - * @bin_attrs: Pointer to NULL terminated list of binary attributes. - * Either attrs or bin_attrs or both must be provided. -@@ -101,42 +91,13 @@ struct attribute_group { - struct bin_attribute **bin_attrs; - }; - --#define SYSFS_PREALLOC 010000 --#define SYSFS_GROUP_INVISIBLE 020000 -- --/* -- * The first call to is_visible() in the create / update path may -- * indicate visibility for the entire group -- */ --#define DEFINE_SYSFS_GROUP_VISIBLE(name) \ -- static inline umode_t sysfs_group_visible_##name( \ -- struct kobject *kobj, struct attribute *attr, int n) \ -- { \ -- if (n == 0 && !name##_group_visible(kobj)) \ -- return SYSFS_GROUP_INVISIBLE; \ -- return name##_attr_visible(kobj, attr, n); \ -- } -- --/* -- * Same as DEFINE_SYSFS_GROUP_VISIBLE, but for groups with only binary -- * attributes -- */ --#define DEFINE_SYSFS_BIN_GROUP_VISIBLE(name) \ -- static inline umode_t sysfs_group_visible_##name( \ -- struct kobject *kobj, struct bin_attribute *attr, int n) \ -- { \ -- if (n == 0 && !name##_group_visible(kobj)) \ -- return SYSFS_GROUP_INVISIBLE; \ -- return name##_attr_visible(kobj, attr, n); \ -- } -- --#define SYSFS_GROUP_VISIBLE(fn) sysfs_group_visible_##fn -- - /* - * Use these macros to make defining attributes easier. - * See include/linux/device.h for examples.. - */ - -+#define SYSFS_PREALLOC 010000 -+ - #define __ATTR(_name, _mode, _show, _store) { \ - .attr = {.name = __stringify(_name), \ - .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \ diff --git a/python/lib/kernel.py b/python/lib/kernel.py index bf8920ca8..3b6973fce 100755 --- a/python/lib/kernel.py +++ b/python/lib/kernel.py @@ -6,9 +6,75 @@ from pathlib import Path import shutil import subprocess +import sys import time -from . import utils +sys.path.append(str(Path(__file__).resolve().parents[1])) +# pylint: disable=wrong-import-position +import lib.utils +# pylint: enable=wrong-import-position + + +def prepare_source(base_name, base_ref='origin/master'): + if base_name == 'linux-debug': + return # managed outside of the script + if base_name not in ('fedora', 'linux-next-llvm', 'linux-mainline-llvm', 'rpi'): + raise RuntimeError(f"Don't know how to handle provided base_name ('{base_name}')?") + + reverts = [] + b4_patches = [] + crl_patches = [] + ln_commits = [] + am_patches = [] + + if base_name == 'fedora': + # PCI: imx6: Fix clang -Wimplicit-fallthrough in imx6_pcie_probe() + b4_patches.append('https://lore.kernel.org/all/20240301-pci-imx6-fix-clang-implicit-fallthrough-v1-1-db78c7cbb384@kernel.org/') # yapf: disable + if base_name == 'linux-mainline-llvm': + # xfrm: Avoid clang fortify warning in copy_to_user_tmpl() + crl_patches.append('https://git.kernel.org/klassert/ipsec/p/1a807e46aa93ebad1dfbed4f82dc3bf779423a6e') # yapf: disable + if base_name == 'rpi': + # drm/sun4i: hdmi: Fix u64 div on 32bit arch + b4_patches.append('https://lore.kernel.org/all/20240304091225.366325-1-mripard@kernel.org/') + + source_folder = Path(os.environ['CBL_SRC_P'], base_name) + + subprocess.run(['git', 'remote', 'update', '--prune', 'origin'], check=True, cwd=source_folder) + subprocess.run(['git', 'reset', '--hard', base_ref], check=True, cwd=source_folder) + + # pylint: disable=subprocess-run-check + try: + common_kwargs = {'check': True, 'cwd': source_folder, 'text': True} + + for revert in reverts: + subprocess.run( # noqa: PLW1510 + ['git', 'revert', '--mainline', '1', '--no-edit', revert], **common_kwargs) + + for b4_patch in b4_patches: + subprocess.run( # noqa: PLW1510 + ['b4', 'shazam', '-l', '-P', '_', '-s', b4_patch], **common_kwargs) + + for crl_patch in crl_patches: + patch_input = subprocess.run(['curl', '-LSs', crl_patch], + capture_output=True, + check=True, + text=True).stdout + subprocess.run(['git', 'am', '-3'], **common_kwargs, input=patch_input) # noqa: PLW1510 + + for ln_commit in ln_commits: + patch_input = subprocess.run(['git', 'fp', '-1', '--stdout', ln_commit], + capture_output=True, + check=True, + cwd=Path(os.environ['CBL_SRC_P'], 'linux-next'), + text=True).stdout + subprocess.run(['git', 'am', '-3'], **common_kwargs, input=patch_input) # noqa: PLW1510 + + for am_patch in am_patches: + subprocess.run(['git', 'am', '-3', am_patch], **common_kwargs) # noqa: PLW1510 + # pylint: enable=subprocess-run-check + except subprocess.CalledProcessError as err: + subprocess.run(['git', 'ama'], check=False, cwd=source_folder) + sys.exit(err.returncode) # Basically '$binary --version | head -1' @@ -17,7 +83,15 @@ def get_tool_version(binary_path): text=True).stdout.splitlines()[0] -def kmake(variables, targets, ccache=True, directory=None, jobs=None, silent=True, use_time=False): +def kmake(variables, + targets, + ccache=True, + directory=None, + env=None, + jobs=None, + silent=True, + stdin=None, + use_time=False): # Handle kernel directory right away if not (kernel_src := Path(directory) if directory else Path()).exists(): raise RuntimeError(f"Derived kernel source ('{kernel_src}') does not exist?") @@ -69,7 +143,8 @@ def kmake(variables, targets, ccache=True, directory=None, jobs=None, silent=Tru if shutil.which('ccache'): variables['CC'] = f"ccache {compiler}" else: - utils.print_yellow('WARNING: ccache requested by it could not be found, ignoring...') + lib.utils.print_yellow( + 'WARNING: ccache requested by it could not be found, ignoring...') # V=1 or V=2 should imply '-v' if 'V' in variables: @@ -81,8 +156,8 @@ def kmake(variables, targets, ccache=True, directory=None, jobs=None, silent=Tru flags += [f"-{'s' if silent else ''}kj{jobs if jobs else os.cpu_count()}"] # Print information about current compiler - utils.print_green(f"\nCompiler location:\033[0m {compiler_location}\n") - utils.print_green(f"Compiler version:\033[0m {get_tool_version(compiler)}\n") + lib.utils.print_green(f"\nCompiler location:\033[0m {compiler_location}\n") + lib.utils.print_green(f"Compiler version:\033[0m {get_tool_version(compiler)}\n") # Print information about the binutils being used, if they are being used # Account for implicit LLVM_IAS change in f12b034afeb3 ("scripts/Makefile.clang: default to LLVM_IAS=1") @@ -94,8 +169,8 @@ def kmake(variables, targets, ccache=True, directory=None, jobs=None, silent=Tru f"GNU as could not be found based on CROSS_COMPILE ('{cross_compile}')?") as_location = Path(gnu_as).parent if as_location != compiler_location: - utils.print_green(f"Binutils location:\033[0m {as_location}\n") - utils.print_green(f"Binutils version:\033[0m {get_tool_version(gnu_as)}\n") + lib.utils.print_green(f"Binutils location:\033[0m {as_location}\n") + lib.utils.print_green(f"Binutils version:\033[0m {get_tool_version(gnu_as)}\n") # Build and run make command make_cmd = [ @@ -108,9 +183,9 @@ def kmake(variables, targets, ccache=True, directory=None, jobs=None, silent=Tru if not (gnu_time := shutil.which('time')): raise RuntimeError('Could not find time binary in PATH?') make_cmd = [gnu_time, '-v', *make_cmd] - utils.print_cmd(make_cmd) + lib.utils.print_cmd(make_cmd) if not use_time: start_time = time.time() - subprocess.run(make_cmd, check=True) + subprocess.run(make_cmd, check=True, env=env, stdin=stdin) if not use_time: - print(f"\nTime: {utils.get_duration(start_time)}") + print(f"\nTime: {lib.utils.get_duration(start_time)}") diff --git a/python/scripts/cbl_bld_krnl_pkg.py b/python/scripts/cbl_bld_krnl_pkg.py new file mode 100755 index 000000000..b6710ed40 --- /dev/null +++ b/python/scripts/cbl_bld_krnl_pkg.py @@ -0,0 +1,360 @@ +#!/usr/bin/env python3 + +from argparse import ArgumentParser +import os +from pathlib import Path +import shutil +import subprocess +import sys + +import korg_tc + +sys.path.append(str(Path(__file__).resolve().parents[1])) +# pylint: disable=wrong-import-position +import lib.kernel +# pylint: enable=wrong-import-position + + +class KernelPkgBuilder: + + def __init__(self, source_folder=None): + + if not source_folder: + source_folder = Path.cwd() + if not Path(source_folder, 'Makefile').exists(): + raise RuntimeError( + f"Derived kernel source ('{source_folder}') does not appear to be a Linux kernel tree?", + ) + + self._source_folder = source_folder + self._build_folder = Path(os.environ['TMP_BUILD_FOLDER'], + self._source_folder.name) # same as tbf + + self.extra_sc_args = [] + self.make_variables = { + 'ARCH': 'x86_64', + 'HOSTLDFLAGS': '-fuse-ld=lld', + 'LLVM': os.environ.get('LLVM', f"{os.environ['CBL_TC_LLVM']}/"), + 'LOCALVERSION': '', + 'O': self._build_folder, + } + + self._kernver = '' + self._pkgname = 'linux-' + self._source_folder.name.replace('linux-', '') + + def _kmake(self, targets, **kwargs): + lib.kernel.kmake(self.make_variables.copy(), + targets, + directory=self._source_folder, + **kwargs) + + def _prepare_files(self, _localmodconfig=False, _menuconfig=False): + src_config_file = Path(os.environ['ENV_FOLDER'], f"configs/kernel/{self._pkgname}.config") + dst_config_file = Path(self._build_folder, '.config') + base_sc_cmd = [Path(self._source_folder, 'scripts/config'), '--file', src_config_file] + kconfig_env = {'KCONFIG_CONFIG': src_config_file, **os.environ} + plain_make_vars = {'ARCH': 'x86_64', 'LOCALVERSION': '', 'O': self._build_folder} + + # Step 1: Copy default Arch configuration and set a few options + crl_cmd = [ + 'curl', + '-LSs', + '-o', src_config_file, + 'https://gitlab.archlinux.org/archlinux/packaging/packages/linux/-/raw/main/config', + ] # yapf: disable + subprocess.run(crl_cmd, check=True) + sc_cmd = [ + *base_sc_cmd, + '-d', 'LOCALVERSION_AUTO', + '-e', 'DEBUG_INFO_DWARF5', + '-e', 'WERROR', + '-m', 'DRM', + ] # yapf: disable + subprocess.run(sc_cmd, check=True) + + # Step 2: Run olddefconfig + lib.kernel.kmake(plain_make_vars.copy(), ['olddefconfig'], + directory=self._source_folder, + env=kconfig_env) + + # Step 3: Run through olddefconfig with Clang + self._kmake(['olddefconfig'], env=kconfig_env) + + # Step 4: Enable ThinLTO, CFI, or UBSAN (and any other requested configurations) + if self.extra_sc_args: + subprocess.run([*base_sc_cmd, *self.extra_sc_args], check=True) + self._kmake(['olddefconfig'], env=kconfig_env) + + # Copy new configuration into place + shutil.rmtree(self._build_folder) + self._build_folder.mkdir(parents=True) + shutil.copyfile(src_config_file, dst_config_file) + + self._kmake(['olddefconfig', 'prepare']) + subprocess.run( + ['git', '--no-pager', 'diff', '--no-index', src_config_file, dst_config_file], + check=False) + + print('Setting version...') + Path(self._build_folder, 'localversion.10-pkgname').write_text('-llvm\n', encoding='utf-8') + + def build(self): + self._kmake(['all']) + + def package(self): + if (pkgroot := Path(self._build_folder, 'pkgbuild')).exists(): + shutil.rmtree(pkgroot) + pkgroot.mkdir(parents=True) + + pkgdir = Path(pkgroot, 'pkg-prepared', self._pkgname) + modulesdir = Path(pkgdir, 'usr/lib/modules', self._kernver) + + print('Installing boot image...') + kernel_image = subprocess.run(['make', '-s', f"O={self._build_folder}", 'image_name'], + capture_output=True, + cwd=self._source_folder, + text=True, + check=False).stdout.strip() + # systemd expects to find the kernel here to allow hibernation + # https://github.com/systemd/systemd/commit/edda44605f06a41fb86b7ab8128dcf99161d2344 + subprocess.run([ + 'install', + '-Dm644', + Path(self._build_folder, kernel_image), + Path(modulesdir, 'vmlinuz'), + ], + check=True) + + # Used by mkinitcpio to name the kernel + (pkgbase := Path(modulesdir, 'pkgbase')).write_text(f"{self._pkgname}\n", encoding='utf-8') + pkgbase.chmod(0o644) + + print('Installing modules...') + modules_env = {'ZSTD_CLEVEL': '19', **os.environ} + modules_vars = { + **self.make_variables, + 'DEPMOD': '/doesnt/exist', + 'INSTALL_MOD_PATH': Path(pkgdir, 'usr'), + 'INSTALL_MOD_STRIP': 1, + } + lib.kernel.kmake(modules_vars, ['modules_install'], + directory=self._source_folder, + env=modules_env) + + # remove build and source links if they exist + for link in ['source', 'build']: + Path(modulesdir, link).unlink(missing_ok=True) + + pkgver = subprocess.run(['git', 'describe'], + capture_output=True, + check=True, + cwd=self._source_folder, + text=True).stdout.strip().replace('-', '.') + pkgbuild_text = fr""" +pkgname={self._pkgname} +pkgver={pkgver} +pkgrel=1 +pkgdesc='{self._pkgname}' +url="https://kernel.org/" +arch=(x86_64) +license=(GPL2) +options=('!strip') + +package() {{ + pkgdesc="$pkgdesc kernel and modules" + depends=(coreutils kmod initramfs) + optdepends=('crda: to set the correct wireless channels of your country' + 'linux-firmware: firmware images needed for some devices') + provides=(VIRTUALBOX-GUEST-MODULES WIREGUARD-MODULE) + replaces=(virtualbox-guest-modules-arch wireguard-arch) + + local pkgroot="${{pkgdir//\/pkg\/$pkgname/}}" + rm -rf "$pkgroot"/pkg + mv -v "$pkgroot"/pkg-prepared "$pkgroot"/pkg +}}""" + Path(pkgroot, 'PKGBUILD').write_text(pkgbuild_text, encoding='utf-8') + subprocess.run(['makepkg', '-R'], check=True, cwd=pkgroot) + + def prepare(self, base_ref, localmodconfig=False, menuconfig=False): + lib.kernel.prepare_source(self._pkgname, base_ref) + + self._prepare_files(localmodconfig, menuconfig) + + self._kernver = subprocess.run( + ['make', '-s', 'LOCALVERSION=', f"O={self._build_folder}", 'kernelrelease'], + capture_output=True, + cwd=self._source_folder, + text=True, + check=False).stdout.strip() + print(f"Prepared {self._pkgname} version {self._kernver}") + + +class DebugPkgBuilder(KernelPkgBuilder): + + def __init__(self): + + super().__init__(Path(os.environ['CBL_SRC_D'], 'linux-debug')) + + # pylint: disable-next=signature-differs + def _prepare_files(self, localmodconfig, menuconfig): + config = Path(self._build_folder, '.config') + base_sc_cmd = [Path(self._source_folder, 'scripts/config'), '--file', config] + + crl_cmd = [ + 'curl', + '-LSs', + '-o', config, + 'https://gitlab.archlinux.org/archlinux/packaging/packages/linux/-/raw/main/config', + ] # yapf: disable + subprocess.run(crl_cmd, check=True) + sc_cmd = [*base_sc_cmd, '-m', 'DRM', *self.extra_sc_args] + subprocess.run(sc_cmd, check=True) + + self._kmake(['olddefconfig']) + + if localmodconfig: + if not (modprobedb := Path('/tmp/modprobed.db')).exists(): # noqa: S108 + raise RuntimeError(f"localmodconfig requested without {modprobedb}!") + self._kmake(['localmodconfig'], + env={ + 'LSMOD': modprobedb, + **os.environ, + }, + stdin=subprocess.DEVNULL) + + if menuconfig: + self._kmake(['menuconfig']) + + self._kmake(['prepare']) + + print('Setting version...') + Path(self._build_folder, 'localversion.10-pkgname').write_text('-debug\n', encoding='utf-8') + + +class MainlinePkgBuilder(KernelPkgBuilder): + + def __init__(self): + + super().__init__(Path(os.environ['CBL_SRC_P'], 'linux-mainline-llvm')) + + def _prepare_files(self, _localmodconfig=False, _menuconfig=False): + super()._prepare_files() + + git_add_files = ['drivers/hwmon/Makefile'] + for part in ['', '_dev', '_hwmon']: + src_url = f"https://github.com/pop-os/system76-io-dkms/raw/master/system76-io{part}.c" + dst_local = Path(self._source_folder, 'drivers/hwmon', src_url.rsplit('/', 1)[-1]) + subprocess.run(['curl', '-LSs', '-o', dst_local, src_url], check=True) + git_add_files.append(dst_local.relative_to(self._source_folder)) + with Path(self._source_folder, git_add_files[0]).open('a', encoding='utf-8') as file: + file.write('obj-m += system76-io.o\n') + subprocess.run(['git', 'add', *git_add_files], check=True, cwd=self._source_folder) + subprocess.run(['git', 'commit', '-m', 'Add system76-io driver'], + check=True, + cwd=self._source_folder) + + git_kwargs = { + 'capture_output': True, + 'check': False, + 'cwd': self._source_folder, + 'text': True, + } + local_ver_parts = [] + + # pylint: disable=subprocess-run-check + head = subprocess.run( # noqa: PLW1510 + ['git', 'rev-parse', '--verify', 'HEAD'], **git_kwargs).stdout.strip() + exact_match = subprocess.run( # noqa: PLW1510 + ['git', 'describe', '--exact-match'], **git_kwargs).stdout.strip() + if head and exact_match == '': + if (atag := subprocess.run( # noqa: PLW1510 + ['git', 'describe'], **git_kwargs).stdout.strip()): + local_ver_parts.append(f"{int(atag.split('-')[-2]):05}") + local_ver_parts.append(f"g{head[0:12]}") + # pylint: enable=subprocess-run-check + + if local_ver_parts: + Path(self._build_folder, + 'localversion.20-git').write_text(f"-{'-'.join(local_ver_parts)}\n", + encoding='utf-8') + + +class NextPkgBuilder(KernelPkgBuilder): + + def __init__(self): + + super().__init__(Path(os.environ['CBL_SRC_P'], 'linux-next-llvm')) + + +def parse_arguments(): + parser = ArgumentParser(description='Build Arch Linux package from kernel source') + + parser.add_argument('--cfi', action='store_true', help='Enable CONFIG_CFI_CLANG') + parser.add_argument('--cfi-permissive', + action='store_true', + help='Enable CONFIG_CFI_PERMISSIVE') + parser.add_argument('--lto', action='store_true', help='Enable CONFIG_LTO_CLANG_THIN') + + parser.add_argument('-g', '--gcc', action='store_true', help='Build with GCC instead of LLVM') + + parser.add_argument('-l', + '--localmodconfig', + action='store_true', + help='Call localmodconfig during configuration') + parser.add_argument('-m', + '--menuconfig', + action='store_true', + help='Call menuconfig during configuration') + + parser.add_argument('-r', + '--ref', + default='origin/master', + help='Reference to base kernel tree on') + + parser.add_argument('pos_args', + nargs='+', + help='Postitional arguments (package name, make arguments)') + + return parser.parse_args() + + +if __name__ == '__main__': + args = parse_arguments() + + make_vars = {} + for arg in args.pos_args: + if '=' in arg: + make_vars.update([arg.split('=', 1)]) + elif arg in ('debug', 'mainline-llvm', 'next-llvm'): + pkgname = arg + else: + raise RuntimeError(f"Cannot handle positional argument ('{arg}')!") + + builder = { + 'debug': DebugPkgBuilder, + 'mainline-llvm': MainlinePkgBuilder, + 'next-llvm': NextPkgBuilder, + }[pkgname]() + + if args.cfi or args.cfi_permissive: + builder.extra_sc_args += ['-e', 'CFI_CLANG'] + if args.cfi_permissive: + builder.extra_sc_args += ['-e', 'CFI_PERMISSIVE'] + if args.lto: + builder.extra_sc_args += [ + '-d', + 'LTO_NONE', + '-e', + 'LTO_CLANG_THIN', + ] + + if args.gcc and 'CROSS_COMPILE' not in make_vars: + make_vars['CROSS_COMPILE'] = korg_tc.GCCManager().get_cc_as_path( + korg_tc.GCCManager.VERSIONS[-1], 'x86_64') + if 'CROSS_COMPILE' in make_vars: + del builder.make_variables['LLVM'] + builder.make_variables.update(make_vars) + + builder.prepare(args.ref, args.localmodconfig, args.menuconfig) + builder.build() + builder.package() diff --git a/python/scripts/gen_lnx_pkgver.py b/python/scripts/gen_lnx_pkgver.py deleted file mode 100755 index 6341b6987..000000000 --- a/python/scripts/gen_lnx_pkgver.py +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env python3 - -from argparse import ArgumentParser -import os -from pathlib import Path -import re -import sys - -sys.path.append(str(Path(__file__).resolve().parents[1])) -# pylint: disable=wrong-import-position -import lib.utils -# pylint: enable=wrong-import-position - - -def gen_linux_pkgver(args): - if not Path(args.directory, 'Makefile').exists(): - raise RuntimeError( - f"Supplied kernel directory ('{args.directory}') does not appear to be a Linux kernel source tree?", - ) - - if args.update: - lib.utils.call_git(args.directory, ['remote', 'update', '--prune', args.remote]) - - ref = f"{args.remote}/{args.branch}" - - describe = lib.utils.get_git_output(args.directory, ['describe', ref]).replace('-', '.') - if not (match := re.search(r'v([0-9]+)\.([0-9]+)\.', describe)): - raise RuntimeError(f"Version regex did not work for '{describe}'?") - - srccommit = lib.utils.get_git_output(args.directory, ['sha', ref]) - print(f"_srccommit={srccommit}") - - # Transform - # v...g - # into - # v.( + 1).rc0..g - pkgver = describe.replace(match[0], f"v{match[1]}.{int(match[2]) + 1}.rc0.") - print(f"pkgver={pkgver}") - - -def parse_arguments(): - parser = ArgumentParser( - description='Generate _srccommit and pkgver for PKGBUILD based on remote reference') - - parser.add_argument('-b', - '--branch', - default='master', - help='Name of branch on remote (default: %(default)s)') - parser.add_argument('-C', - '--directory', - default=Path(os.environ['CBL_SRC'], 'linux'), - help='Repository to run git commands in (default: %(default)s)', - type=Path) - parser.add_argument('-r', - '--remote', - default='origin', - help='Name of remote (default: %(default)s)') - parser.add_argument('-u', - '--update', - action='store_true', - help='Update remote before generating version (default: no update)') - - return parser.parse_args() - - -if __name__ == '__main__': - gen_linux_pkgver(parse_arguments())