| 1 |
TITLE: GNU Autotools |
|---|
| 2 |
AUTHOR: Elko Holl <elko@home.nl> <elko@cyberspace.org> |
|---|
| 3 |
DATE: 2003-09-16 |
|---|
| 4 |
LICENSE: GNU Free Documentation License Version 1.2 |
|---|
| 5 |
SYNOPSIS: Introduction to the GNU Autotools. |
|---|
| 6 |
|
|---|
| 7 |
DESCRIPTION: |
|---|
| 8 |
This document describes the steps you must take to start a project |
|---|
| 9 |
in GNU fashion. You'll learn to use autoconf and automake to create |
|---|
| 10 |
portable configure scripts and Makefiles. |
|---|
| 11 |
|
|---|
| 12 |
PREREQUISITES: |
|---|
| 13 |
This hint requires that you have a little general knowledge of programming. |
|---|
| 14 |
|
|---|
| 15 |
HINT: |
|---|
| 16 |
|
|---|
| 17 |
$Id: autotools.txt,v 1.3 2003/09/16 19:10:55 tushar Exp $ |
|---|
| 18 |
|
|---|
| 19 |
Contents |
|---|
| 20 |
-------- |
|---|
| 21 |
|
|---|
| 22 |
* Preface |
|---|
| 23 |
* Versions |
|---|
| 24 |
* Creating the source file(s) |
|---|
| 25 |
* Adapting configure.in |
|---|
| 26 |
* Creating config.h.in |
|---|
| 27 |
* Creating Makefile.am and Makefile.in |
|---|
| 28 |
* Creating the configure script |
|---|
| 29 |
* Testing the result |
|---|
| 30 |
* Making a distribution |
|---|
| 31 |
* Related documents and links |
|---|
| 32 |
* Suggestions |
|---|
| 33 |
|
|---|
| 34 |
Preface |
|---|
| 35 |
------- |
|---|
| 36 |
|
|---|
| 37 |
Ok, so you have your BLFS finished and have all the applications you can |
|---|
| 38 |
think of installed. Now what? You start to learn BaSH, Perl, C and kernel |
|---|
| 39 |
internals and finally, you code up some cool program which you think is |
|---|
| 40 |
worth uploading to SourceForge or Freshmeat for example. |
|---|
| 41 |
|
|---|
| 42 |
But how are you going to distribute your program? Just pack everything in |
|---|
| 43 |
a tarball and present a Makefile to your users that they have to modify? NO! |
|---|
| 44 |
You want your project to be like all the packages you already installed, so |
|---|
| 45 |
that means having a configure script so you can at least specify the |
|---|
| 46 |
installation --prefix for your program. |
|---|
| 47 |
|
|---|
| 48 |
With the GNU range of applications this means using autoconf and automake, |
|---|
| 49 |
rather then coding the required files yourself; these programs are part of |
|---|
| 50 |
the GNU Autotools collection. |
|---|
| 51 |
|
|---|
| 52 |
Here is a quote from the autoconf manual: |
|---|
| 53 |
|
|---|
| 54 |
"Autoconf is a tool for producing shell scripts that automatically configure |
|---|
| 55 |
software source code packages to adapt to many kinds of UNIX-like systems. |
|---|
| 56 |
The configuration scripts produced by Autoconf are independent of Autoconf |
|---|
| 57 |
when they are run, so their users do not need to have Autoconf." |
|---|
| 58 |
|
|---|
| 59 |
For more information on autoconf and automake skip to the section at the |
|---|
| 60 |
bottom titled "Related documents". |
|---|
| 61 |
|
|---|
| 62 |
It is assumed that you already know a bit about writing a Makefile. If this |
|---|
| 63 |
is not the case, then you can use this Make manual (if needed) while reading: |
|---|
| 64 |
|
|---|
| 65 |
http://www.gnu.org/manual/make/ |
|---|
| 66 |
|
|---|
| 67 |
Note: You can use Makefiles for more then C-program compilation, you can for |
|---|
| 68 |
example create targets for commonly used functions (shell-scripts). If |
|---|
| 69 |
this doesn't ring a bell right now, read the Make manual and it will |
|---|
| 70 |
start to make sense (i.e. `make backup' for your system operators). |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
Versions |
|---|
| 74 |
-------- |
|---|
| 75 |
|
|---|
| 76 |
The versions of autoconf and automake used in this document are: |
|---|
| 77 |
|
|---|
| 78 |
[elko@elkos ~]$ (autoconf -V;automake --version) 2>&1 | grep "^auto" |
|---|
| 79 |
autoconf (GNU Autoconf) 2.52 |
|---|
| 80 |
automake (GNU automake) 1.6.1 |
|---|
| 81 |
|
|---|
| 82 |
Sometimes, newer versions of autoconf and automake may cause some not so |
|---|
| 83 |
up-to-date applications to fail to compile on your system. You can always |
|---|
| 84 |
downgrade autoconf or automake again; so if you experience any problems |
|---|
| 85 |
building certain packages after upgrading to the versions used in this |
|---|
| 86 |
document, try to downgrade first before complaining somewhere. |
|---|
| 87 |
|
|---|
| 88 |
If you are happy with your autoconf and automake release, and don't want to |
|---|
| 89 |
upgrade, then this document can still be used as a quick guide to start a |
|---|
| 90 |
project; some of the semantics may differ though, consult the documentation |
|---|
| 91 |
of your release for the details. |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
Creating the source file(s) |
|---|
| 95 |
--------------------------- |
|---|
| 96 |
|
|---|
| 97 |
This document only uses one source file, since it's just a quick guide to |
|---|
| 98 |
start a GNU fashion project. In almost any situation, your project will |
|---|
| 99 |
have more then one source file. That is why it is wishful that you know how |
|---|
| 100 |
to write Makefiles, since more sourcefiles mean more described dependencies |
|---|
| 101 |
in your 'Makefile.am'; more on that later. Read along or skip to the section |
|---|
| 102 |
called "Related documents" (at the bottom) and get your information there. |
|---|
| 103 |
|
|---|
| 104 |
First, create a directory where you start your project and create the famous |
|---|
| 105 |
"Hello World!" source-file (a slightly altered version though): |
|---|
| 106 |
|
|---|
| 107 |
cd $HOME && mkdir hello && cd hello && |
|---|
| 108 |
cat >hello.c <<EOHF |
|---|
| 109 |
/* |
|---|
| 110 |
* hello.c example for the autotools.txt hint |
|---|
| 111 |
* |
|---|
| 112 |
*/ |
|---|
| 113 |
|
|---|
| 114 |
#ifdef HAVE_CONFIG_H |
|---|
| 115 |
#include <config.h> |
|---|
| 116 |
#endif |
|---|
| 117 |
|
|---|
| 118 |
#include <stdio.h> |
|---|
| 119 |
#include <unistd.h> |
|---|
| 120 |
|
|---|
| 121 |
int main() |
|---|
| 122 |
{ |
|---|
| 123 |
fprintf (stdout, "Hello World!\n"); |
|---|
| 124 |
|
|---|
| 125 |
#ifdef _WITH_GOODBYE |
|---|
| 126 |
sleep (1); |
|---|
| 127 |
fprintf (stdout, "Goodbye Cruel World!\n"); |
|---|
| 128 |
#endif |
|---|
| 129 |
|
|---|
| 130 |
return (0); |
|---|
| 131 |
} |
|---|
| 132 |
EOHF |
|---|
| 133 |
|
|---|
| 134 |
Note that there are some header-files included and there is a symbol |
|---|
| 135 |
definition check present to change the behavior of the program. This is |
|---|
| 136 |
done on purpose to show some details of the Autotools; almost every project |
|---|
| 137 |
you create will have conditionals in the source to enhance or alter the |
|---|
| 138 |
behavior of your software. The #ifdef and #ifndef statements play an |
|---|
| 139 |
important part in your flexibility with the GNU Autotools. |
|---|
| 140 |
|
|---|
| 141 |
The next step is to create the 'config.h' file, which autoscan uses to |
|---|
| 142 |
create the input file for autoheader: |
|---|
| 143 |
|
|---|
| 144 |
cat >config.h <<EOHF |
|---|
| 145 |
#define VERSION=1.0 |
|---|
| 146 |
EOHF |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
Adapting configure.in |
|---|
| 150 |
--------------------- |
|---|
| 151 |
|
|---|
| 152 |
Now that you have your source-file(s) in place, you have to create a file |
|---|
| 153 |
for autoconf - which describes your project - called 'configure.in'. |
|---|
| 154 |
To generate a template for this file, you can use `autoscan', which will |
|---|
| 155 |
create a file named 'configure.scan'; rename that file to 'configure.in': |
|---|
| 156 |
|
|---|
| 157 |
autoscan && |
|---|
| 158 |
mv configure.scan configure.in |
|---|
| 159 |
|
|---|
| 160 |
You have to adapt 'configure.in' for your project now. In this example, |
|---|
| 161 |
it is modified as follows (some blank lines removed): |
|---|
| 162 |
|
|---|
| 163 |
-[snip]- |
|---|
| 164 |
# Process this file with autoconf to produce a configure script. |
|---|
| 165 |
# - Change program presets |
|---|
| 166 |
AC_INIT(hello, 1.0, elko@home.nl) |
|---|
| 167 |
AC_CONFIG_SRCDIR([hello.c]) |
|---|
| 168 |
# - Change AC to AM (automake version) |
|---|
| 169 |
AM_CONFIG_HEADER([config.h]) |
|---|
| 170 |
|
|---|
| 171 |
# - Add this line for a bzip2 dist |
|---|
| 172 |
AM_INIT_AUTOMAKE(dist-bzip2) |
|---|
| 173 |
|
|---|
| 174 |
# - The following lines adds the --enable-goodbye option to configure: |
|---|
| 175 |
# |
|---|
| 176 |
# Give the user the choice to enter one of these: |
|---|
| 177 |
# --enable-goodbye |
|---|
| 178 |
# --enable-goodbye=yes |
|---|
| 179 |
# --enable-goodbye=no |
|---|
| 180 |
# |
|---|
| 181 |
AC_MSG_CHECKING([whether we are enabling goodbye]) |
|---|
| 182 |
AC_ARG_ENABLE(goodbye, |
|---|
| 183 |
AC_HELP_STRING([--enable-goodbye], [Say goodbye as well]), |
|---|
| 184 |
[if test "${enable_goodbye}" = "yes" ; then |
|---|
| 185 |
AC_DEFINE(_WITH_GOODBYE, 1, Say goodbye as well) |
|---|
| 186 |
AC_MSG_RESULT([yes]) |
|---|
| 187 |
else |
|---|
| 188 |
AC_DEFINE(_WITH_GOODBYE, 0, Say goodbye as well) |
|---|
| 189 |
AC_MSG_RESULT([no]) |
|---|
| 190 |
fi], |
|---|
| 191 |
# Default value for configure |
|---|
| 192 |
AC_MSG_RESULT([no]) |
|---|
| 193 |
) |
|---|
| 194 |
|
|---|
| 195 |
# Checks for programs. |
|---|
| 196 |
AC_PROG_CC |
|---|
| 197 |
# Checks for libraries. |
|---|
| 198 |
# Checks for header files. |
|---|
| 199 |
|
|---|
| 200 |
# Automatically added by autoscan |
|---|
| 201 |
AC_CHECK_HEADERS([unistd.h]) |
|---|
| 202 |
|
|---|
| 203 |
# - The following line demonstrates checking for header files yourself: |
|---|
| 204 |
# |
|---|
| 205 |
# do nothing if stdio.h is found, else print an error |
|---|
| 206 |
AC_CHECK_HEADER(stdio.h, , AC_MSG_ERROR([stdio.h not found!])) |
|---|
| 207 |
|
|---|
| 208 |
# Checks for typedefs, structures, and compiler characteristics. |
|---|
| 209 |
# Checks for library functions. |
|---|
| 210 |
|
|---|
| 211 |
# - Add Makefile |
|---|
| 212 |
AC_CONFIG_FILES([Makefile]) |
|---|
| 213 |
AC_OUTPUT |
|---|
| 214 |
-[snip]- |
|---|
| 215 |
|
|---|
| 216 |
The 'AM_INIT_AUTOMAKE' is specified because I wish to have a make target |
|---|
| 217 |
called 'dist-bzip2', which makes a bzipped tarball from my development tree. |
|---|
| 218 |
AC in the AC_CONFIG_HEADER is changed to AM because the version of automake |
|---|
| 219 |
used in this document prefers it over the AC prefix. |
|---|
| 220 |
|
|---|
| 221 |
For other options you can specify in the 'configure.in' file, skip to the |
|---|
| 222 |
section "Related documents" at the bottom of this document. |
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
Creating aclocal.m4 |
|---|
| 226 |
------------------- |
|---|
| 227 |
|
|---|
| 228 |
In order for autoconf and automake to recognize and translate defined |
|---|
| 229 |
macro's, you have to run `aclocal', which generates the 'aclocal.m4' |
|---|
| 230 |
macro-file: |
|---|
| 231 |
|
|---|
| 232 |
aclocal |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
Creating config.h.in |
|---|
| 236 |
-------------------- |
|---|
| 237 |
|
|---|
| 238 |
This file is required by automake because you created a 'config.h' file, |
|---|
| 239 |
so just run `autoheader' and your done: |
|---|
| 240 |
|
|---|
| 241 |
autoheader |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
Creating Makefile.am and Makefile.in |
|---|
| 245 |
------------------------------------ |
|---|
| 246 |
|
|---|
| 247 |
Now you need a way to specify the rules which make must follow. The syntax |
|---|
| 248 |
of a 'Makefile.am' (AutoMake) almost resembles that of an ordinary Makefile, |
|---|
| 249 |
in this example, you create the 'Makefile.am' like this: |
|---|
| 250 |
|
|---|
| 251 |
cat >Makefile.am 2>/dev/null <<EOHF |
|---|
| 252 |
bin_PROGRAMS = hello |
|---|
| 253 |
|
|---|
| 254 |
CC = @CC@ |
|---|
| 255 |
program: |
|---|
| 256 |
$(CC) -o hello hello.c # <-- this line starts with a TAB! |
|---|
| 257 |
# |
|---|
| 258 |
EOHF |
|---|
| 259 |
|
|---|
| 260 |
The 'Makefile.am' file is used to generate a 'Makefile.in', that is used by |
|---|
| 261 |
the configure script, which enables the user of your package to specify |
|---|
| 262 |
system specifics that will be reflected in the final (real) Makefile. |
|---|
| 263 |
|
|---|
| 264 |
Once you have 'Makefile.am', you can run `automake' to create 'Makefile.in'. |
|---|
| 265 |
If you do so at this moment however, it will complain about missing files, |
|---|
| 266 |
which are normally part of a standard GNU package. These files are: |
|---|
| 267 |
|
|---|
| 268 |
install-sh, mkinstalldirs, missing, ChangeLog, depcomp, |
|---|
| 269 |
INSTALL, NEWS, README, COPYING, AUTHORS. |
|---|
| 270 |
|
|---|
| 271 |
However, automake provides an option to add those missing files |
|---|
| 272 |
(in case they are found on your system) if you add the -a flag |
|---|
| 273 |
to automake (short for --add-missing). So let's do that: |
|---|
| 274 |
|
|---|
| 275 |
automake --add-missing |
|---|
| 276 |
|
|---|
| 277 |
The output of this command looks something like: |
|---|
| 278 |
|
|---|
| 279 |
configure.in: installing `./install-sh' |
|---|
| 280 |
configure.in: installing `./mkinstalldirs' |
|---|
| 281 |
configure.in: installing `./missing' |
|---|
| 282 |
Makefile.am: installing `./INSTALL' |
|---|
| 283 |
Makefile.am: required file `./NEWS' not found |
|---|
| 284 |
Makefile.am: required file `./README' not found |
|---|
| 285 |
Makefile.am: installing `./COPYING' |
|---|
| 286 |
Makefile.am: required file `./AUTHORS' not found |
|---|
| 287 |
Makefile.am: required file `./ChangeLog' not found |
|---|
| 288 |
Makefile.am: installing `./depcomp' |
|---|
| 289 |
|
|---|
| 290 |
Some symbolic links will be created in your project directory, pointing |
|---|
| 291 |
to the various locations where the files are found. As you can see, |
|---|
| 292 |
some files are still missing: NEWS, README, AUTHORS and ChangeLog. |
|---|
| 293 |
|
|---|
| 294 |
If you want those files to also be installed when you add the -a flag |
|---|
| 295 |
to automake, create those files in the same place where the symlinks |
|---|
| 296 |
point to. |
|---|
| 297 |
|
|---|
| 298 |
The missing files are just informal ones. It's up to you to decide if |
|---|
| 299 |
you want them, though it isn't a bad idea to follow the GNU convention |
|---|
| 300 |
and execute the following command to create the missing files: |
|---|
| 301 |
|
|---|
| 302 |
touch NEWS README AUTHORS ChangeLog |
|---|
| 303 |
|
|---|
| 304 |
Run `automake' again to verify it isn't complaining anymore: |
|---|
| 305 |
|
|---|
| 306 |
automake |
|---|
| 307 |
|
|---|
| 308 |
In case you are wondering, the symbolic links will be replaced by the |
|---|
| 309 |
programs themselves if you do a `make dist' when you are ready to |
|---|
| 310 |
distribute your project, read along. |
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
Creating the configure script |
|---|
| 314 |
----------------------------- |
|---|
| 315 |
|
|---|
| 316 |
To create the configure script, just run `autoconf' and you're done: |
|---|
| 317 |
|
|---|
| 318 |
autoconf |
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
Testing the result |
|---|
| 322 |
------------------ |
|---|
| 323 |
|
|---|
| 324 |
Before you test the result, it is always a good idea to backup your work: |
|---|
| 325 |
|
|---|
| 326 |
cd .. && |
|---|
| 327 |
cp -a hello hello.ok && |
|---|
| 328 |
cd - |
|---|
| 329 |
|
|---|
| 330 |
Now test if the configure script works as expected; while testing, pay |
|---|
| 331 |
close attention to the output that you get from the configure script, |
|---|
| 332 |
especially the '--enable-goodbye' option and the 'stdio.h' check: |
|---|
| 333 |
|
|---|
| 334 |
./configure --prefix=$HOME/hello-test \ |
|---|
| 335 |
--bindir=$HOME/hello-test && |
|---|
| 336 |
make && |
|---|
| 337 |
make install |
|---|
| 338 |
|
|---|
| 339 |
See if the program works: |
|---|
| 340 |
|
|---|
| 341 |
ls -l ../hello-test && |
|---|
| 342 |
../hello-test/hello |
|---|
| 343 |
|
|---|
| 344 |
Now test if our configure-option gets recognized: |
|---|
| 345 |
|
|---|
| 346 |
./configure --prefix=$HOME/hello-test\ |
|---|
| 347 |
--bindir=$HOME/hello-test \ |
|---|
| 348 |
--enable-goodbye && |
|---|
| 349 |
make && |
|---|
| 350 |
make install |
|---|
| 351 |
|
|---|
| 352 |
And again, see if the program works: |
|---|
| 353 |
|
|---|
| 354 |
./hello && |
|---|
| 355 |
../hello-test/hello |
|---|
| 356 |
|
|---|
| 357 |
If you execute a `make uninstall', you will notice the binary is removed, |
|---|
| 358 |
but the directory is still there; this is a good thing, because if you |
|---|
| 359 |
installed the package in /usr/bin for example, you wouldn't want the |
|---|
| 360 |
uninstall rule to `rm -fr' your entire /usr/bin as well. |
|---|
| 361 |
|
|---|
| 362 |
You could enhance the Makefile to test for an empty directory and then |
|---|
| 363 |
remove it, or just add a `rmdir --i <prefix>', which will quietly fail |
|---|
| 364 |
if the directory is not empty. |
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
Making a distribution |
|---|
| 368 |
--------------------- |
|---|
| 369 |
|
|---|
| 370 |
It is possible to create a tarball from your project by executing: |
|---|
| 371 |
|
|---|
| 372 |
make dist |
|---|
| 373 |
|
|---|
| 374 |
In this example you would end up with a file called "hello-1.0.tar.gz", |
|---|
| 375 |
and a file "hello-1.0.tar.bz2" since the target has dependencies, check |
|---|
| 376 |
what the package contains: |
|---|
| 377 |
|
|---|
| 378 |
tar tvzf hello-1.0.tar.gz |
|---|
| 379 |
|
|---|
| 380 |
If you would only like a bzipped tarball, execute: |
|---|
| 381 |
|
|---|
| 382 |
make dist-bzip2 && |
|---|
| 383 |
ls -l hello-1.0.tar.bz2 && |
|---|
| 384 |
tar tvjf hello-1.0.tar.bz2 |
|---|
| 385 |
|
|---|
| 386 |
Hint: If you install "bash_completion" (available on http://freshmeat.net), |
|---|
| 387 |
then you can get all available make targets by entering 'make ' and |
|---|
| 388 |
pressing TAB twice (notice the space after the `make' command!). With |
|---|
| 389 |
bash_completion, the same is true for `./configure --<TAB><TAB>', |
|---|
| 390 |
which will list the available configure options; very neat indeed! |
|---|
| 391 |
|
|---|
| 392 |
To end the foolishness of making a GNU package of a 326 byte hello.c |
|---|
| 393 |
sourcefile, unpack the distribution you just made and see that it is |
|---|
| 394 |
258048 bytes now; that is ~791.56 times bigger then the original sourcefile: |
|---|
| 395 |
|
|---|
| 396 |
tar xjf hello-1.0.tar.bz2 && |
|---|
| 397 |
du -sb hello-1.0 |
|---|
| 398 |
|
|---|
| 399 |
But it is supposed to be portable now. |
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
Related documents and links |
|---|
| 403 |
--------------------------- |
|---|
| 404 |
|
|---|
| 405 |
For a full description and all the macros's you can use, visit: |
|---|
| 406 |
|
|---|
| 407 |
http://www.gnu.org/manual/make/ |
|---|
| 408 |
http://www.gnu.org/manual/autoconf/ |
|---|
| 409 |
http://www.gnu.org/manual/automake/ |
|---|
| 410 |
|
|---|
| 411 |
For information about installing the Autotools, see Linuxfromscratch: |
|---|
| 412 |
|
|---|
| 413 |
http://www.linuxfromscratch.org/view/cvs/chapter06/make.html |
|---|
| 414 |
http://www.linuxfromscratch.org/view/cvs/chapter06/autoconf.html |
|---|
| 415 |
http://www.linuxfromscratch.org/view/cvs/chapter06/automake.html |
|---|
| 416 |
|
|---|
| 417 |
I recommend reading this as well: |
|---|
| 418 |
|
|---|
| 419 |
http://sources.redhat.com/autobook/autobook/autobook_toc.html |
|---|
| 420 |
|
|---|
| 421 |
Other links to sites mentioned in this document: |
|---|
| 422 |
|
|---|
| 423 |
http://sourceforge.net |
|---|
| 424 |
http://freshmeat.net |
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
Suggestions |
|---|
| 428 |
----------- |
|---|
| 429 |
|
|---|
| 430 |
If you have any questions about, or suggestions for this document, |
|---|
| 431 |
then please contact the author. |
|---|
| 432 |
|
|---|
| 433 |
If this document has been of any use to you or if you are making a |
|---|
| 434 |
translation of it, please drop the author an email, your feedback |
|---|
| 435 |
is very WelkoM. |
|---|
| 436 |
|
|---|
| 437 |
Happy Landings! |
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
CHANGELOG: |
|---|
| 441 |
[2002/07/15] |
|---|
| 442 |
* Initial hint. |
|---|
| 443 |
[2003-09-16] |
|---|
| 444 |
* Just some textual changes for the new format. |
|---|
| 445 |
|
|---|