Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Math in a Makefile: (8 Items)
   
Math in a Makefile  
The fate of Earth depends on this.

In my Makefile, QCC 6.6 on Windows (7)

     echo $$(( 4 * 2 ))
-> 8, correct
     echo $$(( 4 > 2 ))
-> 1, correct
     echo $$(( 4 < 2 ))
-> 0, correct

but

     echo $$(( 4 < 2 ) && ( 5 < 6 ))

[stuff]Temp/make11500-1.sh: line 1: 4 < 2 ) && ( 5 < 6 : syntax error in expression (error token is ") && ( 5 < 6 ")



The question to save Earth (hurry!) is:  What is the magical token string with which to replace && in the expression 
above?  I don't want to do a succession of clumsy ifeq 1 type stuff to create a chain of ands (or ors).
Re: Math in a Makefile  
>      echo $$(( 4 < 2 ) && ( 5 < 6 ))

For this, You do not really need the inner parentheses, so throw them away try: echo $$(4<2 && 5<6)

HTH,
Michael.
Re: Math in a Makefile  
forgot an "and" between "away" and "try" - sorry.
Re: Math in a Makefile  
Ok, I tried it, got:

[code]echo $(4<2 && 5<6)
C:/Users/wztjhp/AppData/Local/Temp/make9816-1.sh: line 1: 2: No such file or directory
[/code]

Is the && allowed (or some other symbol?)

What I'm really looking for is a quick Apache version # >= test.  I have the logic, just not the simplest form for a Makefile.
Re: Math in a Makefile  
Well, the permutations attempting to reverse-engineer a not-intuitive system approaches infinity.

Tried this:

	echo "Logic test follows"
	echo $(or 1,0)
	echo $(or 0,0)
	echo "Or 0 1"
	echo $(or 0,1)
	echo "Or 1 1"
	echo $(or 1,1)
	echo $(and 0,0)
	echo $(and 1,0)
	echo $(and 0,1)
	echo $(and 1,1)
	echo $(xor 0,0)	
	echo $(xor 0,1)	
	echo $(xor 1,0)	
	echo $(xor 1,1)	

Get this:
echo "Logic test follows"
Logic test follows
echo 1
1
echo 0
0
echo "Or 0 1"
Or 0 1
echo 0
0
echo "Or 1 1"
Or 1 1
echo 1
1
echo 0
0
echo 0
0
echo 1
1
echo 1
1
echo

echo

echo

echo


Note:
Or 0 1
echo 0
0
echo "Or 1 1"
Or 1 1
echo 1
1

So whatever or does, it isn't or.

xor doesn't do anything other than nullify input apparently.


Tried or without comma:

	echo $(or 1 1)	
-->
echo 1 1
1 1

These aren't errors, nor are they boolean operations.
Re: Math in a Makefile  
In lieu of that, trying the daisy-chain route :(



QCCMAJORVER := 6
M3 = $$(( $(QCCMAJORVER) > 2 ))
...
(stuff)
	echo "Hello, world!"
	echo $(QCCMAJORVER)
	echo "$(M3)"
	echo $(M3)
ifeq ($(M3),1)
	echo "New"
else
	echo "Old"
endif
	echo "End"


Gives

echo "Hello, world!"
Hello, world!
echo 6
6
echo "$(( 6 > 2 ))"
1
echo $(( 6 > 2 ))
1
echo "Old"
Old
echo "End"
End



Why does 1 not equal 1, causing it to print Old instead of New?  Is it comparing something like a string of "$(( 6 > 2 
))" to 1 instead of evaluating it?  What forces it to evaluate in an ifeq lline?
Re: Math in a Makefile  
Hi Kevin,

The make supplied by QNX is GNU make.   The documentation here: http://www.gnu.org/software/make/manual/html_node/
Conditional-Functions.html describes the behaviour of $(if), $(and), and $(or) - note that the predicate is whether or 
not the argument is empty.   Another aspect to understand is that "$$" is just an escaped "$" character.   As far as 
make is concerned, "var = $$((a))" is you asking it to set the variable "var" to the string "$((a))".   However when you
 put that on a line to be interpreted by the shell - such as an 'echo' line - the shell will get the text "$(())" which 
the shell interprets as a request for arithmetic expansion.   The key point is that it is the shell doing the arithmetic
, not the make engine.

You might try using the $(shell) function to invoke the shell to have the expansion performed earlier.  For example, in 
your test makefile, try:

M3 = $(shell echo $$(( $(QCCMAJORVER) > 2 )))

..to run the shell, compute the expansion, and save the result as '1' or '0', rather than just storing the text "$(( 6 >
 2))".

Hope this helps,
-Will
Re: Math in a Makefile  
If the result of a logical operation such as > is 0 or 1, then that result cannot be passed in to subsequent AND or OR 
operations as they are based on empty string-as-false, not 0-as-false.

Are there any examples of more complex logic being done in a makefile?  Cloning that is probably a lot faster than 
reverse-engineering how to do this.