quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif Introduction

In this tutorial, you will run through quick a tutorials of the UNIX make program, which aids in ASIC testbench, synthesis compilation. This is tutorial should basically help engineers to get quick start with Makefile and how to use them in their work.

   

space.gif

There are few things, you much be familiar with before you could use make. Once should know basics of Unix and how to run given compiler in command line.

   

space.gif

Note : The introduction part of the Makefile tutorial is based on GNU make manual.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Examples in this Tutorial

In this tutorial, we will be using gcc, g++, which is obviously going to be a C++ compiler, given that we are using C++ in this. However, the idea of a Makefile works just as well no matter which compiler you choose to use i.e. Verilog or Specman or or VHDL.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif make and Makefile

The `make' utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them. Makefile is file which holds the command to control make, i.e. Makefile tells `make' how to compile and link a program.

   

space.gif

  ../images/main/bullet_star_pink.gif Example : Simple Simulation Makefile

#Start of Makefile

sim : counter.v

   

space.gif

   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif What a Rule Looks Like

A simple makefile consists of "rules" with the following shape:

   

space.gif

TARGET ... : PREREQUISITES ...

COMMAND

...

...

   

space.gif

A "target" is usually the name of a file that is generated by a program; examples of targets are executable or object files. A target can also be the name of an action to carry out, such as `clean'.

   

space.gif

A "prerequisite" is a file that is used as input to create the target. A target often depends on several files.

   

space.gif

A "command" is an action that `make' carries out. A rule may have more than one command, each on its own line. *Please note:* you need to put a tab character at the beginning of every command line! This is an obscurity that catches the unwary.

   

space.gif

Usually a command is in a rule with prerequisites and serves to create a target file if any of the prerequisites change. However, the rule that specifies commands for the target need not have prerequisites. For example, the rule containing the delete command associated with the target `clean' does not have prerequisites.

   

space.gif

A "rule", then, explains how and when to remake certain files which are the targets of the particular rule. `make' carries out the commands on the prerequisites to create or update the target. A rule can also explain how and when to carry out an action. *Note Writing Rules: Rules.

   

space.gif

A makefile may contain other text besides rules, but a simple makefile need only contain rules. Rules may look somewhat more complicated than shown in this template, but all fit the pattern more or less.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif A Simple Makefile

Here is a straightforward makefile that describes the way an executable file called `edit' depends on eight object files which, in turn, depend on eight C source and three header files.

   

space.gif

In this example, all the C files include `defs.h', but only those defining editing commands include `command.h', and only low level files that change the editor buffer include `buffer.h'.

   

space.gif

edit : main.o kbd.o command.o display.o \

insert.o search.o files.o utils.o

cc -o edit main.o kbd.o command.o display.o \

insert.o search.o files.o utils.o

   

space.gif

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

display.o : display.c defs.h buffer.h

cc -c display.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

utils.o : utils.c defs.h

cc -c utils.c

clean :

rm edit main.o kbd.o command.o display.o \

insert.o search.o files.o utils.o

   

space.gif

We split each long line into two lines using backslash-newline; this is like using one long line, but is easier to read.

   

space.gif

To use this makefile to create the executable file called `edit', type:

   

space.gif

make

   

space.gif

To use this makefile to delete the executable file and all the object files from the directory, type:

   

space.gif

make clean

   

space.gif

In the example makefile, the targets include the executable file `edit', and the object files `main.o' and `kbd.o'. The prerequisites are files such as `main.c' and `defs.h'. In fact, each `.o' file is both a target and a prerequisite. Commands include `cc -c main.c' and

`cc -c kbd.c'.

   

space.gif

When a target is a file, it needs to be recompiled or relinked if any of its prerequisites change. In addition, any prerequisites that are themselves automatically generated should be updated first. In this example, `edit' depends on each of the eight object files; the object file `main.o' depends on the source file `main.c' and on the header

file `defs.h'.

   

space.gif

A shell command follows each line that contains a target and prerequisites. These shell commands say how to update the target file. A tab character must come at the beginning of every command line to distinguish commands lines from other lines in the makefile. (Bear in mind that `make' does not know anything about how the commands work. It is up to you to supply commands that will update the target file properly. All `make' does is execute the commands in the rule you have

specified when the target file needs to be updated.)

   

space.gif

The target `clean' is not a file, but merely the name of an action. Since you normally do not want to carry out the actions in this rule, `clean' is not a prerequisite of any other rule. Consequently, `make' never does anything with it unless you tell it specifically. Note that this rule not only is not a prerequisite, it also does not have any prerequisites, so the only purpose of the rule is to run the specified commands. Targets that do not refer to files but are just actions are called "phony targets". *Note Phony Targets::, for information about this kind of target. *Note Errors in Commands: Errors, to see how to cause `make' to ignore errors from `rm' or any other command.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif How `make' Processes a Makefile

By default, `make' starts with the first target (not targets whose names start with `.'). This is called the "default goal". ("Goals" are the targets that `make' strives ultimately to update. *Note Arguments to Specify the Goals: Goals.)

   

space.gif

In the simple example of the previous section, the default goal is to update the executable program `edit'; therefore, we put that rule first.

   

space.gif

Thus, when you give the command:

   

space.gif

make

   

space.gif

`make' reads the makefile in the current directory and begins by processing the first rule. In the example, this rule is for relinking `edit'; but before `make' can fully process this rule, it must process the rules for the files that `edit' depends on, which in this case are the object files. Each of these files is processed according to its own rule. These rules say to update each `.o' file by compiling its source file. The recompilation must be done if the source file, or any of the header files named as prerequisites, is more recent than the object file, or if the object file does not exist.

   

space.gif

The other rules are processed because their targets appear as prerequisites of the goal. If some other rule is not depended on by the goal (or anything it depends on, etc.), that rule is not processed, unless you tell `make' to do so (with a command such as `make clean').

   

space.gif

Before recompiling an object file, `make' considers updating its prerequisites, the source file and header files. This makefile does not specify anything to be done for them--the `.c' and `.h' files are not the targets of any rules--so `make' does nothing for these files. But `make' would update automatically generated C programs, such as those made by Bison or Yacc, by their own rules at this time.

   

space.gif

After recompiling whichever object files need it, `make' decides whether to relink `edit'. This must be done if the file `edit' does not exist, or if any of the object files are newer than it. If an object file was just recompiled, it is now newer than `edit', so `edit' is relinked.

   

space.gif

Thus, if we change the file `insert.c' and run `make', `make' will compile that file to update `insert.o', and then link `edit'. If we change the file `command.h' and run `make', `make' will recompile the object files `kbd.o', `command.o' and `files.o' and then link the file `edit'.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Variables Make Makefiles Simpler

In our example, we had to list all the object files twice in the rule for `edit' (repeated here):

   

space.gif

edit : main.o kbd.o command.o display.o \

insert.o search.o files.o utils.o

cc -o edit main.o kbd.o command.o display.o \

insert.o search.o files.o utils.o

   

space.gif

Such duplication is error-prone; if a new object file is added to the system, we might add it to one list and forget the other. We can eliminate the risk and simplify the makefile by using a variable. "Variables" allow a text string to be defined once and substituted in multiple places later (*note How to Use Variables: Using Variables.).

   

space.gif

It is standard practice for every makefile to have a variable named `objects', `OBJECTS', `objs', `OBJS', `obj', or `OBJ' which is a list of all object file names. We would define such a variable `objects' with a line like this in the makefile:

   

space.gif

objects = main.o kbd.o command.o display.o \

insert.o search.o files.o utils.o

   

space.gif

Then, each place we want to put a list of the object file names, we can substitute the variable's value by writing `$(objects)' (*note How to Use Variables: Using Variables.).

   

space.gif

Here is how the complete simple makefile looks when you use a variable for the object files:

   

space.gif

objects = main.o kbd.o command.o display.o \

insert.o search.o files.o utils.o

   

space.gif

edit : $(objects)

cc -o edit $(objects)

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

display.o : display.c defs.h buffer.h

cc -c display.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

utils.o : utils.c defs.h

cc -c utils.c

clean :

rm edit $(objects)

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Letting `make' Deduce the Commands

It is not necessary to spell out the commands for compiling the individual C source files, because `make' can figure them out: it has an "implicit rule" for updating a `.o' file from a correspondingly named `.c' file using a `cc -c' command. For example, it will use the command `cc -c main.c -o main.o' to compile `main.c' into `main.o'. We can therefore omit the commands from the rules for the object files. *Note Using Implicit Rules: Implicit Rules.

   

space.gif

When a `.c' file is used automatically in this way, it is also automatically added to the list of prerequisites. We can therefore omit the `.c' files from the prerequisites, provided we omit the commands.

   

space.gif

Here is the entire example, with both of these changes, and a variable `objects' as suggested above:

   

space.gif

objects = main.o kbd.o command.o display.o \

insert.o search.o files.o utils.o

   

space.gif

edit : $(objects)

cc -o edit $(objects)

   

space.gif

main.o : defs.h

kbd.o : defs.h command.h

command.o : defs.h command.h

display.o : defs.h buffer.h

insert.o : defs.h buffer.h

search.o : defs.h buffer.h

files.o : defs.h buffer.h command.h

utils.o : defs.h

   

space.gif

.PHONY : clean

clean :

rm edit $(objects)

   

space.gif

This is how we would write the makefile in actual practice. (The complications associated with `clean' are described elsewhere. See *Note Phony Targets::, and *Note Errors in Commands: Errors.)

   

space.gif

Because implicit rules are so convenient, they are important. You will see them used frequently.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Communicating Variables to a Sub-`make'

Communicating Variables to a Sub-make

   

space.gif

Variable values of the top-level make can be passed to the sub-make through the environment by explicit request. These variables are defined in the sub-make as defaults, but do not override what is specified in the makefile used by the sub-make makefile unless you use the `-e' switch

   

space.gif

To pass down, or export, a variable, make adds the variable and its value to the environment for running each command. The sub-make, in turn, uses the environment to initialize its table of variable values.

   

space.gif

Except by explicit request, make exports a variable only if it is either defined in the environment initially or set on the command line, and if its name consists only of letters, numbers, and underscores. Some shells cannot cope with environment variable names consisting of characters other than letters, numbers, and underscores.

   

space.gif

If you want to export specific variables to a sub-make, use the export directive, like this:

   

space.gif

export variable ...

   

space.gif

If you want to prevent a variable from being exported, use the unexport directive, like this:

   

space.gif

unexport variable ...

   

space.gif

In both of these forms, the arguments to export and unexport are expanded, and so could be variables or functions which expand to a (list of) variable names to be (un)exported.

   

space.gif

As a convenience, you can define a variable and export it at the same time by doing:

   

space.gif

export variable = value

   

space.gif

has the same result as:

   

space.gif

variable = value

export variable

   

space.gif

and

   

space.gif

export variable := value

   

space.gif

has the same result as:

   

space.gif

variable := value

export variable

   

space.gif

Likewise,

   

space.gif

export variable += value

   

space.gif

is just like:

   

space.gif

variable += value

export variable

   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com