# Part of the A-A-P recipe executive: Default variables

# Copyright (c) 2002 stichting NLnet Labs
# Permission to copy and use this file is specified in the file COPYING.
# If this file is missing you can find it here: http://www.a-a-p.org/COPYING

#
# This recipe is read to set all kinds of defaults.
#
# Note: Don't read this as an example for a recipe, it contains all the
# complicated stuff that you (hopefully) will not need to use in a normal
# recipe.
#

CC ?= cc
CXX ?= $CC
CFLAGS ?=
CXXFLAGS ?= $CFLAGS
LDFLAGS ?=
CPPFLAGS ?=

# Check if $CC exists.  If not, try looking for "gcc".
@if not program_path(CC) and program_path('gcc'):
   CC = gcc

# TODO: OBJSUF actually depends on the compiler, how to find out?
@if os.name in [ 'nt', 'dos', 'os2' ]:
    OBJSUF = .obj
    EXESUF = .exe
    LNKSUF =
@else:
    OBJSUF = .o
    EXESUF =
    LNKSUF =

# Get the time, so that it remains the same while executing the recipe.
:python
    import time
    GMTIME = time.gmtime(time.time())
    DATESTR = time.strftime("%Y %b %d", GMTIME)
    TIMESTR = time.strftime("%X", GMTIME)

# Default depedency checkers

# Check if $CC is GCC, remember this in $HASGCC
HASGCC = no
gcccheck {virtual} :
    oldmsg = $MESSAGE
    MESSAGE = error	    # Don't echo the commands here
    @try:
	testfile = `tempfname()`.c
	outfile = `tempfname()`
	:eval "#ifdef __GNUC__\nyes;\n#endif\n" >! $testfile
	:do preproc {target = $outfile} $testfile
	:cat $outfile | :assign tt
	@if string.find(tt, "yes") >= 0:
	    HASGCC = yes
	    :export HASGCC
    @except (StandardError, UserError), e:
	:print Warning: could not test C compiler for being GCC: `str(e)`
    MESSAGE = $oldmsg
    :del {force}{quiet} $testfile $outfile

# GCC can detect included files better than we can, it works recursively.
# If GCC is not available, use an internal function.
:action depend {recursive} c,cpp
    :update gcccheck
    @if HASGCC != "yes":
    @   aap_depend_c(globals(), source, target)
    @else:
        cmd = $CC $CPPFLAGS $CFLAGS -E -MM $-source > $-target
	@prev_files = []
	@while 1:
	@    ok, text = redir_system(cmd)
	@    if ok:
	@	break
	@    # If files are missing try to fetch them and try again.
	@    lines = string.split(text, '\n')
	@    files = []
	@    for line in lines:
	@        m = re.match('.*?([^: ]*): No such file or directory', line)
	@        if m:
	@             files.append(m.group(1))
	@    if not files:
	         :error Generating dependencies for $source failed
	@    if files == prev_files:
	         :error Cannot find included files
	@    try:
	         :fetch `files` # try fetching the missing files
	@    except:
		 :error Cannot find one of the included files: `files`
	@    prev_files = files

#
# Default A-A-P commands for action-filetype pairs.
#

# :do edit

@if os.name == "posix":
    EDITOR = `os.environ.get('VISUAL', os.environ.get('EDITOR', 'vi'))`
@else:
    EDITOR = `os.environ.get('VISUAL', os.environ.get('EDITOR', 'notepad'))`
:action edit default
    @if globals().has_key('line') and os.path.basename(EDITOR)[:2] == "vi":
	:sys {i} $EDITOR +$line $source
    @elif globals().has_key('byte') and os.path.basename(EDITOR)[:3] == "vim":
	:sys {i} $EDITOR -c "go $byte" $source
    @else:
	:sys {i} $EDITOR $source

:action edit gif,jpeg
    :sys gimp $source


# :do view

# Use the $BROWSER environment variable, a proposed standard; colon separated
# alternatives.  If not set, search for one of the known browsers.
BROWSER = `os.environ.get('BROWSER')`
:action view html,xml,php
    @if not BROWSER:
       :progsearch BROWSER netscape mozilla konquerer grail iexplore mosaic lynx w3m
       :export BROWSER
    :syspath '$BROWSER' $source

@if os.name == "posix":
    PAGER = `os.environ.get('PAGER', 'more')`
@else:
    PAGER = `os.environ.get('PAGER', '')`
:action view default
    @if PAGER:		    # assume $PAGER can handle multiple files
	@if (globals().has_key('line')
	@	and (os.path.basename(PAGER)[:4] == "more"
	@	    or os.path.basename(PAGER)[:4] == "less")):
	   :sys {i} $PAGER +$line $source
        @else:
	   :sys {i} $PAGER $source
    @else:		    # MS-Windows more always reads from stdin
    @  for fname in var2list(source):
          :sys {i} more <$fname

:action view gif,jpeg
    :sys xv $source


# :do compile

:action compile object,default c
    @if not globals().has_key("target"):
    @   if os.path.isabs(fname):
	    target = `aap_sufreplace("", OBJSUF, fname)`
    @   else:
	    target = $BDIR/`aap_sufreplace("", OBJSUF, fname)`
    :sys $CC $CPPFLAGS $CFLAGS -c -o $target $source

:action compile object,default cpp
    @if not globals().has_key("target"):
    @   if os.path.isabs(fname):
	    target = `aap_sufreplace("", OBJSUF, fname)`
    @   else:
	    target = $BDIR/`aap_sufreplace("", OBJSUF, fname)`
    :sys $CXX $CPPFLAGS $CXXFLAGS -c -o $target $source


# :do preproc

:action preproc default c,cpp
    @if not globals().has_key("target"):
	target = `aap_sufreplace("", ".i", fname)`
    :sys $CC $CPPFLAGS -E $source > $target


# :do build

:action build default
    :sys $CC $LDFLAGS $CFLAGS -o $target $source $?LIBS


# :do email

MAILER = `os.environ.get('MAILER', 'mail')`
:action email default
    @if globals().get("edit"):
	:do edit $fname
    :sys $MAILER -s $subject $to <$fname


# :do reference

REFERENCER =
:action reference default
    @if not REFERENCER:
	:progsearch REFERENCER aref ctags
	:export REFERENCER
    @if os.path.basename(REFERENCER) == "aref":
	:sys $REFERENCER -u $source
    @else:
	:sys $REFERENCER $source
    
# :do strip
:action strip default
    :sys strip $source

# :do extract
:action extract tar
    :sys tar xf $source
:action extract targz
    :sys tar xzf $source
:action extract tarbz2
    :sys bunzip2 -c $source | tar xf -
:action extract zip
    :sys unzip $source

# vim: set sw=4 sts=4 tw=79 :
