Friday, September 02, 2005

Automake tutorial: xtris example

I write a few automake tutorial before, and this is just another one.

Normally I will write a hello world program as a sample for the automake tutorial, but hello world is just too simple for a automake tutorial, so I use xtris as an example.

What is xtris? xtris is a multi-player version of the classical game of Tetris. The program just base on Xlib and nothing else, most unix operating system have Xlib, so xtris can be compiled on most unix platform eg. Linux, Solaris.

There is a windows port by Vedran Vidovic.

xtris

xtris license under GPL, you can download the source code (version 1.15) from here.

Why xtris is good for an automake tutorial?
1. GPL. The program license under GPL, so I can just make modification on it.
2. Easy to compile. The program can be easily compile in major unix platform eg. Solaris, Linux, so less problem to solve for compilation, user can focus on automake itself.
3. xtris doesn't came with configuration script, you need to edit manually when you compile xtris on Solaris operating system.

Automake for xtris
xtris does not came with configure script. It is a very easy compile program, but you might need to make some modification to the Makefile, to make it compile on Solaris sytem.

xtris is a graphic base program written using X11 library and not depend on any other GUI toolkit. The X11 headers and libraries path for Linux and Solaris is different, Linux use /usr/X11R6 but Solaris (normally) located at /usr/openwin. Ya, Solaris use Open Window and not X-Windows.

Another problem came with socket command. On Solaris, need to link with the socket library, but on Linux is not necessary.

Some programs provide imake file to solve the X-Window library problems, I don't really like the imake. Some programs privide two Makefile, one for Linux and the other for Solaris, but the automake and autoconf script make the compilation in unix world much more easier.

Basic step for automake
1. make sure you have automake, autoconf, and m4.
2. use autoscan to generate configure.scan, and modified configure.scan to configure.in
3. aclocal and autconf
$ aclocal
$ autoconf
4. create Makefile.am
5. automake

That's it, you should have configure script now, and you can use the script to generate the Makefile:

$ ./configure

Let's start
You cane use autoscan to generate a configure.in template, or just create one manually. Different version of autoscan may create different configure.in template, the sample I give here base on old version, but should work.

1.
$ vi configure.in
# Process this file with autoconf to produce a configure.script
AC_INIT(xtris.c)
AM_INIT_AUTOMAKE(xtris, 0.99.0)

# Checks for programs
AC_PROG_CC

# check path and header
AC_PATH_XTRA

CFLAGS="$CFLAGS $X_CFLAGS"
LIBS="$LIBS $X_LIBS"

AC_CHECK_LIB([nsl], [inet_ntoa])
AC_CHECK_LIB([socket], [accept])

AC_OUTPUT(Makefile)

2. create Makefile.am
There binaries to be created, xtris, xtbot and xtserv.
xtris compiled from xtris.c
xtbot compiled from xtbot.c, xtbot.h, decide.c decide.h
xtserv compiled from xtserv.c

$ vi Makefile.am
# process this file with automake to produce Makefile.in

XTRIS_PROGS = xtris xtbot xtserv

bin_PROGRAMS = xtris xtbot xtserv

xtris_SOURCES = xtris.c
xtbot_SOURCES = xtbot.c xtbot.h decide.c decide.h
xtserv_SOURCES = xtserv.c

xtris_LDADD = -lX11
xtbot_LDADD = -lX11
xtserv_LDADD = -lX11

3. Create configure script
$ aclocal
$ autoconf
$ automake [--add-missing]
Automake need install-sh, missing and mkinstalldirs script, normally automake will make a link to these 3 files, but other system might not have these 3 script file for you compilation. So I remove these 3 links and copy it from the system. eg.
$ rm -f install-sh, missing, mkinstalldirs
$ cp -p /usr/local/share/automake{install-sh,missing,mkinstalldirs}.

Few other files like NEWS, AUTHORS could be missing complain by the system. I just create a file for the system to check
$ touch NEWS AUTHORS

4. Now you should have a configure script, which generate the Makefile
$ ./configure
$ make
==> generate xtris

Note:
Complain of VERSION redefined, I just modified:
$ vi xtris.c
#ifndef VERSION
#define VERSION "1.15"
#endif

5. package into tar.gz
$ make dist

Conclusion
This is a very simple makefile tutorial, my modification to the xtris is more than this. I move all the manual file to man subdirectory, add control for gcc and Sun Workshop C compiler, but I don't have any hosting area to upload my modified version of xtris.

No comments:

Post a Comment