הלינקייה: מגזין חודשי למפתחים

רוצה לשמוע על כל האירועים, המדריכים, הקורסים והמאמרים שנכתבו החודש ?
הלינקייה הינו מגזין חופשי בעברית שמשאיר אותך בעניינים.
בלי ספאם. בלי שטויות. פעם בחודש אצלך בתיבה.

Functions

The make utility comes with a huge repository of built in functions, and with the option to define custom user functions to enhance the functionality of the make utility. Here, we examine some usages of builtin and custom functions.

# 
# A user defined function is simply a variable that takes into
# account some special variables: $1, $2, $3, ..., which are called
# the function arguments. 
# In this simple example, foo turns out to hold: b a
# Note the = sign for the variable. In functions, we always use = and not :=
 
reverse = $(2) $(1)
 
foo = $(call reverse,a,b)
 

Since any variable definition can serve as a function, we can nest function calls, like in the following example:

#
# The pathsearch function will return the full path of the file it 
# took as $1. 
# In this example, we use four builtin functions to do the trick, and 
# one environment variable. 
# The subst's job is to replace : with spaces, because addsuffix takes a
# list of words separated by spaces. 
# 
 
pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
 
LS := $(call pathsearch,ls)

And finally, it is also possible to nest calls to call itself, masking the value of $1 each stack frame.

#
# The map user defined function goes takes a function and a list
# and calls the function for each element in the list
# In the end, value of o is something like: 'file file default'
#
 
map = $(foreach a,$(2),$(call $(1),$(a)))
o = $(call map,origin,o map MAKE)
 
#########################
# When a macro takes arguments, we call it a function
# We can activate that macro with the arguments using the builtin
# call function 
 
AWK  := awk
KILL := kill
 
 
define hello
  @echo "Hello, $1 $2"
endef
 
.PHONY: all
 
all:
	$(call hello, "Ynon", "Perek")
AWK  := awk
KILL := kill
 
define kill-perl
  @ ps -w |                                       \
  $(AWK) ' /[p]erl/ { system("$(KILL) " $$1 )} ' 
endef
 
all:
	$(kill-perl)
AWK  := awk
KILL := kill
 
 
define kill-perl
  @ ps -w |                                       \
  $(AWK) ' /$1/ { system("$(KILL) " $$1 )} ' 
endef
 
.PHONY: all
 
all:
	$(call kill-perl,[p]erl)
course: