0

I am looking for guidance here as I recently encountered an issue when it came to comparisons using ifeq on variables that originated from a top-level Makefile I have no control over, and it made me wonder if there is a way to sanitize or standardize variables so that I can consistently use the same type of comparison going forward.

This is the problem I ran into, notice the use of " around values.

my_target:
ifeq ($(strip $(TOP_VAR_1)), "VALUE")
    #this comparison yields true
endif

ifeq ($(strip $(TOP_VAR_2)), VALUE)
    #this comparison yields true
endif

I was able to get these comparisons to work by trial and error based on GNU's conditional syntax guide, but this took me a great amount of time to do so.

Previous attempts include playing around with strip and quotation marks on variables and values, yet those still yielded a false equality.

e.g.

my_target:
ifeq ($(strip $(TOP_VAR_1)), VALUE)
    #this comparison yields false
endif

ifeq ($(strip $(TOP_VAR_2)), "VALUE")
    #this comparison yields false
endif
3
  • 1
    You will have to restrict or at least describe which "sanitization" (sp?) you have in mind - spaces, " and ', dots, slashes etc. If the problem is only dealing with " then a simple TOP_VAR := $(subst ",,$(TOP_VAR)) to canonicalize their format would be enough. Oct 31, 2022 at 13:32
  • @Vroomfondel I did not envision some specific sanitization given printing out values did not show any characters or white spaces out of place (e.g. no "VALUE" or VALUE ``) so I didn't understand what was going on that caused the comparison to fail, thus me attempting to get the values to a "standard" format Oct 31, 2022 at 13:58
  • Well, we are still in exact-string-compare land with make, so for two strings to compare equal, you will need to be totally specific otherwise you won't be able to accomodate to makes wish of character-by-character equality. I take it that you would be ok with all funny quote characters and spaces removed. Oct 31, 2022 at 16:24

2 Answers 2

1

It's very unclear what exactly your question is. And, since you didn't show us what the actual values of the TOP_VAR_1 or TOP_VAR_2 variables are, we can't really provide any guidance.

I will say two things: first if you use the ifeq (...) form, then GNU make completely ignores quotes (or rather, treats them as any other character and not special in any way). And second, the only thing the strip function does is remove extra whitespace. It doesn't do anything with quotes.

So in your examples:

ifeq ($(strip $(TOP_VAR_1)), "VALUE")

This will be true if the value of the TOP_VAR_1 variable is the string "VALUE" (including the quotes!!), possibly with leading or trailing whitespace (as in, it would match "VALUE" but it would NOT match " VALUE " or VALUE etc.)

ifeq ($(strip $(TOP_VAR_2)), VALUE)

This matches if TOP_VAR_2 is the string VALUE (without quotes!) possibly with leading or trailing whitespace (as in, it would match VALUE but it would NOT match "VALUE" etc.)

If you want to see what these variables contain the simplest way is using the info function:

$(info TOP_VAR_2='$(TOP_VAR_2)')

Whatever is inside the '' in the output is the string that ifeq will operate on. You can of course also add the strip function to see how that changes things.

1

Following my comment from above, a sanitizing function applied to variable contents would look like this:

sanitize-var = $(subst ',,$(subst `,,$(subst ",,$(strip $1))))

TOP_VAR_1_san := $(call sanitize-var,$(TOP_VAR_1))
TOP_VAR_2_san := $(call sanitize-var,$(TOP_VAR_2))

ifeq ($(TOP_VAR_1_san),FOO bar)
....
1
  • 1
    This function is very handy and is really well complemented by @MadScientis's explanation. It turned out you we're right and "hidden" quotations were contained in the top variable that were escaped by echo Oct 31, 2022 at 17:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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