Portál AbcLinuxu, 30. dubna 2025 09:07
Aneb Úvod do autotools
Po netu se povaluje spousta kvalitních projektů, ale nekvalitně distribuovaných. Občas chybí důležité skripty configure nebo makefile. Projekt pak často nejde vůbec přeložit či sestavit.
Projekt autotools je kolekce nástrojů (autoconf, automake, libtool, ...), které mají za cíl zjednodušit konfigurování, sestavování a balíčkování software. Ve speciálních souborech (viz 1. obrázek) nadefinujeme pravidla pro sestavování, závislosti komponent a můžeme též vytvořit šablony pro témeř libovolné soubory. Nástroje z kolekce autotools přechroustají tento vstup (viz 2. obrázek) a vygenerují skript configure a šablony pro Makefile. Nebudu vysvětlovat, co dělá configure, co to je Makefile ani jak se kompiluje. Na webu je kolem toho spousta materiálu.
Mějme jednoduchý projekt. V kořenovém adresáři projektu ($DIR) vytvoříme podadresář src
. V podadresáři src
jsou soubory:
$DIR/src/hello_c.c: #ifdef HAVE_CONFIG_H # include "config.h" #endif #ifdef HAVE_STDIO_H # include <stdio.h> #endif int main(void) { printf("Hello world using C\n"); return 0; }
$DIR/src/hello_cpp.cpp: #ifdef HAVE_CONFIG_H # include "config.h" #endif #include <iostream> int main(void) { std::cout << "Hello world using C++" << std::endl; return 0; }
$DIR/src/hello.sh.in (šablona pro hello.sh): #!@BASH@ echo "Hello world using @BASH@" exit 0
$DIR/src/Makefile.am (pomocí něj se vygeneruje Makefile.in-šablona pro Makefile): bin_PROGRAMS = hello_c hello_cpp CLEANFILES = hello.sh if HAVE_BASH bin_SCRIPTS = hello.sh endif hello_c_SOURCES = hello_c.c hello_cpp_SOURCES = hello_cpp.cpp
V kořenovém adresáři projektu jsou soubory:
$DIR/Makefile.am (provede ponoření do uvedených adresářů): SUBDIRS = src
$DIR/configure.ac (hlavní konfigurační soubor pro autotools, obsahuje posloupnost maker, z kterých se vygeneruje configure): #inicializace autoconf (jméno balíčku, verze, email) AC_INIT([hello], [1.0], [mail@mail.cz]) #inicializace automake (vlastnosti) AM_INIT_AUTOMAKE([-Wall -Werror foreign]) #provede "checking for gcc..." AC_PROG_CC #provede "checking for g++..." AC_PROG_CXX #provede "checking for bash..." AC_CHECK_PROGS([BASH], [bash sh], [none]) #pokud nenajde bash... if test "$BASH" = none; then #nastavi podmínku pro automake (viz $DIR/src/Makefile.am) AM_CONDITIONAL([HAVE_BASH], [false]) #vytiskne chybovou hlášku a ukončí se AC_MSG_ERROR([Could not find sh-compatible interpreter]) else AM_CONDITIONAL([HAVE_BASH], [true]) #na závěr configure nastaví skriptu $DIR/src/hello.sh práva pro spuštění AC_CONFIG_COMMANDS([chmod], [chmod +x src/hello.sh]) fi #pokusí se najít hlavičku AC_CHECK_HEADERS([stdio.h]) #sem zapíše zjištěnou konfiguraci, kterou si v projektu includneme AC_CONFIG_HEADERS([src/config.h]) #vygeneruje tyto soubory ze šablon *.in) AC_CONFIG_FILES([Makefile src/Makefile src/hello.sh ]) #zapíše config.status AC_OUTPUT
Pokud jste se dostali až sem, stačí spustit příkaz autoreconf --install
, který se postará o zavolání autoconf, automake, autoheader, ... ve správném pořadí (viz 3. a 4. obrázek).
Nyní máme projekt připraven. Stačí spustit ./configure && make
.
diverman@lenovo:~/autotools/hello$ ./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 for gcc... gcc checking for C compiler default output file name... a.out checking whether the C compiler works... yes checking whether we are cross compiling... no checking for suffix of executables... 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 for style of include used by make... GNU checking dependency style of gcc... gcc3 checking for g++... g++ checking whether we are using the GNU C++ compiler... yes checking whether g++ accepts -g... yes checking dependency style of g++... gcc3 checking for bash... /bin/sh checking how to run the C preprocessor... gcc -E checking for grep that handles long lines and -e... /bin/grep checking for egrep... /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 stdio.h usability... yes checking stdio.h presence... yes checking for stdio.h... yes configure: creating ./config.status config.status: creating Makefile config.status: creating src/Makefile config.status: creating src/hello.sh config.status: creating src/config.h config.status: executing depfiles commands config.status: executing chmod commands diverman@lenovo:~/autotools/hello$ make Making all in src make[1]: Entering directory `/home/diverman/autotools/hello/src' make all-am make[2]: Entering directory `/home/diverman/autotools/hello/src' gcc -DHAVE_CONFIG_H -I. -g -O2 -MT hello_c.o -MD -MP -MF .deps/hello_c.Tpo -c -o hello_c.o hello_c.c mv -f .deps/hello_c.Tpo .deps/hello_c.Po gcc -g -O2 -o hello_c hello_c.o g++ -DHAVE_CONFIG_H -I. -g -O2 -MT hello_cpp.o -MD -MP -MF .deps/hello_cpp.Tpo -c -o hello_cpp.o hello_cpp.cpp mv -f .deps/hello_cpp.Tpo .deps/hello_cpp.Po g++ -g -O2 -o hello_cpp hello_cpp.o make[2]: Leaving directory `/home/diverman/autotools/hello/src' make[1]: Leaving directory `/home/diverman/autotools/hello/src' make[1]: Entering directory `/home/diverman/autotools/hello' make[1]: Nothing to be done for `all-am'. make[1]: Leaving directory `/home/diverman/autotools/hello'
Tiskni
Sdílej:
stdio.h
tak sa bude napr. kontrolovať prítomnosť stdlib.h
, string.h
, stdint.h
... ? Alebo sa tie zdrojáky preliezajú a rekurzívne sa sleduje čo všetko sa #includne? Ako to funguje?
deb http://ftp.cz.debian.org/debian jessie main contrib non-free
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.