lfs-uefi: fix efivar-37 FTBFS
[linux_from_scratch_hints.git] / autotools.txt
blob70cbeaaf410d3f6433a8abc4548b21d2d38f46d3
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.
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.
12 PREREQUISITES:
13 This hint requires that you have a little general knowledge of programming.
15 HINT:
17 $Id: autotools.txt,v 1.3 2003/09/16 19:10:55 tushar Exp $
19 Contents
20 --------
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
34 Preface
35 -------
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.
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.
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.
52   Here is a quote from the autoconf manual:
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."
59   For more information on autoconf and automake skip to the section at the
60   bottom titled "Related documents".
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:
65         http://www.gnu.org/manual/make/
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).
73 Versions
74 --------
76   The versions of autoconf and automake used in this document are:
78   [elko@elkos ~]$ (autoconf -V;automake --version) 2>&1 | grep "^auto"
79   autoconf (GNU Autoconf) 2.52
80   automake (GNU automake) 1.6.1
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.
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.
94 Creating the source file(s)
95 ---------------------------
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.
104   First, create a directory where you start your project and create the famous
105   "Hello World!" source-file (a slightly altered version though):
107 cd $HOME && mkdir hello && cd hello &&
108 cat >hello.c <<EOHF
110  * hello.c example for the autotools.txt hint
112  */
114 #ifdef HAVE_CONFIG_H
115 #include <config.h>
116 #endif
118 #include <stdio.h>
119 #include <unistd.h>
121 int main()
123         fprintf (stdout, "Hello World!\n");
125 #ifdef _WITH_GOODBYE
126         sleep (1);
127         fprintf (stdout, "Goodbye Cruel World!\n");
128 #endif
130         return (0);
132 EOHF
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.
141   The next step is to create the 'config.h' file, which autoscan uses to
142   create the input file for autoheader:
144 cat >config.h <<EOHF
145 #define VERSION=1.0
146 EOHF
149 Adapting configure.in
150 ---------------------
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':
157 autoscan &&
158 mv configure.scan configure.in
160   You have to adapt 'configure.in' for your project now. In this example,
161   it is modified as follows (some blank lines removed):
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])
171   # - Add this line for a bzip2 dist
172   AM_INIT_AUTOMAKE(dist-bzip2)
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         )
195   # Checks for programs.
196   AC_PROG_CC
197   # Checks for libraries.
198   # Checks for header files.
200   # Automatically added by autoscan
201   AC_CHECK_HEADERS([unistd.h])
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!]))
208   # Checks for typedefs, structures, and compiler characteristics.
209   # Checks for library functions.
211   # - Add Makefile
212   AC_CONFIG_FILES([Makefile])
213   AC_OUTPUT
214   -[snip]-
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.
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.
225 Creating aclocal.m4
226 -------------------
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:
232 aclocal
235 Creating config.h.in
236 --------------------
238   This file is required by automake because you created a 'config.h' file,
239   so just run `autoheader' and your done:
241 autoheader
244 Creating Makefile.am and Makefile.in
245 ------------------------------------
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:
251 cat >Makefile.am 2>/dev/null <<EOHF
252 bin_PROGRAMS = hello
254 CC = @CC@
255 program:
256         $(CC) -o hello hello.c     # <-- this line starts with a TAB!
258 EOHF
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.
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:
268         install-sh, mkinstalldirs, missing, ChangeLog, depcomp,
269         INSTALL, NEWS, README, COPYING, AUTHORS.
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:
275 automake --add-missing
277   The output of this command looks something like:
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'
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.
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.
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:
302 touch NEWS README AUTHORS ChangeLog
304   Run `automake' again to verify it isn't complaining anymore:
306 automake
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.
313 Creating the configure script
314 -----------------------------
316   To create the configure script, just run `autoconf' and you're done:
318 autoconf
321 Testing the result
322 ------------------
324   Before you test the result, it is always a good idea to backup your work:
326 cd .. &&
327 cp -a hello hello.ok &&
328 cd -
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:
334 ./configure --prefix=$HOME/hello-test \
335         --bindir=$HOME/hello-test &&
336 make &&
337 make install
339   See if the program works:
341 ls -l ../hello-test &&
342 ../hello-test/hello
344   Now test if our configure-option gets recognized:
346 ./configure --prefix=$HOME/hello-test\
347         --bindir=$HOME/hello-test \
348         --enable-goodbye &&
349 make &&
350 make install
352   And again, see if the program works:
354 ./hello &&
355 ../hello-test/hello
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.
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.
367 Making a distribution
368 ---------------------
370   It is possible to create a tarball from your project by executing:
372 make dist
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:
378 tar tvzf hello-1.0.tar.gz
380   If you would only like a bzipped tarball, execute:
382 make dist-bzip2 &&
383 ls -l hello-1.0.tar.bz2 &&
384 tar tvjf hello-1.0.tar.bz2
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!
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:
396 tar xjf hello-1.0.tar.bz2 &&
397 du -sb hello-1.0
399   But it is supposed to be portable now.
402 Related documents and links
403 ---------------------------
405   For a full description and all the macros's you can use, visit:
407         http://www.gnu.org/manual/make/
408         http://www.gnu.org/manual/autoconf/
409         http://www.gnu.org/manual/automake/
411   For information about installing the Autotools, see Linuxfromscratch:
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
417   I recommend reading this as well:
419         http://sources.redhat.com/autobook/autobook/autobook_toc.html
421   Other links to sites mentioned in this document:
423         http://sourceforge.net
424         http://freshmeat.net
427 Suggestions
428 -----------
430   If you have any questions about, or suggestions for this document,
431   then please contact the author.
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.
437   Happy Landings!
440 CHANGELOG:
441 [2002/07/15]
442   * Initial hint.
443 [2003-09-16]
444   * Just some textual changes for the new format.