make utility

57
make 1 make Utility A Unix utility to creat e and maintain program Present by …. N oppamas Pukkhem

Upload: eagan-lancaster

Post on 01-Jan-2016

45 views

Category:

Documents


1 download

DESCRIPTION

make Utility. A Unix utility to create and maintain program. Present by …. Noppamas Pukkhem. Overview. Compiling multi-part programs Overview of the make Utility The Description file and its Components Operation of the make Utility Running make. Compiling Multi-part programs. - PowerPoint PPT Presentation

TRANSCRIPT

make 1

make Utility

A Unix utility to create and maintain program

Present by …. Nopp amas Pukkhem

make 2

Overview

• Compiling multi-part programs• Overview of the make Utility• The Description file and its Components• Operation of the make Utility• Running make

make 3

Compiling Multi-part programs

make 4

• การ compile โปรแกรมที่�ประกอบด้�วย source file มากกว�าหนึ่��งตั�ว โด้ยพื้��นึ่ฐานึ่ม 2 ขั้��นึ่ตัอนึ่ คื�อ :– compile แตั�ละ module ให�อย"�ในึ่ร"ปขั้อง object file– link ตั�ว object file เหล�านึ่��นึ่ให�อย"�ในึ่ร"ปขั้อง

executable program

Compiling multi-part programs

make 5

ขั้��นึ่ตัอนึ่ที่� 2 :

Compiling multi-part programs

ตั�วอย�าง ขั้��นึ่ตัอนึ่ที่� 1 :

mod1.o

mod2.o

mod3.o

Compile โปรแกรมแตั�ละตั�วเพื้��อ ให�ได้� object file

Link ตั�ว object file เขั้�าด้�วยก�นึ่

cc -c mod1.ccc -c mod2.ccc -c mod3.c

- cc o final mod1 .o 2 3mod .o mod .o

make 6

• จากตั�วอย�าง จะเห'นึ่ว�า 1. แตั�ละขั้��นึ่ตัอนึ่ย�งประกอบด้�วยขั้��นึ่ตัอนึ่ย�อย เช่�นึ่ ขั้��นึ่ตัอนึ่ ที่� 1 ถ้�าใช่� source file จ*านึ่วนึ่ 3 file ตั�องที่*าการ

compile 3 คืร��งจ�งจะได้� object file ขั้องที่+กตั�ว

2. เม��อมการเปล�ยนึ่แปลง source ตั�วใด้ตั�วหนึ่��ง ตั�องที่*าการrecompile ตั�ว source file ใหม� แล�วก'ตั�องที่*าการ link file ใหม� เพื้��อให�ได้�ตั�ว executable file ที่� up-to-date

Compiling multi-part programs

make 7

ซึ่��งความต้องการข้างต้นน�� ค�ณทำ�าได้ เม��อใช้ “make utility”

• สิ่-�งที่�อาจผู้"�ใช่�ตั�องการคื�อ : – ที่*าอย�างไรให�สิ่ามารถ้ compile โปรแกรมที่�มหลาย source file ได้�

ด้�วยคื*าสิ่� �งเพื้ยงคื*าสิ่� �งเด้ยว– เม��อมการเปล�ยนึ่แปลง source file แล�ว คื*าสิ่� �งนึ่��นึ่จะช่�วย update

โปรแกรมให�โด้ยอ�ตัโนึ่ม�ตั- โด้ยผู้"�ใช่�ไม�ตั�องที่*าการ compile และ link ตั�ว object file ใหม�

Compiling multi-part programs

make 8

Overview of make Utility

make 9

Overview of the make Utility

• make utility เป/นึ่ software engineering tool เพื้��อจ�ด้การและบ*าร+งร�กษาโปรแกรมให�เป/นึ่ป1จจ+บ�นึ่

• ช่�วยในึ่การ compile โปรแกรมที่�ประกอบด้�วยหลาย source file “โด้ยใช่�เพื้ยงคื*าสิ่��ง make” ที่*าให�ขั้� �นึ่ตัอนึ่ตั�าง ๆ ลด้ลง

• มการสิ่ร�าง description file “ที่�เรยกว�า makefile” เพื้��อบรรจ+ dependency rules, macro และ suffix

rule และ command ตั�างๆสิ่*าหร�บ update file เม��อใช่� คื*าสิ่��ง make จะที่*าการ rebuild โปรแกรมโด้ยอ�ตัโนึ่ม�ตั-เม��อ มการ modify ตั�ว source file ที่�ใช่�โด้ยจะ recompile

เฉพื้าะตั�วที่�มการเปล�ยนึ่แปลงเที่�านึ่��นึ่ จ�งเป/นึ่เหตั+ผู้ลที่�ที่*าให� โปรแกรม up-to-date

make 10

• “make” can be used with any programming language (including C/C++) whose compiler can be run with a unix shell command.

Overview of the make Utility

make 11

The Description file and its Components

make 12

The description file and its Components

• Description file

เป/นึ่ขั้�อม"ลเก�ยวก�บการที่*างานึ่ขั้อง make ในึ่การสิ่ร�าง target ด้�วยการ ระบ+ตั�ว dependent หร�อ dependency file ซึ่��งเป/นึ่การอ�างถ้�งคืวาม

สิ่�มพื้�นึ่ธ์6ขั้อง file ตั�าง ๆ ในึ่ procedure

• Make จะร" �จ�ก description file โด้ย default โด้ย file ช่��อ ‘makefile’ หร�อ ‘Makefile’

make 13

The description file and its Components

- % make [ f make_name] [other options] [targets]

ร"ปแบบการใช่�คื*าสิ่��ง

make 14

The description file and its Components

• Comments• Dependency rules

– Target– Phony target – Dependencies– Commands

• Macro• Suffix rules

Components of description file :

make 15

• Comments

เราสิ่ามารถ้ใสิ่� comment เขั้�าไปในึ่description file ได้�โด้ยใช่� เคืร��องหมายpound sign (#) และตัามด้�วยขั้�อคืวามที่�

ตั�องการให�ถ้"ก ignored โด้ย make ถ้�ามหลายบรรที่�ด้ตั�องใสิ่�เคืร��องหมายนึ่�ที่+กบรรที่�ด้

The description file and its Components

# This is comment line in description file # 2This is comment line

Example

make 16

• Dependency rules

เป/นึ่ file ที่�เก'บสิ่�วนึ่ขั้อง code ตั�าง ๆ และกฎตั�าง ๆเพื้��อบอก การที่*างานึ่ให�ก�บ make ในึ่การสิ่ร�างเป8าหมายขั้องโปรแกรม

ประกอบด้�วย 3 สิ่�วนึ่ คื�อ 1. Targets

2. Dependents 3. Commands

The description file and its Components

make 17

• ร"ปแบบขั้องการใช่� dependency rule

1 2target [ target …] : [dependency1 …][; commands]

[<tab> command] ตั�วอย�าง

mod1.o : mod1.c

cc -c mod1.c

The description file and its Components

target

dependenc

y

command

tab

make 18

• ร"ปแบบอ��นึ่ ๆ

1 1target_ : dependency_ …. dependency _n\

dependency_n+1 …;\ _1 ; _2 ; _3;…;

…… ; command_m

The description file and its Components

make 19

“ โด้ยปกตั-ที่��วไป target จะเป/นึ่ช่��อ file ซึ่��งmake สิ่ร�างขั้��นึ่ เช่�นึ่ object

file หร�อ executable program”

The description file and its Components

Targets

final : mod1.o mod2.o mod3.o cc -o final mod1.o mod2.o mod3.o

make 20

The description file and its Components

Phony target

Clean : rm *.o

“ เราสิ่ามารถ้ก*าหนึ่ด้ให� target นึ่��นึ่มช่��อเป/นึ่ ขั้��นึ่ตัอนึ่การที่*างานึ่ที่�ตั�องการก'ได้� ซึ่��งจะเรยก

target ประเภที่นึ่�ว�า phony target ”

make 21

The description file and its Components

Dependents“ เป/นึ่ตั�วที่�ใช่�ระบ+คืวามขั้��นึ่อย"�แก�ก�นึ่ขั้อง target

ก�บ file ที่�ใช่�ในึ่การสิ่ร�าง เช่�นึ่ โปรแกรม .c file ใช่�เป/นึ่ source file ขั้องการสิ่ร�าง .o file ซึ่��งนึ่*ามาใช่�สิ่ร�าง executable file”

make 22

The description file and its Components

final

mod1.o mod2.o mod3.o

mod1.c mod2.c mod3.c

Simple dependencies for make

make 23

The description file and its Components

commands“ แตั�ละ command ในึ่กฎจะถ้"กแปลโด้ยshell เพื้��อถ้"กสิ่��งให�ที่*างานึ่ โด้ย default แล�วmake จะใช่� /bin/sh shell ตั�ว default

นึ่�ละได้�โด้ยการใช่�โด้ยการใช่� macroSHELL= /bin/sh ในึ่ description file”

make 24

macro

macro เป/นึ่ช่��อที่�ใช่�แที่นึ่ตั�วแปรหร�อกล+�มขั้องขั้�อม"ลตั�าง ๆ ที่� ที่*าให�เก-ด้คืวามสิ่ะด้วกในึ่กรณีที่�ตั�องการให�ตั�วแปรเหล�านึ่�ปรากฎอย"�ในึ่

หลาย ๆ ที่�ขั้อง description file หร�อ ตั�องการ update กล+�ม ขั้�อม"ลนึ่�บ�อย ๆ

The description file and its Components

make 25

• Macro used to define setting :

* platform-specific commands

* list of required files for target

* list of command options

* so on• make reads all the macro definitions before executing any

commands• it is often convenient to put all the macro definitions at the head

of the makefile, but this is necessary

The description file and its Components

make 26

• Macro format and Usage

macro_name = value

โด้ย macro_name คื�อ ช่��อที่�ตั�องการเรยกแที่นึ่คื�า value ที่�ถ้"ก define ไว� ซึ่��ง make จะที่*าการแที่นึ่คื�าขั้อง value ในึ่ macro_name ที่�ม�นึ่หาเจอ

ในึ่ description file โด้ยสิ่ามารถ้ระบ+ macro ได้�ตัามขั้��นึ่ตัอนึ่ด้�งนึ่� :

1. เร-�มตั�นึ่บรรที่�ด้ใหม�ด้�วยช่��อขั้อง macro2. ตัามหล�งช่��อด้�วย equal sign (=)3. พื้-มพื้6 string ขั้องตั�วอ�กษรขั้อง value ที่�ตั�องการแที่นึ่ด้�วย macro_name ที่างด้�านึ่ขั้วาม�อขั้องเคืร��องหมาย =

The description file and its Components

make 27

• Example

# macro ABC has a value of “ls -la”

ABC = ls -la

# macro LIBES has a null value

LIBES =

# macro DIRECT includes the definition of macro ROOT

# the expanded value of DIRECT is “/usr/home/fred”

ROOT = /usr/home

DIRECT = $(ROOT)/fred

DIRECT macro ในึ่ command line เป/นึ่การใช่� definition อ��นึ่เป/นึ่สิ่�วนึ่ ขั้อง definition ขั้อง ROOT

The description file and its Components

make 28

• การใช่� macro ในึ่ description file– ในึ่การอ�างถ้�งคื�าที่�อย"�ในึ่ macro ที่*าได้�โด้ยการใช่�เคืร��องหมาย $ ก�อนึ่ช่��อ

ขั้อง macro – ถ้�าช่��อขั้อง macro ยาวกว�า 1 ตั�วอ�กษรจะตั�องใช่�วงเล'บ () หร�อป;กกา

{} คืรอบช่��อ macro นึ่��นึ่ด้�วย เช่�นึ่ :

$(XY)$(CFLAGS)$(Z)$Z

The description file and its Components

make 29

• การใช่� macro substitution1. การแที่นึ่ที่�คื*าในึ่ macro เช่�นึ่ แที่นึ่ string1

ขั้อง macro ด้�วย string 2

The description file and its Components

[$(MACRO : string1 = string2)]• ตั�วอย�าง

#Define macro MAC1MAC1 = xxx yyy zzz…

# 1Evaluate MAC:

= ====(1: =) xxx abc zzz

make 30

2. เป/นึ่การแที่นึ่ที่�ในึ่แตั�ละ word ในึ่คื�าที่�ระบ+ไว� แล�ว โด้ยมการระบ+ location parameter

เพื้��อบอกสิ่�วนึ่ขั้องคื*าที่�ตั�องการให�แที่นึ่ที่�

The description file and its Components

[$(MACRO/location/string)]

location :Circumflex (^) Asterisk *( )

Dollar sign ($)

make 31

• Circumflex (^) - string value adde d as a prefix to each defined word :

# 1Define macro MAC = 1 === === ===….

# 1Evaluate MAC: 1@ echo $(MAC /^/xyz)

The description file and its Components

xyz abc xyz def xyzghi

make 32

• Asterisk (*) - string value replaces all of each defined word :

# 1Define macro MAC = 1 === === ===….

# 1Evaluate MAC: 1*@ echo $(MAC / /xyz)

The description file and its Components

xyz xyz xyz

make 33

• Asterisk (*) - use ampersand (&) in the strin ===== = == ==========,

the defined word that is being substituted for, and cause that word to be interpolated in the re

sul t : # 1Define macro MAC

= 1 === === ===….

# 1Evaluate MAC: 1*@ echo $(MAC / /x&z)

The description file and its Components

xabcz xdefz xghiz

make 34

• Dollar sign ($) - string value is appended to each defined word :

# 1Define macro MAC 1MAC = abc def ghi

…. # 1Evaluate MAC

: 1@ echo $(MAC /$/xyz)

The description file and its Components

abcxyz defxyz ghixyz

make 35

3. เป/นึ่ร"ปแบบการเล�อกคื�าที่�เป/นึ่ไปได้�ในึ่การแที่นึ่คื�าขั้องคื*าที่�ระบ+ไว�:

# 1Define macro MAC = 1 === === ===….

# 1 2 2Evaluate MAC MAC , MAC is not defined.: 1@ echo $(MAC ?=== : xyz)

2 123@ echo $(MAC ? :456)

The description file and its Components

1 2[$(MACRO?string : string )]

Used Use

d

uvw456

make 36

The description file and its Components

Internal Macrosmake utility ม built-in macro definition สิ่*าหร�บใช่�ในึ่ description file โด้ยตั�ว macro จะช่�วยระบ+ตั�วแปรที่�ใช่�ในึ่ description fileโด้ย make utility จะแที่นึ่ macro ด้�งตั�อไปนึ่�

Macro Valuex$@ The name of the current target file$$@ The target names on the dependency line$? The names of the dependency files that have changed

more recently than thetarget

$< - - The name of the out of date file that caused a target file to be cr

eated$* The name of the current dependency file without the suffix

make 37

ตั�วอย�าง Internal target file name macro :

/u/tom/test : test.occ test.o -o $@

/u/tom/test : test.occ test.o -o /u/tom/test

The description file and its Components

make 38

• ตั�วอย�าง Internal label name macro : การใช่� $$@ เป/นึ่การแที่นึ่ที่�ด้�วยคื�าที่�อย"�ที่าง ซึ่�ายม�อ เช่�นึ่

cat : [email protected] แปลเป/นึ่ cat : cat.c

The description file and its Components

make 39

#Define macro CMDS as a series of command=== == = === == ==== ==== == === ==== == == ======

# Each command depends on a .c file$(CMD) : [email protected]#Create the new command set by compiling the

out of date file#( $?) to the current target file name ($@)

cc -O $? -o $@

The description file and its Components

make 40

The description file and its Components

• Suffix rules

make has a set of default rules called suffix or implicit rule. These are generalized rules that make can use to build a

program , example : rule tell make that .o object files are made

from .c source file .c.o : - - cc g c $<

Name of the file that is out of date with

the target

make 41

The description file and its Components

.o

.c .f .s .y .l

.y .l

Summary of Default Transformation Path

make 42

Example of description file

OBJS = file1.o file2.o file3.0

prog1 : $(OBJS)

cc -o prog1 $(OBJS)

file1.o : file1.c mydefs.h

cc -c file1.c

file2.o : file2.c mydefs.h

cc -c file2.c

file3.o : file3.c

cc -c file3.c

clean :

rm $(OBJS)

The description file and its Components

make 43

Operation of the make Utility

make 44

Operation of the make Utility

Maintain program :• m ake ut i l i t y ที่*างานึ่โด้ยการเปรยบเที่ยบ

creation date ขั้องโปรแกรมที่�ถ้"กสิ่ร�าง ซึ่��งเรยก ว�า target หร�อ target file ด้�วย date ขั้อง file ถ้"ก modify เรยกว�า dependency files หร�อ

dependents โด้ย make จะพื้-จารณีาว�า targe t นึ่��นึ่ เป/นึ่ตั�ว out-of-date ในึ่กรณีนึ่� make จะ r

ebuild ตั�ว target โด้ยการ compile สิ่�วนึ่ที่�จ*าเป/นึ่ แล�ว link ใหม� โด้ย command ที่�เขั้ยนึ่ไว�ในึ่

description file

make 45

Operation of the make Utility

TTTTTT13.10:12/2/1999

Dependency13.20:12/2/1999

Older than

TTTTTTTTTT TT TT TTTT

stop

make 46

Operation of the make Utility

TTTTTT13.30:12/2/1999

Dependency13.20:12/2/1999

New than

stop

make 47

Operation of the make Utility

• The make utility uses the following sources of information :

- A description file that you create

- File names

- Time stamps of the file from the file system

- A set of rules the tell make how to build files

• For make to work properly on a distributed system, the date

and time on all systems in the network must be synchronized

make 48

• ‘ - - make’ creates a target file using the following step by step procedure :

1 . Finds the name of target file in the description file 2. Findsa l i ne t hat descr i bes t he dependent s of t he t ar get ,

called a dependency line 3. ’Ensuret hat al l t he t ar get s dependency fi l es exi st and ar e up to date

4. Det er mi nes i f t he t ar get i s cur r ent wi t h r espect t o=== ==========

Operation of the make Utility

make 49

5. Create the target by one of the following= ====== == === ====== == ===

== === ========== == === == ====- ======== === = ==== ==== === =========== ====- ==== ======== ===== == ====== === ==== - Uses default rules from the description file

Operation of the make Utility

make 50

• If all file on the dependency line are up to date when make is run, make indicate that the target i

s up to date and then stops.• If any dependents are newer than their targets ,

make recreates only those target that are out of date

• If a given target has no dependents, it is always out of date, and make rebuilds it every time you r

un make.

Operation of the make Utility

make 51

• The make process works from the top down in determining what targets ne

ed to be rebuilt and from the bottom up in the actual rebuilding stage

Operation of the make Utility

make 52

Running make

make 53

General usage :

options :

-n เป/นึ่การ preview ตั�ว command โด้ยไม�ตั�องที่*าการ execute มประโยช่นึ่6มากในึ่การที่ด้สิ่อบ makefile

-d เป/นึ่การ debug รายการขั้องการด้*าเนึ่-นึ่การที่�ใช่�และ หาเหตั+ผู้ลว�า (“read make’s mind”) platforms ที่�ยกเว�นึ่คื�อ OSF1

Running make

% [] []

make 54

-e Environment variables ยกเว�นึ่ built-ins

-f makefile_name ถ้�ามการตั��งช่��อ description file ที่�ไม�ใช่� makefile หร�อ Makefile จะตั�องใช่� option นึ่�โด้ยการระบ+ช่��อ description file ที่�ตั� �งขั้��นึ่ (คื��นึ่ด้�วยช่�องว�างระหว�าง option ก�บ filename)

-p –f /dev/null [|less]พื้-มพื้6 built-in rules

Running make

make 55

Command Result

make use the default description file,

build the first target in the file

make myprog use the default description file, build the target myprog

make -f mymakefile use the file mymakefile as the description file, build the first target in the file

make -f mymakefile myprog use the file mymakefile as the

description file, build the target myprog

Running make

TTTTTTTT T TTT

make 56

Running make in background

“ ในึ่การใช่� make ก�บงานึ่ที่�ใช่�เวลาในึ่การ ประมวลผู้ลนึ่านึ่ เราสิ่ามารถ้ให� make ที่*างานึ่

แบบ background ได้� โด้ยเก'บขั้�อม"ลการ ที่*างานึ่ไว�ในึ่ LOG file แล�วผู้"�ใช่�ก'สิ่ามารถ้

ที่*างานึ่อ��นึ่คื"�ไปได้�ด้�วย ”โด้ยมร"ปแบบ ด้�งตั�อไปนึ่�

Running make

%make target>&LOG< /dev/null&

make 57

Running make

Run make in general purpose

“ make เป/นึ่เพื้ยงตั�วกลางในึ่การที่*างานึ่ย�อย ๆ โด้ยอ�ตัโนึ่ม�ตั- ไม�ใช่�เฉพื้าะงานึ่ compile

เที่�านึ่��นึ่ โด้ยสิ่ามารถ้เพื้-�ม target เขั้�าไปในึ่description file ซึ่��งเป/นึ่ที่�เก'บคื*าสิ่��งที่�จะใช่�

จ�ด้การ file ในึ่งานึ่โปรแกรม” เช่�นึ่ :Backup:

cp *.c *.h makefile backupdir