Test if a list of variables are defined in a Makefile

nowox

My Makefile is based on multiple variables defined in a configuration file or ENV vars. My current solution is to test all of them manually:

NOGOAL = help clean distclean mrproper
ifeq ($(strip $(filter $(NOGOAL), $(MAKECMDGOALS))),)
VAR1 ?= $(error VAR1 undefined)
VAR2 ?= $(error VAR2 undefined)
VAR3 ?= $(error VAR3 undefined)
...
VARn ?= $(error VARn undefined)
endif

I would like to use a foreach loop instead:

ifeq ($(strip $(filter $(NOGOAL), $(MAKECMDGOALS))),)
TESTVAR = TEST1 TEST2 TEST3
$(foreach v, $(TESTVAR), $(eval $v ?= $$(warning Error: $v undefined)))
endif

Unfortunately eval doesn't work as I expected. Did I miss something?

Here a full test of my tests with 2 implementations of the tests. Even if TEST3 is not defined I don't get any error

TEST1 = 1
TEST2 = 1
#TEST3 = 1   # NOT DEFINED


TESTVAR := TEST1 TEST2 TEST3

# First implementation
$(foreach v, $(TESTVAR), $(eval $v ?= $$(warning Error: $v undefined)))

# Second implementation
$(foreach v, $(TESTVAR), $(eval $(call TESTER,$v)))
define TESTER
ifndef $1
$(warning $1 not defined)
endif
endef

# Dummy rule
all:
    @echo Hello World   

However, my first implementation works if I use $(TEST3) somewhere.

EDIT

Here I get no error but TEST3 is not defined:

~$ cat Makefile
TEST1 = 1
TEST2 = 1
#TEST3 = 1   # NOT DEFINED


TESTVAR := TEST1 TEST2 TEST3

# First implementation
$(foreach v, $(TESTVAR), $(eval $v ?= $$(warning Error: $v undefined)))

# Dummy rule
all:
        @echo Hello World

~$ make
Hello World
MadScientist

Well, I guess I don't get it. Your original version, that you say works the way you want it, will not print any warnings unless you USE one of the variables which is not defined. Your first alternative with foreach works the same way: it will print a warning but only when you use the variable that's undefined.

If you want it that way, then testing for clean, etc. doesn't really make much sense since presumably those rules won't use the variables that are not defined so you won't get any errors (and if they did use the variables that weren't defined, presumably you'd want those rules to fail as well).

But in your second edit, you say that you want the make to fail immediately if the variables are not defined, regardless of whether or not they're used (in your last example you don't define TEST3, but you don't use TEST3 for anything either so no warning is printed). If that's what you want I don't see why you are assigning values to the variables with ?= at all, or using eval. Just write something like:

ifeq ($(strip $(filter $(NOGOAL), $(MAKECMDGOALS))),)
  $(foreach v,$(TESTVAR),$(if $($v),,$(error Error: $v undefined))
endif

(In this version you do need to check MAKECMDGOALS since it fails immediately on an unset variable).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Where can I get a list of Ansible pre-defined variables?

From Dev

Understanding Makefile Syntax and Variables

From Dev

Global Makefile with scoped variables

From Dev

Variables Defined in rSpec Not Changing

From Dev

Test if a list of environment variables are all set to some values

From Dev

Setting environment variables in a makefile

From Dev

Test that term is a list of distinct variables

From Dev

How to call list of variables from a resource file into Robot test?

From Dev

Escaping makefile variables (for internal makefile use)

From Dev

Using bash variables in Makefile

From Dev

Conditional variables in a makefile

From Dev

Makefile adding spaces to variables

From Dev

Do variables defined inside list comprehensions leak into the enclosing scope?

From Dev

Dollars in Makefile environment variables

From Dev

Passing Variables in Makefile

From Dev

Is there a way to print all variables defined/visible within a makefile?

From Dev

Global Makefile with scoped variables

From Dev

Test if a list of environment variables are all set to some values

From Dev

How to run a shapiro test with a subset of data defined by more than 2 variables

From Dev

Test if a list of variables are defined in a Makefile

From Dev

Conditional variables in a makefile

From Dev

Makefile adding spaces to variables

From Dev

How to get list of class variables in the order they are defined?

From Dev

How are Makefile variables calculated?

From Dev

Test in if condition makefile on cygwin

From Dev

How to introduce variables in Makefile

From Dev

Generic Makefile disregarding variables

From Dev

Intersperse items in Makefile variables

From Dev

r- Feed a list of variables into a matrix for a t.test

Related Related

HotTag

Archive