GNU Make

Martin Všetička
2011

GNU Make | About the tool

Initial release in 1977.
Original Make author: Stuart Feldman
GNU Make authors: Richard Stallman, Roland McGrath and Paul Smith
Development: In active development. New features are being added.

Project homepage: http://savannah.gnu.org/projects/make/
Manual: http://www.gnu.org/software/make/manual/

GNU Make | Terminology

Rule syntax:

target ... : prerequisites ...        myprog: prog.c lib.c
       recipe                              gcc -o myprog prog.c lib.c
       ...

GNU Make | Recipes

Example:

.PHONY test clean
test: 
    tmpDir="/tmp"  # variable in shell
    mkdir $$tmpDir/my_project_name  # does not know the 'tmpDir' variable!

clean:
    -rm -f *.o   # first '-' is processed via make 
                 # and suppresses errors of the rm command
    +echo "Done" # The line is run in a subshell despite 
                 # the make's flags '-n' (just print), '-t' (touch) and 
                 # '-q' (question) 
             

GNU Make | Makefile

GNU Make | A Simple Makefile

 edit : main.o kbd.o command.o insert.o \
        search.o files.o
         cc -o edit main.o kbd.o command.o insert.o \
                    search.o files.o 
 main.o : main.c defs.h
         cc -c main.c
 kbd.o : kbd.c defs.h command.h
         cc -c kbd.c
 command.o : command.c defs.h command.h
         cc -c command.c
 insert.o : insert.c defs.h buffer.h
         cc -c insert.c
 search.o : search.c defs.h buffer.h
         cc -c search.c
 files.o : files.c defs.h buffer.h command.h
         cc -c files.c
 clean :
         rm edit main.o kbd.o command.o search.o files.o

GNU Make | Order of rules and default goal

GNU Make | include directive

GNU Make | include directive (2)


What file is included?

GNU Make | Phony targets

GNU Make | Static Pattern Rules

GNU Make | Variables

GNU Make | Variables and their flavours (1)

  1. Recursively expanded variable:
    • They are defined by lines using '='
      (or by define directive for values with newlines).
    • The value is stored verbatim and expanded whenever the value is substituted.
    • Examples:
      foo = $(bar)
      bar = $(ugh)
      ugh =                    Hi there!                    
      all:;echo $(foo)   # prints 'Hi there!' (whitespace is gone!)
      CFLAGS = $(include_dirs) -O  # It is OK because value is ... 
      include_dirs = -Ifoo -Ibar   # ... stored verbatim! (advantage)
      CFLAGS = $(CFLAGS) -O        # INFINITE LOOP! (disadvantage)

GNU Make | Variables and their flavours (2)

  1. Simply expanded variables:
    • They are defined by lines using ':='.
    • The value of a simply expanded variable is scanned once and for all, expanding any references to other variables and functions, when the variable is defined.
    • Example:
      x := foo
      y := $(x) bar
      x := later
      
      is equivalent to
      y := foo bar
      x := later

GNU Make | Appending More Text to Variables

GNU Make | substitution reference

GNU Make | How Variables Get Their Values

GNU Make | How Variables Get Their Values (2)

GNU Make | Functions for Transforming Text

GNU Make | Common goals (1)

GNU Make | Common goals (2)

GNU Make | Implicit rules and variables (1)

GNU Make | Implicit rules and variables (2)


An implicit rule applies if the required prerequisites “exist or can be made”. A file “can be made” if it is mentioned explicitly in the makefile as a target or a prerequisite, or if an implicit rule can be recursively found for how to make it. When an implicit prerequisite is the result of another implicit rule, we say that chaining is occurring.

GNU Make | Implicit rules and variables (3)

GNU Make | Implicit rules and variables (4)

GNU Make | Generating Prerequisites Automatically (1)

GNU Make | Generating Prerequisites Automatically (2)