Hello Autotools World 

はてなブックマーク - Hello Autotools World
Bookmark this on Delicious

名前とぼんやりとした役割や機能だけは知ってるけど、腰が引けて使えないという状態だったautomakeやautoconfですが、覚える必要が出てきたので概要だけでも把握しようという事で調べてみました。出てくるツールや中間ファイルの種類が多いので、まずは図を書いて整理してみました。

Autotoolsのフローチャート

パッと見は複雑で見て見ぬ振りをしたくなる図ですが、よく見ると自分で作成・編集する必要があるのは、オレンジの項目だけなので、ソースコードを除くとconfigure.acとMakefile.amの2つだけです。これなら何とか覚え始めようという気になりますね。

実際に図の手順を実行しながら追ってみますが、その前に手元のAutotoolsのバージョンを確認します。バージョンが違うと上手く動かないかもしれません。

$ autoconf --version
autoconf (GNU Autoconf) 2.61
# 残りの出力は省略
$ automake --version
automake (GNU automake) 1.10
# 残りの出力は省略

今回のビルド対象のソースコードは以下になります。単純ですがまぁ練習なのでこれくらいにしておきましょう。

$ tree -a
`-- src
    |-- hello.cpp
    |-- hello.h
    `-- main.cpp

まずはMakefile.amを作成します。Makefile.amにはビルド対象のソースコードやリンク情報を記述します。このファイルはディレクトリごとに作成する必要があるので注意して下さい。

$ vim Makefile.am
$ cat Makefile.am
SUBDIRS = src
 
$ vim src/Makefile.am
$ cat src/Makefile.am
bin_PROGRAMS = hello
hello_SOURCES = hello.cpp hello.h main.cpp
 
$ tree -a
|-- Makefile.am
`-- src
    |-- Makefile.am
    |-- hello.cpp
    |-- hello.h
    `-- main.cpp

次にconfigureスクリプトを作成するまでのプロセスを実行してみます。

$ autoscan    # configure.scanを生成
$ tree -a
|-- Makefile.am
|-- autoscan.log
|-- configure.scan
`-- src
    |-- Makefile.am
    |-- hello.cpp
    |-- hello.h
    `-- main.cpp
 
$ mv configure.scan configure.ac
$ vim configure.ac   # 内容は後述
$ tree -a
|-- Makefile.am
|-- autoscan.log
|-- configure.ac
`-- src
    |-- Makefile.am
    |-- hello.cpp
    |-- hello.h
    `-- main.cpp
 
$ aclocal    # aclocal.m4を生成
$ tree -a
|-- Makefile.am
|-- aclocal.m4
|-- autom4te.cache
|   |-- output.0
|   |-- requests
|   `-- traces.0
|-- autoscan.log
|-- configure.ac
`-- src
    |-- Makefile.am
    |-- hello.cpp
    |-- hello.h
    `-- main.cpp
 
$ autoconf     # configureスクリプトを生成
$ tree -a
|-- Makefile.am
|-- aclocal.m4
|-- autom4te.cache
|   |-- output.0
|   |-- output.1
|   |-- requests
|   |-- traces.0
|   `-- traces.1
|-- autoscan.log
|-- configure
|-- configure.ac
`-- src
    |-- Makefile.am
    |-- hello.cpp
    |-- hello.h
    `-- main.cpp

長い道のりを経て無事にconfigureスクリプトが生成されました。途中で編集したconfigure.acの内容は以下になります。エラーが発生する場合はAC_PREREQの値を確認しましょう。インストールされているautoconfパッケージよりも新しいバージョンが指定されているとエラーで先に進めません。僕もosx上でハマりました。linuxだと多分大丈夫だと思います。

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
 
AC_PREREQ(2.61)
AC_INIT(hello, 1.0, taichino@gmail.com)
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src/hello.cpp])
AC_CONFIG_HEADER([config.h])
 
# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
 
# Checks for libraries.
 
# Checks for header files.
 
# Checks for typedefs, structures, and compiler characteristics.
 
# Checks for library functions.
 
AC_CONFIG_FILES([Makefile
                 src/Makefile])
AC_OUTPUT

さて生成されたconfigureを早速実行したいわけですが、これを実行する為にはまだ準備が必要です。実行したい気持ちをぐっと抑えてconfig.h.inとMakefile.inを作成しましょう。

$ autoheader    # config.h.inを生成
$ tree -a
|-- Makefile.am
|-- aclocal.m4
|-- autom4te.cache
|   |-- output.0
|   |-- output.1
|   |-- requests
|   |-- traces.0
|   `-- traces.1
|-- autoscan.log
|-- config.h.in
|-- configure
|-- configure.ac
`-- src
    |-- Makefile.am
    |-- hello.cpp
    |-- hello.h
    `-- main.cpp
 
$ touch NEWS README AUTHORS ChangeLog
$ tree -a
|-- AUTHORS
|-- ChangeLog
|-- Makefile.am
|-- NEWS
|-- README
|-- aclocal.m4
|-- autom4te.cache
|   |-- output.0
|   |-- output.1
|   |-- requests
|   |-- traces.0
|   `-- traces.1
|-- autoscan.log
|-- config.h.in
|-- configure
|-- configure.ac
`-- src
    |-- Makefile.am
    |-- hello.cpp
    |-- hello.h
    `-- main.cpp
 
$ automake --add-missing --copy    # Makefile.inを生成
# ブログに貼付けると表示が乱れるので出力は省略
$ tree -a
|-- AUTHORS
|-- COPYING
|-- ChangeLog
|-- INSTALL
|-- Makefile.am
|-- Makefile.in
|-- NEWS
|-- README
|-- aclocal.m4
|-- autom4te.cache
|   |-- output.0
|   |-- output.1
|   |-- requests
|   |-- traces.0
|   `-- traces.1
|-- autoscan.log
|-- config.h.in
|-- configure
|-- configure.ac
|-- depcomp
|-- install-sh
|-- missing
`-- src
    |-- Makefile.am
    |-- Makefile.in
    |-- hello.cpp
    |-- hello.h
    `-- main.cpp

図には書いていませんがautomakeを実行するには予めNEWS README AUTHORS ChangeLogが作成されている必要があります。これはGNUの推奨するパッケージ構成なのですが、ここでは空ファイルを作成してお茶を濁しています。さてこれで漸くconfigureを実行する準備が整いました。早速実行してみましょう。

$ ./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... ./install-sh -c -d
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for g++... g++
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 g++ accepts -g... yes
checking for style of include used by make... GNU
checking dependency style of g++... gcc3
checking for gcc... gcc
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
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating config.h
config.status: executing depfiles commands
$ tree -a
|-- AUTHORS
|-- COPYING
|-- ChangeLog
|-- INSTALL
|-- Makefile        # Makefileができてる!
|-- Makefile.am
|-- Makefile.in
|-- NEWS
|-- README
|-- aclocal.m4
|-- autom4te.cache
|   |-- output.0
|   |-- output.1
|   |-- requests
|   |-- traces.0
|   `-- traces.1
|-- autoscan.log
|-- config.h
|-- config.h.in
|-- config.log
|-- config.status
|-- configure
|-- configure.ac
|-- depcomp
|-- install-sh
|-- missing
|-- src
|   |-- .deps
|   |   |-- hello.Po
|   |   `-- main.Po
|   |-- Makefile
|   |-- Makefile.am
|   |-- Makefile.in
|   |-- hello.cpp
|   |-- hello.h
|   `-- main.cpp
`-- stamp-h1

おなじみの出力の後にMakefileが作成されましたね。以上でだいたいAutotoolsの作業手順を追う事が出来ました。もうちょっと調べた事を書こうと思ってたのですが、長くなったので割愛します。infoにやたらと詳しいドキュメントが準備されているので、ちらっと読んでみると理解が進むかもしれません。

あと本質とは関係ないですけど、.acがAutoConf、.amがAutoMake、.inはconfigureの入力ファイルだと言うのを覚えれば見通しが良くなるかもしれません。

2010.9.13 追記
またMakefile.amおよびconfigure.acの作成以降のプロセスは以下のコマンドで一括して行えます。特に理由がないときはこちらのコマンドを使えば良さそうです。

$ autoreconf --install --verbose # もしくは
$ autoreconf -iv                          # 省略形でもok

関連する記事

  • 関連する記事はありません

コメント / トラックバック3件

  1. [...] This post was mentioned on Twitter by taichino. taichino said: Blogged Hello Autotools World http://bit.ly/aOki7G [...]

  2. Emily N. より:

    Hi, I’m very interested in Linux but Im a Super Newbie and I’m having trouble deciding on the right distribution for me (Havent you heard this a million times?) anyway here is my problem, I need a distribution that can switch between reading and writing in English and Japanese (Japanese Language Support) with out restarting the operating system.

  3. taichino より:

    I’m sorry for late response. your message was filtered as spam..
    Well, I’m unfamiliar with Linux Distributions, though, I’m using ubuntu and CentOS.
    We can easily swith language configuration on both of them. Just edit LANG in /etc/sysconfig/i18n.

コメントをどうぞ