Quantcast
Channel: Kurt's Weblog
Viewing all articles
Browse latest Browse all 108

A simple autoconf example

$
0
0
In going through autoconf setup for mb-system, I have started feeling much better about autoconf. I think that one of the main things missing are a nice collection of simple examples. I wanted to see what it looked like to setup an example of the bare minimum to test byte order. Here is what I did.

First, create a file called configure.ac (formerly known as configure.in):
AC_PREREQ([2.65])

AC_INIT([endian],[1.0])

AM_INIT_AUTOMAKE
dnl AM_MAINTAINER_MODE
AM_PROG_CC_C_O

AC_C_BIGENDIAN

dnl AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile])

AC_OUTPUT
Note that "dnl" is the start of comment designation (aka Do Not Load).

This basically says that we have an autoconf 2.65 based configuration for a project called "endian". We want to use automake to build C programs and that we need to know the endian-ness of the machine.

Next, I need an automake input file, Makefile.am, that says how to build our endian.c code.
bin_PROGRAMS = endian
endian_SOURCES = endian.c
And now for the source code:
#include <stdio.h>

int main(void) {
#ifdef WORDS_BIGENDIAN
  printf("big\n");
  return 1;
#else
  printf("little\n");
  return 0;
#endif
}
To set this up, create an autogen.sh script:
#!/bin/bash

autoreconf --force --install
We are now ready to try building our project.
chmod +x autogen.sh
ls -l
total 32
-rw-r--r--  1 schwehr  5000   48 Jun 29 23:04 Makefile.am
-rwxr-xr-x  1 schwehr  5000  102 Jun 30 07:50 autogen.sh
-rw-r--r--  1 schwehr  5000  226 Jun 30 07:51 configure.ac
-rw-r--r--  1 schwehr  5000  140 Jun 29 23:23 endian.c
./autogen.sh
configure.ac:8: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './INSTALL'
Makefile.am: error: required file './NEWS' not found
Makefile.am: error: required file './README' not found
Makefile.am: error: required file './AUTHORS' not found
Makefile.am: error: required file './ChangeLog' not found
Makefile.am: installing './COPYING' using GNU General Public License v3 file
Makefile.am:     Consider adding the COPYING file to the version control system
Makefile.am:     for your code, to avoid questions about which license your project uses
Makefile.am: installing './depcomp'
autoreconf: automake failed with exit status: 1
touch AUTHORS ChangeLog NEWS README
./autogen.sh
# No output
ls -l
total 696
-rw-r--r--  1 schwehr  5000       0 Jun 30 07:54 AUTHORS
-rw-r--r--  1 schwehr  5000   35147 Jun 30 07:53 COPYING
-rw-r--r--  1 schwehr  5000       0 Jun 30 07:54 ChangeLog
-rw-r--r--  1 schwehr  5000   15749 Jun 30 07:55 INSTALL
-rw-r--r--  1 schwehr  5000      48 Jun 29 23:04 Makefile.am
-rw-r--r--  1 schwehr  5000   20989 Jun 30 07:55 Makefile.in
-rw-r--r--  1 schwehr  5000       0 Jun 30 07:54 NEWS
-rw-r--r--  1 schwehr  5000       0 Jun 30 07:54 README
-rw-r--r--  1 schwehr  5000   36026 Jun 30 07:55 aclocal.m4
-rwxr-xr-x  1 schwehr  5000     106 Jun 30 07:55 autogen.sh
drwxr-xr-x  7 schwehr  5000     238 Jun 30 07:55 autom4te.cache
-rwxr-xr-x  1 schwehr  5000    7333 Jun 30 07:55 compile
-rwxr-xr-x  1 schwehr  5000  160168 Jun 30 07:55 configure
-rw-r--r--  1 schwehr  5000     226 Jun 30 07:51 configure.ac
-rwxr-xr-x  1 schwehr  5000   23910 Jun 30 07:55 depcomp
-rw-r--r--  1 schwehr  5000     140 Jun 29 23:23 endian.c
-rwxr-xr-x  1 schwehr  5000   13997 Jun 30 07:55 install-sh
-rwxr-xr-x  1 schwehr  5000   10179 Jun 30 07:55 missing
Now we have a world that will hopefully work. We need to try it out.
./configure --help
`configure' configures endian 1.0 to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
...
  CPP         C preprocessor

Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.

Report bugs to the package provider.

./configure --prefix=/tmp/endian
checking for a BSD-compatible install... /sw/bin/ginstall -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /sw/bin/gmkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for style of include used by make... GNU
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking dependency style of gcc... gcc3
checking whether gcc and cc understand -c and -o together... yes
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking whether byte ordering is bigendian... no
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: executing depfiles commands

ls -l config.{status,log} Makefile
-rw-r--r--  1 schwehr  5000  21248 Jun 30 07:59 Makefile
-rw-r--r--  1 schwehr  5000  14512 Jun 30 07:59 config.log
-rwxr-xr-x  1 schwehr  5000  29185 Jun 30 07:59 config.status

make
gcc -DPACKAGE_NAME=\"endian\" -DPACKAGE_TARNAME=\"endian\" \
-DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"endian\ 1.0\" \
-DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"endian\" \
-DVERSION=\"1.0\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 \
-DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 \
-DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 \
-DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -I.     \
-g -O2 -MT endian.o -MD -MP -MF .deps/endian.Tpo -c -o endian.o endian.c
mv -f .deps/endian.Tpo .deps/endian.Po
gcc  -g -O2   -o endian endian.o

ls -l endian.o endian
-rwxr-xr-x  1 schwehr  5000  9048 Jun 30 08:00 endian
-rw-r--r--  1 schwehr  5000  2328 Jun 30 08:00 endian.o

make install
 /sw/bin/gmkdir -p '/tmp/endian/bin'
  /sw/bin/ginstall -c endian '/tmp/endian/bin'
make[1]: Nothing to be done for `install-data-am'.

ls -la /tmp/endian/bin/
total 24
drwxr-xr-x  3 schwehr  wheel   102 Jun 30 08:01 .
drwxr-xr-x  3 schwehr  wheel   102 Jun 30 08:01 ..
-rwxr-xr-x  1 schwehr  wheel  9048 Jun 30 08:01 endian

/tmp/endian/bin/endian 
little
That's great and it works, but the build line is pretty long. We can use a config.h C include file to track all the defines rather than -D flags in the gcc compile line.

First, add this line to the configure.ac right after AC_CONFIG_FILES:
AC_CONFIG_HEADERS([config.h])
At the top of endian.c, add this include:
#include "config.h"
Then build again:
./autogen.sh
./configure
make 
make  all-am
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT endian.o -MD -MP -MF .deps/endian.Tpo -c -o endian.o endian.c
mv -f .deps/endian.Tpo .deps/endian.Po
gcc  -g -O2   -o endian endian.o
The new config.h file contains this c-pre-processor (cpp) setup:
/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
   significant byte first (like Motorola and SPARC, unlike Intel). */
#if defined AC_APPLE_UNIVERSAL_BUILD
# if defined __BIG_ENDIAN__
#  define WORDS_BIGENDIAN 1
# endif
#else
# ifndef WORDS_BIGENDIAN
/* #  undef WORDS_BIGENDIAN */
# endif
#endif
The final step is removing all the configured files so that only the key sources are left.
make maintainer-clean
test -z "endian" || rm -f endian
rm -f *.o
rm -f *.tab.c
test -z "" || rm -f 
test . = "." || test -z "" || rm -f 
rm -f config.h stamp-h1
rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags
rm -f cscope.out cscope.in.out cscope.po.out cscope.files
This command is intended for maintainers to use
it deletes files that may require special tools to rebuild.
rm -f config.status config.cache config.log configure.lineno config.status.lineno
rm -rf ./autom4te.cache
rm -rf ./.deps
rm -f Makefile

ls -l
total 744
-rw-r--r--  1 schwehr  5000       0 Jun 30 07:54 AUTHORS
-rw-r--r--  1 schwehr  5000   35147 Jun 30 07:53 COPYING
-rw-r--r--  1 schwehr  5000       0 Jun 30 07:54 ChangeLog
-rw-r--r--  1 schwehr  5000   15749 Jun 30 08:18 INSTALL
-rw-r--r--  1 schwehr  5000      48 Jun 29 23:04 Makefile.am
-rw-r--r--  1 schwehr  5000   21594 Jun 30 08:18 Makefile.in
-rw-r--r--  1 schwehr  5000       0 Jun 30 07:54 NEWS
-rw-r--r--  1 schwehr  5000       0 Jun 30 07:54 README
-rw-r--r--  1 schwehr  5000   36026 Jun 30 08:17 aclocal.m4
-rwxr-xr-x  1 schwehr  5000     106 Jun 30 07:55 autogen.sh
-rwxr-xr-x  1 schwehr  5000    7333 Jun 30 08:18 compile
-rw-r--r--  1 schwehr  5000    1947 Jun 30 08:18 config.h.in
-rw-r--r--  1 schwehr  5000    1947 Jun 30 08:08 config.h.in~
-rwxr-xr-x  1 schwehr  5000  164563 Jun 30 08:18 configure
-rw-r--r--  1 schwehr  5000     224 Jun 30 08:17 configure.ac
-rwxr-xr-x  1 schwehr  5000   23910 Jun 30 08:18 depcomp
-rw-r--r--  1 schwehr  5000     161 Jun 30 08:17 endian.c
-rwxr-xr-x  1 schwehr  5000   13997 Jun 30 08:18 install-sh
-rwxr-xr-x  1 schwehr  5000   10179 Jun 30 08:18 missing

rm COPYING INSTALL Makefile.in aclocal.m4 compile config.h.in* configure depcomp install-sh missing
ls -l
total 32
-rw-r--r--  1 schwehr  5000    0 Jun 30 07:54 AUTHORS
-rw-r--r--  1 schwehr  5000    0 Jun 30 07:54 ChangeLog
-rw-r--r--  1 schwehr  5000   48 Jun 29 23:04 Makefile.am
-rw-r--r--  1 schwehr  5000    0 Jun 30 07:54 NEWS
-rw-r--r--  1 schwehr  5000    0 Jun 30 07:54 README
-rwxr-xr-x  1 schwehr  5000  106 Jun 30 07:55 autogen.sh
-rw-r--r--  1 schwehr  5000  224 Jun 30 08:17 configure.ac
-rw-r--r--  1 schwehr  5000  161 Jun 30 08:17 endian.c

Viewing all articles
Browse latest Browse all 108

Trending Articles