From fa52195536f0b3e4dccb23cbea40f035c41582c9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Luttringer?= <seblu@seblu.net>
Date: Sun, 14 Jun 2020 06:06:55 +0200
Subject: [PATCH] Improve packaging

- fix perms
- strip kernel and executables
- include all headers (e.g: missing vdso/const.h)
- provides VIRTUALBOX-GUEST-MODULES WIREGUARD-MODULE
- remove documentation
- remove sources broken symlinks
- remove sources lost objects
---
 PKGBUILD | 181 +++++++++++++++++++++----------------------------------
 1 file changed, 69 insertions(+), 112 deletions(-)

diff --git a/PKGBUILD b/PKGBUILD
index 6195d32..570cfce 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -21,10 +21,8 @@ sha256sums=('SKIP'
 
 prepare() {
   [[ -d linux-stable ]] && ln -snf linux-stable linux
-
   cd linux
 
-
   # apply patch from the source array (should be a pacman feature)
   local src
   for src in "${source[@]}"; do
@@ -57,7 +55,7 @@ prepare() {
 
 pkgver() {
   cd linux
-  make kernelversion|tr '-' '_'
+  make -s kernelversion|tr '-' '_'
 }
 
 build() {
@@ -80,149 +78,108 @@ package() {
   kernrel=$(make kernelrelease)
   pkgdesc+=", version $kernrel"
   pkgname="linux-$kernrel"
-  provides=("linux=$pkgver" "linux-headers=$pkgver")
+  provides=("linux=$pkgver" "linux-headers=$pkgver" VIRTUALBOX-GUEST-MODULES WIREGUARD-MODULE)
 
   msg2 "Kernel release: $kernrel"
   msg2 "Package kernel version: $pkgver"
 
-  msg2 'Install vmlinuz'
-  install -D -m644 arch/x86/boot/bzImage "$pkgdir/usr/lib/kernel/vmlinuz-$kernrel"
+  msg2 'Installing vmlinuz'
+  install -D -m644 "$(make -s image_name)" "$pkgdir/usr/lib/kernel/vmlinuz-$kernrel"
+
+  msg2 'Stripping vmlinuz'
+  strip -v $STRIP_STATIC "$pkgdir/usr/lib/kernel/vmlinuz-$kernrel"
 
-  msg2 'Install modules'
+  msg2 'Installing modules'
   # disable DEPMOD because it fail due to poor hack in scripts/depmod.sh
   # INSTALL_MOD_STRIP will cause modules to be fully stripped
   make -s INSTALL_MOD_PATH="$pkgdir/usr" INSTALL_MOD_STRIP=1 DEPMOD=/bin/true modules_install
 
-  msg2 'Install sources'
+  msg2 'Generating modules.dep and map files'
+  # run depmod to keep track of these files and remove them with the package
+  depmod -b "$pkgdir/usr" -F System.map "$kernrel"
+
   install_sources
 
-  msg2 'Remove firmwares'
+  msg2 'Removing firmwares'
   rm -rf "$pkgdir/usr/lib/firmware"
-
-  msg2 'Generate modules.dep and map files'
-  # run depmod to keep track of these files and remove them with the package
-  depmod -b "$pkgdir/usr" -F System.map "$kernrel"
 }
 
 install_sources() {
   local kernsrc="$pkgdir/usr/src/$pkgname"
+  local i
+  cd "$srcdir/linux"
 
-  # create $kernsrc directory
+  msg2 'Installing sources'
   install -d -m 755 "$kernsrc"
 
+  msg2 'Installing symlinks in modules/'
   # remove previous build and source links
   rm -f "$pkgdir/usr/lib/modules/$kernrel"/{source,build}
   ln -rs  "$kernsrc" "$pkgdir/usr/lib/modules/$kernrel"/source
   ln -rs  "$kernsrc" "$pkgdir/usr/lib/modules/$kernrel"/build
 
-  # Copy kernel config files
-  cd "$srcdir/linux"
-  install -D -m644 Makefile "$kernsrc/Makefile"
-  install -D -m644 kernel/Makefile "$kernsrc/kernel/Makefile"
-  install -D -m644 .config "$kernsrc/.config"
-  for i in localversion*; do
+  msg2 'Installing build files'
+  install -Dt "$kernsrc" -m644 .config Makefile Module.symvers System.map
+  install -Dt "$kernsrc/kernel" -m644 kernel/Makefile
+  install -Dt "$kernsrc/arch/x86" -m644 arch/x86/Makefile
+  for i in version localversion*; do
     [[ -e "$i" ]] && install -m644 "$i" "$kernsrc/$i"
   done
 
-  # create include directory
-  install -d "$kernsrc/include"
+  msg2 'Installing scripts'
+  cp -t "$kernsrc" -a scripts
 
-  for i in acpi asm-generic config crypto drm generated linux math-emu \
-    media net pcmcia scsi sound trace uapi video xen; do
-    cp -a include/$i "$kernsrc/include/"
-  done
-
-  # copy arch includes for external modules
-  install -d "$kernsrc/arch/x86"
-  cp -a arch/x86/include "$kernsrc/arch/x86/"
-
-  # copy files necessary for later builds, like nvidia and vmware
-  cp Module.symvers "$kernsrc"
-  cp -a scripts "$kernsrc"
-
-  # remove broken symlinks of unneeded architectures in scripts
-  rm -r "$kernsrc/scripts/dtc/include-prefixes"
-
-  # fix permissions on scripts dir
-  chmod og-w -R "$kernsrc/scripts"
-
-  # add x86 makefile
-  install -d "$kernsrc/.tmp_versions"
-  install -d "$kernsrc/arch/x86/kernel"
-  cp arch/x86/Makefile "$kernsrc/arch/x86/"
-  cp arch/x86/kernel/asm-offsets.s "$kernsrc/arch/x86/kernel/"
-
-  # add dm headers
-  install -d "$kernsrc/drivers/md"
-  cp drivers/md/*.h  "$kernsrc/drivers/md"
-
-  # add inotify.h
-  install -d "$kernsrc/include/linux"
-  cp include/linux/inotify.h "$kernsrc/include/linux/"
-
-  # add wireless headers
-  install -d "$kernsrc/net/mac80211/"
-  cp net/mac80211/*.h "$kernsrc/net/mac80211/"
-
-  # add dvb headers for external modules
-  # in reference to:
-  # http://bugs.archlinux.org/task/11194
-  install -d "$kernsrc/include/config/dvb/"
-  cp include/config/dvb/*.h "$kernsrc/include/config/dvb/"
-
-  # add dvb headers for http://mcentral.de/hg/~mrec/em28xx-new
-  # in reference to:
-  # http://bugs.archlinux.org/task/13146
-  install -Dm644 drivers/media/dvb-frontends/lgdt330x.h \
-    "$kernsrc/drivers/media/dvb-frontends/dvb-frontends/lgdt330x.h"
-  install -Dm644 drivers/media/i2c/msp3400-driver.h \
-    "$kernsrc/drivers/media/i2c/msp3400-driver.h"
-
-  # add dvb headers
-  # in reference to:
-  # http://bugs.archlinux.org/task/20402
-  install -d "$kernsrc/drivers/media/usb/dvb-usb"
-  cp drivers/media/usb/dvb-usb/*.h "$kernsrc/drivers/media/usb/dvb-usb/"
-  install -d "$kernsrc/drivers/media/dvb-frontends"
-  cp drivers/media/dvb-frontends/*.h "$kernsrc/drivers/media/dvb-frontends/"
-  install -d "$kernsrc/drivers/media/tuners"
-  cp drivers/media/tuners/*.h "$kernsrc/drivers/media/tuners/"
-
-  # copy in Kconfig files
-  for i in $(find . -name "Kconfig*"); do
-    install -d "$kernsrc"/$(echo $i | sed 's|/Kconfig.*||')
-    cp "$i" "$kernsrc/$i"
-  done
-
-  # remove unneeded architectures added by copying Kconfig
+  # add objtool for external module building when enabled VALIDATION_STACK option
+  # this is adding dependencty to glibc and libelf to the kernel package
+  msg2 'Installing objtool'
+  install -Dt "$kernsrc/tools/objtool" tools/objtool/objtool
+
+  msg2 'Installing headers'
+  cp -t "$kernsrc" -a include
+  # arch includes for external modules
+  cp -t "$kernsrc/arch/x86" -a arch/x86/include
+  install -Dt "$kernsrc/arch/x86/kernel" -m644 arch/x86/kernel/asm-offsets.s
+  # dm headers
+  install -Dt "$kernsrc/drivers/md" -m644 drivers/md/*.h
+  # wireless headers
+  install -Dt "$kernsrc/net/mac80211" -m644 net/mac80211/*.h
+  # dvb headers, http://bugs.archlinux.org/task/13146
+  install -Dt "$kernsrc/drivers/media/i2c" -m644 drivers/media/i2c/msp3400-driver.h
+  # dvb headers, http://bugs.archlinux.org/task/20402
+  install -Dt "$kernsrc/drivers/media/usb/dvb-usb" -m644 drivers/media/usb/dvb-usb/*.h
+  install -Dt "$kernsrc/drivers/media/dvb-frontends" -m644 drivers/media/dvb-frontends/*.h
+  install -Dt "$kernsrc/drivers/media/tuners" -m644 drivers/media/tuners/*.h
+
+  msg2 'Installing Kconfig files'
+  find . -name 'Kconfig*' -exec install -Dm644 {} "$kernsrc/{}" \;
+
+  msg2 'Removing unneeded architectures'
   for i in "$kernsrc"/arch/*; do
     [[ "${i##*/}" != x86 ]] && rm -rf "$i"
   done
 
-  # add objtool for external module building when enabled VALIDATION_STACK option
-  # this is adding dependencty to glibc and libelf to the kernel package
-  install -d "$kernsrc/tools/objtool"
-  cp tools/objtool/objtool "$kernsrc/tools/objtool"
-
-  # fix permissions
-  chown -R root:root "$kernsrc"
-  find "$kernsrc" -type d -exec chmod 755 {} \;
-
-  # strip scripts directory
-  find "$kernsrc/scripts" -type f -perm -u+w 2>/dev/null | while read binary ; do
-    case "$(file -bi "$binary")" in
-      *application/x-sharedlib*) # Libraries (.so)
-        strip $STRIP_SHARED "$binary";;
-      *application/x-archive*) # Libraries (.a)
-        strip $STRIP_STATIC "$binary";;
-      *application/x-executable*) # Binaries
-        strip $STRIP_BINARIES "$binary";;
+  msg2 'Removing documentation'
+  rm -r "$kernsrc/Documentation"
+
+  msg2 'Stripping exectuables'
+  while read -rd '' i; do
+    case "$(file -Sbi "$i")" in
+      application/x-sharedlib\;*)      # Libraries (.so)
+        strip -v $STRIP_SHARED "$i" ;;
+      application/x-archive\;*)        # Libraries (.a)
+        strip -v $STRIP_STATIC "$i" ;;
+      application/x-executable\;*)     # Binaries
+        strip -v $STRIP_BINARIES "$i" ;;
+      application/x-pie-executable\;*) # Relocatable binaries
+        strip -v $STRIP_SHARED "$i" ;;
     esac
-  done
+  done < <(find "$kernsrc" -type f -perm -u+x -print0)
+
+  msg2 'Removing sources broken symlinks'
+  find -L "$kernsrc" -type l -printf 'Removing %P\n' -delete
 
-  # check rights
-  chown -R root.root "$kernsrc"
-  chmod -R u=rwX,go=rX "$kernsrc"
+  msg2 'Removing sources lost objects'
+  find "$kernsrc" -type f -name '*.o' -printf 'Removing %P\n' -delete
 }
 
 # vim:set ts=2 sw=2 ft=sh et:
-- 
GitLab