Using Automake with GCJ

David Pashley

Revision History
Revision 0.3 26 May 2002
Updated info about fixed bugs
Revision 0.2 24 May 2002
Changed to Docbook source
Revision 0.1 20 May 2002
First draft in HTML.

Table of Contents

Fix the M4 files
depend.m4
gcj.m4
Editing configure.ac and Makefile.am

Abstract

GCJ is the Java compiler of GCC, and with a little bit of tweaking, you can use it with Automake. Automake 1.5 has support for using gcj, but it has a few bugs. This document will explain how to fix those bugs and allow you to use the flexibility of the autotools for your Java project.

Fix the M4 files

A couple of the M4 macro files shipped with Automake have small bugs. You should be able to find these files in /usr/share/aclocal.

depend.m4

This file is missing a comma. Find the line below around line 31

         [$1], OBJC, [depcc="$OBJC" am_compiler_list='gcc3 gcc']
            

and change it to

         [$1], OBJC, [depcc="$OBJC" am_compiler_list='gcc3 gcc'],
         

Note

This has been fixed and should be in 1.6.2

gcj.m4

This file needs to call the _AM_DEPENDENCIES macro in depend.m4.

# Check for Java compiler.
# For now we only handle the GNU compiler.

AC_DEFUN([AM_PROG_GCJ],[
AC_CHECK_PROGS(GCJ, gcj, gcj)
test -z "$GCJ" && AC_MSG_ERROR([no acceptable gcj found in \$PATH])
if test "x${GCJFLAGS+set}" = xset; then
   GCJFLAGS="-g -O2"
fi
AC_SUBST(GCJFLAGS)
])
            

becomes

# Check for Java compiler.
# For now we only handle the GNU compiler.

AC_DEFUN([AM_PROG_GCJ],[
AC_CHECK_PROGS(GCJ, gcj, gcj)
test -z "$GCJ" && AC_MSG_ERROR([no acceptable gcj found in \$PATH])
if test "x${GCJFLAGS+set}" = xset; then
   GCJFLAGS="-g -O2"
fi
AC_SUBST(GCJFLAGS)
_AM_DEPENDENCIES(GCJ)
])
            

Note

This has been fixed in 1.6.1

Editing configure.ac and Makefile.am

You now need to tell autoconf that you will be using gcj. You can do this by adding the following line to configure.ac (or configure.in):

AM_PROG_GCJ
         

You can now use Java sources in your Makefile.am like you would with any C or C++ source file.

bin_PROGRAMS=hello
hello_SOURCES=Hello.java
         

If you only have java sources for your project, you may find that you have trouble with LDFLAGS. This is because you may not include any macros which define this variable and so is left in the Makefile as @LDFLAGS@. The simplest solution to this is th include AM_PROG_CC in your configure.ac file.

There is just one more obstacle before everything will work. In C and C++ there can only be one main function. However in Java there can be one per class. This means that gcj can not work out which one you want to use. Fortunately gcj has a command line argument (--main=Class) which you can use to tell it which class to use. Unfortunately, automake doesn't provide you will an easy way of passing this argument, but you can still give it to gcj by using the prog_LDFLAGS variable in Makefile.am.

bin_PROGRAMS=hello
hello_SOURCES=Hello.java
hello_LDFLAGS=--main=Hello
         

Valid XHTML 1.0! Valid CSS!