diff mbox series

[v2] kbuild: add script and target to generate pacman package

Message ID 20240706-kbuild-pacman-pkg-v2-1-613422a03a7a@weissschuh.net (mailing list archive)
State New
Headers show
Series [v2] kbuild: add script and target to generate pacman package | expand

Commit Message

Thomas Weißschuh July 6, 2024, 7:33 a.m. UTC
pacman is the package manager used by Arch Linux and its derivates.
Creating native packages from the kernel tree has multiple advantages:

* The package triggers the correct hooks for initramfs generation and
  bootloader configuration
* Uninstallation is complete and also invokes the relevant hooks
* New UAPI headers can be installed without any manual bookkeeping

The PKGBUILD file is a simplified version of the one used for the
downstream Arch Linux "linux" package.
Extra steps that should not be necessary for a development kernel have
been removed and an UAPI header package has been added.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
Changes in v2:
- Replace ${MAKE} with $MAKE for consistency with other variables
- Use $MAKE for "-s image_name"
- Avoid permission warnings from build directory
- Clarify reason for /build symlink removal
- Install System.map and config
- Install dtbs where available
- Allow cross-build through arch=any
- Sort Contributor/Maintainer chronologically
- Disable some unneeded makepkg options
- Use DEPMOD=true for consistency with rpm-package
- Link to v1: https://lore.kernel.org/r/20240704-kbuild-pacman-pkg-v1-1-ac2f63f5fa7b@weissschuh.net
---
 .gitignore               |  6 ++++
 scripts/Makefile.package | 15 +++++++++
 scripts/package/PKGBUILD | 83 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 104 insertions(+)


---
base-commit: 1dd28064d4164a4dc9096fd1a7990d2de15f2bb6
change-id: 20240625-kbuild-pacman-pkg-b4f87e19d036

Best regards,

Comments

Nathan Chancellor July 8, 2024, 5:50 a.m. UTC | #1
Hi Thomas,

On Sat, Jul 06, 2024 at 09:33:46AM +0200, Thomas Weißschuh wrote:
> pacman is the package manager used by Arch Linux and its derivates.
> Creating native packages from the kernel tree has multiple advantages:
> 
> * The package triggers the correct hooks for initramfs generation and
>   bootloader configuration
> * Uninstallation is complete and also invokes the relevant hooks
> * New UAPI headers can be installed without any manual bookkeeping
> 
> The PKGBUILD file is a simplified version of the one used for the
> downstream Arch Linux "linux" package.
> Extra steps that should not be necessary for a development kernel have
> been removed and an UAPI header package has been added.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

Thanks a lot for addressing my comments. From a PKGBUILD perspective,
this looks good to me (I have a couple more comments below). I am not as
familiar with the Kbuild packaging infrastructure, so Masahiro might
have more comments on that, but it works for me in my basic testing so
consider it:

Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Tested-by: Nathan Chancellor <nathan@kernel.org>

> ---
> Changes in v2:
> - Replace ${MAKE} with $MAKE for consistency with other variables
> - Use $MAKE for "-s image_name"
> - Avoid permission warnings from build directory
> - Clarify reason for /build symlink removal
> - Install System.map and config
> - Install dtbs where available
> - Allow cross-build through arch=any
> - Sort Contributor/Maintainer chronologically
> - Disable some unneeded makepkg options
> - Use DEPMOD=true for consistency with rpm-package
> - Link to v1: https://lore.kernel.org/r/20240704-kbuild-pacman-pkg-v1-1-ac2f63f5fa7b@weissschuh.net
> ---
>  .gitignore               |  6 ++++
>  scripts/Makefile.package | 15 +++++++++
>  scripts/package/PKGBUILD | 83 ++++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 104 insertions(+)
> 
> diff --git a/.gitignore b/.gitignore
> index c59dc60ba62e..7902adf4f7f1 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -92,6 +92,12 @@ modules.order
>  #
>  /tar-install/
>  
> +#
> +# pacman files (make pacman-pkg)
> +#
> +/PKGBUILD
> +/pacman/
> +
>  #
>  # We don't want to ignore the following even if they are dot-files
>  #
> diff --git a/scripts/Makefile.package b/scripts/Makefile.package
> index bf016af8bf8a..8c0c80f8bec0 100644
> --- a/scripts/Makefile.package
> +++ b/scripts/Makefile.package
> @@ -141,6 +141,20 @@ snap-pkg:
>  	cd $(objtree)/snap && \
>  	snapcraft --target-arch=$(UTS_MACHINE)
>  
> +# pacman-pkg
> +# ---------------------------------------------------------------------------
> +
> +PHONY += pacman-pkg
> +pacman-pkg:
> +	@ln -srf $(srctree)/scripts/package/PKGBUILD $(objtree)/PKGBUILD
> +	cd $(objtree) && \
> +		srctree="$(realpath $(srctree))" \
> +		objtree="$(realpath $(objtree))" \
> +		BUILDDIR="$(realpath $(objtree))/pacman" \
> +		KBUILD_MAKEFLAGS="$(MAKEFLAGS)" \
> +		KBUILD_REVISION="$(shell $(srctree)/init/build-version)" \
> +		makepkg
> +
>  # dir-pkg tar*-pkg - tarball targets
>  # ---------------------------------------------------------------------------
>  
> @@ -221,6 +235,7 @@ help:
>  	@echo '  bindeb-pkg          - Build only the binary kernel deb package'
>  	@echo '  snap-pkg            - Build only the binary kernel snap package'
>  	@echo '                        (will connect to external hosts)'
> +	@echo '  pacman-pkg          - Build only the binary kernel pacman package'
>  	@echo '  dir-pkg             - Build the kernel as a plain directory structure'
>  	@echo '  tar-pkg             - Build the kernel as an uncompressed tarball'
>  	@echo '  targz-pkg           - Build the kernel as a gzip compressed tarball'
> diff --git a/scripts/package/PKGBUILD b/scripts/package/PKGBUILD
> new file mode 100644
> index 000000000000..fe899c77a976
> --- /dev/null
> +++ b/scripts/package/PKGBUILD
> @@ -0,0 +1,83 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +# Maintainer: Thomas Weißschuh <linux@weissschuh.net>
> +# Contributor: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
> +
> +pkgbase=linux-upstream
> +pkgname=("$pkgbase" "$pkgbase-headers" "$pkgbase-api-headers")
> +pkgver="${KERNELRELEASE//-/_}"
> +pkgrel="$KBUILD_REVISION"
> +pkgdesc='Linux'
> +url='https://www.kernel.org/'
> +arch=(any)

I see why you went this way but this feels a little dangerous because
this means the package will be installable on architectures other than
the one that it is built for. I think a better solution for this problem
would be moving arch back to $UTS_MACHINE but setting CARCH to that same
value in scripts/Makefile.package above. This diff works for me,
allowing me to build an aarch64 package on x86_64:

diff --git a/scripts/Makefile.package b/scripts/Makefile.package
index 8c0c80f8bec0..a5b5b899d90c 100644
--- a/scripts/Makefile.package
+++ b/scripts/Makefile.package
@@ -151,6 +151,7 @@ pacman-pkg:
 		srctree="$(realpath $(srctree))" \
 		objtree="$(realpath $(objtree))" \
 		BUILDDIR="$(realpath $(objtree))/pacman" \
+		CARCH="$(UTS_MACHINE)" \
 		KBUILD_MAKEFLAGS="$(MAKEFLAGS)" \
 		KBUILD_REVISION="$(shell $(srctree)/init/build-version)" \
 		makepkg
diff --git a/scripts/package/PKGBUILD b/scripts/package/PKGBUILD
index fe899c77a976..7f1a4588c3d3 100644
--- a/scripts/package/PKGBUILD
+++ b/scripts/package/PKGBUILD
@@ -8,7 +8,7 @@ pkgver="${KERNELRELEASE//-/_}"
 pkgrel="$KBUILD_REVISION"
 pkgdesc='Linux'
 url='https://www.kernel.org/'
-arch=(any)
+arch=($UTS_MACHINE)
 options=(!debug !strip !buildflags !makeflags)
 license=(GPL-2.0-only)
 

> +options=(!debug !strip !buildflags !makeflags)
> +license=(GPL-2.0-only)
> +
> +build() {
> +  export MAKEFLAGS="${KBUILD_MAKEFLAGS}"
> +  cd "$objtree"
> +
> +  # makepkg does a "chmod a-srw", triggering warnings during kbuild
> +  chmod 0755 "$pkgdirbase" || true
> +
> +  $MAKE -f "${srctree}/Makefile"
> +}
> +
> +package_linux-upstream() {
> +  pkgdesc="The $pkgdesc kernel and modules"
> +
> +  export MAKEFLAGS="${KBUILD_MAKEFLAGS}"
> +  cd "$objtree"
> +  local modulesdir="$pkgdir/usr/$MODLIB"
> +
> +  echo "Installing boot image..."
> +  # systemd expects to find the kernel here to allow hibernation
> +  # https://github.com/systemd/systemd/commit/edda44605f06a41fb86b7ab8128dcf99161d2344
> +  install -Dm644 "$($MAKE -s image_name)" "$modulesdir/vmlinuz"
> +
> +  # Used by mkinitcpio to name the kernel
> +  echo "$pkgbase" | install -Dm644 /dev/stdin "$modulesdir/pkgbase"
> +
> +  echo "Installing modules..."
> +  $MAKE INSTALL_MOD_PATH="$pkgdir/usr" INSTALL_MOD_STRIP=1 \
> +    DEPMOD=true modules_install
> +
> +  if $MAKE run-command KBUILD_RUN_COMMAND='test -d ${srctree}/arch/${SRCARCH}/boot/dts' 2>/dev/null; then
> +    echo "Installing dtbs..."
> +    $MAKE INSTALL_DTBS_PATH="$modulesdir/dtb" dtbs_install
> +  fi
> +
> +  # remove build link, will be part of -headers package
> +  rm -f "$modulesdir/build"
> +}
> +
> +package_linux-upstream-headers() {
> +  pkgdesc="Headers and scripts for building modules for the $pkgdesc kernel"
> +
> +  export MAKEFLAGS="${KBUILD_MAKEFLAGS}"
> +  cd "$objtree"
> +  local builddir="$pkgdir/usr/$MODLIB/build"
> +
> +  echo "Installing build files..."
> +  "$srctree/scripts/package/install-extmod-build" "$builddir"
> +
> +  echo "Installing System.map and config..."
> +  cp System.map "$builddir/System.map"
> +  cp .config "$builddir/.config"

Remove the dot on the installation location so that it is more visible.

> +  echo "Adding symlink..."
> +  mkdir -p "$pkgdir/usr/src"
> +  ln -sr "$builddir" "$pkgdir/usr/src/$pkgbase"
> +}
> +
> +package_linux-upstream-api-headers() {
> +  pkgdesc="Kernel headers sanitized for use in userspace"
> +  provides=(linux-api-headers)
> +  conflicts=(linux-api-headers)
> +
> +  export MAKEFLAGS="${KBUILD_MAKEFLAGS}"
> +  cd "$objtree"
> +
> +  $MAKE headers_install INSTALL_HDR_PATH="$pkgdir/usr"
> +}
> +
> +# vim:set ts=8 sts=2 sw=2 et:
> 
> ---
> base-commit: 1dd28064d4164a4dc9096fd1a7990d2de15f2bb6
> change-id: 20240625-kbuild-pacman-pkg-b4f87e19d036
> 
> Best regards,
> -- 
> Thomas Weißschuh <linux@weissschuh.net>
>
Thomas Weißschuh July 8, 2024, 3:56 p.m. UTC | #2
Hi Nathan,

On 2024-07-07 22:50:46+0000, Nathan Chancellor wrote:
> On Sat, Jul 06, 2024 at 09:33:46AM +0200, Thomas Weißschuh wrote:
> > pacman is the package manager used by Arch Linux and its derivates.
> > Creating native packages from the kernel tree has multiple advantages:
> > 
> > * The package triggers the correct hooks for initramfs generation and
> >   bootloader configuration
> > * Uninstallation is complete and also invokes the relevant hooks
> > * New UAPI headers can be installed without any manual bookkeeping
> > 
> > The PKGBUILD file is a simplified version of the one used for the
> > downstream Arch Linux "linux" package.
> > Extra steps that should not be necessary for a development kernel have
> > been removed and an UAPI header package has been added.
> > 
> > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> 
> Thanks a lot for addressing my comments. From a PKGBUILD perspective,
> this looks good to me (I have a couple more comments below). I am not as
> familiar with the Kbuild packaging infrastructure, so Masahiro might
> have more comments on that, but it works for me in my basic testing so
> consider it:
> 
> Reviewed-by: Nathan Chancellor <nathan@kernel.org>
> Tested-by: Nathan Chancellor <nathan@kernel.org>

Thanks!

> 
> > ---
> > Changes in v2:
> > - Replace ${MAKE} with $MAKE for consistency with other variables
> > - Use $MAKE for "-s image_name"
> > - Avoid permission warnings from build directory
> > - Clarify reason for /build symlink removal
> > - Install System.map and config
> > - Install dtbs where available
> > - Allow cross-build through arch=any
> > - Sort Contributor/Maintainer chronologically
> > - Disable some unneeded makepkg options
> > - Use DEPMOD=true for consistency with rpm-package
> > - Link to v1: https://lore.kernel.org/r/20240704-kbuild-pacman-pkg-v1-1-ac2f63f5fa7b@weissschuh.net
> > ---
> >  .gitignore               |  6 ++++
> >  scripts/Makefile.package | 15 +++++++++
> >  scripts/package/PKGBUILD | 83 ++++++++++++++++++++++++++++++++++++++++++++++++
> >  3 files changed, 104 insertions(+)

<snip>

> > diff --git a/scripts/package/PKGBUILD b/scripts/package/PKGBUILD
> > new file mode 100644
> > index 000000000000..fe899c77a976
> > --- /dev/null
> > +++ b/scripts/package/PKGBUILD
> > @@ -0,0 +1,83 @@
> > +# SPDX-License-Identifier: GPL-2.0-only
> > +# Maintainer: Thomas Weißschuh <linux@weissschuh.net>
> > +# Contributor: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
> > +
> > +pkgbase=linux-upstream
> > +pkgname=("$pkgbase" "$pkgbase-headers" "$pkgbase-api-headers")
> > +pkgver="${KERNELRELEASE//-/_}"
> > +pkgrel="$KBUILD_REVISION"
> > +pkgdesc='Linux'
> > +url='https://www.kernel.org/'
> > +arch=(any)
> 
> I see why you went this way but this feels a little dangerous because
> this means the package will be installable on architectures other than
> the one that it is built for. I think a better solution for this problem
> would be moving arch back to $UTS_MACHINE but setting CARCH to that same
> value in scripts/Makefile.package above. This diff works for me,
> allowing me to build an aarch64 package on x86_64:

This is what I used in v1 of the patch.
But I felt that this only works by pure chance.
IMO users of this feature should know what they are doing.

That said, if anybody has strong opinions on this, I'll be happy to change it.

> diff --git a/scripts/Makefile.package b/scripts/Makefile.package
> index 8c0c80f8bec0..a5b5b899d90c 100644
> --- a/scripts/Makefile.package
> +++ b/scripts/Makefile.package
> @@ -151,6 +151,7 @@ pacman-pkg:
>  		srctree="$(realpath $(srctree))" \
>  		objtree="$(realpath $(objtree))" \
>  		BUILDDIR="$(realpath $(objtree))/pacman" \
> +		CARCH="$(UTS_MACHINE)" \
>  		KBUILD_MAKEFLAGS="$(MAKEFLAGS)" \
>  		KBUILD_REVISION="$(shell $(srctree)/init/build-version)" \
>  		makepkg
> diff --git a/scripts/package/PKGBUILD b/scripts/package/PKGBUILD
> index fe899c77a976..7f1a4588c3d3 100644
> --- a/scripts/package/PKGBUILD
> +++ b/scripts/package/PKGBUILD
> @@ -8,7 +8,7 @@ pkgver="${KERNELRELEASE//-/_}"
>  pkgrel="$KBUILD_REVISION"
>  pkgdesc='Linux'
>  url='https://www.kernel.org/'
> -arch=(any)
> +arch=($UTS_MACHINE)
>  options=(!debug !strip !buildflags !makeflags)
>  license=(GPL-2.0-only)
>  
> 
> > +options=(!debug !strip !buildflags !makeflags)
> > +license=(GPL-2.0-only)
> > +

<snip>

> > +
> > +package_linux-upstream-headers() {
> > +  pkgdesc="Headers and scripts for building modules for the $pkgdesc kernel"
> > +
> > +  export MAKEFLAGS="${KBUILD_MAKEFLAGS}"
> > +  cd "$objtree"
> > +  local builddir="$pkgdir/usr/$MODLIB/build"
> > +
> > +  echo "Installing build files..."
> > +  "$srctree/scripts/package/install-extmod-build" "$builddir"
> > +
> > +  echo "Installing System.map and config..."
> > +  cp System.map "$builddir/System.map"
> > +  cp .config "$builddir/.config"
> 
> Remove the dot on the installation location so that it is more visible.

This is the location used by the downstream linux-headers package.
I can add a symlink for better visibility, though.

> > +  echo "Adding symlink..."
> > +  mkdir -p "$pkgdir/usr/src"
> > +  ln -sr "$builddir" "$pkgdir/usr/src/$pkgbase"
> > +}
> > +

<snip>
Nathan Chancellor July 8, 2024, 4:53 p.m. UTC | #3
On Mon, Jul 08, 2024 at 05:56:44PM +0200, Thomas Weißschuh wrote:
> Hi Nathan,
> 
> On 2024-07-07 22:50:46+0000, Nathan Chancellor wrote:
> > On Sat, Jul 06, 2024 at 09:33:46AM +0200, Thomas Weißschuh wrote:
> > > pacman is the package manager used by Arch Linux and its derivates.
> > > Creating native packages from the kernel tree has multiple advantages:
> > > 
> > > * The package triggers the correct hooks for initramfs generation and
> > >   bootloader configuration
> > > * Uninstallation is complete and also invokes the relevant hooks
> > > * New UAPI headers can be installed without any manual bookkeeping
> > > 
> > > The PKGBUILD file is a simplified version of the one used for the
> > > downstream Arch Linux "linux" package.
> > > Extra steps that should not be necessary for a development kernel have
> > > been removed and an UAPI header package has been added.
> > > 
> > > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> > 
> > Thanks a lot for addressing my comments. From a PKGBUILD perspective,
> > this looks good to me (I have a couple more comments below). I am not as
> > familiar with the Kbuild packaging infrastructure, so Masahiro might
> > have more comments on that, but it works for me in my basic testing so
> > consider it:
> > 
> > Reviewed-by: Nathan Chancellor <nathan@kernel.org>
> > Tested-by: Nathan Chancellor <nathan@kernel.org>
> 
> Thanks!
> 
> > 
> > > ---
> > > Changes in v2:
> > > - Replace ${MAKE} with $MAKE for consistency with other variables
> > > - Use $MAKE for "-s image_name"
> > > - Avoid permission warnings from build directory
> > > - Clarify reason for /build symlink removal
> > > - Install System.map and config
> > > - Install dtbs where available
> > > - Allow cross-build through arch=any
> > > - Sort Contributor/Maintainer chronologically
> > > - Disable some unneeded makepkg options
> > > - Use DEPMOD=true for consistency with rpm-package
> > > - Link to v1: https://lore.kernel.org/r/20240704-kbuild-pacman-pkg-v1-1-ac2f63f5fa7b@weissschuh.net
> > > ---
> > >  .gitignore               |  6 ++++
> > >  scripts/Makefile.package | 15 +++++++++
> > >  scripts/package/PKGBUILD | 83 ++++++++++++++++++++++++++++++++++++++++++++++++
> > >  3 files changed, 104 insertions(+)
> 
> <snip>
> 
> > > diff --git a/scripts/package/PKGBUILD b/scripts/package/PKGBUILD
> > > new file mode 100644
> > > index 000000000000..fe899c77a976
> > > --- /dev/null
> > > +++ b/scripts/package/PKGBUILD
> > > @@ -0,0 +1,83 @@
> > > +# SPDX-License-Identifier: GPL-2.0-only
> > > +# Maintainer: Thomas Weißschuh <linux@weissschuh.net>
> > > +# Contributor: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
> > > +
> > > +pkgbase=linux-upstream
> > > +pkgname=("$pkgbase" "$pkgbase-headers" "$pkgbase-api-headers")
> > > +pkgver="${KERNELRELEASE//-/_}"
> > > +pkgrel="$KBUILD_REVISION"
> > > +pkgdesc='Linux'
> > > +url='https://www.kernel.org/'
> > > +arch=(any)
> > 
> > I see why you went this way but this feels a little dangerous because
> > this means the package will be installable on architectures other than
> > the one that it is built for. I think a better solution for this problem
> > would be moving arch back to $UTS_MACHINE but setting CARCH to that same
> > value in scripts/Makefile.package above. This diff works for me,
> > allowing me to build an aarch64 package on x86_64:
> 
> This is what I used in v1 of the patch.

You had $UTS_MACHINE as arch but I don't see where CARCH was set for
makepkg? If you tried to cross compile with v1, there was an error
because the default CARCH value (the host) does not match the arch
value, but explicitly passing CARCH to makepkg with $UTS_MACHINE should
allow makepkg to build a package that is only installable on that
machine?

> But I felt that this only works by pure chance.

I don't think it is by pure chance, it should be entirely based off of the
builder's ARCH value, no? I might be misunderstanding something though.

> IMO users of this feature should know what they are doing.
> 
> That said, if anybody has strong opinions on this, I'll be happy to change it.

I don't feel strongly about it but I think this is different from pretty
much all of the other package builds, which only build a package that is
installable/usable on one archictecture, and the solution seems
simple/robust enough.

> > diff --git a/scripts/Makefile.package b/scripts/Makefile.package
> > index 8c0c80f8bec0..a5b5b899d90c 100644
> > --- a/scripts/Makefile.package
> > +++ b/scripts/Makefile.package
> > @@ -151,6 +151,7 @@ pacman-pkg:
> >  		srctree="$(realpath $(srctree))" \
> >  		objtree="$(realpath $(objtree))" \
> >  		BUILDDIR="$(realpath $(objtree))/pacman" \
> > +		CARCH="$(UTS_MACHINE)" \
> >  		KBUILD_MAKEFLAGS="$(MAKEFLAGS)" \
> >  		KBUILD_REVISION="$(shell $(srctree)/init/build-version)" \
> >  		makepkg
> > diff --git a/scripts/package/PKGBUILD b/scripts/package/PKGBUILD
> > index fe899c77a976..7f1a4588c3d3 100644
> > --- a/scripts/package/PKGBUILD
> > +++ b/scripts/package/PKGBUILD
> > @@ -8,7 +8,7 @@ pkgver="${KERNELRELEASE//-/_}"
> >  pkgrel="$KBUILD_REVISION"
> >  pkgdesc='Linux'
> >  url='https://www.kernel.org/'
> > -arch=(any)
> > +arch=($UTS_MACHINE)
> >  options=(!debug !strip !buildflags !makeflags)
> >  license=(GPL-2.0-only)
> >  
> > 
> > > +options=(!debug !strip !buildflags !makeflags)
> > > +license=(GPL-2.0-only)
> > > +
> 
> <snip>
> 
> > > +
> > > +package_linux-upstream-headers() {
> > > +  pkgdesc="Headers and scripts for building modules for the $pkgdesc kernel"
> > > +
> > > +  export MAKEFLAGS="${KBUILD_MAKEFLAGS}"
> > > +  cd "$objtree"
> > > +  local builddir="$pkgdir/usr/$MODLIB/build"
> > > +
> > > +  echo "Installing build files..."
> > > +  "$srctree/scripts/package/install-extmod-build" "$builddir"
> > > +
> > > +  echo "Installing System.map and config..."
> > > +  cp System.map "$builddir/System.map"
> > > +  cp .config "$builddir/.config"
> > 
> > Remove the dot on the installation location so that it is more visible.
> 
> This is the location used by the downstream linux-headers package.

Ah, I did not realize that!

> I can add a symlink for better visibility, though.

It probably isn't that big of a deal if it is in the package somewhere,
I don't feel strongly enough about it.

> > > +  echo "Adding symlink..."
> > > +  mkdir -p "$pkgdir/usr/src"
> > > +  ln -sr "$builddir" "$pkgdir/usr/src/$pkgbase"
> > > +}
> > > +
> 
> <snip>
Thomas Weißschuh July 8, 2024, 8 p.m. UTC | #4
On 2024-07-08 09:53:42+0000, Nathan Chancellor wrote:
> On Mon, Jul 08, 2024 at 05:56:44PM +0200, Thomas Weißschuh wrote:
> > On 2024-07-07 22:50:46+0000, Nathan Chancellor wrote:
> > > On Sat, Jul 06, 2024 at 09:33:46AM +0200, Thomas Weißschuh wrote:

<snip>

> > > > diff --git a/scripts/package/PKGBUILD b/scripts/package/PKGBUILD
> > > > new file mode 100644
> > > > index 000000000000..fe899c77a976
> > > > --- /dev/null
> > > > +++ b/scripts/package/PKGBUILD
> > > > @@ -0,0 +1,83 @@
> > > > +# SPDX-License-Identifier: GPL-2.0-only
> > > > +# Maintainer: Thomas Weißschuh <linux@weissschuh.net>
> > > > +# Contributor: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
> > > > +
> > > > +pkgbase=linux-upstream
> > > > +pkgname=("$pkgbase" "$pkgbase-headers" "$pkgbase-api-headers")
> > > > +pkgver="${KERNELRELEASE//-/_}"
> > > > +pkgrel="$KBUILD_REVISION"
> > > > +pkgdesc='Linux'
> > > > +url='https://www.kernel.org/'
> > > > +arch=(any)
> > > 
> > > I see why you went this way but this feels a little dangerous because
> > > this means the package will be installable on architectures other than
> > > the one that it is built for. I think a better solution for this problem
> > > would be moving arch back to $UTS_MACHINE but setting CARCH to that same
> > > value in scripts/Makefile.package above. This diff works for me,
> > > allowing me to build an aarch64 package on x86_64:
> > 
> > This is what I used in v1 of the patch.
> 
> You had $UTS_MACHINE as arch but I don't see where CARCH was set for
> makepkg? If you tried to cross compile with v1, there was an error
> because the default CARCH value (the host) does not match the arch
> value, but explicitly passing CARCH to makepkg with $UTS_MACHINE should
> allow makepkg to build a package that is only installable on that
> machine?

Indeed.

> > But I felt that this only works by pure chance.
> 
> I don't think it is by pure chance, it should be entirely based off of the
> builder's ARCH value, no? I might be misunderstanding something though.

"Chance" for the fact that UTS_MACHINE and Arch Linux CARCH are
matching.

> > IMO users of this feature should know what they are doing.
> > 
> > That said, if anybody has strong opinions on this, I'll be happy to change it.
> 
> I don't feel strongly about it but I think this is different from pretty
> much all of the other package builds, which only build a package that is
> installable/usable on one archictecture, and the solution seems
> simple/robust enough.

I'll pick up your proposal and will send v3 with it and your tags.
diff mbox series

Patch

diff --git a/.gitignore b/.gitignore
index c59dc60ba62e..7902adf4f7f1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -92,6 +92,12 @@  modules.order
 #
 /tar-install/
 
+#
+# pacman files (make pacman-pkg)
+#
+/PKGBUILD
+/pacman/
+
 #
 # We don't want to ignore the following even if they are dot-files
 #
diff --git a/scripts/Makefile.package b/scripts/Makefile.package
index bf016af8bf8a..8c0c80f8bec0 100644
--- a/scripts/Makefile.package
+++ b/scripts/Makefile.package
@@ -141,6 +141,20 @@  snap-pkg:
 	cd $(objtree)/snap && \
 	snapcraft --target-arch=$(UTS_MACHINE)
 
+# pacman-pkg
+# ---------------------------------------------------------------------------
+
+PHONY += pacman-pkg
+pacman-pkg:
+	@ln -srf $(srctree)/scripts/package/PKGBUILD $(objtree)/PKGBUILD
+	cd $(objtree) && \
+		srctree="$(realpath $(srctree))" \
+		objtree="$(realpath $(objtree))" \
+		BUILDDIR="$(realpath $(objtree))/pacman" \
+		KBUILD_MAKEFLAGS="$(MAKEFLAGS)" \
+		KBUILD_REVISION="$(shell $(srctree)/init/build-version)" \
+		makepkg
+
 # dir-pkg tar*-pkg - tarball targets
 # ---------------------------------------------------------------------------
 
@@ -221,6 +235,7 @@  help:
 	@echo '  bindeb-pkg          - Build only the binary kernel deb package'
 	@echo '  snap-pkg            - Build only the binary kernel snap package'
 	@echo '                        (will connect to external hosts)'
+	@echo '  pacman-pkg          - Build only the binary kernel pacman package'
 	@echo '  dir-pkg             - Build the kernel as a plain directory structure'
 	@echo '  tar-pkg             - Build the kernel as an uncompressed tarball'
 	@echo '  targz-pkg           - Build the kernel as a gzip compressed tarball'
diff --git a/scripts/package/PKGBUILD b/scripts/package/PKGBUILD
new file mode 100644
index 000000000000..fe899c77a976
--- /dev/null
+++ b/scripts/package/PKGBUILD
@@ -0,0 +1,83 @@ 
+# SPDX-License-Identifier: GPL-2.0-only
+# Maintainer: Thomas Weißschuh <linux@weissschuh.net>
+# Contributor: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
+
+pkgbase=linux-upstream
+pkgname=("$pkgbase" "$pkgbase-headers" "$pkgbase-api-headers")
+pkgver="${KERNELRELEASE//-/_}"
+pkgrel="$KBUILD_REVISION"
+pkgdesc='Linux'
+url='https://www.kernel.org/'
+arch=(any)
+options=(!debug !strip !buildflags !makeflags)
+license=(GPL-2.0-only)
+
+build() {
+  export MAKEFLAGS="${KBUILD_MAKEFLAGS}"
+  cd "$objtree"
+
+  # makepkg does a "chmod a-srw", triggering warnings during kbuild
+  chmod 0755 "$pkgdirbase" || true
+
+  $MAKE -f "${srctree}/Makefile"
+}
+
+package_linux-upstream() {
+  pkgdesc="The $pkgdesc kernel and modules"
+
+  export MAKEFLAGS="${KBUILD_MAKEFLAGS}"
+  cd "$objtree"
+  local modulesdir="$pkgdir/usr/$MODLIB"
+
+  echo "Installing boot image..."
+  # systemd expects to find the kernel here to allow hibernation
+  # https://github.com/systemd/systemd/commit/edda44605f06a41fb86b7ab8128dcf99161d2344
+  install -Dm644 "$($MAKE -s image_name)" "$modulesdir/vmlinuz"
+
+  # Used by mkinitcpio to name the kernel
+  echo "$pkgbase" | install -Dm644 /dev/stdin "$modulesdir/pkgbase"
+
+  echo "Installing modules..."
+  $MAKE INSTALL_MOD_PATH="$pkgdir/usr" INSTALL_MOD_STRIP=1 \
+    DEPMOD=true modules_install
+
+  if $MAKE run-command KBUILD_RUN_COMMAND='test -d ${srctree}/arch/${SRCARCH}/boot/dts' 2>/dev/null; then
+    echo "Installing dtbs..."
+    $MAKE INSTALL_DTBS_PATH="$modulesdir/dtb" dtbs_install
+  fi
+
+  # remove build link, will be part of -headers package
+  rm -f "$modulesdir/build"
+}
+
+package_linux-upstream-headers() {
+  pkgdesc="Headers and scripts for building modules for the $pkgdesc kernel"
+
+  export MAKEFLAGS="${KBUILD_MAKEFLAGS}"
+  cd "$objtree"
+  local builddir="$pkgdir/usr/$MODLIB/build"
+
+  echo "Installing build files..."
+  "$srctree/scripts/package/install-extmod-build" "$builddir"
+
+  echo "Installing System.map and config..."
+  cp System.map "$builddir/System.map"
+  cp .config "$builddir/.config"
+
+  echo "Adding symlink..."
+  mkdir -p "$pkgdir/usr/src"
+  ln -sr "$builddir" "$pkgdir/usr/src/$pkgbase"
+}
+
+package_linux-upstream-api-headers() {
+  pkgdesc="Kernel headers sanitized for use in userspace"
+  provides=(linux-api-headers)
+  conflicts=(linux-api-headers)
+
+  export MAKEFLAGS="${KBUILD_MAKEFLAGS}"
+  cd "$objtree"
+
+  $MAKE headers_install INSTALL_HDR_PATH="$pkgdir/usr"
+}
+
+# vim:set ts=8 sts=2 sw=2 et: