CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 14 – Makefiles and Compilation Management.

Slides:



Advertisements
Similar presentations
The make Utility Programming Tools and Environments Winter 2006.
Advertisements

Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
David Notkin Autumn 2009 CSE303 Lecture 20 static make.
Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey.
1 CSE 390 Lecture 8 Large Program Management: Make; Ant slides created by Marty Stepp, modified by Josh Goodwin
Compilation & linkage.h read.h.c read.c.c main.c.c list.c.h list.h prog1 Linkage: g++ read.o main.o list.o –o prog1.o main.o.o list.o.o read.o Compilation:
1 The Makefile Utility ABC – Chapter 11,
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 9.
CS465 - Unix C Programming (cc/make and configuration control)
1 Building. 2 Goals of this Lecture Help you learn about: The build process for multi-file programs Partial builds of multi-file programs make, a popular.
Make: the good, the bad, and the ugly Dan Berger Titus Winters
Introduction Use of makefiles to manage the build process Declarative, imperative and relational rules Environment variables, phony targets, automatic.
Configuration Management (CM)
Jump to first page (C) 1998, Arun Lakhotia 1 Software Configuration Management: Build Control Arun Lakhotia University of Southwestern Louisiana Po Box.
Ant Build Tools.  Creating a product from source may take several steps: Compile Link Copy files to various directories Remove intermediate files Generate.
Scons Writing Solid Code Overview What is scons? scons Basics Other cools scons stuff Resources.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
C Tutorial - Program Organization CS Introduction to Operating Systems.
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
1 CSE 390 Lecture 8 Large Program Management: Make; Ant slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Separate Compilation make and makefiles
1 Building Jennifer Rexford. 2 Goals of this Lecture Help you learn about: The build process for multi-file programs Partial builds of multi-file programs.
Data Display Debugger (DDD)
Tools – Ant-MakeEtc 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Tools 12 – Hamcrest 10/02/
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 6 – sed, command-line tools wrapup.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Multiple File Compilation and linking By Bhumik Sapara.
SPI NIGHTLIES Alex Hodgkins. SPI nightlies  Build and test various software projects each night  Provide a nightlies summary page that displays all.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 4 – Shell Variables, More Shell Scripts.
Build Tools 1. Building a program for a large project is usually managed by a build tool that controls the various steps involved. These steps may include:
Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects can become huge My research consists of 1600.
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.
Lecture 6 UNIX Development Tools. Software Development Tools.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 18 – Linking and Libraries.
Multiple file project management & Makefile
The make utility (original presentation courtesy of Alark Joshi)
Large Program Management: Make; Ant
CSE 303 Lecture 17 Makefiles reading: Programming in C Ch. 15
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
CSE 374 Programming Concepts & Tools
C – Multi-file development and make
CSE 374 Programming Concepts & Tools
Large Program Management: Make; Ant
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Large Program Management: Make; Ant
Large Program Management: Make; Ant
Large Program Management: Make; Ant
CSE 390 Lecture 8 Large Program Management: Make; Ant
Final C Details, Build Tools CSE 333 Spring 2018
Large Program Management: Make; Ant
Writing Large Programs
Large Program Management: Make; Ant
Build Tools (make) CSE 333 Autumn 2018
Large Program Management: Make; Ant
Large Program Management: Make; Ant
Large Program Management: Make; Ant
CSE 390 Lecture 8 Large Program Management: Make; Ant
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
Large Program Management: Make; Ant
The make utility (original presentation courtesy of Alark Joshi)
Large Program Management: Make, Ant
Presentation transcript:

CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 14 – Makefiles and Compilation Management

Where we are Onto tools... Basics of make, particular the concepts Some fancier make features (revenge of funky characters) Besides the slides and online Unix docs, the Stanford CSLib notes on Unix Programming Tools has a nice overview of make and other tools: 2

Onto tools The language implementation (preprocessor, compiler, linker, standard-library) is hardly the only useful thing for developing software The rest of the course: –Tools (recompilation managers, version control, maybe profilers; we’ve already seen a debugger) –Software-engineering issues –A taste of C++ –Concurrency 3

make make is a classic program for controlling what gets (re)compiled and how. Many other such programs exist (e.g., ant, maven, “projects” in IDEs,...) make has tons of fancy features, but only two basic ideas: 1.Scripts for executing commands 2.Dependencies for avoiding unnecessary work To avoid “just teaching make features” (boring and narrow), let’s focus more on the concepts... 4

Building software Programmers spend a lot of time “building” (creating programs from source code) –Programs they write –Programs other people write Programmers automate repetitive tasks. Trivial example: gcc -Wall –g -std=c11 -o widget foo.c bar.c baz.c If you: –Retype this every time: “shame, shame” –Use up-arrow or history: “shame” (retype after logout) –Have an alias or bash script: “good-thinkin” –Have a Makefile: you’re ahead of us 5

“Real” build process On larger projects, you can’t or don’t want to have one big (set of) command(s) that redoes everything every time you change anything 1.If gcc didn’t combine steps behind your back, you’d need to preprocess and compile each file, then run the linker 2.If another program (e.g., sed) created some C files, you would need an “earlier” step 3.If you have other outputs for the same source files (e.g., javadoc), it’s unpleasant to type the source file names multiple times 4.If you want to distribute source code to be built by other users, you don’t want to explain the build logic to them 5.If you have 10 5 to 10 7 lines of source code, you don’t want to recompile them all every time you change something A simple script handles 1–4 (use a variable for filenames for 3), but 5 is trickier 6

Recompilation management The “theory” behind avoiding unnecessary compilation is a “dependency dag” (directed, acyclic graph): To create a target t, you need sources s 1,s 2, …,s n and a command c (that directly or indirectly uses the sources) If t is newer than every source (file-modification times), assume there is no reason to rebuild it Recursive building: If some source s i is itself a target for some other sources, see if it needs to be rebuilt… Cycles “make no sense” 7

Theory applied to C Here is what we need to know today for C (still need to talk more about linking in a future lecture) –Compiling a.c creates a.o – the.o depends on the.c and all included files (.h files, recursively/transitively) –Creating an executable (“linking”) depends on.o files –So if one.c file changes, just need to recreate one.o file and relink –If a header file changes, may need to rebuild more –Of course, this is only the simplest situation 8

An algorithm What would a program (e.g., a shell script) that did this for you look like? It would take: –a bunch of triples: target, sources, command(s) –a “current target to build” It would compute what commands needed to be executed, in what order, and do it (it would detect cycles and give an error) This is exactly what programs like make, ant, and build tools integrated into IDEs do! 9

make basics The “triples” are typed into a “makefile” like this: target:sources command Example: foo.o: foo.c foo.h bar.h gcc -Wall -o foo.o -c foo.c Syntax gotchas: The colon after the target is required Command lines must start with a TAB NOT SPACES You can actually have multiple commands (executed in order); if one command spans lines you must end the previous line with \ Which shell-language interprets the commands? (Typically bash; to be sure, set the SHELL variable in your makefile.) 10 TAB

Using make At the prompt: prompt% make -f nameOfMakefile aTarget Defaults: If no -f specified, use a file named Makefile If not target specified, use the first one in the file Open source usage: You can download a tarball, extract it, type make (four characters) and everything should work Actually, there’s typically a “configure” step too, for finding things like “where is the compiler” that generates the Makefile (but we won’t get into that) –The mantra:./configure; make; make install 11

Basics summary So far, enough for next homework and basic use. A tool that combines scripting with dependency analysis to avoid unnecessary recompilation Not language or tool-specific: just uses file-modification times and shell-commands But there’s much more you want so that your Makefiles are: Short and modular Easy to reuse (with different flags, platforms, etc.) Useful for many tasks Automatically maintained with respect to dependencies Also, reading others’ makefiles can be tough because of all the features: see info make or entire books 12

Precise review A Makefile has a bunch of these: target: source_1...source_n shell_command Running make target does this: For each source, if it is a target in the Makefile, process it recursively Then: –If some source does not exist, error –If some source is newer than the target (or target does not exist), run shell_command (presumably updates target, but that is up to you; shell_command can do anything) 13

make variables You can define variables in a Makefile. Example: CC = gcc CFLAGS = -Wall foo.o: foo.c foo.h bar.h $(CC) $(CFLAGS) -c foo.c -o foo.o Why do this? Easy to change things once and affect many commands Can change variables on the command-line (overrides definitions in file) (For example make CFLAGS=-g) Easy to reuse most of a Makefile on new projects Can use conditionals to set variables (using inherited environment variables)… 14

make conditionals EXE= ifdef WINDIR # defined on Windows (from folklore) EXE=.exe endif widget$(EXE): foo.o bar.o $(CC) $(CFLAGS) -o widget$(EXE) foo.o bar.o Other forms of conditionals exist (e.g., are two strings equal) 15

More variables It’s also common to use variables to hold list of filenames: OBJFILES = foo.o bar.o baz.o widget: $(OBJFILES) gcc -o widget $(OBJFILES) clean: rm $(OBJFILES) widget clean is a convention: remove any generated files, to “start over” and have just the source It’s “funny” because the target doesn’t exist and there are no sources, but that’s okay: –If target doesn’t exist, it must be “remade” so run the commands –These “phony” targets have several uses, another is an “all” target

“all” example all: prog B.class someLib.a # notice no commands this time prog: foo.o bar.o main.o gcc -o prog foo.o bar.o main.o B.class: B.java javac B.java someLib.a: foo.o baz.o ar r foo.o baz.o foo.o: foo.c foo.h header1.h header2.h gcc -c -Wall foo.c...(similar targets for bar.o, main.o, baz.o)... 17

Revenge of the funny characters And you thought we were done with this after bash, sed… In commands: for target –$^ for all sources –$< for left-most source –… Examples: widget$(EXE): foo.o bar.o $(CC) $(CFLAGS) -o $^ foo.o: foo.c foo.h bar.h $(CC) $(CFLAGS) -c $< 18

And more… There are a lot of “built-in” rules. E.g., make just “knows” to create foo.o by calling $(CC) $(CFLAGS) on foo.c. (Opinion: may be more confusing than helpful. YMMV) There are “suffix” rules and “pattern” rules. Example: %.class: %.java javac $< # Note we need $< here Remember you can put any shell command on the command-line, even whole scripts You can repeat target names to add more dependencies (useful with automatic dependency generation) Often this stuff is more useful for reading makefiles than writing your own (until some day…) 19

Dependency generation So far, we are still listing dependencies manually, e.g.: foo.o: foo.c foo.h bar.h If you forget, say, bar.h, you can introduce subtle bugs in your program (or if you’re lucky, get confusing errors) This is not make’s problem: It has no understanding of different programming languages, commands, etc., just file-mod times But it does seem too error-prone and busy-work to have to remember to update dependencies, so there are often language-specific tools that do it for you … 20

Dependency-generator example gcc -M Actually lots of useful variants, including -MM and -MG. See man gcc (or info gcc) Automatically creates a rule for you Then include the resulting file in your Makefile Typically run via a phony depend target, e.g.: depend: $(PROGRAM_C_FILES) gcc -M $^ The program makedepend combines many of these steps; again it is C-specific but some other languages have their own 21

Build-script summary Always script complicated tasks Always automate “what needs rebuilding” via dependency analysis make is a text-based program with lots of bells and whistles for doing this. It is not language-specific. Use it. –It also is independent of particular IDEs/editors so everyone on the project can have a repeatable build With language-specific tools, you can automate dependency generation make files have a way of starting simple and ending up unreadable. It is worth keeping them clean. There are conventions like make all and make clean common when distributing source code 22