Monday, November 30, 2015

Makefile Arithmetic Hack

One of the annoyances of GNU Makefile is the inability for native arithmetics.   I had to solve this recently where I needed to create multiple targets based on simple loop. I found this, but it wasn't as programmable as I would have liked. So here is my stupidly simple hacked solution. Hope you like ittt.

# -------------------------------------
# Some Helpful arithmetics
# -------------------------------------
#Addition
ADD = $(shell echo $(1)+$(2) | bc)

#Subtraction
SUBTRACT = $(shell echo $(1)-$(2) | bc)

#Multiplication
MULTIPLY = $(shell echo $(1)*$(2) | bc)

#Division
DIVIDE = $(shell echo $(1)/$(2) | bc)

#Division (Floating Point)
DIVIDEF = $(shell echo "scale=3; $(2)/$(1)" | bc)

#Modulo
MODULO = $(shell echo $(1)%$(2) | bc)

#Comparison Greater Than
COMPARISON1 = $(shell echo $(1)\>=$(2) | bc)

#Comparison Smaller Than
COMPARISON2 = $(shell echo $(2)\<=$(2) | bc)

and how you use them:
# Function = a*15+b*33+2
Function = $(call ADD,$(call ADD,$(call MULTIPLY,$(1),15)),$(call MULTIPLY,$(2),33),2)


# calling the function
test:
     echo $(call Function,4,5)

Got a better idea?

No comments: