Patching

This wiki article explains how to patch the source code of a package. Such patching enables you to do the following:
 * fix compiler errors
 * add debug output to understand bugs
 * fix bugs in the software
 * develop new features

Finding the aport
First of all, you need to find the aport of the package. The aport is the package build recipe, consisting at least of one APKBUILD file. In postmarketOS, these can be found in  (that may change in the future, see ). If you found it there already, you can continue with the next section.

postmarketOS is building on Alpine Linux, which means if you can't find a certain package in our aports folder, you need to grab the aport from Alpine. They store their aports in a git repository as well. You can either manually download one file after another for your aport from a git frontend such as GitHub or let pmbootstrap clone the repo and copy a package in pmaport's  directory (e.g.  ):

Alternatively, you can achieve this manually by cloning the entire git repository to your harddrive (see listing below). Make sure you put the original aport from Alpine into the  folder of the postmarketOS aports.

Using "build --src"
Let's quickly take a look at the aport's. If it has a  line inside it, you can use pmbootstrap's   feature, in that case read on here. Otherwise, see the legacy guide below.

Get the package's source code
With this approach, we extract the package's source code somewhere on your disk (so you can easily modify it outside of the chroots). To directly use the source tarball, which pmbootstrap also uses to build the aport, do the following:
 * start building the package:
 * open a second terminal with
 * in that terminal, you will see that it starts downloading the source package. mind the file name displayed there, and wait until the source has been downloaded.
 * then abort the build with  (or let it run through once to make sure this works, if the aport was not from pmOS but from Alpine - the compiler output will be cached, so it will be faster next time)

Now extract the source in your desired location:

Initialize a git repository in that folder, so we can generate patch files from what you have changed in the source later:

As alternative to extracting the tarball, you can also directly clone the upstream's git URL.

Apply the aport's existing patches
If the aport already has patches, which may be necessary to compile the package with musl, you can add them to your checked out source as follows:

Building
Now build the package once without modifying anything else, just to make sure that it works:

As usually, the compiled packages can be found in. You can copy the resulting apk file to your device via SCP and install it on the target device with  if you like to.

Making changes
Change the source code now, and commit your change:

Then you can compile and test again.

Create a patch file
With git you can create the patch now. Some examples:

Legacy Guide
This guide was written with the following use case in mind: you want to package an Android kernel. But it does not compile with our GCC6, so you need to patch the kernel. Unlike in the Android world, we do not fork the kernel repository and apply our patches there. Instead we ship our custom patches with the package build recipe in the aport folder.

This guide assumes, that you already know what to patch - if you don't know what to patch and only have the error, try to search the web for that error message. More often than not, there is a patch already available, that will fix it. If you don't have any luck, ask in the chat.

As everywhere else in the wiki, please expand this guide wherever information is missing that would have helped you!

Workflow

 * 1) Try to compile your program with  . Note in which chroot it will actually build, it is the start of the building message. When it starts with   for example, the chroot is the "native" chroot (which is the default).
 * 2) Wait until it fails or runs through (it works in both cases)
 * 3) The sources of the kernel are now extracted in   in the chroot where it was building.
 * 4) Use   to enter the chroot and to create the patch. It will enter the native chroot by default, if you need another chroot, then look at  . Then do the following inside the chroot shell:
 * 5) Install   and an editor such as   with:
 * 6) Switch from root to the user that we use for building:
 * 7) Go into the source folder:
 * 8) Patches get applied in the builddir, so usually there is another folder inside , that you need to enter. You can check with   which folders/files are inside the   folder. Enter that subfolder if necessary.
 * 9) We'll be using git to create a patch file, so we will initialize a new repository and add everything:
 * 10)   (it will say: "Initialized empty Git repository in /home/pmos/build/src/.git/")
 * 11) Now it's time to edit the file you want to edit.
 * 12)   (or whichever file you want to edit)
 * 13) NOTE: nano is glitchy when used like that. If it is unusable for you, run   and then edit the file from outside the chroot with your favorite editor (a graphical one works, too!). The path is similar to:.
 * 14) (You need to still be in the chroot!) run   to get a patch file generated to the screen. You can save it to a file by redirecting it:.
 * 15) Quit the chroot by pressing   (CTRL+D) twice (typing   also works).
 * 16) Copy the patch file you have created to your aport folder (adjust the work path, that you have given   and the chroot suffix   if necessary):
 * 17) Rename the   file to give it a meaningful name!
 * 18) You can add arbitrary text at the top of the patch file. Use this to paste the error message that you got during compilation there, with a note that this will be fixed by this patch.
 * 1) Copy the patch file you have created to your aport folder (adjust the work path, that you have given   and the chroot suffix   if necessary):
 * 2) Rename the   file to give it a meaningful name!
 * 3) You can add arbitrary text at the top of the patch file. Use this to paste the error message that you got during compilation there, with a note that this will be fixed by this patch.
 * 1) You can add arbitrary text at the top of the patch file. Use this to paste the error message that you got during compilation there, with a note that this will be fixed by this patch.