-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example_with_explanation
161 lines (132 loc) · 6.58 KB
/
Example_with_explanation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
A Small Hello World
---------------------
Create the following files in an empty directory
• src/main.c is the source file for the hello program
]$ mkdir learn
]$ cd learn
]$ mkdir src
]$ cd src
]$ cat main.c
#include <config.h>
#include <stdio.h>
int main (void) {
puts ("Hello World!");
return 0;
}
]$ cd ..
• README contains some very limited documentation for our little package
]$ cat README
This is a demonstration package for GNU Automake.
Type ’info Automake’ to read the Automake manual.
• Makefile.am and src/Makefile.am contain Automake instructions for these two directories.
]$ cat src/Makefile.am
bin_PROGRAMS = hello
hello_SOURCES = main.c
]$ cat Makefile.am
SUBDIRS = src
dist_doc_DATA = README
• Finally, configure.ac contains Autoconf instructions to create the configure script.
]$ cat configure.ac
AC_INIT([to_learn], [1.0], [bug-automake@gnu.org])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AC_PROG_CC
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([
Makefile
src/Makefile
])
AC_OUTPUT
Once you have these five files, it is time to run the Autotools to instantiate the build
system.
]$ autoreconf --install
configure.ac: installing ’./install-sh’
configure.ac: installing ’./missing’
configure.ac: installing ’./compile’
src/Makefile.am: installing ’./depcomp’
In addition to the three scripts mentioned in its output, you can see that autoreconf created four other files:
configure, config.h.in, Makefile.in, and src/Makefile.in.
The latter three files are templates that will be adapted to the system by configure under the names config.h,
Makefile, and src/Makefile
]$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
....
...
checking dependency style of gcc... gcc3
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating config.h
config.status: executing depfiles commands
]$
You can see Makefile, src/Makefile, and config.h being created at the end after configure has probed the system.
It is now possible to run all the targets we wish.
]$ make
.....
]$ src/hello
Hello World!
]$ make distcheck
=============================================
to_learn-1.0 archives ready for distribution:
to_learn-1.0.tar.gz
=============================================
Note that running autoreconf is only needed initially when the GNU Build System does not exist. When you later change some
instructions in a Makefile.am or configure.ac, the relevant part of the build system will be regenerated automatically when
you execute make.
autoreconf is a script that calls autoconf, automake , and a bunch of other commands in the right order. If you are beginning
with these tools, it is not important to figure out in which order all of these tools should be invoked and why. However,
because Autoconf and Automake have separate manuals, the important point to understand is that autoconf is in charge of
creating configure from configure.ac , while automake is in charge of creating Makefile.ins from Makefile.ams and configure.ac.
configure.ac Setup Explained
--------------------------------
AC_INIT([to_learn], [1.0], [bug-automake@gnu.org])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AC_PROG_CC
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([
Makefile
src/Makefile
])
AC_OUTPUT
This file is read by both autoconf (to create configure) and automake (to create the various Makefile.in s). It contains a
series of M4 macros that will be expanded as shell code to finally form the configure script.
AC_ are Autoconf macros
AM_are Automake macros
The first two lines of configure.ac initialize Autoconf and Automake.
The foreign option tells Automake that this package will not follow the GNU Standards. GNU packages should always distribute
additional files such as ChangeLog ,AUTHORS, etc.
The AC_PROG_CC line causes the configure script to search for a C compiler and define the variable CC with its name.
The src/Makefile.in file generated by Automake uses the variable CC to build hello , so when configure creates src/Makefile
from src/Makefile.in, it will define CC with the value it has found. If Automake is asked to create a Makefile.in that uses
CC but configure.ac does not define it, it will suggest you add a call to AC_PROG_CC.
The AC_CONFIG_HEADERS([config.h]) invocation causes the configure script to create a config.h file gathering ‘#define’s defined by other macros in
configure.ac . In our case, the AC_INIT macro already defined a few of them. Here is an excerpt of config.h after configure
has run:
...
/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT "bug-automake@gnu.org"
/* Define to the full name and version of this package. */
#define PACKAGE_STRING "to_lean 1.0"
The AC_CONFIG_FILES macro declares the list of files that configure should create from their *.in templates.
AC_OUTPUT line is a closing command that actually produces the part of the script in charge of creating the files registered
with AC_CONFIG_HEADERS and AC_CONFIG_FILES.
Makefile.am Setup Explained
---------------------------------
bin_PROGRAMS = hello
hello_SOURCES = main.c
When automake processes a Makefile.am it copies the entire file into the output Makefile.in(that will be later turned
into Makefile by configure)
Variables that end with _PROGRAMS are special variables that list programs that the resulting Makefile should build.
_SCRIPTS,_DATA,_LIBRARIES are some other available variables.
The ‘bin’ part of the bin_PROGRAMS tells automake that the resulting programs should be installed in bindir.
Programs need to be built from source files, so for each program prog listed in a _PROGRAMS variable,automake
will look for another variable named prog _SOURCES list-ing its source files. There may be more than one source file
they will all be compiled and linked together.
top-level Makefile.am
SUBDIRS = src
dist_doc_DATA = README
SUBDIRS is a special variable listing all directories that make should recurse into before processing the current directory.
The line dist_doc_DATA = README causes README to be distributed and installed in docdir. Files listed with the
_DATA primary are not automatically part of the tarball built with make dist , so we add the dist_ prefix so they get
distributed. However, for README it would not have been necessary: automake automatically distributes any README file it
encounters (the list of other files automatically distributed is presented by automake --help ).
The only important effect of this second line is therefore to install README during make install.