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