# -----------------------------------------------------------------------------------------
#
#   jamrules
#
#   Bob Cook Development, Robotics Library
#   http://www.bobcookdev.com/rl/
#    
#   Implements the base jamrules for all projects.
#
#   Copyright (C) 2009 Bob Cook
#
#       This program is free software; you can redistribute it and/or modify it
#       under the terms of the GNU General Public License as published by the
#       Free Software Foundation; either version 2 of the License, or (at your
#       option) any later version.
#     
#       This program is distributed in the hope that it will be useful, but 
#       WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
#       or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
#       for more details.
#     
#       You should have received a copy of the GNU General Public License along
#       with this program; if not, write to the Free Software Foundation, Inc., 
#       51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# -----------------------------------------------------------------------------------------


if $(TRACE) { echo "trace /jamrules" ; }


# -----------------------------------------------------------------------------------------
#   Name some standard files
#

JAMDEFS = jamdefs ;


# -----------------------------------------------------------------------------------------
#   SubDirHook is called after the built-in SubDir rule executes
#

rule SubDirHook
{
    ### echo "this is SubDirHook: $(1)" ;
}

SUBDIRRULES += SubDirHook ;


# -----------------------------------------------------------------------------------------

rule SubIncludeJamdefs # TOP dir ... : subdir ... ;
{
    local subdir ;
    
    for subdir in $(>)
    {
        local subdir_path = [ FSubDirPath $(<) $(subdir) ] ;
        include $(JAMDEFS:D=$(subdir_path)) ;
    }
}


# -----------------------------------------------------------------------------------------

rule MakePackageNameVar # dir ... ;
{
    local pkg_name = ;
    local dir ;
    
    for dir in $(1)
    {
        if $(pkg_name)
        {
            pkg_name = $(pkg_name).$(dir:L) ;
        }
        else
        {
            pkg_name = $(dir:L) ;
        }
    }
    
    if ! $(pkg_name)
    {
        exit "MakePackageNameVar: no package path specified" ;
    }
    
    pkg_name = $(pkg_name).pkg ;
    
    return $(pkg_name) ;
}


# -----------------------------------------------------------------------------------------

rule PackageDirFromNameVar # pkgname ;
{
    if ! $(1[1]) { exit "*** PackageDirFromNameVar: must have one pkg name" ; }
    if   $(1[2]) { exit "*** PackageDirFromNameVar: must have one pkg name" ; }
    if   $(2[1]) { exit "*** PackageDirFromNameVar: must have one pkg name" ; }
    
    return $($(1)) ;
}


# -----------------------------------------------------------------------------------------

rule DefinePackages # TOP dir ... : pkg ... ;
{
    local parent_dir = [ FSubDirPath $(1) ] ;
    local pkg ;
    
    for pkg in $(2)
    {
        local pkg_name = [ MakePackageNameVar $(1[2-]) $(pkg) ] ;
        local pkg_dir  = [ FDirName $(parent_dir) $(pkg) ] ;
        $(pkg_name) = $(pkg_dir) ;
    }
}


# -----------------------------------------------------------------------------------------

rule PackageSources # src ... ;
{
    local src ;
    
    for src in $(1)
    {
        PKG_SOURCES += [ FSubDirPath TOP $(SUBDIR_TOKENS) $(src) ] ;
    }
}


# -----------------------------------------------------------------------------------------

rule IncludePackage # pkg ;
{
    if ! $(1[1]) { exit "*** IncludePackage: must have one pkg name" ; }
    if   $(1[2]) { exit "*** IncludePackage: must have one pkg name" ; }
    
    # save some vars overwritten by the include/SubDir rules
    
    local sv_SUBDIR_UP     = $(SUBDIR_UP) ;
    local sv_SUBDIR_DOWN   = $(SUBDIR_DOWN) ;
    local sv_SUBDIR_ROOT   = $(SUBDIR_ROOT) ;
    local sv_SUBDIR_TOKENS = $(SUBDIR_TOKENS) ;
    local sv_SUBDIR        = $(SUBDIR) ;
    local sv_SUBDIRHDRS    = $(SUBDIRHDRS) ;
    local sv_SEARCH_SOURCE = $(SEARCH_SOURCE) ;
    local sv_LOCATE_SOURCE = $(LOCATE_SOURCE) ;
    local sv_LOCATE_TARGET = $(LOCATE_TARGET) ;
    local sv_SOURCE_GRIST  = $(SOURCE_GRIST) ;
    local sv_SUBDIRRESET   = $(SUBDIRRESET) ;
    local sv_SUBDIRRULES   = $(SUBDIRRULES) ;

    # include the specified jamfile
    
    local pkgdir = [ PackageDirFromNameVar $(1) ] ;    
    include $(JAMFILE:D=$(pkgdir)) ;

    # restore the saved vars
    
    SUBDIR_UP     = $(sv_SUBDIR_UP) ;
    SUBDIR_DOWN   = $(sv_SUBDIR_DOWN) ;
    SUBDIR_ROOT   = $(sv_SUBDIR_ROOT) ;
    SUBDIR_TOKENS = $(sv_SUBDIR_TOKENS) ;
    SUBDIR        = $(sv_SUBDIR) ;
    SUBDIRHDRS    = $(sv_SUBDIRHDRS) ;
    SEARCH_SOURCE = $(sv_SEARCH_SOURCE) ;
    LOCATE_SOURCE = $(sv_LOCATE_SOURCE) ;
    LOCATE_TARGET = $(sv_LOCATE_TARGET) ;
    SOURCE_GRIST  = $(sv_SOURCE_GRIST) ;
    SUBDIRRESET   = $(sv_SUBDIRRESET) ;
    SUBDIRRULES   = $(sv_SUBDIRRULES) ;
}


# -----------------------------------------------------------------------------------------

local avrincfile = [ FSubDirPath TOP jamrules.avr.inc ] ;
include $(avrincfile) ;

local hammerincfile = [ FSubDirPath TOP jamrules.hammer.inc ] ;
include $(hammerincfile) ;

if $(NT)
{
    local incfile = [ FSubDirPath TOP jamrules.win.inc ] ;
    include $(incfile) ;
}
else if $(UNIX)
{
    local incfile = [ FSubDirPath TOP jamrules.unix.inc ] ;
    include $(incfile) ;
}
else
{
    exit "*** jamrules: unsupported platform" ;
}


# -----------------------------------------------------------------------------------------

SubIncludeJamdefs : TOP ;


# -----------------------------------------------------------------------------------------

