从标题就可以看出了,autogen.sh是个shell脚本,它的作用就是将前一篇Linux工具之autoconf和automake讲的自动产生Makefile的过程集成到脚本中,简化操作。
接下来看看如何操作,直接上代码:
#!/bin/sh
echo
echo ... helloworld autogen ...
echo
MISSING=""
env aclocal --version >/dev/null 2>&1
if [ $? -eq 0 ];then
ACLOCAL=aclocal
else
MISSING="$MISSING aclocal"
fi
env autoconf --version > /dev/null 2>&1
if [ $? -eq 0 ];then
AUTOCONF=autoconf
else
MISSING="$MISSING autoconf"
fi
env autoheader --version > /dev/null 2>&1
if [ $? -eq 0 ]; then
AUTOHEADER=autoheader
else
MISSING="$MISSING autoconf"
fi
env automake --version > /dev/null 2>&1
if [ $? -eq 0 ];then
AUTOMAKE=automake
else
MISSING="$MISSING automake"
fi
env libtoolize --version > /dev/null 2>&1
if [ $? -eq 0 ];then
TOOL=libtoolize
else
env glibtoolize --version > /dev/null 2>&1
if [$? -eq 0 ];then
TOOL=glibtoolize
else
MISSING="$MISSING libtoolize/glibtoolize"
fi
fi
env tar -cf /dev/null /dev/null > /dev/null 2>&1
if [ $? -ne 0 ];then
MISSING="$MISSING tar"
fi
if [ "x$MISSING" != "x" ];then
echo "ABORTING."
echo
echo "The following build tools are missing:"
echo
for pkg in $MISSING;do
echo " * $pkg"
done
echo
echo "please install them and try again."
echo
fi
echo Running $ACLOCAL
$ACLOCAL
echo Running $AUTOHEADER
$AUTOHEADER
echo Running $TOOL
$TOOL
echo Running $AUTOCONF
$AUTOCONF
echo Running $AUTOMAKE
$AUTOMAKE --add-missing --force-missing --copy --foreign
echo
echo "Please proceed with configuring, compiling and installing"
如脚本所示, 先检测命令aclocal,autoconf,autoheader,automake,libtoolize,tar是否存在,之后再按照上篇的步骤来执行这几个命令;当然在执行脚本前配置文件configure.ac和Mafile.am还是必须的。
configure.ac
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([helloworld], [1.0], [[email protected]])
AM_INIT_AUTOMAKE(helloworld,1.0)
AC_CONFIG_SRCDIR([src/hello.c])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile
src/Makefile])
#AC_CONFIG_MACRO_DIRS([m4])
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
AC_PROG_LIBTOOL
# Checks for header files.
AC_CHECK_HEADER([stddef.h string.h])
# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_TYPE_SIZE_T
# Checks for library functions.
AC_FUNC_REALLOC
AC_CHECK_FUNCS([memset socket])
AC_OUTPUT
Makefile.am
SUBDIRS = src
#ACLOCAL_AMFLAGS = -I m4
.PHONY:clean
clean: distclean
find . -name Makefile.in -exec rm -rf {} \;
rm -rf autom4te.cache
rm -f aclocal.m4 autoscan.log config.h config.log config.sub configure depcomp Makefile compile config.h.in config.status install-sh missing stamp-h1 config.guess config.h.in~ ltmain.sh
底层Makefile.am如下
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=hello.c
helloworld_LDADD=
LIBS=-lm
INCLUDE=-I/usr/include
源码文件hello.c如下
#include
int main()
{
printf("hello.\n");
return 0;
}
执行过程如下:
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ls
autogen.sh configure.ac Makefile.am src
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ./autogen.sh... helloworld autogen ...
Running aclocal
Running autoheader
Running libtoolize
libtoolize: putting auxiliary files in '.'.
libtoolize: linking file './ltmain.sh'
libtoolize: Consider adding 'AC_CONFIG_MACRO_DIRS([m4])' to configure.ac,
libtoolize: and rerunning libtoolize and aclocal.
libtoolize: Consider adding '-I m4' to ACLOCAL_AMFLAGS in Makefile.am.
Running autoconf
Running automake
configure.ac:6: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated. For more info, see:
configure.ac:6: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation
configure.ac:15: installing './compile'
configure.ac:18: installing './config.guess'
configure.ac:18: installing './config.sub'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
src/Makefile.am: installing './depcomp'Please proceed with configuring, compiling and installing
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ll
total 996
drwxrwxr-x 5 osrc osrc 4096 3月 6 11:08 ./
drwxrwxr-x 8 osrc osrc 4096 2月 25 10:20 ../
-rw-r--r-- 1 root root 373029 3月 6 11:08 aclocal.m4
-rwxr-xr-x 1 root root 1381 3月 4 11:48 autogen.sh*
drwxr-xr-x 2 root root 4096 3月 6 11:08 autom4te.cache/
-rwxr-xr-x 1 root root 7333 3月 6 11:08 compile*
-rwxr-xr-x 1 root root 43499 3月 6 11:08 config.guess*
-rw-r--r-- 1 root root 2192 3月 6 11:08 config.h.in
-rwxr-xr-x 1 root root 36144 3月 6 11:08 config.sub*
-rwxr-xr-x 1 root root 435434 3月 6 11:08 configure*
-rw-r--r-- 1 root root 698 3月 4 12:19 configure.ac
-rwxr-xr-x 1 root root 23566 3月 6 11:08 depcomp*
drwxr-xr-x 2 root root 4096 2月 25 19:25 .deps/
-rwxr-xr-x 1 root root 15155 3月 6 11:08 install-sh*
lrwxrwxrwx 1 root root 38 3月 6 11:08 ltmain.sh -> /usr/share/libtool/build-aux/ltmain.sh
-rw-r--r-- 1 root root 327 3月 4 12:19 Makefile.am
-rw-r--r-- 1 root root 25368 3月 6 11:08 Makefile.in
-rwxr-xr-x 1 root root 6872 3月 6 11:08 missing*
drwxr-xr-x 2 root root 4096 3月 6 11:08 src/root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
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 whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking how to print strings... printf
checking for a sed that does not truncate output... /bin/sed
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for fgrep... /bin/grep -F
checking for ld used by gcc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking how to convert x86_64-pc-linux-gnu file names to x86_64-pc-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-pc-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking command to parse /usr/bin/nm -B output from gcc object... ok
checking for sysroot... no
checking for a working dd... /bin/dd
checking how to truncate binary pipes... /bin/dd bs=4096 count=1
checking for mt... mt
checking if mt is a manifest tool... no
checking how to run the C preprocessor... gcc -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 for dlfcn.h... yes
checking for objdir... .libs
checking if gcc supports -fno-rtti -fno-exceptions... no
checking for gcc option to produce PIC... -fPIC -DPIC
checking if gcc PIC flag -fPIC -DPIC works... yes
checking if gcc static flag -static works... yes
checking if gcc supports -c -o file.o... yes
checking if gcc supports -c -o file.o... (cached) yes
checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
checking stddef.h string.h usability... no
checking stddef.h string.h presence... no
checking for stddef.h string.h... no
checking for stdbool.h that conforms to C99... yes
checking for _Bool... yes
checking for size_t... yes
checking for stdlib.h... (cached) yes
checking for GNU libc compatible realloc... yes
checking for memset... yes
checking for socket... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating config.h
config.status: executing depfiles commands
config.status: executing libtool commandsroot@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ll
total 1456
drwxrwxr-x 5 osrc osrc 4096 3月 6 11:10 ./
drwxrwxr-x 8 osrc osrc 4096 2月 25 10:20 ../
-rw-r--r-- 1 root root 373029 3月 6 11:08 aclocal.m4
-rwxr-xr-x 1 root root 1381 3月 4 11:48 autogen.sh*
drwxr-xr-x 2 root root 4096 3月 6 11:08 autom4te.cache/
-rwxr-xr-x 1 root root 7333 3月 6 11:08 compile*
-rwxr-xr-x 1 root root 43499 3月 6 11:08 config.guess*
-rw-r--r-- 1 root root 2422 3月 6 11:10 config.h
-rw-r--r-- 1 root root 2192 3月 6 11:08 config.h.in
-rw-r--r-- 1 root root 28985 3月 6 11:10 config.log
-rwxr-xr-x 1 root root 58276 3月 6 11:10 config.status*
-rwxr-xr-x 1 root root 36144 3月 6 11:08 config.sub*
-rwxr-xr-x 1 root root 435434 3月 6 11:08 configure*
-rw-r--r-- 1 root root 698 3月 4 12:19 configure.ac
-rwxr-xr-x 1 root root 23566 3月 6 11:08 depcomp*
drwxr-xr-x 2 root root 4096 2月 25 19:25 .deps/
-rwxr-xr-x 1 root root 15155 3月 6 11:08 install-sh*
-rwxr-xr-x 1 root root 339900 3月 6 11:10 libtool*
lrwxrwxrwx 1 root root 38 3月 6 11:08 ltmain.sh -> /usr/share/libtool/build-aux/ltmain.sh
-rw-r--r-- 1 root root 25578 3月 6 11:10 Makefile
-rw-r--r-- 1 root root 327 3月 4 12:19 Makefile.am
-rw-r--r-- 1 root root 25368 3月 6 11:08 Makefile.in
-rwxr-xr-x 1 root root 6872 3月 6 11:08 missing*
drwxr-xr-x 3 root root 4096 3月 6 11:10 src/
-rw-r--r-- 1 root root 23 3月 6 11:10 stamp-h1
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# make
make all-recursive
make[1]: Entering directory '/home/osrc/driver_test/autoconf_work'
Making all in src
make[2]: Entering directory '/home/osrc/driver_test/autoconf_work/src'
gcc -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT hello.o -MD -MP -MF .deps/hello.Tpo -c -o hello.o hello.c
mv -f .deps/hello.Tpo .deps/hello.Po
/bin/sh ../libtool --tag=CC --mode=link gcc -g -O2 -o helloworld hello.o -lm
libtool: link: gcc -g -O2 -o helloworld hello.o -lm
make[2]: Leaving directory '/home/osrc/driver_test/autoconf_work/src'
make[2]: Entering directory '/home/osrc/driver_test/autoconf_work'
make[2]: Leaving directory '/home/osrc/driver_test/autoconf_work'
make[1]: Leaving directory '/home/osrc/driver_test/autoconf_work'
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# ll
total 1456
drwxrwxr-x 5 osrc osrc 4096 3月 6 11:10 ./
drwxrwxr-x 8 osrc osrc 4096 2月 25 10:20 ../
-rw-r--r-- 1 root root 373029 3月 6 11:08 aclocal.m4
-rwxr-xr-x 1 root root 1381 3月 4 11:48 autogen.sh*
drwxr-xr-x 2 root root 4096 3月 6 11:08 autom4te.cache/
-rwxr-xr-x 1 root root 7333 3月 6 11:08 compile*
-rwxr-xr-x 1 root root 43499 3月 6 11:08 config.guess*
-rw-r--r-- 1 root root 2422 3月 6 11:10 config.h
-rw-r--r-- 1 root root 2192 3月 6 11:08 config.h.in
-rw-r--r-- 1 root root 28985 3月 6 11:10 config.log
-rwxr-xr-x 1 root root 58276 3月 6 11:10 config.status*
-rwxr-xr-x 1 root root 36144 3月 6 11:08 config.sub*
-rwxr-xr-x 1 root root 435434 3月 6 11:08 configure*
-rw-r--r-- 1 root root 698 3月 4 12:19 configure.ac
-rwxr-xr-x 1 root root 23566 3月 6 11:08 depcomp*
drwxr-xr-x 2 root root 4096 2月 25 19:25 .deps/
-rwxr-xr-x 1 root root 15155 3月 6 11:08 install-sh*
-rwxr-xr-x 1 root root 339900 3月 6 11:10 libtool*
lrwxrwxrwx 1 root root 38 3月 6 11:08 ltmain.sh -> /usr/share/libtool/build-aux/ltmain.sh
-rw-r--r-- 1 root root 25578 3月 6 11:10 Makefile
-rw-r--r-- 1 root root 327 3月 4 12:19 Makefile.am
-rw-r--r-- 1 root root 25368 3月 6 11:08 Makefile.in
-rwxr-xr-x 1 root root 6872 3月 6 11:08 missing*
drwxr-xr-x 4 root root 4096 3月 6 11:12 src/
-rw-r--r-- 1 root root 23 3月 6 11:10 stamp-h1
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work# cd src/
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work/src# ll
total 84
drwxr-xr-x 4 root root 4096 3月 6 11:12 ./
drwxrwxr-x 5 osrc osrc 4096 3月 6 11:10 ../
drwxr-xr-x 2 root root 4096 3月 6 11:12 .deps/
-rw-rw-r-- 1 osrc osrc 69 2月 25 18:40 hello.c
-rw-r--r-- 1 root root 6696 3月 6 11:12 hello.o
-rwxr-xr-x 1 root root 11432 3月 6 11:12 helloworld*
drwxr-xr-x 2 root root 4096 3月 6 11:12 .libs/
-rw-r--r-- 1 root root 18862 3月 6 11:10 Makefile
-rw-r--r-- 1 root root 128 3月 4 09:32 Makefile.am
-rw-r--r-- 1 root root 19144 3月 6 11:08 Makefile.in
root@osrc-virtual-machine:/home/osrc/driver_test/autoconf_work/src#
可以看到只需要执行./autogen.sh, ./configure, make 这三步即可编译整个工程了。
整个文件包可以在中https://github.com/fiu300/autogen找到