-1

I need my make to work in the following scenarios. At least one among flag_a or flag_b is to be set while doing make. There is possibility for flag_c also in future. make flag_a=true or make flag_b=true or make flag_a=true flag_b=true

Cleanup is simple "make clean". It should clean everything irrespective of which flag was set during make.

In my makefile, I want to something like set a variable when a flag is set and finally add the variables. If the sum of variables is 0, that means no flag was set in make. I want to throw an error and terminate make in this case. My makefile is something like the following.

1 FLAG_A_SET=0

2 FLAG_B_SET=0

3 all:

4 ifdef flag_a

5 FLAG_A_SET=1

6 endif

7 ifdef flag_b

8 FLAG_B_SET=1

9 endif

10 FLAGS_SUM=$(shell echo $(expr "$(FLAG_A_SET)" + "$(FLAG_B_SET)"))

11 ifeq ($(FLAGS_SUM), 0)

12 tab $(error You must specify at least one among flag_a and flag_b)

13 else

14 tab make -C files

15 endif

Then I am getting error "commands commence before first target" at line 14. Also I am doubtful about statement in line 10. Is the expression correct? ~

1 Answer 1

0

Yikes. You give no reason for wanting to do this in such a complex way. If you really just want to know if one or more of a set of variables has any value, you can to it much more easily:

ifeq ($(flag_a)$(flag_b),)
  $(info no flag is set)
else
  $(info one or more flags are set)
endif

Basically, you expand all the flags and see if the result is equal to the empty string or not.

ETA

If you want to avoid this check when running clean you can look at the $(MAKECMDGOALS) variable to see what command line goals were provided and skip this test if it was clean:

ifneq ($(MAKECMDGOALS),clean)
  ifeq ($(flag_a)$(flag_b),)
    $(info no flag is set)
  else
    $(info one or more flags are set)
  endif
endif

You could combine this into a single test using filter-out but it'd probably be more confusing than it's worth.

2
  • Hi MadScientist, that solves one of my problem. My logic for sum of flags is no longer required. Now my file is like following 1 all: $(BUILDDIRS) 2 (BUILDDIRS): 3 ifeq ($(flag_a)$(flag_b),) 4 $(error no flag is set) 5 else 6 tab$(MAKE) -C files 7 endif. Then there is another section for clean. Now when I am doing make clean, I am getting error "*** no flag is set. Stop".
    – Vimt
    Feb 10, 2015 at 16:19
  • Thanks MadScientist. Now all my issues are resolved.
    – Vimt
    Feb 11, 2015 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.