1 2 3 Make 4 5 Author: Stuart Feldman 6 7 Make originated with a visit from Steve Johnson (author of yacc, etc.), 8 storming into my office, cursing the Fates that had caused him to waste 9 a morning debugging a correct program (bug had been fixed, file had not 10 been compiled, cc *.o was therefore unaffected). As I had spent a part 11 of the previous evening coping with the same disaster on a project I was 12 working on, the idea of a tool to solve it came up. It began with an 13 elaborate idea of a dependency analyzer, boiled down to something much 14 simpler, and turned into Make that weekend. Use of tools that were still 15 wet was part of the culture. Makefiles were text files, not magically 16 encoded binaries, because that was the Unix ethos: printable, debuggable, 17 understandable stuff. 18 19 -- Stuart Feldman 20 21 22 23 .h ---> 24 .s ----> .o ----> a.out 25 .c ---> 26 27 28 make components 29 - explicit rules 30 - implicit rules 31 - variable definitions 32 - dependencies 33 - productions 34 - comments 35 36 explicit rule 37 how to remake the target 38 39 implicit rule 40 how to remake a class of files based on their names 41 42 variable definitions 43 src = a.cpp b.cpp \ 44 c.cpp d.cpp 45 46 directives 47 reading other makefiles 48 include otherMakefile1 otherMakefile2 ... 49 50 (Trick: generate depenences into otherMakefile, than execute it) 51 52 decide whether to ignore some part of a makefile 53 defining variable from a verbatim string 54 55 comments 56 # 57 58 59 60 Wildcards - like in shell 61 62 '*' '?' '[...]' 63 64 65 Automatic variables 66 67 $@ the file name of the target of the rule 68 69 $% the tarhet member name, when the target i san archive member 70 71 $< the name of the first prerequisite 72 73 $? the name of all prerequisits that are newer than the target 74 75 $^ the names of all prerequisits (separated by spaces) 76 77 $+ similar than the previous 78 79 $* 80 81 $(@D) the directory part of the target 82 83 $(@F) the file part of the target 84 85 86 Conditional parts 87 88 ifeq($(CC),gcc) 89 $(CC) -o foo $(objects) $(libs_for_gcc) 90 else 91 $(CC) -o foo $(objects) $(libs_for_other) 92 endif 93 94 95 Implicit rules 96 97 foo: foo.o bar.o 98 cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS) 99 100 No rule for foo.o, but make automatically looks for it. 101 102 103 x.o : x.c 104 $(CC) -c $(CPPFLAGS) $(CFLAGS) x.c 105 106 x.o : x.cc | x.C 107 $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) x.cc x.C 108 109 Also there are rules for pascal, fortran, modula2, assembly 110 111 Linking a single object: 112 113 x: x.o 114 $(CC) $(LDFLAGS) x.o $(LOADLIBES) $(LDLIBS) 115 116 117 We can define custom implicite rules by writing a pattern rule 118 119 Looks like an ordinary rule, but the target includes exactly 120 one '%' character. 121 122 %.c as a pattern matches any file name that ends .c 123 s.%.c matches any at least 5 char strings starting 's' ending 'c' 124 125 The actual matching is called the "stem". 126 127 The '%' in the prerequisite of a pattern rule stands for the 128 same stem that was matched by the '%' in the target. 129 130 %.o : %.c ; command ... 131 132 Sometimes the prerequisites do not use '%', matching all files. 133 134 Examples: 135 136 %.o : %.c 137 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ 138 139 $@ is the target and $< is the source 140 for each case the rule applies. 141 142 % :: RCS/%,v 143 $(CO) $(COFLAGS) $< 144 145 for all files in RCS subdirectory 146 :: is a terminal rule. 147 148 Suffix rules (old style) 149 150 .o : .c is equivalent with %.o : %.c 151 152 .c.o : 153 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $< 154 is today: 155 %.c : %.o 156 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $< 157 158 159 Functions 160 161 $(function arguments...) 162 ${function arguments...} 163 164 165 166 $(subst from,to,text) 167 $(strip string) 168 $(findstring what,where) 169 $(sort list) 170 $(word n text) 171 $(dir names...) 172 $(suffix names...) 173 $(wildcard pattern...) 174 175 176 Wildcard expansion happens automatically in rules. But no otherwise. 177 178 Examples: 179 180 $(wildcard *.c) lists all the .c files 181 $(patsubst %.c,%.o,$(wildcard *.c)) changing x.c to x.o 182 183 Very complex makefiles, especially when they call subsidiary makefiles, 184 can become a source of complications rather than simplifying the build 185 process. A now-classic warning is issued in Recursive Make Considered 186 Harmful. 187 http://www.tip.net.au/~millerp/rmch/recu-make-cons-harm.html 188 189 Lines starting vith TABs - command lines - are presented to the shell 190 as they are - including the TABs. 191 192 Why the tab in column 1? Yacc was new, Lex was brand new. I had not tried 193 either, so I figured this would be a good excuse to learn. After getting 194 myself snarled up with my first stab at Lex, I just did something simple 195 with the pattern newline-tab. It worked, it stayed. And then a few weeks 196 later I had a user population of about a dozen, most of them friends, and 197 I did not want to screw up my embedded base. The rest, sadly, is history. 198 199 -- Stuart Feldman 200 201 202 $ make 203 looks for filenames, like GNUmakefile makefile and Makefile 204 205 $ make -f my_make_file.mk 206 vagy 207 $ make --file 208 209 If a MAKEFILES environment variable is exists, then make read 210 makefiles enlisted in MAKEFILES. 211 212 MAKEFILE_LIST is automatically appended by make with the names 213 of the executed makefiles. 214 215 .VARIABLES 216 the names defined by make 217 218 $ make -n # only prints the commands not executes. 219 220 221 Typical non-file (.PHONY) productions: 222 223 all 224 225 - this should be the first production in your makefile. 226 - executed when the developer types make with no argument. 227 228 test 229 230 - run the automated test suit or check installation. 231 232 clean 233 234 - remove all files created by make all. 235 236 dist 237 238 - make a source archive can be shipped as a unit 239 - equivalent of depending on all 240 241 install 242 uninstall 243 244 245 ######################## sample ###################### 246 247 .PHONY: doc clean show graph 248 249 SRC = $(wildcard src/*) 250 TEXSRC = $(wildcard *.tex) 251 DIASRC = $(wildcard pic/*.dia) 252 253 article.dvi: $(TEXSRC) $(DIASRC) 254 255 %.dvi: %.tex 256 latex $< 257 latex $< 258 259 %.eps: pic/%.dia 260 dia --nosplash --export=$@ $< 261 262 263 default: doc 264 265 show: doc 266 xdvi.bin article.dvi & 267 268 doc: pics $(SRC) article.dvi 269 270 pics: $(DIASRC) 271 272 purge: clean 273 rm -f *.eps 274 275 clean: 276 rm -f *.aux 277 rm -f *.lo* 278 rm -f *.toc 279 rm -f *.dvi 280 rm -f *.bbl *.blg 281 rm -f *~ pic/*~ src/*~ 282 283 help: 284 @echo "Available targets:" 285 @echo " show - compile and show document" 286 @echo " doc - compile documents (default)" 287 @echo " pics - compile pictures to document format" 288 @echo " clean - remove generated files" 289 # @echo " retab - retabulates the sources" 290 # @echo " longlines - show too long lines in the sources" 291 @echo "" 292 293 294 # --- Still unused 295 %.eps: %.plot 296 echo 'set terminal postscript eps; set output "$@"' | \ 297 gnuplot - $< 298 299 retab: 300 for i in $(SRC); do vim -s retab.vim -e $$i; done 301 302 longlines: 303 egrep '^.{76,}' $(SRC) 304 305 306 #source.tex: $(SRC) 307 #graph: benchmark.eps 308 309 ######################### end sample ######################### 310 311 312 ****************************************************************** 313 314 315 Makefile generators: 316 317 318 makedepend 319 ========== 320 321 - reads sources, parses precompiler directives 322 323 SRCS = file1.c file2.c ... 324 CFLAGS = -O -DHACK -I../foobar -xyz 325 depend: 326 makedepend -- $(CFLAGS) -- $(SRCS) 327 328 329 // a.h: 330 #include "b.h" 331 #include "b.c" 332 333 // b.h: 334 /* empty */ 335 336 // a.c: 337 #include <list> 338 #include "a.h" 339 340 int main() 341 { 342 return 0; 343 } 344 345 // b.c: 346 #include "b.h" 347 348 349 $ makedepend -fMakefile a.c 350 $ cat Makefile 351 352 # DO NOT DELETE 353 354 a.o: a.h b.h 355 b.o: b.h 356 357 358 359 ***************************************************************** 360 361 362 363 364 Imake 365 ===== 366 367 Imake is a Makefile generator to separate machine-dependent issues 368 from other make instructions. 369 370 Imake is based on CPP (C precompiler). 371 372 373 374 Imake was written in an attempt to mechanize makefile generation 375 for the X window system. It builds on makedepend to tackle both 376 the dependency-derivation and portability problems. 377 378 hard to learn, hard to use :( 379 380 Archive for "Software Portability with imake" (2nd edition) (O Reilly) 381 http://www.kitebird.com/imake-book/ 382 FAQ: 383 http://www.snake.net/software/imake-stuff/imake-faq.html 384 385 $ xmkmf 386 mv -f Makefile Makefile.bak 387 imake -DUseInstalled -I/usr/X11R6/lib/X11/config 388 imake: No such file or directory 389 imake: No description file. 390 Stop. 391 392 393 You never run imake directly. You should use a bootstrapper, to tell 394 imake 395 396 397 ******************************************************************** 398 399 400 401 autoconf 402 ======== 403 404 - produces per-project configure shell scripts 405 - shell scripts generate makefiles 406 407 Manual: 408 http://www.gnu.org/software/autoconf/manual/index.html 409 410 411 your source files --> [autoscan*] --> [configure.scan] --> configure.ac 412 configure.ac --. 413 | .------> autoconf* -----> configure 414 [aclocal.m4] --+---+ 415 | .-----> [autoheader*] --> [config.h.in] 416 [acsite.m4] ---. 417 418 Makefile.in -------------------------------> Makefile.in 419 420 421 Files used in configuring a software package: 422 423 .-------------> [config.cache] 424 configure* --------------+-------------> config.log 425 | 426 [config.h.in] ----. v .-> [config.h] -. 427 +--> config.status* -+ +--> make* 428 Makefile.in ------. .-> Makefile ---. 429 430 431 automake 432 ======== 433 434 - put imake-like dependency derivation as a layer on 435 top of autoconf. 436 - relatively new 437 438 automake 439 Makefile.am ---------> Makefile.in 440 441 442 443 $ make love 444 "Don't know how to make love" (original) 445 make: *** No rule to make target "love". Stop. (current gnu make) 446