引き続き「UNIXプログラミングの道具箱」(Autoconf)

環境設定用のヘッダファイルを自動生成するconfigureスクリプトを生成するツール
configureスクリプトを使うとコンパイル環境に合わせたMakefileが自動的に生成できてプログラムの可搬性が向上する

configureスクリプトを使ったMakefile生成の流れは

  1. configure.scan生成(autoscanコマンドで)
  2. configure.inの作成(configure.scanを修正して)
  3. configure作成(autoconfコマンドで)
  4. Makefile.in作成(Makefileを修正して)
  5. Makefile生成(configureスクリプトで)

autoconfはインストール媒体に入っているものをそのまま使って試します。

とりあえず作ってみる

まず、"Hello World"を出力するソースとコンパイルするためのMakefileを用意

[kobakoba0723@fedora13-intel64 temp]$ ls
Makefile  hello.c
[kobakoba0723@fedora13-intel64 temp]$ cat Makefile 
CC=cc
CFLAGS=-O
INSTALL=install -c
exec_prefix=/usr/local
bindir=$(exec_prefix)/bin

TARGET=hello
SOURCE=hello.c

all: $(TARGET)

$(TARGET): $(SOURCE)
	$(CC) $(CFLAGS) -o $(TARGET) $(SOURCE)

install:
	$(INSTALL) -m 755 $(TARGET) $(bindir)

clean:
	rm -f $(TARGET)
	rm -f *~

autoscanというコマンドで、configure.scanファイルを生成

[kobakoba0723@fedora13-intel64 temp]$ autoscan 
[kobakoba0723@fedora13-intel64 temp]$ ls
Makefile  autoscan.log  configure.scan  hello.c
[kobakoba0723@fedora13-intel64 temp]$ cat configure.scan 
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

AC_INITの()内を修正してconfigure.inファイルを作成する
AC_INITにはソースツリーの中で特徴的なCのソースファイル名などを指定する
この状態でautoconfを実行すると、めでたくconfigureファイルが生成される

[kobakoba0723@fedora13-intel64 temp]$ autoconf 
[kobakoba0723@fedora13-intel64 temp]$ ls
Makefile  autom4te.cache  autoscan.log  configure  configure.in  hello.c

そして、MakefileからMakefile.inを作成し、
configureスクリプト実行時に環境依存箇所を検知し、修正できるように該当箇所を@で囲んで置き換える

Makefile.inが出来たのでconfigureスクリプトを実行してMakefileを生成する

[kobakoba0723@fedora13-intel64 temp]$ ./configure 
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
configure: error: cannot find install-sh, install.sh, or shtool in "." "./.." "./../.."

install-shファイルがないって怒られた。。。


install-shは、installコマンドが実行しない環境で make install を実行するために使うファイルで、
autoconfの配布ファイルに入っているものを使えばオーケー。

[kobakoba0723@fedora13-intel64 temp]$ ls
Makefile.in     autoscan.log  configure     hello.c
autom4te.cache  config.log    configure.in  install-sh
[kobakoba0723@fedora13-intel64 temp]$ ./configure 
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 for a BSD-compatible install... /usr/bin/install -c
configure: creating ./config.status
config.status: creating Makefile
config.status: error: cannot find input file: `config.h.in'

また怒られた。


どうもAC_CONFIG_HEADERSを定義しているのが原因らしい。
config.h.inファイル自体はautoheaderコマンドで生成することが出来るみたい。
http://www.bookshelf.jp/texi/autoconf-ja/autoconf-ja_4.html#SEC29

[kobakoba0723@fedora13-intel64 temp]$ autoheader 
[kobakoba0723@fedora13-intel64 temp]$ ls
Makefile        autoscan.log  config.status  hello.c
Makefile.in     config.h.in   configure      install-sh
autom4te.cache  config.log    configure.in

お、config.h.inファイルが出来てる。

じゃあ、この状態でもう一度configureを実行してみると

[kobakoba0723@fedora13-intel64 temp]$ ./configure 
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 for a BSD-compatible install... /usr/bin/install -c
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
[kobakoba0723@fedora13-intel64 temp]$ ls
Makefile        autoscan.log  config.log     configure.in
Makefile.in     config.h      config.status  hello.c
autom4te.cache  config.h.in   configure      install-sh
[kobakoba0723@fedora13-intel64 temp]$ cat Makefile
CC=gcc
CFLAGS=-g -O2
INSTALL=/usr/bin/install -c
prefix=/usr/local
exec_prefix=${prefix}
bindir=${prefix}/${exec_prefix}/bin

TARGET=hello
SOURCE=hello.c

all: $(TARGET)

$(TARGET): $(SOURCE)
	$(CC) $(CFLAGS) -o $(TARGET) $(SOURCE)

install:
	$(INSTALL) -m 755 $(TARGET) $(bindir)

clean:
	rm -f $(TARGET)
	rm -f *~

おぉ、ちゃんと出来てる。

じゃあ、make して make install もついでに

[kobakoba0723@fedora13-intel64 temp]$ make
gcc -g -O2 -o hello hello.c
[kobakoba0723@fedora13-intel64 temp]$ sudo make install
/usr/bin/install -c -m 755 hello /usr/local/bin
[kobakoba0723@fedora13-intel64 temp]$ /usr/local/bin/hello 
Hello World

とりあえず、マクロとか定義関係は全く無視して、configureスクリプトを作るだけやった。
マクロとかの定義関連はチンプンカンプン意味不明。明日はその辺をちゃんと調べるないと。