Get package name or package build directory inside dpkg-buildpackage (debhelper)

441 views Asked by At

When creating a Debian package with dpkg-buildpackage (and debhelper), I am using the following entry in debian/rules for assigning ownership of my-config to my-user (instead of root):

override_dh_fixperms:
        dh_fixperms
        chown my-user:my-user $(CURDIR)/debian/my-package/etc/my-config

dpkg-buildpackage runs as part of a build pipeline where the package name may be chosen as either my-package or my-package-n (n is an integer). It currently fails in the case of my-package-n because of the mismatch to the constant directory name my-package in the path for chown.

Is there mechanism in dpkg_buildpackage (and debhelper in particular) that gives access to the package name (my-package or my-package-n in my case) or the package build directory ($(CURDIR)/debian/my-package or $(CURDIR)/debian/my-package-n in my case)?

I've printed "ordinary" environment variables with set between in dh_fixperms and chown: apparently none such holds the desired information.

1

There are 1 answers

0
Sebastian Schrader On

Approach 1: Introspection

The debhelper package includes two dh_*-programs that parse the debhelper command line arguments (e.g. -v/--verbose, -B/--builddir/--builddirectory, -N/--no-package), environment variables (e.g. DH_VERBOSE, DH_OPTIONS), and package metadata to print them for introspection/use by other scripts/tooling:

  1. dh_listpackages lists the binary packages debhelper programs would act on:

    $ dh_listpackage
    my-package
    
  2. dh_assistant provides various sub commands, that print information as JSON:

    $ dh_assistant which-build-system
    {
       "build-directory": "obj-x86_64-linux-gnu",
       "build-system": "makefile",
       "buildpath": "obj-x86_64-linux-gnu",
       "dest-directory": null,
       "for-build-step": "configure",
       "parallel": "16",
       "source-directory": ".",
       "upstream-arguments": []
    }
    $ dh_assistant active-compat-level
    {
       "active-compat-level": 13,
       "declared-compat-level": 13,
       "declared-compat-level-source": "Build-Depends: debhelper-compat (= 13)"
    }
    

In your particular case, dh_listpackages should do the job:

PACKAGE := $(shell dh_listpackages)

override_dh_fixperms:
        dh_fixperms
        chown my-user:my-user $(CURDIR)/debian/$(PACKAGE)/etc/my-config

Please keep in mind, that dh_listpackages will return multiple packages, if your source package produces multiple binary packages.

Approach 2: Well-known binary package directory

You appear to have a source package that produces a single binary package (with a varying name) with a compatibility less than 15. Until compatibility level 15, by default debian/<package-name> is used for single binary and debian/tmp for multiple binary packages.

Starting with compatibility 15, debhelper will by default always use debian/tmp also for single binary packages.

Regardless of the compatibility level, you can force a specific package directory with the -P/--tmpdir option:

%:
    dh $@ --tmpdir=debian/tmp

override_dh_fixperms:
        dh_fixperms
        chown my-user:my-user $(CURDIR)/debian/tmp/etc/my-config