SlideShare a Scribd company logo
GUN Make

Reference: O’Reilly – Managing Projects with GNU Make 3th.




                                               Eric Hsieh
Outline
• Rule
• Variable and macro
• Functions
Introduction
• Why do we need GUN Make?
• Source -> Object -> Executable
• Goal: The make program is intended to
  automate the mundane aspects of
  transforming source code into an executable.
• Base on timestamp.
Rule
Target: Prereq1 Prereq2 …
<Tab> commands1
<Tab> commands2

Example:
hello: hello.c
      gcc hello.c –o hello
Rule
$make
gcc hello.c –o hello

#make again
$make
make: `hello.c’ is up to date.

(1)
Rule
Just print
$make - -just-print or –n

Comment character
# gcc hello.c –o hello

(M)
Rule
• Default goal
• Top-down (2)
Command Modifiers
• @   Do not echo the command. If you want to
      apply this modifier to all, you can use the
      --silent (or -s) option.
• -   The dash prefix indicates that errors in the
      command should be ignored by make.
      You can use --ignore-errors (or -i) option.
• +   The plus modifier tells make to execute the
      command even if the --just-print (or -n)
      command-line option is given to make.
Phony target
Goal: phony target is out of date.
Example:
clean:
      rm –rf *.o

$touch clean
$make clean
make: `clean’ is up to date.
Phony target
• Remember! Make cannot distinguish the
  different between target and phony target. So,
  we need define following:

.PHONY: clean
clean:
     rm –rf *.o
(2)
Standard phony target list
Empty target (Cookie)
Example: pl1029 opensource hello
.PHONY: install disclean
$(SOURCE)/.configured:
      cd $(SOURCE) && LDFLAGS='-s' CFLAGS='-Os' ./configure
      .......
      touch $@

install: $(SOURCE)/.configured
         make -C $(SOURCE) install-strip

distclean:
        make -C $(SOURCE) distclean
        rm -f $(SOURCE)/.configured
Variable name
• #define
• A = 1234
• CC := cc

• #use
• $A or $(A) or ${A}
• $(CC) or ${CC}
Variable name
• Run on Bash
hello:
         @echo "Hello, $${USER}" #shell variable USER
         @echo "Hello, ${USER}" #make variable USER
or
hello:
         @echo "Hello, $$USER" #shell variable USER
         @echo "Hello, $(USER)" #make variable USER
Automatic variable
• $@          target
• $%          archive member,
       Example: $@ is foo.a, $% is bar.o
       foo.a(bar.o): bar.o
             commands…
• $<          the first prereq
• $?          The names of all prerequisites that
              are newer than the target
Automatic variable
• $^   The names of all the prerequisites. This list
       has duplicate names removed.
• $+   Similar to $^, but it includes duplicates.
• $*   The stem of the target filename.
       Example: $* is hello
       hello.o: hello.c
              gcc hello.c –o hello.o
(3)
VPATH and vpath
• We can use VPATH or vpath to tell make where to
   find the source code.
.
|-- inc
| `-- saydone.h
|-- log
|-- Makefile
`-- src
   |-- hello.c
   |-- saydone.c
VPATH and vpath
• Without VPATH or vpath, we make it and we
  will get~

$make
make: *** No rule to make target `hello.c', needed by
`hello.o'. Stop.


• So, we need to tell make where to find source
  code by VPATH or vpath.(4)
Pattern rule
For example:
hello: hello.o
hello.o: hello.c

$make
cc -c -o hello.o hello.c
cc hello.o -o hello

We need to key “make - -print-data-base” to
understand pattern rule.(5)
Dependencies
$ echo "#include <stdio.h>" > stdio.c
$ gcc -M stdio.c
stdio.o: stdio.c /usr/include/stdio.h /usr/include/features.h 
/usr/include/bits/predefs.h /usr/include/sys/cdefs.h 
/usr/include/bits/wordsize.h /usr/include/gnu/stubs.h 
/usr/include/gnu/stubs-32.h 
/usr/lib/gcc/i686-linux-gnu/4.4.5/include/stddef.h 
/usr/include/bits/types.h /usr/include/bits/typesizes.h 
 /usr/include/libio.h /usr/include/_G_config.h
/usr/include/wchar.h 
/usr/lib/gcc/i686-linux-gnu/4.4.5/include/stdarg.h 
/usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h
Dependencies
• So, we need to .d and include it.(6)
Manager archive library
libsay.a: libsay.a(saydone.o) libsay.a(hello.o)

libsay.a(saydone.o): saydone.o
       ar rv $@ $^
libsay.a(hello.o): hello.o
       ar rv $@ $^

Same as
libsay.a: saydone.o hello.o
       commands…
(8)
Assign variable
• Simply expanded variable --- immediately expanded
      MAKE_DEPEND := $(CC) –M

• Recursively expanded variable --- lazily expanded
      CURRENT_TIME = $(shell date)

• Conditional variable assignment operator
      OUTPUT_DIR ?= $(PRERIX)

• Append operator
      CPPFLAG += -lpthread
When Variables Are Expanded
When Variables Are Expanded
• The righthand side of += is expanded
  immediately if the lefthand side was originally
  defined as a simple variable. Otherwise, its
  evaluation is deferred.
• For rules, the targets and prerequisites are
  always immediately expanded while the
  commands are always deferred.
• (7.1)
Macro
• Syntax
define macro-name
     commands1
     commands2
endef
Macro
For example:
define ericEcho
      @echo Eric....
      @echo $(CC)
      @echo $(CPPFLAGS)
endef

#use
$(ericEcho) or $(call ericEcho)
Target- and Pattern-Specific Variables
Example:
gui.o: gui.h
         $(COMPILE.c) -DUSE_NEW_MALLOC=1 $(OUTPUT_OPTION) $<


or you can do it same as following
gui.o: CPPFLAGS += -DUSE_NEW_MALLOC=1
gui.o: gui.h
       #default pattern rule
Another example is libffmpeg.
Where Variables Come From
• Command line: (V)
   – make CFLAGS=-g CPPFLAGS=‘-DBSD –DDEBUG’
• File:
   – by include
• Environment:
   – by export or unexport
Conditional
• ifdef or ifndef
      ifdef RANLIB
             $(RANLIB) $@
      endif

• ifeq or ifneq
      ifeq “$(strip $(OPTIONS))” “-d”
             CPPFLAGS += - -DDEBUG
      endif
(3)
include
• The include directive is used like this:
     include definitions.mk
• Special characteristic.
     (4)
• If any of the include files is updated by a rule,
  make then clears its internal database and
  rereads the entire makefile.
Standard make Variables
• MAKE_VERSION:
      – This is the version number of GNU make.
• CURDIR:
      – This variable contains the current working directory of the
        executing make process.
• MAKECMDGOALS:
      – It contains a list of all the targets specified on the
         command line for the current execution of make.
      ifneq "$(MAKECMDGOALS)" "clean"
          -include $(DEPEND)
      Endif
(v)
Functions
• $(call macro,parm1 ….)
• $(shell command)
   – CURRENT := $(shell date)
• $(filter pattern,text) or $(filter-out pattern,text)
   HEADER := $(filter %.h, $(SOURCES))
Functions
• $(subst search-string,replace-string,text)
  OBJECTS := $(subst .c,.o,$(SOURCES))
• $(patsubst search-pattern,replace-pattern,text)
  and $(variable:search=replace)
  OBJECTS := $(patsubst %.c,%.o,$(SOURCES))
  DEPENDS := $(SOURCES:%.c=%.d)
Functions
• $(wildcard pattern…)
  SRCS := $(wildcard *.c)
• $(suffix name)
  See example
• $(addsuffix suffix,name)
  common := $(addsuffix /common.mk,prefix)
  #common = prefix/common.mk
• $(error text)
Functions
• $(warning text)
• $(strip text)
• $(if condition,true-part,false-part)
     PATH_SEP := $(if $(COMSPEC),;,: )

More Related Content

PDF
CLI, the other SAPI phpnw11
ZIP
AnyMQ, Hippie, and the real-time web
PDF
関西PHP勉強会 php5.4つまみぐい
PDF
Cli the other SAPI confoo11
PDF
What's new in PHP 8.0?
PDF
SPL: The Missing Link in Development
PDF
Trading with opensource tools, two years later
PPTX
A Functional Guide to Cat Herding with PHP Generators
CLI, the other SAPI phpnw11
AnyMQ, Hippie, and the real-time web
関西PHP勉強会 php5.4つまみぐい
Cli the other SAPI confoo11
What's new in PHP 8.0?
SPL: The Missing Link in Development
Trading with opensource tools, two years later
A Functional Guide to Cat Herding with PHP Generators

What's hot (20)

PDF
TRunner
PDF
Just-In-Time Compiler in PHP 8
PDF
Perforce Object and Record Model
PDF
Smolder @Silex
PDF
PHP 5.3 Overview
PDF
Perl Bag of Tricks - Baltimore Perl mongers
KEY
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
PDF
The Magic Of Tie
PPTX
Introducing PHP Latest Updates
PDF
OSDC.TW - Gutscript for PHP haters
PDF
How-to Integração Postfi
PDF
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
PDF
Bag of tricks
PDF
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PDF
實戰 Hhvm extension php conf 2014
PDF
Parsing JSON with a single regex
PDF
Code Generation in PHP - PHPConf 2015
PDF
What's new in PHP 5.5
PDF
Cli the other sapi pbc11
PDF
Nikita Popov "What’s new in PHP 8.0?"
TRunner
Just-In-Time Compiler in PHP 8
Perforce Object and Record Model
Smolder @Silex
PHP 5.3 Overview
Perl Bag of Tricks - Baltimore Perl mongers
Introduction to CloudForecast / YAPC::Asia 2010 Tokyo
The Magic Of Tie
Introducing PHP Latest Updates
OSDC.TW - Gutscript for PHP haters
How-to Integração Postfi
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Bag of tricks
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
實戰 Hhvm extension php conf 2014
Parsing JSON with a single regex
Code Generation in PHP - PHPConf 2015
What's new in PHP 5.5
Cli the other sapi pbc11
Nikita Popov "What’s new in PHP 8.0?"
Ad

Viewers also liked (20)

PDF
work order of logic laboratory
PDF
Bozorgmeh os lab
TXT
Programs for Operating System
 
PDF
Os file
DOCX
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
DOCX
Os lab file c programs
PDF
O.s. lab all_experimets
KEY
KEY
DOCX
Ooad lab manual
DOCX
Ooad lab manual(original)
KEY
ipv6 introduction & environment buildup
PDF
Operating system lab manual
PPTX
UML Modeling and Profiling Lab - Advanced Software Engineering Course 2014/2015
DOC
Os lab manual
PPT
Intro Uml
PPT
Memory reference instruction
DOC
Bank Database Project
PDF
MATLAB programs Power System Simulation lab (Electrical Engineer)
work order of logic laboratory
Bozorgmeh os lab
Programs for Operating System
 
Os file
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Os lab file c programs
O.s. lab all_experimets
Ooad lab manual
Ooad lab manual(original)
ipv6 introduction & environment buildup
Operating system lab manual
UML Modeling and Profiling Lab - Advanced Software Engineering Course 2014/2015
Os lab manual
Intro Uml
Memory reference instruction
Bank Database Project
MATLAB programs Power System Simulation lab (Electrical Engineer)
Ad

Similar to Gun make (20)

PDF
Introduction to GNU Make Programming Language
PDF
Linux intro 5 extra: makefiles
PDF
LOSS_C11- Programming Linux 20221006.pdf
ODP
Makefiles Bioinfo
PPT
Introduction to Makefile
ODP
Introduction To Makefile
PPT
Ch3 gnu make
PDF
Linux intro 4 awk + makefile
PPTX
Makefile for python projects
PDF
makefiles tutorial
PPTX
Introduction to Makefile
ODP
Basic Make
PDF
Makefile
PDF
OS_Compilation_Makefile_kt4jerb34834343553
PDF
LONI_MakeTutorial_Spring2012.pdf
PPT
Linux operating system by Quontra Solutions
PDF
Paexec -- distributed tasks over network or cpus
PDF
Gnubs pres-foss-cdac-sem
PDF
Gnubs-pres-foss-cdac-sem
PDF
CMake Tutorial
Introduction to GNU Make Programming Language
Linux intro 5 extra: makefiles
LOSS_C11- Programming Linux 20221006.pdf
Makefiles Bioinfo
Introduction to Makefile
Introduction To Makefile
Ch3 gnu make
Linux intro 4 awk + makefile
Makefile for python projects
makefiles tutorial
Introduction to Makefile
Basic Make
Makefile
OS_Compilation_Makefile_kt4jerb34834343553
LONI_MakeTutorial_Spring2012.pdf
Linux operating system by Quontra Solutions
Paexec -- distributed tasks over network or cpus
Gnubs pres-foss-cdac-sem
Gnubs-pres-foss-cdac-sem
CMake Tutorial

Recently uploaded (20)

PPTX
Introduction to Building Materials
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Indian roads congress 037 - 2012 Flexible pavement
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PDF
1_English_Language_Set_2.pdf probationary
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Complications of Minimal Access Surgery at WLH
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
Lesson notes of climatology university.
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Cell Types and Its function , kingdom of life
Introduction to Building Materials
LDMMIA Reiki Yoga Finals Review Spring Summer
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
A systematic review of self-coping strategies used by university students to ...
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Indian roads congress 037 - 2012 Flexible pavement
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Paper A Mock Exam 9_ Attempt review.pdf.
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
1_English_Language_Set_2.pdf probationary
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Complications of Minimal Access Surgery at WLH
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Lesson notes of climatology university.
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Cell Types and Its function , kingdom of life

Gun make

  • 1. GUN Make Reference: O’Reilly – Managing Projects with GNU Make 3th. Eric Hsieh
  • 2. Outline • Rule • Variable and macro • Functions
  • 3. Introduction • Why do we need GUN Make? • Source -> Object -> Executable • Goal: The make program is intended to automate the mundane aspects of transforming source code into an executable. • Base on timestamp.
  • 4. Rule Target: Prereq1 Prereq2 … <Tab> commands1 <Tab> commands2 Example: hello: hello.c gcc hello.c –o hello
  • 5. Rule $make gcc hello.c –o hello #make again $make make: `hello.c’ is up to date. (1)
  • 6. Rule Just print $make - -just-print or –n Comment character # gcc hello.c –o hello (M)
  • 8. Command Modifiers • @ Do not echo the command. If you want to apply this modifier to all, you can use the --silent (or -s) option. • - The dash prefix indicates that errors in the command should be ignored by make. You can use --ignore-errors (or -i) option. • + The plus modifier tells make to execute the command even if the --just-print (or -n) command-line option is given to make.
  • 9. Phony target Goal: phony target is out of date. Example: clean: rm –rf *.o $touch clean $make clean make: `clean’ is up to date.
  • 10. Phony target • Remember! Make cannot distinguish the different between target and phony target. So, we need define following: .PHONY: clean clean: rm –rf *.o (2)
  • 12. Empty target (Cookie) Example: pl1029 opensource hello .PHONY: install disclean $(SOURCE)/.configured: cd $(SOURCE) && LDFLAGS='-s' CFLAGS='-Os' ./configure ....... touch $@ install: $(SOURCE)/.configured make -C $(SOURCE) install-strip distclean: make -C $(SOURCE) distclean rm -f $(SOURCE)/.configured
  • 13. Variable name • #define • A = 1234 • CC := cc • #use • $A or $(A) or ${A} • $(CC) or ${CC}
  • 14. Variable name • Run on Bash hello: @echo "Hello, $${USER}" #shell variable USER @echo "Hello, ${USER}" #make variable USER or hello: @echo "Hello, $$USER" #shell variable USER @echo "Hello, $(USER)" #make variable USER
  • 15. Automatic variable • $@ target • $% archive member, Example: $@ is foo.a, $% is bar.o foo.a(bar.o): bar.o commands… • $< the first prereq • $? The names of all prerequisites that are newer than the target
  • 16. Automatic variable • $^ The names of all the prerequisites. This list has duplicate names removed. • $+ Similar to $^, but it includes duplicates. • $* The stem of the target filename. Example: $* is hello hello.o: hello.c gcc hello.c –o hello.o (3)
  • 17. VPATH and vpath • We can use VPATH or vpath to tell make where to find the source code. . |-- inc | `-- saydone.h |-- log |-- Makefile `-- src |-- hello.c |-- saydone.c
  • 18. VPATH and vpath • Without VPATH or vpath, we make it and we will get~ $make make: *** No rule to make target `hello.c', needed by `hello.o'. Stop. • So, we need to tell make where to find source code by VPATH or vpath.(4)
  • 19. Pattern rule For example: hello: hello.o hello.o: hello.c $make cc -c -o hello.o hello.c cc hello.o -o hello We need to key “make - -print-data-base” to understand pattern rule.(5)
  • 20. Dependencies $ echo "#include <stdio.h>" > stdio.c $ gcc -M stdio.c stdio.o: stdio.c /usr/include/stdio.h /usr/include/features.h /usr/include/bits/predefs.h /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h /usr/include/gnu/stubs.h /usr/include/gnu/stubs-32.h /usr/lib/gcc/i686-linux-gnu/4.4.5/include/stddef.h /usr/include/bits/types.h /usr/include/bits/typesizes.h /usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h /usr/lib/gcc/i686-linux-gnu/4.4.5/include/stdarg.h /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h
  • 21. Dependencies • So, we need to .d and include it.(6)
  • 22. Manager archive library libsay.a: libsay.a(saydone.o) libsay.a(hello.o) libsay.a(saydone.o): saydone.o ar rv $@ $^ libsay.a(hello.o): hello.o ar rv $@ $^ Same as libsay.a: saydone.o hello.o commands… (8)
  • 23. Assign variable • Simply expanded variable --- immediately expanded MAKE_DEPEND := $(CC) –M • Recursively expanded variable --- lazily expanded CURRENT_TIME = $(shell date) • Conditional variable assignment operator OUTPUT_DIR ?= $(PRERIX) • Append operator CPPFLAG += -lpthread
  • 24. When Variables Are Expanded
  • 25. When Variables Are Expanded • The righthand side of += is expanded immediately if the lefthand side was originally defined as a simple variable. Otherwise, its evaluation is deferred. • For rules, the targets and prerequisites are always immediately expanded while the commands are always deferred. • (7.1)
  • 26. Macro • Syntax define macro-name commands1 commands2 endef
  • 27. Macro For example: define ericEcho @echo Eric.... @echo $(CC) @echo $(CPPFLAGS) endef #use $(ericEcho) or $(call ericEcho)
  • 28. Target- and Pattern-Specific Variables Example: gui.o: gui.h $(COMPILE.c) -DUSE_NEW_MALLOC=1 $(OUTPUT_OPTION) $< or you can do it same as following gui.o: CPPFLAGS += -DUSE_NEW_MALLOC=1 gui.o: gui.h #default pattern rule Another example is libffmpeg.
  • 29. Where Variables Come From • Command line: (V) – make CFLAGS=-g CPPFLAGS=‘-DBSD –DDEBUG’ • File: – by include • Environment: – by export or unexport
  • 30. Conditional • ifdef or ifndef ifdef RANLIB $(RANLIB) $@ endif • ifeq or ifneq ifeq “$(strip $(OPTIONS))” “-d” CPPFLAGS += - -DDEBUG endif (3)
  • 31. include • The include directive is used like this: include definitions.mk • Special characteristic. (4) • If any of the include files is updated by a rule, make then clears its internal database and rereads the entire makefile.
  • 32. Standard make Variables • MAKE_VERSION: – This is the version number of GNU make. • CURDIR: – This variable contains the current working directory of the executing make process. • MAKECMDGOALS: – It contains a list of all the targets specified on the command line for the current execution of make. ifneq "$(MAKECMDGOALS)" "clean" -include $(DEPEND) Endif (v)
  • 33. Functions • $(call macro,parm1 ….) • $(shell command) – CURRENT := $(shell date) • $(filter pattern,text) or $(filter-out pattern,text) HEADER := $(filter %.h, $(SOURCES))
  • 34. Functions • $(subst search-string,replace-string,text) OBJECTS := $(subst .c,.o,$(SOURCES)) • $(patsubst search-pattern,replace-pattern,text) and $(variable:search=replace) OBJECTS := $(patsubst %.c,%.o,$(SOURCES)) DEPENDS := $(SOURCES:%.c=%.d)
  • 35. Functions • $(wildcard pattern…) SRCS := $(wildcard *.c) • $(suffix name) See example • $(addsuffix suffix,name) common := $(addsuffix /common.mk,prefix) #common = prefix/common.mk • $(error text)
  • 36. Functions • $(warning text) • $(strip text) • $(if condition,true-part,false-part) PATH_SEP := $(if $(COMSPEC),;,: )