Gun make


Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Gun make

  1. 1. GUN MakeReference: O’Reilly – Managing Projects with GNU Make 3th. Eric Hsieh
  2. 2. Outline• Rule• Variable and macro• Functions
  3. 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. 4. RuleTarget: Prereq1 Prereq2 …<Tab> commands1<Tab> commands2Example:hello: hello.c gcc hello.c –o hello
  5. 5. Rule$makegcc hello.c –o hello#make again$makemake: `hello.c’ is up to date.(1)
  6. 6. RuleJust print$make - -just-print or –nComment character# gcc hello.c –o hello(M)
  7. 7. Rule• Default goal• Top-down (2)
  8. 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. 9. Phony targetGoal: phony target is out of date.Example:clean: rm –rf *.o$touch clean$make cleanmake: `clean’ is up to date.
  10. 10. Phony target• Remember! Make cannot distinguish the different between target and phony target. So, we need define following:.PHONY: cleanclean: rm –rf *.o(2)
  11. 11. Standard phony target list
  12. 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-stripdistclean: make -C $(SOURCE) distclean rm -f $(SOURCE)/.configured
  13. 13. Variable name• #define• A = 1234• CC := cc• #use• $A or $(A) or ${A}• $(CC) or ${CC}
  14. 14. Variable name• Run on Bashhello: @echo "Hello, $${USER}" #shell variable USER @echo "Hello, ${USER}" #make variable USERorhello: @echo "Hello, $$USER" #shell variable USER @echo "Hello, $(USER)" #make variable USER
  15. 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. 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. 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. 18. VPATH and vpath• Without VPATH or vpath, we make it and we will get~$makemake: *** 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. 19. Pattern ruleFor example:hello: hello.ohello.o: hello.c$makecc -c -o hello.o hello.ccc hello.o -o helloWe need to key “make - -print-data-base” tounderstand pattern rule.(5)
  20. 20. Dependencies$ echo "#include <stdio.h>" > stdio.c$ gcc -M stdio.cstdio.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. 21. Dependencies• So, we need to .d and include it.(6)
  22. 22. Manager archive librarylibsay.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 aslibsay.a: saydone.o hello.o commands…(8)
  23. 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. 24. When Variables Are Expanded
  25. 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. 26. Macro• Syntaxdefine macro-name commands1 commands2endef
  27. 27. MacroFor example:define ericEcho @echo Eric.... @echo $(CC) @echo $(CPPFLAGS)endef#use$(ericEcho) or $(call ericEcho)
  28. 28. Target- and Pattern-Specific VariablesExample:gui.o: gui.h $(COMPILE.c) -DUSE_NEW_MALLOC=1 $(OUTPUT_OPTION) $<or you can do it same as followinggui.o: CPPFLAGS += -DUSE_NEW_MALLOC=1gui.o: gui.h #default pattern ruleAnother example is libffmpeg.
  29. 29. Where Variables Come From• Command line: (V) – make CFLAGS=-g CPPFLAGS=‘-DBSD –DDEBUG’• File: – by include• Environment: – by export or unexport
  30. 30. Conditional• ifdef or ifndef ifdef RANLIB $(RANLIB) $@ endif• ifeq or ifneq ifeq “$(strip $(OPTIONS))” “-d” CPPFLAGS += - -DDEBUG endif(3)
  31. 31. include• The include directive is used like this: include• 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. 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. 33. Functions• $(call macro,parm1 ….)• $(shell command) – CURRENT := $(shell date)• $(filter pattern,text) or $(filter-out pattern,text) HEADER := $(filter %.h, $(SOURCES))
  34. 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. 35. Functions• $(wildcard pattern…) SRCS := $(wildcard *.c)• $(suffix name) See example• $(addsuffix suffix,name) common := $(addsuffix /,prefix) #common = prefix/• $(error text)
  36. 36. Functions• $(warning text)• $(strip text)• $(if condition,true-part,false-part) PATH_SEP := $(if $(COMSPEC),;,: )