Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deferred conditional assignment in makefiles

Tags:

makefile

I understand deferred assignment (assigning things with = as opposed to :=), and I understand how to use #ifneq and similar commands for conditional assignment. How do I make a deferred conditional assignment?

Here is a psuedo-code example:

FOO = defined
BAR = $(defined(FOO) ? one : two)

test_1: $(BAR) #depends on one

FOO =

test_2: $(BAR) #depends on two

The main purpose of this is to configure my linker flags on the fly for static and dynamic builds. Building statically is more annoying that building dynamically because of circular dependencies (in addition to -fPIC removal and such). In this way, a couple variables change based on the definedness of STATIC (i.e. make STATIC=y). I could just define VAR and VAR_STATIC for each varable that needs variants, but I'd like to keep it has one global switch.

The kicker is that some submakes define rules that must be compiled dynamically in addition to rules that don't. Therefore, only a select few rules need to ignore the flag. This is why I would like to be able to toggle it on and off within the same Makefile.

Alternatively again, I could just reinclude the global Makefile when I need to toggle a flag, but that would not be preferred.

EDIT: From Come Raczy's solution, this is the specific syntax for my particular problem:

#Makefile
all: test_1 test_2

CFLAGS = -Wfatal-errors -std=c++11 -Wall -Werror -O3
CFLAGS += $(if $(DSYM),-g,)
test_1: DSYM:=y
test_2: DSYM:=

test_%: test.cpp
    g++ $(CFLAGS) $< -o $@

clean: 
    rm -f test test_sym test_back
like image 917
Suedocode Avatar asked Dec 11 '25 11:12

Suedocode


1 Answers

If I understand the question correctly, it is a combination of a conditional, deferred and per-target. Something like this:

BAR = $(if $(FOO), one, two)
all: test_1 test_2
FOO = defined
test_1: local_BAR:=$(BAR) #depends on one
FOO =
test_2: local_BAR:=$(BAR) #depends on two
test_%:
        echo $*: $(local_BAR)
like image 164
Come Raczy Avatar answered Dec 14 '25 11:12

Come Raczy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!