Makefile Flashcards
What is the function and syntax to evaluate each word in a list and expand each one individually in some expression?
$(foreach var,words,text) Evaluate text with var bound to each word in words, and concatenate the results.
What is $? ?
The names of all prerequisites that are newer than the target.
What is the function and syntax for selecting one word from a list?
$(word n,text) Extract the nth word (one-origin) of text.
Which directive identifies targets which do not represent a file of the same name (the default behavior)?
.PHONY: targets…
What is the function and syntax for selecting the non-directory part of a file name?
$(notdir names…) Extract the non-directory part of each file name.
What are the three elements of a rule?
Targets, Prerequisites (or Dependencies), and Commands
target [more targets] :[:] [prerequisites] [; commands] [commands] [commands]
What is the function and syntax for adding a suffix to a name?
$(addsuffix suffix,names…) Append suffix to each word in names.
What is $% ?
The name of an archive member (used with ar).
Give an example of an explicit rule with a multi-line command
$(targets) : @echo $@; \ @touch $@ The key is the backslash.
What is the function and syntax for general pattern substitution?
$(patsubst pattern,replacement,text) Replace words matching pattern with replacement in text.
What is the function and syntax for counting the words in a list?
$(words text) Count the number of words in text.
What is the function and syntax to generate a warning?
$(warning text…) When this function is evaluated, make generates a warning with the message text.
Which option or options show how make analyzes the dependency graph?
–debug More powerful: –debug=option,option basic - targets and update actions verbose - additional info implicit - basic plus implicit rule searches jobs - subprocesses makefile - info about includes all - all (default with -d)
Invoking with –print-data-base (-p) results in what?
Make prints Variables, Directories, Implicit Rules, Pattern-Specific Variable Values, Files (Explicit Rules), and VPath Search Values. In that order.
What is the difference between include and -include?
-include ignores missing files.
What is the function and syntax for selecting the directory part of a file name?
$(dir names…) Extract the directory part of each file name.
What does a list look like?
A list looks like a string with words separated by spaces. Ex: MYLIST = fub.c bar.h goo.cpp
What option can be used to find mistyped or misnamed variables?
–warn-undefined-variables Combine with –just-print to debug without building.
What is $* ?
The stem with which an implicit rule matches. The stem is the pattern match in an implicit rule.
What is the function and syntax to call a user-defined function?
$(call var,param,…) Evaluate the variable var replacing any references to $(1),$(2) with the first, second, etc. param values.
What is the function and syntax for selecting the base part (no suffix or file extension) of a file name?
$(basename names…) Extract the base name (name without suffix) of each file name.
Give an example of a one-line explicit rule.
$(targets) :; @echo $@; touch $@ The semicolon is the key
What are $(@D), $(@F), $(*D), $(*F), $(%D), $(%F), $(
The directory and file-within-directory parts of $@, $*, $%, $
How do you expand a variable? Call a function?
$(var) $(call functionname)
What is the function and syntax for removing excess whitespace?
$(strip string) Remove excess whitespace characters from string.
What is the function and syntax for sorting a list?
$(sort list) Sort the words in list lexicographically, removing duplicates.
What is the function and syntax for removing a pattern or pattern from a string?
$(filter-out pattern…,text) Select words in text that do not match any of the pattern words.
What is the difference between = and := ?
= is assignment := is expansion assignment Assignment expands the definition when the variable is used. Expansion assignment expands the definition when it appears in the Makefile, immediately (like procedural code).
What is the function and syntax to run a shell command and return its output?
$(shell command) Execute a shell command and return its output.
Which command line option invokes make to print commands that would be executed without actually executing them?
–just-print or -n
What is the function and syntax for finding a pattern or pattern within a string?
$(filter pattern…,text) Select words in text that match one of the pattern words.
What does pattern notation do? Give an example.
Pattern notation enables rules to be written once instead of repeating the same rule for many files. %.pdf: %.ps; -ps2pdf $<
Multiple explicit rules with the same target names is bad practice; you get a warning. How do you tell combine the commands from all rules?
target :: dependencies The double-colon is the trick
What is the function and syntax for selecting the suffix or file extension part of a file name?
$(suffix names…) Extract the suffix (the last dot and following characters) of each file name.
What does $(a:b=c) do?
This is modified variable expansion, specifically a substitution reference. When $(a) is expanded, every occurrence of b at the END OF A WORD is replaced with c.
Write a makefile that converts and Postscript(PS) file into a PDF using the command ps2pdf.
all: $(patsubst %.ps,%.pdf,$(wildcard *.ps)) %.pdf: %.ps; -ps2pdf $<
What is a computed variable name?
$($(x)) The variable name is expanded, then the variable is expanded.
What are the conditional directives?
ifdef variable ifndef variable ifeq (a,b) ifeq “a” “b” ifeq ‘a’ ‘b’ ifneq (a,b) ifneq “a” “b” ifneq ‘a’ ‘b’ else endif
What is the difference between an implicit rule and an explicit rule?
An implicit rule has targets and prerequisites, an explicit rule has commands. Rules can be split into explicit/implicit rules or merged together.
What is the function and syntax for selecting a slice of a list?
$(wordlist s,e,text) Returns the list of words in text from s to e.
What are the command modifiers?
@command prevents display of the command -command suppresses error detection (make will not halt) !command executes command for EACH dependent file if $** or $? is used. $** expands into all dependencies, $? expands into those with later timestamps than targets.
What is $+ ?
The names of all prerequisites, including duplicates.
What is $@ ?
The name of the target
What is the function and syntax for joining two parallel lists of words?
$(join list1,list2) Join two parallel lists of words.
What is the function and syntax to generate a fatal error?
$(error text…) When this function is evaluated, make generates a fatal error with the message text.
What is the function and syntax to print a string describing how a variable was defined?
$(origin variable) Return a string describing how the make variable variable was defined.
What is the function and syntax to get a list of files given a wildcard pattern?
$(wildcard pattern…) Find file names matching a shell file name pattern (not a `%’ pattern).
What is $^ ?
The names of all prerequisites with duplicates removed.
What is the function and syntax for testing for a string within a string?
$(findstring find,text) Locate find in text. Returns the found or “”.
What is the function and syntax for general text substitution?
$(subst from,to,text) Replace from with to in text.
What is the function and syntax for selecting the first word of a list?
$(firstword names…) Extract the first word of names.
What is $< ?
The name of the first prerequisite.
What is the function and syntax for adding a prefix to a name?
$(addprefix prefix,names…) Prepend prefix to each word in names.