gittup / tup

Tup is a file-based build system.
http://gittup.org/tup/
GNU General Public License v2.0
1.18k stars 145 forks source link

Feature: per-file compiler flags #485

Closed typeless closed 1 year ago

typeless commented 1 year ago

Currently, we can set the CFLAGS variable for a bunch of source files in a Tup rule such as

srcs-y += a.c
srcs-y += b.c
CFLAGS += -Wall
: foreach $(srcs-y) |> $(CC) $(CFLAGS) -o %o %f |>

However, there is no straightforward way to customize CFLAGS for a specific source file. For instance

[...]
## Additional CFLAGS for `a.c` only
CFLAGS:a.c += -D_POSIX_C_SOURCE -DNDEBUG
[...]

That is, when a variable is expanded in the 'foreach' rule, only specific files (a.c, in this case) are compiled with the overriden CFLAGS.

gittup commented 1 year ago

The Tupfiles for tup itself do this (for one file at least). In Tuprules.tup: https://github.com/gittup/tup/blob/8739d02408b4e215bb419ae6dfbe17a76b5d0518/Tuprules.tup#L85

Which gets used for lua.c in this Tupfile: https://github.com/gittup/tup/blob/8739d02408b4e215bb419ae6dfbe17a76b5d0518/src/lua/Tupfile#L41

The %-flags are expanded before the variable substitutions, so $(CFLAGS_%f) becomes $(CFLAGS_lua.c)which is then expanded to -Usig_atomic_t

Does something similar work for your setup?

typeless commented 1 year ago

Cool! I didn't expect $(CFLAGS_%f) would work. I'll try this for my project now and see if it fits well.

typeless commented 1 year ago

It does work. Even better, the %f in $(CFLAGS_%f) can contain slashes withou problems. So, I am cloing this feature request. Thank you.