Makefile
Makefile
03/02/2022
1
Introduction
2
Introduction
3
Introduction
Make:
Created in 1976, Still widely used
Three major flavors : BSD Make, GNU Make and Microsoft Nmake
Autotools :
Autoconf 1992, Automake 1994, Libtool 1996
CMake :
First release in 2004, Cpack 2012
Meant to be cross-platform
4
Makefiles
5
Makefiles : basics & examples
Simple Makefile example (POSIX compliant):
6
Makefiles : basics & examples
More complete example:
7
Makefiles: macros
8
Makefiles : macros
Macros basics :
Evaluated when used
Can be overridden when invoking make:
9
Makefiles : macros
GNU extensions:
10
Makefiles : macros
Predefined macros:
MACRO DEFINITION MACRO (GNU) DEFINITION
AR Archiver name CXX C++ compiler
ARFLAGS Archiver flags CXXFLAGS C++ flags
YACC Parser name CPP Preprocessor
YFLAGS Parser flags CPPFLAGS Preprocessor
flags
LEX Lexer name
LINT Lint program
LFLAGS Lexer flags
MAKEINFO Texinfo converter
LDFLAGS Linker flags
RM Command to
CC Compiler
remove a file
CFLAGS Compiler flags
...
FC Fortran compiler
FFLAGS Fortran flags
11
Makefiles : rules
12
Makefiles : rules
Rules syntax:
Targets:
Most of the time targets are files to generate
It can still be just a name and will be callable from the command-line (ex: clean)
Prerequisites
The target's dependencies
Expanded when the target is evaluated
Can be file names (this file's mod time will be automatically compared with the
target)
Commands
Prefixed with a <tab>
Used to generate the target(s)
13
Makefiles : rules
Commands
Prefixed with a <tab>
Used to generate the target(s)
Some prefixes are available:
PREFIX DESCRIPTION
- Ignore errors
@ Do not display command
+ Execute the command not regarding make
execution mode (see -n, -q, -t arguments)
14
Makefiles : rules
Inference rules
Inference rules are rules that contains a '.'
15
Makefiles : rules
Internal macros
Generated by the tool
MACRO DEFINITION
$@ Target name
$* Target name without suffix
$< First prerequisite
$? All prerequisites newer than target
$+ All prerequisites (GNU)
$^ All prerequisites, duplicates removed (GNU)
...
17
Makefiles : exercise
18
Makefiles : exercise
Exercise (15min) 10pts :
Write the world's famous helloworld.c source file
Write its associated Makefile to generate helloworld executable
Do not rely on predefined inference rules
Use an intermediate object file (.o)
Bonus
Write the "clean" rule (1pt)
Use a "template" makefile defining everything in macros (2pt)
Write your own inference rule (2pt)
Display how many times your project has been built (5pt)
19
Autotools
20
Autotools : basics & examples
Purpose :
Check dependencies
Manage options/conditionals
Compile on any unix-like platform
Standardize makefile rules, options definitions …
Manage out of source builds
21
Autotools : basics & examples
Global overview :
22
Autotools : configure.ac
23
Autotools : configure.ac
Example "configure.ac" :
The project description file
24
Autotools : configure.ac
Variables, defines and options :
25
Autotools : configure.ac
Tests and checks (1/2) :
26
Autotools : configure.ac
Tests and checks (2/2) :
27
Autotools : Makefile.am
28
Autotools : Makefile.am
Example "Makefile.am" :
The simplified Makefile
29
Autotools : Makefile.am
Variables :
One "primary" that will be recognized by the tool
Primaries :
Most used primaries are PROGRAMS, LIBRARIES, LTLIBRARIES, DATA,
HEADERS, SCRIPTS, MANS ...
30
Autotools : Makefile.am
Building a program :
Primary : PROGRAMS
31
Autotools : Makefile.am
Building a static library :
Primary : LIBRARIES
32
Autotools : Makefile.am
Building a libtool library :
Will be either static or dynamic depending on destination and platform
Primary : LTLIBRARIES
33
Autotools : Makefile.am
Installing headers :
Primary : HEADERS
Installing scripts :
Scripts are executables that doesn't need to be compiled
Primary : SCRIPTS
34
Autotools : Makefile.am
Installing data :
Primary : DATA
Custom destination :
Custom destinations prefixes can be created by filling-in variables
35
Autotools : Makefile.am
Subdirectories :
Subdirectories are recursed in depth-first mode (entered before current
directory is parsed)
Conditionals :
Conditionals are declared in configure.ac using AM_CONDITIONAL
36
Autotools : pkg-config
37
Autotools : pkg-config
Purpose :
Standardized module description
Contents
Name, description
Version information
38
Autotools : usage
39
Autotools : usage
40
Autotools : usage
Enabling/disabling options :
Compiling :
41
CMake
42
CMake : basics & examples
Requirements :
A working CMake (>= 3.17) and compiler installation
43
CMake : basics & examples
Recommendations :
No internet connection is required, referring to online documentation
(or stackoverflow ...) is strongly discouraged
44
CMake : basics & examples
CMake is a Makefile generator that supports:
UNIX, Borland, MSYS, MinGW, NMake, Ninja, Watcom Makefiles
And more:
Package bundling, installers, unit-tests …
45
CMake : basics & examples
46
CMake : basics & examples
Generating Makefiles :
Compiling :
☟ 48
CMake : basics & examples
☞ hands-on !
49
CMake : usage ☞ hands-on !
Create your first CMake project
write an helloworld.c (1pt)
Hints :
commands: cmake_minimum_required, project, add_executable (use
cmake --help-command <cmd>)
Note: This step is a base for all incoming hands-on exercise (mandatory !)
50
CMake : usage ☞ hands-on !
(Possible) Solution
Shell commands
helloworld.c
CMakelists.txt
51
CMake : syntax
52
CMake : syntax & purpose
53
CMake : syntax & purpose
54
CMake : variables & cache
55
CMake : variables & cache
To set/unset a variable :
To use a variable :
56
CMake : variables & cache
Variables are directory scoped by default
Modules inclusion (“include” function) doesn’t create a new scope
57
CMake : variables & cache
A global cache can also be used
Makes variables persistent and global
Located in ${CMAKE_BINARY_DIR}/CMakeCache.txt
58
CMake : variables & cache
Default built-in variables :
Complete list in man (7) cmake-variables
59
CMake : variables & cache
60
CMake : variables & cache
Advice :
do always quote a string when expanding a variable
☟ 62
CMake : variables & cache
☞ hands-on !
63
CMake : variables & cache ☞ hands-on !
Be organized ! (1pt)
Set your sources list in a variable and use it
Be nice ! (2pt)
Display "Building <project_name>" on CMake invocation
Be pedantic ! (5pt)
Add "-pedantic" to your C_FLAGS (mind the _)
64
CMake : usage ☞ hands-on !
(Possible) Solution
CMakelists.txt
Shell commands
65
CMake : conditionals & loops
66
CMake : conditionals & loops
CMake has some constants evaluation rules:
False : 0, OFF, NO, FALSE, N, IGNORE, NOTFOUND, empty string, anything
that ends with –NOTFOUND.
67
CMake : conditionals & loops
Built-in conditional keywords:
Usual keywords are available: NOT, AND, OR
68
CMake : conditionals & loops
69
CMake : conditionals & loops
70
CMake : conditionals & loops
☟ 72
CMake : conditionals & loops
☞ hands-on !
73
CMake : ☞ hands-on !
If there's a ".git" directory in your source dir (2pt)
display a message
74
CMake : usage ☞ hands-on !
(Possible) Solution
CMakelists.txt
75
CMake : functions & macros
76
CMake : functions & macros
Functions creates a new scope, macros don't !
Note: macro arguments are not real variables (specific rules apply)
77
CMake : functions & macros
A CMake built-in function exists to parse arguments
Getopt like
78
CMake : functions & macros
Common usage :
optional arguments uses keywords <args...> syntax
79
CMake : functions & macros
Builtin commands (functions or macros) :
Complete list in man (7) cmake-commands
☟ 81
CMake : functions & macros
☞ hands-on !
82
CMake : ☞ hands-on !
Write a function that executes "date" command (3pt)
It must store its result in a variable (1pt)
Variable name is an argument (2pt)
Do it again with a macro !
Hints:
cmake --help-command function
83
CMake : ☞ hands-on !
(Possible) Solution
Note: command is ran at
CMake execution time, not
when calling make
CMakelists.txt
84
CMake : targets
85
CMake : targets
Targets are used to declare build-time actions
Similar to Makefile rules : executed when calling make
Or libraries :
86
CMake : targets
87
CMake : targets
88
CMake : targets
89
CMake : targets
☟ 91
CMake : targets ☞ hands-on !
92
CMake : targets ☞ hands-on !
Install your helloworld binary (2pt)
Use an intermediate static library (2pt)
Split your program in 3 files (helloworld.c/.h main.c)
Hints :
commands: install, add_library, target_link_libraries, add_custom_target
(use cmake --help-command <cmd>)
93
CMake : usage ☞ hands-on !
(Possible) Solution
helloworld.h
CMakelists.txt
helloworld.c
94
CMake : properties
95
CMake : properties
Properties are "variables" (kind of)
bound to a target, file or directory (or global)
can be accessed with set_property/get_property
or dedicated versions set_target_properties/get_target_property ...
Most built-in commands manipulate properties (link_libraries,
include_directories ...)
☟ 97
CMake : properties
☞ hands-on !
98
CMake : properties ☞ hands-on !
Set your executable sources using properties (3pt)
Hints :
properties : SOURCES (use cmake --help-property <prop>)
lists are ";" separated strings in CMake (if you have more than a file)
99
CMake : usage ☞ hands-on !
(Possible) Solution
CMakelists.txt
No worries, properties are really advanced topic, one can use CMake without
being a property expert !
100
CMake : modules
101
CMake : modules
Modules are libraries of functions
102
CMake : modules
103
CMake : modules
104
CMake : modules
CMake script mode (-P)
Using CMake as a build tool can be really convenient
105
CMake : modules
More elaborate example :
Generating a git badge (with external dependency on shields.io)
☟
107
CMake : modules ☞ hands-on !
Check for "-march=native" compiler flag support (1pt)
and add it to your CFLAGS
Hints :
module: CheckCCompilerFlag (use cmake --help-module <module>)
108
CMake : modules ☞ hands-on !
(Possible) Solution
cmake/GenHeader.cmake
helloworld.c
CMakelists.txt 109
CMake : dependencies
110
CMake : dependencies
Finding libraries and headers :
Using it :
111
CMake : dependencies
Dependencies for CMake heroes !
CMake >= 3 recommended way
Using it :
112
CMake : dependencies
113
CMake : dependencies
Finding CMake pre-bundled dependencies
Uses Find* modules
☟ 115
CMake : dependencies ☞ hands-on !
Write your very own FindPWET module
must find curl.h header
Hints :
module: FindPackageHandleStandardArgs (use cmake --help-module
<module>)
commands: find_path, find_library, include (use cmake --help-command
<cmd>)
116
CMake : dependencies ☞ hands-on !
(Possible) Solution
CMakelists.txt
cmake/FindPWET.cmake
117
CMake : CTest & CPack
118
CMake : CTest & CPack
119
References
120
References
Makefile references :
Name Link
POSIX spec http://pubs.opengroup.org/onlinepubs/009695399/utiliti
es/make.html
GNU Make http://www.gnu.org/software/make/manual/make.html
BSD Make http://www.khmere.com/freebsd_book/html/ch01.html
Microsoft NMake http://msdn.microsoft.com/en-us/library/dd9y37ha.aspx
121
References
Autotools references :
Name Link
Automake doc https://www.gnu.org/software/automake/manual/automake.html
Autoconf doc http://www.gnu.org/software/autoconf/manual/autoconf.html
General overview http://devmanual.gentoo.org/general-concepts/autotools/
122
References
CMake references :
Name Link
Man pagews cmake (1), cmake-commands (7), cmake-modules (7) ...
CMake doc https://cmake.org/documentation/
123