Re: CFLAGS and other flag variables

Alejandro Colomar <[email protected]> Tue, 16 Jul 2024 16:27:22 +0200
Newsgroups gmane.comp.gnu.make.general
Message-ID <6xxaqjabdyid6cntv2qt4lt7s5sgxx2ql7nw2mkh5ixhisihey@ujlu6nc6wbif>
Hi Paul,

On Tue, Jul 16, 2024 at 10:16:27AM GMT, Paul Smith wrote:
> On Tue, 2024-07-16 at 15:37 +0200, Alejandro Colomar wrote:
> > However, I think this is not standard practise.  Is there any
> > standard (or de-facto standard) practice regarding these variables?
> 
> Generally users who want to have more sophisticated build systems don't
> try to re-use the built-in recipes.  Instead they replace those with
> their own pattern rules.  This allows them to use separate variables
> (as Sébastien suggests).

Thanks for the reference to Sébastien's mail; I didn't receive a copy,
but have now checked the archive.

> For example, if you examine automake which implements the GNU standards
> for writing makefiles, they suggest that CFLAGS have a simple default
> value, typically:
> 
>     CFLAGS = -g -O2
> 
> But be reserved for users to set on the command line:
> 
>     make CFLAGS=-g
> 
> which means that CFLAGS should never contain any critical flags.

Hmm, that makes sense.

> 
> Instead these standards recommend creating your own pattern rules to
> avoid being limited to only the default settings; for example:
> 
>     EXTRA_CFLAGS := $(shell pkgconf --cflags somedep)
> 
>     %.o : %.c
>             $(CC) $(EXTRA_CFLAGS) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $(OUTPUT_OPTION) $<

Now that I see this, I wonder:
Is there any order preference between CFLAGS and CPPFLAGS?  I tend to
have CPPFLAGS first.  Probably it doesn't matter, but I'll ask just in
case.

Also: I didn't know about TARGET_ARCH.  Is that documented anywhere?

> 
> (you can simplify this if you prefer of course, this reuses what the
> default rule implements).

Makes sense.  I guess I'll do:

	FOO_CFLAGS := -critical -flags
	CFLAGS ?=
	CFLAGS_ := $(FOO_CFLAGS) $(CFLAGS)

	%.o: %.c
		$(CC) $(CPPFLAGS_) $(CFLAGS_) ...

Thanks!

Have a lovely day!
Alex

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

iQIzBAABCgAdFiEE6jqH8KTroDDkXfJAnowa+77/2zIFAmaWg0oACgkQnowa+77/
2zKD7A/9HeBJCgwm/RzUlDUzOJy7iuw9pcZ1ckGJs+3Hnv0UNwKnjC/F/M+Xj8Vn
vxFESPCPgEnRa9YWMkZUgHQReiIK6MqZVbXY6CgFt3sXjYTfeReTq0J/bwHMo0cq
zGVGCJhFl//QrH6DNZJUHGrspaimPIgtuHdL8tYTHvUHcEKfqdh8VZPzZ/fC/iJc
mMmKrf0pcLSVCC+a4/Vnnce6Bh2Z+IWzank/FGg7gthtQL6o1BLiamUyL4BiKMZg
MzoVk3POd1BcrVOAZLIyRUFs5yiTesUrY74dGU/y8soReTXl/O+VXSNj9sOk6o8G
x00JV3g1pe3TvopgFxvCNah8GMq0hFrq9p1F2kNUehnx8URpj7RdFmfHldBTkwOa
c/SZqzbSH9N0mRHrLWbJ779THLGIgIfhG80cjOBBZmTXOaNjpCJYP33e2/MeG3Pl
cunWq18ZhP+C/3zHQceqR4YxKNaIDNib0WRN4u/qLD/U5ST/PYriwFRCQ9XHUR6y
oIh93jyBMnk6x7zpEhriBRtB1bMDSAuk9mQZI4KIcUuXew/D22SLtuUvSpQYDDKH
nTGMoDJQS62rp72pq1pw5GdyqxgqukigSUQp+ISOLx+wfVCQSAEtTuwVLn8eYrpr
11JhY5W6rUwLrEL6efvcbTie3snh8k2fMDLtge2sp8gO22vNy4E=
=uQZj
-----END PGP SIGNATURE-----