Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Greater than string comparison in a Makefile

Tags:

makefile

How do I express the following logic in a Makefile?

if $(XORG_VERSION) > "7.7"
   <do some thing>
fi

Conditional Parts of Makefiles only provides ifeq or ifneq.

like image 585
Dyno Fu Avatar asked Aug 09 '10 03:08

Dyno Fu


2 Answers

I use the sort function to compare values lexicographically. The idea is, sort the list of two values, $(XORG_VERSION) and 7.7, then take the first value - if it's 7.7 then the version is the same or greater.

ifeq "7.7" "$(word 1, $(sort 7.7 $(XORG_VERSION)))"
   <do some thing>
endif

Adjust 7.7 to 7.8 if you need the strict greater-than condition.

This approach improves portability by avoiding shell scripts and concomitant assumptions about the capabilities of the available OS shell. However, it fails if the lexicographic ordering is not equivalent to the numerical ordering, for example when comparing 7.7 and 7.11.

like image 108
anatolyg Avatar answered Nov 20 '22 11:11

anatolyg


You're not restricted to using the make conditional statements - each command is a shell command which may be as complex as you need (including a shell conditional statement):

Consider the following makefile:

dummy:
    if [ ${xyz} -gt 8 ] ; then \
        echo urk!! ${xyz} ;\
    fi

When you use xyz=7 make --silent, there is no output. When you use xyz=9 make --silent, it outputs urk!! 9 as expected.

like image 7
paxdiablo Avatar answered Nov 20 '22 13:11

paxdiablo