CFLAGS and other flag variables

Alejandro Colomar <[email protected]> Tue, 16 Jul 2024 15:37:31 +0200
Newsgroups gmane.comp.gnu.make.general
Message-ID <t7ojygbvo3k7ymjgqpdvdcmmhbtremuehe6papcb34df5os3k2@psecl7grgtuy>
Hi Paul!

Usual compilation in a makefile is something like

	foo.o: foo.c; $(CC) $(CFLAGS) $< -o $@

If a project needs some CFLAGS, I guess it would do the following

	CFLAGS ?= $(shell pkgconf --cflags somedep)

But then, a user must not blindly overwrite CFLAGS.  Those missing flags
from somedep would probably break compilation.  For allowing one to
append instead of overwriting, I added EXTRA_CFLAGS, so that it looks
like

	CFLAGS ?= $(shell pkgconf --cflags somedep) $(EXTRA_CFLAGS)

And so one can run `make EXTRA_CFLAGS=-O2` to append to the default
ones.

However, I think this is not standard practise.  Is there any standard
(or de-facto standard) practice regarding these variables?

Same question for CPPFLAGS, LDFLAGS, etc.

I've been talking to some distributions, and they seem to want my
Makefile to read from CFLAGS and magically append to CFLAGS, which
sounds at least weird.  Has this behavior been always like that?  When
did it change?  Is it autotools weirdness?  Should I follow it?

I've been thinking that I could do it with something like the following:

	ifdef CFLAGS
	EXTRA_CFLAGS := $(CFLAGS)
	endif
	CFLAGS := $(shell pkgconf --cflags somedep) $(EXTRA_CFLAGS)

But that looks very weird, at least.  (And it doesn't allow overwriting
all CFLAGS, which seems also meh.)  I could go even weirder, and use:

	ifdef CFLAGS
	EXTRA_CFLAGS := $(CFLAGS)
	endif
	OVR_CFLAGS := $(shell pkgconf --cflags somedep)
	CFLAGS  := $(OVR_CFLAGS) $(EXTRA_CFLAGS)

Should I?


Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es/>
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEE6jqH8KTroDDkXfJAnowa+77/2zIFAmaWd5UACgkQnowa+77/
2zKaug//WHVk8rNV1gogVh/BXJ5CeDfpp96D4cnvVR6rQ/2Wy56OGO+QvQUbwA8F
gvgqzus/g0M37/SlqGQi5EKenvuVoPR6ZsHEZhxzVMVpjdfFBfFmJDtj5dhe7ZAR
i97nw4dXJ/RyhZKVnirkmY0uOroIg6aHhXURFCkW2sw93WEvYxjX+/AMKyNcxUa/
nv1MOgVNOp24Kz05Yb7WtHFNB5c6Ckai1AjvhTTdzrCZGy5o/aMXOv1yy4Lzrcld
Fju4JoO18+We5987GPHxRjwZ7QV1Jr5u1D7HLu2aWpSUQOeCfe+ioD3sprnSIga+
cFLeLnfxkNkYLcuDK5JrcTMP+EkuduAx41Dp9FzYHZKd47+DPhpuaPUjWLwNy3ya
Js3mddilGSa+FrpdBP+bI5W0J0Z7hDiyL4FLZ84rXRboZFHkvTdsd9mublwcNv4h
XadpFKIAzVLzZYJDNs6NlAYTn/4DoYtP3u+5CKsYIn+6SV63cAROVRagepHpm0KA
0e8kcX0YNex4HhU1p4bMwSv2YRcExywonIuKG3tpcfgTKKg7Cu1PWieP6ztLOEDu
UXmhSpJpZJEsB6khYbZ1K4CCpbpJ7rqhnNT/HCd3SBAUkDU1Dhdb+whmFySJNqkN
9UpQIFgT9Xr61ASoAEDRcOuWHTTXQJ4RSMSPtX/ObRHUbuOTN5o=
=YyFK
-----END PGP SIGNATURE-----