Patching Upstream Source Packages

About This Task

Once your application package is installed and working successfully, you will eventually need to make updates to it. Whether you are working with your own package, such as the fibonacci application from Creating a Sample Application, or a package you retrieved using the wget command, patching is one method for updating the package source.

In this procedure, you will learn how to use diff to patch the fibonacci application to modify the installation directory so the binary installs to the /usr/bin/fibonacci directory instead of /bin/fibonacci, which is the path currently listed in the Makefile, for example:

install: fibonacci
    install -D fibonacci \
        $(DESTDIR)$(prefix)/bin/fibonacci

In this example, the $(prefix) variable is undefined, resolving as empty, and thereby creating a default installation path of /usr/fibonacci. The patch will add the following line to the Makefile to define the variable and change the installation path.

prefix := /usr

Note that diff is one method for patching package source recognized by the Debian tools. Other methods, such as the dquilt and dpkg-source commands, provide alternatives to help meet your patching requirements. For details on using these tools for your eLxr package updates, see Guide for Debian Maintainers: Modification to the upstream source.

Before You Begin

Procedure

  1. Make a copy of the fibonacci-1.0 directory.

    $ cp -a fibonacci-1.0 fibonacci-1.0.orig
    
  2. Edit the Makefile with vi.

    $ vi fibonacci-1.0/Makefile
    
  3. Add prefix := /usr to the Makefile and save the file.

    #
    # This is public domain software
    #
    VERSION := 1.0
    DEPS := src/math.h
    SRC := src/main.c src/math.c
    OBJ := $(SRC:.c=.o)
    prefix := /usr
    all: fibonacci
    
  4. Diff the changes to a new 000-prefix-usr.patch file.

    $ diff -Nru fibonacci-1.0.orig fibonacci-1.0 >000-prefix-usr.patch
    
    diff: fibonacci-1.0.orig/debian/.debhelper/fibonacci/dbgsym-root/usr/share/doc/fibonacci-dbgsym: No such file or directory
    diff: fibonacci-1.0/debian/.debhelper/fibonacci/dbgsym-root/usr/share/doc/fibonacci-dbgsym: No such file or directory
    diff -Nru fibonacci-1.0.orig/Makefile fibonacci-1.0/Makefile
    --- fibonacci-1.0.orig/Makefile 2024-07-09 15:17:43.000000000 -0700
    +++ fibonacci-1.0/Makefile      2024-07-09 15:32:04.959089587 -0700
    @@ -5,7 +5,7 @@
     DEPS := src/math.h
      SRC := src/main.c src/math.c
       OBJ := $(SRC:.c=.o)
       -
       +prefix := /usr
        all: fibonacci
    
  5. View the contents of the patch to see the new addition.

    $ cat 000-prefix-usr.patch
    
    --- fibonacci-1.0.orig/Makefile 2024-07-09 15:17:43.000000000 -0700
    +++ fibonacci-1.0/Makefile      2024-07-09 15:32:04.959089587 -0700
    @@ -5,7 +5,7 @@
     DEPS := src/math.h
     SRC := src/main.c src/math.c
     OBJ := $(SRC:.c=.o)
     -
     +prefix := /usr
     all: fibonacci
    
  6. Remove the fibonacci-1.0 directory you copied to make the patch.

    $ rm -rf fibonacci-1.0
    
  7. Restore the fibonacci-1.0.orig directory to its original name and navigate in to it.

    $ mv -f fibonacci-1.0.orig fibonacci-1.0
    
    $ cd fibonacci-1.0
    
  8. Edit the patch to make it DEP-3 conformant. This includes adding a From and a Description and the diff command used to create the patch.

    $ echo '000-prefix-usr.patch' >debian/patches/series
    
    $ vi ../000-prefix-usr.patch
    
    From: eLxr User <elxruser@elxr.org>
    Description: Add prefix var to set prefix = /usr patch
    diff -Nru fibonacci-1.0.orig/Makefile fibonacci-1.0/Makefile
    --- fibonacci-1.0.orig/Makefile 2024-07-09 15:17:43.000000000 -0700
    +++ fibonacci-1.0/Makefile      2024-07-09 15:32:04.959089587 -0700
    @@ -5,7 +5,7 @@
     DEPS := src/math.h
     SRC := src/main.c src/math.c
     OBJ := $(SRC:.c=.o)
    -
    +prefix := /usr
     all: fibonacci
    
     archive: fibonacci-1.0.tar.gz
    
  9. Move the patch to the debian/patches/ directory.

    $ mv -f ../000-prefix-usr.patch debian/patches/000-prefix-usr.patch
    
  10. Rebuild the package with the debuild command.

  11. Check the contents of the fibonacci-1.0/debian.

    $ tree
    
    ├── debian
    │   ├── changelog
    │   ├── control
    │   ├── copyright
    │   ├── debhelper-build-stamp
    │   ├── fibonacci
    │   │   ├── DEBIAN
    │   │   │   ├── control
    │   │   │   └── md5sums
    │   │   └── usr
    │   │       ├── bin
    │   │       │   └── fibonacci
    │   │       └── share
    │   │           └── doc
    │   │               └── fibonacci
    │   │                   ├── changelog.Debian.gz
    │   │                   ├── copyright
    │   │                   └── README.Debian
    │   ├── fibonacci.substvars
    │   ├── files
    │   ├── patches
    │   │   ├── 000-prefix-usr.patch
    │   │   └── series
    │   ├── README.Debian
    │   ├── rules
    │   ├── source
    │   │   ├── format
    │   │   └── local-options
    │   └── watch
    ├── fibonacci
    ├── fibonacci-1.0.tar.gz
    ├── LICENSE
    ├── Makefile
    └── src
        ├── main.c
            ├── main.o
            ├── math.c
            ├── math.h
            └── math.o
    

Notice that the fibonacci binary is now located in the debian/usr/bin directory, instead of debian/bin, like it was prior to adding the patch. This indicates the patch was successful.

Results

Now that your package is updated, you should test it to make sure it works as expected. For details, see Testing New and Updated Packages.

This procedure provides only one method for patching a package. Making changes may include creating a patch, as described here, but may also include updating the templates to make changes to your build. For additional information and examples, see Guide for Debian Maintainers: Modification to the upstream source.