| 1 | <h2>Comments</h2> |
| 2 | <pre><code># This is a comment |
| 3 | include foo # This is another comment</code></pre> |
| 4 | |
| 5 | <h2>Targets</h2> |
| 6 | <pre><code>kbd.o command.o files.o : command.h |
| 7 | display.o insert.o search.o files.o : buffer.h |
| 8 | |
| 9 | .PHONY: clean |
| 10 | clean: |
| 11 | rm *.o temp</code></pre> |
| 12 | |
| 13 | <h2>Variables</h2> |
| 14 | <pre><code>objects = main.o kbd.o command.o display.o \ |
| 15 | insert.o search.o files.o utils.o |
| 16 | |
| 17 | edit : $(objects) |
| 18 | cc -o edit $(objects) |
| 19 | |
| 20 | $(objects) : defs.h |
| 21 | |
| 22 | %oo: $$< $$^ $$+ $$* |
| 23 | |
| 24 | foo : bar/lose |
| 25 | cd $(@D) && gobble $(@F) > ../$@</code></pre> |
| 26 | |
| 27 | <h2>Strings</h2> |
| 28 | <pre><code>STR = 'A string!' |
| 29 | |
| 30 | HELLO = 'hello \ |
| 31 | world' |
| 32 | |
| 33 | HELLO2 = "hello \ |
| 34 | world"</code></pre> |
| 35 | |
| 36 | <h2>Directives</h2> |
| 37 | <pre><code>include foo *.mk $(bar) |
| 38 | |
| 39 | vpath %.c foo |
| 40 | |
| 41 | override define two-lines = |
| 42 | foo |
| 43 | $(bar) |
| 44 | endef |
| 45 | |
| 46 | ifeq ($(CC),gcc) |
| 47 | libs=$(libs_for_gcc) |
| 48 | else |
| 49 | libs=$(normal_libs) |
| 50 | endif</code></pre> |
| 51 | |
| 52 | <h2>Functions</h2> |
| 53 | <pre><code>whoami := $(shell whoami) |
| 54 | host-type := $(shell arch) |
| 55 | |
| 56 | y = $(subst 1,2,$(x)) |
| 57 | |
| 58 | dirs := a b c d |
| 59 | files := $(foreach dir,$(dirs),$(wildcard $(dir)/*)) |
| 60 | |
| 61 | reverse = $(2) $(1) |
| 62 | foo = $(call reverse,a,b) |
| 63 | |
| 64 | $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))</code></pre> |
| 65 | |
| 66 | <h2>Complete example</h2> |
| 67 | <pre><code>#!/usr/bin/make -f |
| 68 | # Generated automatically from Makefile.in by configure. |
| 69 | # Un*x Makefile for GNU tar program. |
| 70 | # Copyright (C) 1991 Free Software Foundation, Inc. |
| 71 | |
| 72 | # This program is free software; you can redistribute |
| 73 | # it and/or modify it under the terms of the GNU |
| 74 | # General Public License … |
| 75 | … |
| 76 | … |
| 77 | |
| 78 | SHELL = /bin/sh |
| 79 | |
| 80 | #### Start of system configuration section. #### |
| 81 | |
| 82 | srcdir = . |
| 83 | |
| 84 | # If you use gcc, you should either run the |
| 85 | # fixincludes script that comes with it or else use |
| 86 | # gcc with the -traditional option. Otherwise ioctl |
| 87 | # calls will be compiled incorrectly on some systems. |
| 88 | CC = gcc -O |
| 89 | YACC = bison -y |
| 90 | INSTALL = /usr/local/bin/install -c |
| 91 | INSTALLDATA = /usr/local/bin/install -c -m 644 |
| 92 | |
| 93 | # Things you might add to DEFS: |
| 94 | # -DSTDC_HEADERS If you have ANSI C headers and |
| 95 | # libraries. |
| 96 | # -DPOSIX If you have POSIX.1 headers and |
| 97 | # libraries. |
| 98 | # -DBSD42 If you have sys/dir.h (unless |
| 99 | # you use -DPOSIX), sys/file.h, |
| 100 | # and st_blocks in `struct stat'. |
| 101 | # -DUSG If you have System V/ANSI C |
| 102 | # string and memory functions |
| 103 | # and headers, sys/sysmacros.h, |
| 104 | # fcntl.h, getcwd, no valloc, |
| 105 | # and ndir.h (unless |
| 106 | # you use -DDIRENT). |
| 107 | # -DNO_MEMORY_H If USG or STDC_HEADERS but do not |
| 108 | # include memory.h. |
| 109 | # -DDIRENT If USG and you have dirent.h |
| 110 | # instead of ndir.h. |
| 111 | # -DSIGTYPE=int If your signal handlers |
| 112 | # return int, not void. |
| 113 | # -DNO_MTIO If you lack sys/mtio.h |
| 114 | # (magtape ioctls). |
| 115 | # -DNO_REMOTE If you do not have a remote shell |
| 116 | # or rexec. |
| 117 | # -DUSE_REXEC To use rexec for remote tape |
| 118 | # operations instead of |
| 119 | # forking rsh or remsh. |
| 120 | # -DVPRINTF_MISSING If you lack vprintf function |
| 121 | # (but have _doprnt). |
| 122 | # -DDOPRNT_MISSING If you lack _doprnt function. |
| 123 | # Also need to define |
| 124 | # -DVPRINTF_MISSING. |
| 125 | # -DFTIME_MISSING If you lack ftime system call. |
| 126 | # -DSTRSTR_MISSING If you lack strstr function. |
| 127 | # -DVALLOC_MISSING If you lack valloc function. |
| 128 | # -DMKDIR_MISSING If you lack mkdir and |
| 129 | # rmdir system calls. |
| 130 | # -DRENAME_MISSING If you lack rename system call. |
| 131 | # -DFTRUNCATE_MISSING If you lack ftruncate |
| 132 | # system call. |
| 133 | # -DV7 On Version 7 Unix (not |
| 134 | # tested in a long time). |
| 135 | # -DEMUL_OPEN3 If you lack a 3-argument version |
| 136 | # of open, and want to emulate it |
| 137 | # with system calls you do have. |
| 138 | # -DNO_OPEN3 If you lack the 3-argument open |
| 139 | # and want to disable the tar -k |
| 140 | # option instead of emulating open. |
| 141 | # -DXENIX If you have sys/inode.h |
| 142 | # and need it 94 to be included. |
| 143 | |
| 144 | DEFS = -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \ |
| 145 | -DVPRINTF_MISSING -DBSD42 |
| 146 | # Set this to rtapelib.o unless you defined NO_REMOTE, |
| 147 | # in which case make it empty. |
| 148 | RTAPELIB = rtapelib.o |
| 149 | LIBS = |
| 150 | DEF_AR_FILE = /dev/rmt8 |
| 151 | DEFBLOCKING = 20 |
| 152 | |
| 153 | CDEBUG = -g |
| 154 | CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \ |
| 155 | -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \ |
| 156 | -DDEFBLOCKING=$(DEFBLOCKING) |
| 157 | LDFLAGS = -g |
| 158 | |
| 159 | prefix = /usr/local |
| 160 | # Prefix for each installed program, |
| 161 | # normally empty or `g'. |
| 162 | binprefix = |
| 163 | |
| 164 | # The directory to install tar in. |
| 165 | bindir = $(prefix)/bin |
| 166 | |
| 167 | # The directory to install the info files in. |
| 168 | infodir = $(prefix)/info |
| 169 | |
| 170 | #### End of system configuration section. #### |
| 171 | |
| 172 | SRCS_C = tar.c create.c extract.c buffer.c \ |
| 173 | getoldopt.c update.c gnu.c mangle.c \ |
| 174 | version.c list.c names.c diffarch.c \ |
| 175 | port.c wildmat.c getopt.c getopt1.c \ |
| 176 | regex.c |
| 177 | SRCS_Y = getdate.y |
| 178 | SRCS = $(SRCS_C) $(SRCS_Y) |
| 179 | OBJS = $(SRCS_C:.c=.o) $(SRCS_Y:.y=.o) $(RTAPELIB) |
| 180 | |
| 181 | AUX = README COPYING ChangeLog Makefile.in \ |
| 182 | makefile.pc configure configure.in \ |
| 183 | tar.texinfo tar.info* texinfo.tex \ |
| 184 | tar.h port.h open3.h getopt.h regex.h \ |
| 185 | rmt.h rmt.c rtapelib.c alloca.c \ |
| 186 | msd_dir.h msd_dir.c tcexparg.c \ |
| 187 | level-0 level-1 backup-specs testpad.c |
| 188 | |
| 189 | .PHONY: all |
| 190 | all: tar rmt tar.info |
| 191 | |
| 192 | tar: $(OBJS) |
| 193 | $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) |
| 194 | |
| 195 | rmt: rmt.c |
| 196 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ rmt.c |
| 197 | |
| 198 | tar.info: tar.texinfo |
| 199 | makeinfo tar.texinfo |
| 200 | |
| 201 | .PHONY: install |
| 202 | install: all |
| 203 | $(INSTALL) tar $(bindir)/$(binprefix)tar |
| 204 | -test ! -f rmt || $(INSTALL) rmt /etc/rmt |
| 205 | $(INSTALLDATA) $(srcdir)/tar.info* $(infodir) |
| 206 | |
| 207 | $(OBJS): tar.h port.h testpad.h |
| 208 | regex.o buffer.o tar.o: regex.h |
| 209 | # getdate.y has 8 shift/reduce conflicts. |
| 210 | |
| 211 | testpad.h: testpad |
| 212 | ./testpad |
| 213 | |
| 214 | testpad: testpad.o |
| 215 | $(CC) -o $@ testpad.o |
| 216 | |
| 217 | TAGS: $(SRCS) |
| 218 | etags $(SRCS) |
| 219 | |
| 220 | .PHONY: clean |
| 221 | clean: |
| 222 | rm -f *.o tar rmt testpad testpad.h core |
| 223 | |
| 224 | .PHONY: distclean |
| 225 | distclean: clean |
| 226 | rm -f TAGS Makefile config.status |
| 227 | |
| 228 | .PHONY: realclean |
| 229 | realclean: distclean |
| 230 | rm -f tar.info* |
| 231 | |
| 232 | .PHONY: shar |
| 233 | shar: $(SRCS) $(AUX) |
| 234 | shar $(SRCS) $(AUX) | compress \ |
| 235 | > tar-`sed -e '/version_string/!d' \ |
| 236 | -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \ |
| 237 | -e q |
| 238 | version.c`.shar.Z |
| 239 | |
| 240 | .PHONY: dist |
| 241 | dist: $(SRCS) $(AUX) |
| 242 | echo tar-`sed \ |
| 243 | -e '/version_string/!d' \ |
| 244 | -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \ |
| 245 | -e q |
| 246 | version.c` > .fname |
| 247 | -rm -rf `cat .fname` |
| 248 | mkdir `cat .fname` |
| 249 | ln $(SRCS) $(AUX) `cat .fname` |
| 250 | tar chZf `cat .fname`.tar.Z `cat .fname` |
| 251 | -rm -rf `cat .fname` .fname |
| 252 | |
| 253 | tar.zoo: $(SRCS) $(AUX) |
| 254 | -rm -rf tmp.dir |
| 255 | -mkdir tmp.dir |
| 256 | -rm tar.zoo |
| 257 | for X in $(SRCS) $(AUX) ; do \ |
| 258 | echo $$X ; \ |
| 259 | sed 's/$$/^M/' $$X \ |
| 260 | > tmp.dir/$$X ; done |
| 261 | cd tmp.dir ; zoo aM ../tar.zoo * |
| 262 | -rm -rf tmp.dir |
| 263 | </code></pre> |