User:Kangie/Musl patching notes

From Gentoo Wiki
Jump to:navigation Jump to:search

My notes on applying patches for musl support in packages.

This example uses sys-apps/accountsservice but the same process can be followed to patch any package.

If upstream patches apply directly to the source code there is no need to 'rebase' the patches - they can be placed in ${FILESDIR} directly


  • A fork of ::Gentoo that can be used to submit PRs. This example uses /data/development/gentoo

Unpack our sources and prepare them to be patched

In this step we use the ebuild command to unpack (and prepare) our sources.

Prepare in this context means 'apply anything in the src_prepare' phase - this typically means 'Apply patches in PATCHES'

We use Git to keep track of the changes that we make to the source code and (later) to generate a .patch file that can be applied by Portage.

user $ cd /data/development/gentoo
user $ cd sys-apps/accountsservice
user $ # unpack the ebuild sources and apply existing patches
user $ doas ebuild accountsservice-23.13.9.ebuild clean prepare
user $ # own the directory
user $ doas chown -R kangie:kangie /var/tmp/portage/sys-apps/accountsservice-23.13.9
user $ pushd /var/tmp/portage/sys-apps/accountsservice-23.13.9/work/accountsservice-23.13.9
user $ # initialise the source code as a git repo
user $ git init && git add . && git commit -m "Initial commit"

Acquire and apply the patches

In this step we acquire the patches and apply them to the unpacked source code.

At a high level we will:

  1. Download the Patches
  2. Apply the patches
  3. Make required changes
user $ # Get the patches
user $ # Apply the first patch
user $ git apply --reject musl-wtmp.patch
user $ # no errors, we can remove it
user $ rm musl-wtmp.patch
user $ # Apply the second patch
user $ git apply --reject musl-fgetspent_r.patch
user $ # It doesn't apply, let's see what we need to do
user $ cat src/daemon.c.rej
user $ # fix it
user $ nvim src/daemon.c
user $ # tidy up
user $ rm src/daemon.c.rej
user $ rm musl-fgetspent_r.patch

Make our patch

Here we use git to take the changes that we applied to the source code and transform them into .patch files that can be applied by Portage.

At a high level we will:

  1. Commit those changes
  2. Turn those changes (in one or more commits) into a patch file
  3. Copy or move the patch file back into the Gentoo fork
user $ git status
user $ # Stage our changes
user $ git add .
user $ # Commit the changes
user $ git commit
user $ # Turn the commit(s) into a patch
user $ git format-patch -1 HEAD
user $ # Move it to portage fork
user $ mv 0001-This-patch-applies-the-alpine-musl-fixes.patch /data/development/gentoo/sys-apps/accountsservice/files/acccountsservice-23.13.9-musl-fixes.patch
user $ popd

Update the ebuild

In this final step the ebuild is updated to point to the new patch, we perform some QA and validate that Portage can successfully apply the patch, and push our changes (to be later submitted as a PR)

user $ # Add our new patch to PATCHES
user $ nvim accountsservice-23.13.9.ebuild
user $ scrub-patch files/acccountsservice-23.13.9-musl-fixes.patch
user $ # test if it applies
user $ doas ebuild accountsservice-23.13.9.ebuild clean prepare
user $ git status
user $ # push the changes
user $ git add .
user $ pkgdev commit
user $ git push