Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every...

100
Building Soſtware CMPT 373 Soſtware Development Methods Nick Sumner [email protected]

Transcript of Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every...

Page 1: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Building Software

CMPT 373Software Development Methods

Nick [email protected]

Page 2: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● How many of you know how to build software?– Really.

Page 3: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● How many of you know how to build software?– Really.

● What does the process include?

Page 4: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● How many of you know how to build software?– Really.

● What does the process include?

● How many of you have heard terms like– Build Engineering?

– Build Configuration?

– Build Automation?

– Dependency Management?

– Continuous Integration?

Page 5: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● How many of you know how to build software?– Really.

● What does the process include?

● How many of you have heard terms like– Build Engineering?

– Build Configuration?

– Build Automation?

– Dependency Management?

– Continuous Integration?

Just getting something to compile reproducibly can be nontrivial

Page 6: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● Building software includes (at least):– version control integration

Page 7: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● Building software includes (at least):– version control integration

– identifying dependencies & their versions

Page 8: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● Building software includes (at least):– version control integration

– identifying dependencies & their versions

– configuring build commands for different build modes & environments

Page 9: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● Building software includes (at least):– version control integration

– identifying dependencies & their versions

– configuring build commands for different build modes & environments

– writing instructions for how to configure & build

Page 10: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● Building software includes (at least):– version control integration

– identifying dependencies & their versions

– configuring build commands for different build modes & environments

– writing instructions for how to configure & build

– test configuration & execution (performance & correctness)

Page 11: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● Building software includes (at least):– version control integration

– identifying dependencies & their versions

– configuring build commands for different build modes & environments

– writing instructions for how to configure & build

– test configuration & execution (performance & correctness)

– automated code quality checking

Page 12: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● Building software includes (at least):– version control integration

– identifying dependencies & their versions

– configuring build commands for different build modes & environments

– writing instructions for how to configure & build

– test configuration & execution (performance & correctness)

– automated code quality checking

– scalable compilation & linking

Page 13: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● Building software includes (at least):– version control integration

– identifying dependencies & their versions

– configuring build commands for different build modes & environments

– writing instructions for how to configure & build

– test configuration & execution (performance & correctness)

– automated code quality checking

– scalable compilation & linking

– possibly even deployment

Page 14: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● Building software includes (at least):– version control integration

– identifying dependencies & their versions

– configuring build commands for different build modes & environments

– writing instructions for how to configure & build

– test configuration & execution (performance & correctness)

– automated code quality checking

– scalable the compilation & linking

– possibly even deployment

● It is the foundation of getting anything done.

Page 15: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● How many of you know how to build software?– Really.

Page 16: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does it mean to build software?

● How many of you know how to build software?– Really.

● You should at least ask yourself:– What tools do you use?

– What workflow?

– What benefits do you get?

– What are the painful points?

– Why haven't you made them less painful?

Page 17: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● To build software, we must consider:

Page 18: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● To build software, we must consider:– Components & Objectives

Client ServerProgram Program

Page 19: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● To build software, we must consider:– Components & Objectives

Client Server

JSON Networking

Program Program

LibraryLibrary

Page 20: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● To build software, we must consider:– Components & Objectives

Client Server

JSON Networking

Formatting

Program Program

LibraryLibrary

Library

Page 21: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● To build software, we must consider:– Components & Objectives

Client Server

JSON Networking

Formatting

All

Program Program

LibraryLibrary

Library

Project

Page 22: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● To build software, we must consider:– Components & Objectives

– Dependencies between them

Client Server

JSON Networking

Formatting

All

Program Program

LibraryLibrary

Library

Project

Page 23: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● To build software, we must consider:– Components & Objectives

– Dependencies between them

Client Server

JSON Networking

Formatting

All

Program Program

LibraryLibrary

Library

Project

Page 24: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● To build software, we must consider:– Components & Objectives

– Dependencies between them

Client Server

JSON Networking

Formatting

All

Program Program

LibraryLibrary

Library

Project

Page 25: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● To build software, we must consider:– Components & Objectives

– Dependencies between them

Client Server

JSON Networking

Formatting

All

Program Program

LibraryLibrary

Library

Project

Page 26: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● To build software, we must consider:– Components & Objectives

– Dependencies between them

Client Server

JSON Networking

Formatting

All

Program Program

LibraryLibrary

Library

Project

This defines the dependency graph of a project.

Page 27: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!

Client Server

JSON Networking

Formatting

All

Program Program

LibraryLibrary

Library

Project

Page 28: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!

Client Server

JSON Networking

Formatting

All

Program Program

LibraryLibrary

Library

Project

Page 29: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

Client Server

JSON Networking

Formatting

All

Program Program

LibraryLibrary

Library

Project

Page 30: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

Client Server

JSON Networking

Formatting

All

Program Program

LibraryLibrary

Library

Project● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Page 31: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formatting

Program

LibraryLibrary

Library

Page 32: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Library

Library Library

Program

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formattingformat.cpp

libformat.a

Page 33: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Library

Library Library

Program

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formattingformat.cpp

libformat.a

libjson.a

Page 34: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Library

Library Library

Program

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formattingformat.cpp

libformat.a

libjson.a

json.cpp

Page 35: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Library

Library Library

Program

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formattingformat.cpp

libformat.a

libjson.a

json.cpp <...>/format.h

Page 36: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Library

Library Library

Program

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formattingformat.cpp

libformat.a

<...>/libformat.a

libjson.a

json.cpp <...>/format.h

Page 37: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Library

Library Library

Program

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formattingformat.cpp

libformat.a

<...>/libformat.a

libjson.a

json.cpp <...>/format.h ?

Page 38: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Library

Library Library

Program

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formattingformat.cpp

libformat.a

libjson.a

json.cpp <...>/format.h

Page 39: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Library

Library Library

Program

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formattingformat.cpp

libformat.a

libjson.a

json.cpp <...>/format.hlibnetworking.a

net.cpp <...>/format.h

Page 40: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Library

Library Library

Program

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formattingformat.cpp

libformat.a

libjson.a

json.cpp <...>/format.hlibnetworking.a

net.cpp <...>/format.h

<...>/libformat.a

bin/client

client.cpp <...>/net.h

<...>/json.h <...>/libnetworking.a

<...>/libjson.a

Page 41: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formatting

Program

LibraryLibrary

LibraryCan you think of problems that may arise?

Page 42: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formatting

Program

LibraryLibrary

LibraryCan you think of problems that may arise?

Page 43: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formatting

Program

LibraryLibrary

LibraryCan you think of problems that may arise?

bin/client

client.cpp <...>/net.h

<...>/json.h <...>/libnetworking.a

<...>/libjson.a

<...>/libformat.a <...>/libformat.a

Page 44: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formatting

Program

LibraryLibrary

LibraryCan you think of problems that may arise?

foo/<...>/format.h

bar/<...>/format.h

Page 45: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formatting

Program

LibraryLibrary

LibraryCan you think of problems that may arise?

foo/<...>/format.h

bar/<...>/format.h

libjson.a

json.cpp ?/format.h

Page 46: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

Server

All

Program

Project

What must the build system perform?

● To build software, we must consider:– Components & Objectives

– Dependencies between them

● The dependency graph can alreadyhelp to analyze our project!– A good dependency graph is a DAG

● Modern build management uses thedependency DAG to drive build process

Client

JSON Networking

Formatting

Program

LibraryLibrary

LibraryCan you think of problems that may arise?

Page 47: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● For each component, build management requires– Direct build requirements

Page 48: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● For each component, build management requires– Direct build requirements– Transitive usage requirements

Page 49: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● For each component, build management requires– Direct build requirements– Transitive usage requirements

– Note, these just separate interfaces from implementation

Page 50: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● For each component, build management requires– Direct build requirements– Transitive usage requirements

– Note, these just separate interfaces from implementation

● The dependency graph allows us to use these to infer how to build a component correctly based on its dependencies– NOTE:

Even if a dependency uses a new library, the build system should detect it.

Page 51: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● For each component, build management requires– Direct build requirements– Transitive usage requirements

– Note, these just separate interfaces from implementation

● The dependency graph allows us to use these to infer how to build a component correctly based on its dependencies– NOTE:

Even if a dependency uses a new library, the build system should detect it.Client

JSON Networking

Formatting

Page 52: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● For each component, build management requires– Direct build requirements– Transitive usage requirements

– Note, these just separate interfaces from implementation

● The dependency graph allows us to use these to infer how to build a component correctly based on its dependencies– NOTE:

Even if a dependency uses a new library, the build system should detect it.Client

JSON Networking

Formatting FancyIO

Page 53: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● For each component, build management requires– Direct build requirements– Transitive usage requirements

– Note, these just separate interfaces from implementation

● The dependency graph allows us to use these to infer how to build a component correctly based on its dependencies– NOTE:

Even if a dependency uses a new library, the build system should detect it.Client

JSON Networking

Formatting FancyIO

Include directories for Networking should change

Page 54: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● For each component, build management requires– Direct build requirements– Transitive usage requirements

– Note, these just separate interfaces from implementation

● The dependency graph allows us to use these to infer how to build a component correctly based on its dependencies– NOTE:

Even if a dependency uses a new library, the build system should detect it.Client

JSON Networking

Formatting FancyIO

Include directories for Networking should changeLinked libraries for Client should change

Page 55: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Modeling a Build

● For each component, build management requires– Direct build requirements– Transitive usage requirements

– Note, these just separate interfaces from implementation

● The dependency graph allows us to use these to infer how to build a component correctly based on its dependencies– NOTE:

Even if a dependency uses a new library, the build system should detect it.

● Let’s dive into one specific system to see....

Page 56: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What will be be using?

● CMake– Cross-platform build management tool

– Used by large projects like KDE, Wireshark, LLVM, ...

Page 57: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What will be be using?

● CMake– Cross-platform build management tool

– Used by large projects like KDE, Wireshark, LLVM, ...

● What does it do?– Given a specification & configuration of your project, CMake creates the

build commands for you

– Analogous to autoconf (but easier to use)

Page 58: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What will be be using?

● CMake– Cross-platform build management tool

– Used by large projects like KDE, Wireshark, LLVM, ...

● What does it do?– Given a specification & configuration of your project, CMake creates the

build commands for you

– Analogous to autoconf (but easier to use)

[DEMO]

Page 59: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does this add?

● Why not just write makefiles manually?

Page 60: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does this add?

● Why not just write makefiles manually?– May need different makefiles for different...

Page 61: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does this add?

● Why not just write makefiles manually?– May need different makefiles for different

● Operating Systems● Compilers● Libraries● Build Modes● ...

Page 62: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does this add?

● Why not just write makefiles manually?– May need different makefiles for different

● Operating Systems● Compilers● Libraries● Build Modes● ...

– May need different source files for different “”

Page 63: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does this add?

● Why not just write makefiles manually?– May need different makefiles for different

● Operating Systems● Compilers● Libraries● Build Modes● ...

– May need different source files for different “”

– Specification can clearly capture● Libraries, versions, & even how to download them automatically● Semantics of compilation & how to use in analysis tools

Page 64: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does this add?

● Scalability– Replace “make” with analogous scalable tools (“ninja”)

Page 65: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does this add?

● Scalability– Replace “make” with analogous scalable tools (“ninja”)

● Easier tool integration– CMake can export compilation rules for other tools

Page 66: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

What does this add?

● Scalability– Replace “make” with analogous scalable tools (“ninja”)

● Easier tool integration– CMake can export compilation rules for other tools

[DEMO]

Page 67: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Preliminary: Out of source builds

● A common bad habit is “in source” building

Page 68: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Preliminary: Out of source builds

● A common bad habit is “in source” building– Why is this bad?

Page 69: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Preliminary: Out of source builds

● A common bad habit is “in source” building– Why is this bad?

– May need multiple builds at once: debug, release, ...

– Pollutes version control

– Makes clean builds complicated

Page 70: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Preliminary: Out of source builds

● A common bad habit is “in source” building– Why is this bad?

– May need multiple builds at once: debug, release, ...

– Pollutes version control

– Makes clean builds complicated

● Use “out of source” builds instead

Page 71: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Using CMake

● CMakeLists.txt– A script in every directory of your project that controls how to build

“things” in that directory

Page 72: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Using CMake

● CMakeLists.txt– A script in every directory of your project that controls how to build

“things” in that directory

● Simple syntax– Case insensitive commands

– Let's revisit demo 1!

command( argument1 argument2 argument3 ...)

Page 73: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

add_executable(helloworld)add_library(hellohelper STATIC)

Targets & Commands

● CMake allows you to specify targets– Executables, libraries, “objects”

Page 74: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Targets & Commands

● CMake allows you to specify targets– Executables, libraries, “objects”

● And commands that can describe how to build those targets– Automatic for executable & library

– add_custom_command can build others● Documentation● Media

add_executable(helloworld)add_library(hellohelper STATIC)

Page 75: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Specifying Requirements

● Recall build requirements & usage requirements.

Page 76: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Specifying Requirements

● Recall build requirements & usage requirements.

● target_* commands allow you to specify the requirements of a target

Page 77: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Specifying Requirements

● Recall build requirements & usage requirements.

● target_* commands allow you to specify the requirements of a target

target_sources(hellohelper PRIVATE helloworld.cpp)

Page 78: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Specifying Requirements

● Recall build requirements & usage requirements.

● target_* commands allow you to specify the requirements of a target

target_sources(hellohelper PRIVATE helloworld.cpp)target_include_directories(hellohelper INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include/)

Page 79: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Specifying Requirements

● Recall build requirements & usage requirements.

● target_* commands allow you to specify the requirements of a target

target_sources(hellohelper PRIVATE helloworld.cpp)target_include_directories(hellohelper INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include/)target_link_libraries(helloworld PRIVATE hellohelper)

Page 80: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Specifying Requirements

● Recall build requirements & usage requirements.

● target_* commands allow you to specify the requirements of a target

target_sources(hellohelper PRIVATE helloworld.cpp)target_include_directories(hellohelper INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include/)target_link_libraries(helloworld PRIVATE hellohelper)

Page 81: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Using Libraries

● You can simply specify the libraries that a target directly uses

target_link_libraries(helloworld PRIVATE hellohelper aaa bbb)target_link_libraries(hellohelper INTERFACE fancyformatting ccc)

Page 82: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Using Libraries

● You can simply specify the libraries that a target directly uses

● Transitive interface dependences of libraries will be linked in as required

target_link_libraries(helloworld PRIVATE hellohelper aaa bbb)target_link_libraries(hellohelper INTERFACE fancyformatting ccc)

Page 83: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Using Libraries

● You can simply specify the libraries that a target directly uses

● Transitive interface dependences of libraries will be linked in as required

● Include directories, etc. From libraries will also be inferred

target_link_libraries(helloworld PRIVATE hellohelper aaa bbb)target_link_libraries(hellohelper INTERFACE fancyformatting ccc)

Page 84: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Using Libraries

● You can simply specify the libraries that a target directly uses

● Transitive interface dependences of libraries will be linked in as required

● Include directories, etc. From libraries will also be inferred

target_link_libraries(helloworld PRIVATE hellohelper aaa bbb)target_link_libraries(hellohelper INTERFACE fancyformatting ccc)

bin/helloworld

hello.cpp <...>/hello.h <...>/libfancyformatting.a

<...>/libhellohelper.a

Page 85: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Using Libraries

● Note: This means using creating & defining new libraries is easy!

Page 86: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Using Libraries

● Note: This means using creating & defining new libraries is easy!

● How might this affect program structure and design?

Page 87: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Using Libraries

● Note: This means using creating & defining new libraries is easy!

● How might this affect program structure and design?

● How might it help us begin to handle complexity?

Page 88: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Using Libraries

● Note: This means using creating & defining new libraries is easy!

● How might this affect program structure and design?

● How might it help us begin to handle complexity?

CMake has several other mundane build system facilities...

Page 89: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

General project management

● Specifying project properties– Define a project to access variables that control that project

project(projectname)

Page 90: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

General project management

● Specifying project properties– Define a project to access variables that control that project

● Print information out during the build processmessage(“Built with flags: ${CMAKE_CXX_FLAGS}”)

project(projectname)

Page 91: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

General project management

● Specifying project properties– Define a project to access variables that control that project

● Print information out during the build process

● Controlling where things are built

project(projectname)

message(“Built with flags: ${CMAKE_CXX_FLAGS}”)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")

Page 92: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

General project management

● Finding a resource that you need to use

find_package(externalproject)find_library(library)

Page 93: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

General project management

● Finding a resource that you need to use

● Installation

find_package(externalproject)find_library(library)

install(TARGETS target1 target2 ... DESTINATION /tmp/)

Page 94: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Control structures

● IF if(condition)elsif(condition2)else()endif()

Page 95: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Control structures

● IF

● Looping

if(condition)elsif(condition2)else()endif()

foreach(loop_var arg1 arg2 ...) command(${loop_var})endforeach(loop_var)while(condition)...

Page 96: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

Control structures

● IF

● Looping

● Functions

foreach(loop_var arg1 arg2 ...) command(${loop_var})endforeach(loop_var)while(condition)...

function(function_name arg1 arg2 ...) command(${arg1})endFunction(function_name)

if(condition)elsif(condition2)else()endif()

Page 97: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

In Summary

● A modern build system leverages the dependency graph of a project

Page 98: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

In Summary

● A modern build system leverages the dependency graph of a project

● Dependency graphs enable1) inference of build and usage requirements2) compositional reasoning about modules and build management

Page 99: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

In Summary

● A modern build system leverages the dependency graph of a project

● Dependency graphs enable1) inference of build and usage requirements2) compositional reasoning about modules and build management

● One dominant system for C and C++ is Cmake

Page 100: Building Software - Simon Fraser Universitywsumner/teaching/373/02-building.pdf– A script in every directory of your project that controls how to build “things” in that directory

In Summary

● A modern build system leverages the dependency graph of a project

● Dependency graphs enable1) inference of build and usage requirements2) compositional reasoning about modules and build management

● One dominant system for C and C++ is Cmake

● You will get more personal experience with it over the semester if you have not already