Introduction To Makefile
Introduction To Makefile
Tusharadri Sarkar
IBM
Tusharadri Sarkar
program need to be recompiled and issues the command to recompile them First implemented by Richard Stallman and Ronald McGrath. Development since version 3.76 is handled by Paul D. Smith GNU make conforms to section 6.2 of IEEE Standard 1003.2-1992 (POSIX.2) To build inside ClearCase project use clearmake instead of make
2 Tusharadri Sarkar August 21, 2009
About clearmake
clearmake is the Rational ClearCase variant of
the UNIX make utility Includes most of the features of UNIX System V make It also features compatibility modes, which enables you to use clearmake with makefiles that were constructed for use with other popular make variants, including Gnu make
gmake is gnu variant of make utility
Tusharadri Sarkar
About clearmake
clearmake features the following Rational ClearCase
extensions:
Configuration lookup: A build-avoidance scheme that
which uses time stamps of build objects. The automatic detection guarantees correct build behavior in case header files change, even if the header files are not listed as dependencies in the Makefile Derived object sharing: Developers using different views can share files created by clearmake builds Creation of configuration records: Software bill-ofmaterials records that fully document a build and support the ability to rebuild
4 Tusharadri Sarkar August 21, 2009
makefile) that describes the relationships (dependencies) among files in the program and provides commands for updating each file
In a program, typically the executable file is updated
from object files, which are in turn updated (built) by compiling (or, recompiling) source files
The make program uses the Makefile data base and
the last modification times of the files to decide which of the files need to be updated
5 Tusharadri Sarkar August 21, 2009
to control which files should be compiled, or how the files should be compiled
Tusharadri Sarkar
Writing a Makefile
All Makefiles can be described with the following
FIVE components:
Explicit Rules Implicit Rules Variable definitions Directives Comments
7 Tusharadri Sarkar August 21, 2009
It says how to make one or more files, called the rule's targets. It lists the other files that the targets depend on, called the prerequisites of the target, and may also specify the commands to create or update the targets
Rule syntax
or like this:
targets : prerequisites ; command1 command2 ...
8 Tusharadri Sarkar August 21, 2009
determining the default goal which is the target of the first rule of the first Makefile
Tusharadri Sarkar
The targets are file names, separated by spaces. Wildcard characters may be used Usually there is only one target per rule, but you can have multiple targets The command start with a tab character The first command may appear on the line after the prerequisites, with a tab character, or may appear on the same line, with a semicolon
Tusharadri Sarkar August 21, 2009
10
the targets are out of date how to update them when necessary
The criterion for being out of date is specified in terms of the prerequisites, which consist of file names separated by spaces (Wildcards and archive members are allowed)
11
Tusharadri Sarkar
modification time, when the rule's commands were last executed. It does so because one of the commands is a touch command to update the target file
13 Tusharadri Sarkar August 21, 2009
command if any source file has changed since make print was last run
14
Tusharadri Sarkar
the target of the rule is a nonexistent file, then make imagines the target to have been updated whenever its rule is run
This implies that all targets depending on this one
15
Tusharadri Sarkar
conditions, so the target clean that depends on FORCE is forced to run its commands every time
16
Tusharadri Sarkar
It says when and how to remake a class of files based on their names. It describes how a target may depend on a file with a name similar to the target and gives make commands to create or update such a target
It helps to avoid writing customary techniques so that
you do not have to specify them in detail when you want to use them
17
Tusharadri Sarkar
C/C++ compilation typically takes a .c, .cc or .cpp file and makes a .o file. make applies the implicit rule for C/C++ compilation when it sees this combination of file names:
foo : foo.o bar.o cc g o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
18
Tusharadri Sarkar
in sequence; for example, make will remake a .o file from a .y file by way of a .c file
The built-in implicit rules use several variables in their
commands. So, by changing the values of the variables, you can change the way the implicit rule works Example: The variable CFLAGS controls the flags given to the C compiler by the implicit rule for C compilation
19 Tusharadri Sarkar August 21, 2009
20
Tusharadri Sarkar
new pattern rule with the same target and prerequisites, but different commands
Cancelling an implicit rule
You can cancel a built-in implicit rule by defining a
pattern rule with the same target and prerequisites, but no commands
Example: %.o : %.s
A line that specifies a text string value for a variable that can be substituted into the text later
Variables make your Makefile simpler and more
compact
22
Tusharadri Sarkar
Now with a variable OBJECTS you can write the same as:
OBJECTS = main.o kbd.o command.o display.o \ insert.o search.o files.o utils.o edit : $(OBJECTS) cc -o edit $(OBJECTS)
23 Tusharadri Sarkar August 21, 2009
references to other variables, these references are expanded whenever this variable is substituted When this happens, it is called recursive expansion
24 Tusharadri Sarkar August 21, 2009
25
Tusharadri Sarkar
Will expand CFLAGS = -Isrc Ih Iplugin g O Disadvantage: Can not appended any value For example: CFLAGS = $(CFLAGS) wall Is not permissible Any function referenced in the definition will be executed every time the variable is expanded. This will result in slower execution.
26 Tusharadri Sarkar August 21, 2009
once and for all, expanding any references to other variables and functions, when the variable is defined. The actual value of the simply expanded variable is the result of expanding the text that you write. It does not contain any references to other variables; it contains their values as of the time this variable was defined
27
Tusharadri Sarkar
value (or its value processed in some way by one of the expansion functions) and to use the expansion functions much more efficiently Example:
CDEFINES := -DSOLID_NBASE D_AVL_H ifeq ($(OS), Linux) CDEFINES = $(CDEFINES) DLINUX_NBASE endif
It will set SOURCES = a.c b.c c.c Another example which is common to Makefile: OBJS := a.o b.o c.o SRCS := $(OBJS:%.o=%.c) With the same result, this format actually substitutes the patsubst function: $(patsubst %.c,%.o)
29 Tusharadri Sarkar August 21, 2009
needed only for sophisticated Makefile programming. For most purposes you need not consider them
Example:
x = var1 var2 := Hello y = $(subst 1,2,$(x)) z = y msg := $($($(z)))
31
Tusharadri Sarkar
More on variables
Automatic variable:
Used extensively with pattern rules and implicit rules Example: %.o : %.c $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ Builds any file x.o from x.c where $< and $@ substitutes
the names of targets and sources in each case where the rule applies Another example:
% :: SUBDIR/%,v $(CO) $(COFLAGS) $<
More on variables
Automatic variables are useful when operating on only
archive, mentioned by $?
Some other automatic variables: $% The target member name, when the target is an archive member $^ The names of all the prerequisites, with spaces between them $| The names of all the order-only prerequisites, with spaces between them
33 Tusharadri Sarkar August 21, 2009
More on variables
Implicit variables:
Variables used by the implicit rules Can be modified to modify the way implicit rule works
More on variables
Special variables/Environment variables:
They lose their special properties if they are set by a
the command line .INCLUDE_DIRS Expands to a list of directories that make searches for included makefiles .SECONDEXPANSION If set as target, all the prerequisites will expand a second times after make reads them in first-phase .NOTPARALLEL If mentioned as target, invocation of make will run serially
35 Tusharadri Sarkar August 21, 2009
rules, each with one target You can have same commands with different output by substituting target name by $@
Example:
gopt wallopt : main.c $(CC)-c main.c -$(subst opt,,$@) -O > $@
is equivalent to:
gopt : main.cpp $(CC)-c main.c g -O > gopt wallopt : main.c $(CC)-c main.c wall -O > wallopt
36 Tusharadri Sarkar August 21, 2009
prerequisites mentioned in all the rules are merged into one list of prerequisites for the target. If the target is older than any prerequisite from any rule, the commands are executed
Example:
objects = megaco.o sip.o megaco.o : protocol.c sip.o : protocol.c threads.c $(objects) : config.h
All of them must be recompiled if config.h changes This could be inserted or taken out without changing the rules
that really specify how to make the object files, making it a convenient form to use if you wish to add the additional prerequisites internally
37 Tusharadri Sarkar August 21, 2009
A directive is a command for make to do something special while reading the Makefile
These could be
Reading another Makefile: The include directive Deciding (based on the values of variables) whether to
current Makefile and read one or more other makefiles before continuing
Extra spaces are allowed and ignored at the beginning of
39
Tusharadri Sarkar
If you have three .mk files, a.mk, b.mk, and c.mk, and $(MYFILE) expands to f1.mk and f2.mk, then the following expression
include *.mk $(MYFILE)
is equivalent to
include a.mk b.mk c.mk f1.mk f2.mk
40
Tusharadri Sarkar
Makefile, so they cannot be used to control shell commands at the time of execution
41
Tusharadri Sarkar
Setting up a compilation flag depending on the condition which platform you are building
Ifeq ($(OS), CFLAGS = endif Ifeq ($(OS), CFLAGS = endif SunOS) -g O Wall
-D_SPARC_NBASE
42
Tusharadri Sarkar
references, which are expanded when the directive is read to find the actual variable name to use
Example:
define TWO_LINES echo hiQ echo $(PLTFRM) endef
search, separated by colons or spaces It will search those paths both for targets and prerequisites Example: VPATH = sources:../headers vpath can hold a list of directories for a class of files that match a particular pattern Example: vpath %.h ../headers This tells make to search for all the files with extension .h inside directory ../headers
44 Tusharadri Sarkar August 21, 2009
Makefile Functions
A function call resembles a variable reference. Function
or ${function arguments}
Makefile Functions
Function shell: Calling shell inside a Makefile is
runs. They are used to provide information to the user of the Makefile or to cause make to stop if some sort of environmental error is detected
$(error text ...) or $(warning text ) or $(info text )
trailing backslash not escaped by another backslash will continue the comment across multiple lines Within a define directive, comments are not ignored during the definition of the variable, but rather kept intact in the value of the variable When the variable is expanded they will either be treated as make comments or as command script text, depending on the context in which the variable is evaluated
47 Tusharadri Sarkar August 21, 2009
Running make
How to name you Makefile?
By default, when make looks for the Makefile, it tries the
following names, in the order: (1)GNUmakefile, (2)makefile and (3)Makefile. The name GNUmakefile is not recommended
If make finds none of them in current directory it will exit
Running make
Targets for your Makefile
You can specify different targets for your Makefile to run with
49
Tusharadri Sarkar
Running make
Exit status of make:
0 The exit status is zero if make is successful. 2 The exit status is two if make encounters any
Running make
Common errors while running make:
Dont know how to make <target name> No target specified and no Makefile
found
Syntax error in line no. xxxx.
References
http://www.gnu.org/software/make/manual/make.html#Ru
52
Tusharadri Sarkar
Thank You !!
53
Tusharadri Sarkar