15

I got a package with:

$ apt-get source <pkg-name>

and now I am trying to build it with:

$ dpkg-buildpackage -uc -us -j8

At the beginning of the output, there is stated:

dpkg-buildpackage: export CFLAGS from dpkg-buildflags (origin: vendor): -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security
dpkg-buildpackage: export CPPFLAGS from dpkg-buildflags (origin: vendor): -D_FORTIFY_SOURCE=2
dpkg-buildpackage: export CXXFLAGS from dpkg-buildflags (origin: vendor): -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security
dpkg-buildpackage: export FFLAGS from dpkg-buildflags (origin: vendor): -g -O2
dpkg-buildpackage: export LDFLAGS from dpkg-buildflags (origin: vendor): -Wl,-Bsymbolic-functions -Wl,-z,relro

I would like to override these CFLAGS (also, the LDFLAGS). I have tried exporting the CFLAGS envvar, the same way we do with plain configure, at no avail. How can I override these values?

lvella
  • 12,754
  • 11
  • 54
  • 106

1 Answers1

29

the package you are trying to rebuild, sets (read: overrides) the *FLAGS with hardening-specifics flags retrieved from dpkg-buildflags.

if you need to override those flags for your own purposes, you should tell dpkg-buildflags to provide the flags you want, rather than the (hardening) defaults. looking at man dpkg-buildflags, you find the section about environment variables, esp. see DEB_flag_SET and DEB_flag_APPEND

so this should do the trick (fill in your own *FLAGS):

$ DEB_CPPFLAGS_SET="-I/foo/bar/baz" DEB_CFLAGS_SET="-g -O6" DEB_LDFLAGS_SET="-L/fruzzel/frazzel/" dpkg-buildpackage -uc -us -j8 -rfakeroot
umläute
  • 28,885
  • 9
  • 68
  • 122
  • 2
    What if it doesn't look like dpkg-buildflags is being run? How hard is it to add that to a package? Is there an alternative command? – Adam Baxter Aug 08 '15 at 14:07
  • This answer may no longer be current. (See http://manpages.ubuntu.com/manpages/xenial/man1/dpkg-buildpackage.1.html#Notes) A more recent recommendation is to edit the debian/rules script inside your the distribution you've downloaded with apt-get source and search for instances of CFLAGS/CXXFLAGS/LFLAGS. – Dave McMordie Jan 10 '19 at 21:39
  • @DaveMcMordie Recommendation by whom? But indeed some packages might ignore `DEB_CFLAGS_SET`. I'm looking at you, `exim4` on Debian 6. In which case you can do `dpkg-buildpackage -R'make -f debian/rules CFLAGS=-g3`. Or `dpkg-buildpackage -R'sh ./1.sh'`, where `1.sh`: `make -f debian/rules CFLAGS='-g3 -Og' "$@"` (if your command needs quotes). On a side note, if I were up to debugging a program, I'd go with `-g3 -Og` or just `-g3` (if `-Og` is not supported). `-g` might also suffice, but the resulting image doesn't include macro info (can't use macros in expressions). – x-yuri May 01 '19 at 12:57
  • The DEB_CFLAGS_SET variable has no effect on debian bullseye. – Graham Leggett Sep 07 '22 at 11:21
  • @GrahamLeggett i doubt it. i've just tested on bookworm/sid (bookworm is the next (upcoming) release after bullseye, and it still works. the package i tested this on uses **short-form `dh`** (pretty much the standard). of course package maintainers can do all kind of things in d/rules, and might have (accidentally) broken `DEB_*_SET` (many non-official Debian packages out there are of sub-par quality from a packaging perspective) – umläute Sep 07 '22 at 15:09