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

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

Make Variables

It is time to take a deeper look at the make variables. Here are some idead and gotcha to use variables with.

#
# Recursive variables can refer to other recursive variables. 
# The following example prints "Huh ?"
#
foo = $(bar)
bar = $(ugh)
ugh = Huh?
 
all:;echo $(foo)

Substitutional variables

We can use the $(var:a=b) syntax, to substitute every 'a' in the end of a word into a 'b'. For example:

# 
# Using substitution reference, we end up with bar
# equals to: a.c b.c c.c
#
 
foo := a.o b.o c.o
bar := $(foo:.o=.c)
 

We can also use the '%' character to have more powerful substitutions

# 
# Using the '%' means any sequence of characters. 
# This one also sets bar to: a.c b.c c.c
#
 
foo := a.o b.o c.o
bar := $(foo:%.o=%.c)
#################
# Make does not erase trailing spaces
# which makes this makefile fail miserably
 
# there's a trailing space after the libio.a
LIBRARY = libio.a 
 
missing_file:
	touch $(LIBRARY)
	ls -l | grep '$(LIBRARY)'
 
################################
# A variable can hold a string, and if that
# string is a shell command, we can even execute it
# in the rules to make a target. 
# But, using this syntax does not put the result of the 
# shell command in the variable. 
# So, while free-space actually prints the free space when
# it is used in a rule, half-free-space does not
 
DF = df
AWK = awk
 
free-space := $(DF) . | $(AWK) 'NR==2 {print $$4}'
half-free-space := $(free-space)/2
 
all:
        #$(free-space)
        #$(half-free-space)
        $(free-space)
###############################
# It is possible to declare multi line variables
# and they are called macros
# Writing the macro as a rule executes the entire
# sequence line-by line
 
CLASSES = Hello.class
SRC = Hello.java
APP_JAR = hello.jar
CP = cp
RM = rm -rf
TMP_JAR_DIR = tmp
MKDIR = mkdir -p
JAR = jar
JARFLAGS = -cvf
MANIFEST = Manifest
 
define create-jar
 @echo Creating $@...
 $(RM) $(TMP_JAR_DIR)
 $(MKDIR) $(TMP_JAR_DIR)
 $(CP) -r $^ $(TMP_JAR_DIR)
 cd $(TMP_JAR_DIR) && $(JAR) $(JARFLAGS) ../$@ .
 $(JAR) -umf $(MANIFEST) $@ 
 $(RM) $(TMP_JAR_DIR)
endef
 
all: $(APP_JAR)
 
%.class:%.java
	javac $<
 
$(APP_JAR): $(CLASSES) $(MANIFEST)
	@$(create-jar)
 
clean:
	$(RM) $(TMP_JAR_DIR) $(CLASSES) $(APP_JAR)
 
     immediate = deferred
     immediate ?= deferred
     immediate := immediate
     immediate += deferred or immediate
     
     define immediate
       deferred
     endef
     
     define immediate =
       deferred
     endef
     
     define immediate ?=
       deferred
     endef
     
     define immediate :=
       immediate
     endef
     
     define immediate +=
       deferred or immediate
     endef
course: