# (C) Copyright David Abrahams 2001. Permission to copy, use,
# modify, sell and distribute this software is granted provided this
# copyright notice appears in all copies. This software is provided
# "as is" without express or implied warranty, and with no claim as
# to its suitability for any purpose.

# You can either use Metrowerks-supplied scripts (e.g. "<CodeWarrior>/Other
# Metrowerks Tools/Command Line Tools/cwenv.bat") to set up the appropriate
# environment, set up environment variables by hand as described in
# "<CodeWarrior>/Release Notes/CLT Notes/CLT_Usage_Notes.txt", or you can define
# METROWERKS_ROOT to be the metrowerks installation directory, and cwenv.bat
# will be invoked before each tool invocation.

# We go this route because the environment needed to run the metrowerks tools
# from the command-line is sufficiently complex to make using the borland
# approach impractical.

# if METROWERKS_ROOT is unset, this will "multiply away" to nothing.
METROWERKS_SETUP = "call "\"$(METROWERKS_ROOT)$(SLASH)"Other Metrowerks Tools"$(SLASH)"Command Line Tools"$(SLASH)cwenv.bat\"" -quiet" ;
METROWERKS_LINK_SETUP = "set MWWinx86LibraryFiles=" ;

flags metrowerks CFLAGS <cflags> ;
flags metrowerks C++FLAGS <cxxflags> ;
flags metrowerks DEFINES <define> ;
flags metrowerks UNDEFS <undef> ;
flags metrowerks HDRS <include> ;

flags metrowerks CFLAGS <debug-symbols>on : -g ;
flags metrowerks LINKFLAGS <debug-symbols>on : -g ;

flags metrowerks LINKFLAGS <target-type>DLL : -shared ;
flags metrowerks CFLAGS <optimization>off : -O0 ;
flags metrowerks CFLAGS <optimization>speed : -opt full ;
flags metrowerks CFLAGS <optimization>space : -O4,s intrinsics ;
flags metrowerks CFLAGS <inlining>off : -inline off ;
flags metrowerks CFLAGS <inlining>on : -inline on ;
flags metrowerks CFLAGS <inlining>full : -inline auto -inline level=8 ;
flags metrowerks CFLAGS <user-interface>console/<runtime-link>dynamic : -prefix UseDLLPrefix.h ;

if $(BOOST_CODEWARRIOR_7)
{
  flags metrowerks STDLIBS <runtime-link>static/<runtime-build>release : MSL_C_x86.lib;MSL_Runtime_x86.lib;msl_c++_x86.lib;gdi32.lib;user32.lib;kernel32.lib ;
  flags metrowerks STDLIBS <runtime-link>dynamic/<runtime-build>release : MSL_All-DLL_x86.lib;gdi32.lib;user32.lib;kernel32.lib ;
  flags metrowerks STDLIBS <runtime-link>static/<runtime-build>debug : MSL_C_x86_D.lib;MSL_Runtime_x86_D.lib;msl_c++_x86_D.lib;gdi32.lib;user32.lib;kernel32.lib ;
  flags metrowerks STDLIBS <runtime-link>dynamic/<runtime-build>debug : MSL_All-DLL_x86_D.lib;gdi32.lib;user32.lib;kernel32.lib ;
}
else
{
  flags metrowerks STDLIBS <runtime-link>static : ansicx86.lib;mwcrtl.lib;ansicppx86.lib;gdi32.lib;user32.lib;kernel32.lib ;
  flags metrowerks STDLIBS <runtime-link>dynamic : mwcrtldll.lib;gdi32.lib;user32.lib;kernel32.lib ;
}

flags metrowerks LIBPATH <library-path> ;
flags metrowerks NEEDLIBS <library-file> ;
flags metrowerks FINDLIBS <find-library> ;

# How will we find metrowerks standard headers?
# If the user specified METROWERKS_INCLUDE_ROOT, use that
# Otherwise, if he specified METROWERKS_ROOT, use that
# Otherwise, look at the "standard" environment variable CWFOLDER
METROWERKS_INCLUDE_ROOT ?= $(METROWERKS_ROOT) ;
METROWERKS_INCLUDE_ROOT ?= $(CWFOLDER) ;

# This is the list of subdirectories below METROWERKS_INCLUDE_ROOT to search.
# Might need customization for platforms other than Win32
gMETROWERKS_STD_INCLUDES = MSL "Win32-X86 Support" MSL$(SLASH)MSL_C$(SLASH)MSL_Common$(SLASH)Include "Win32-X86 Support"$(SLASH)Headers$(SLASH)"Win32 SDK" ;

# Multiply these out to get the STDHDRS settings for metrowerks
flags metrowerks STDHDRS : $(METROWERKS_INCLUDE_ROOT)$(SLASH)$(gMETROWERKS_STD_INCLUDES) ;

#### Link ####

rule Link-action
{
    IMPLIB_COMMAND on $(<) = "-implib " ; # will be "multiplied away" if not
                                        # needed.
    with-command-file metrowerks-Link-action $(<) : $(>) $(NEEDLIBS) ;
}

# Here we set the environment variable MWWinx86LibraryFiles (customize for other
# platforms) because the alternatives for getting the right libraries on the
# command-line would produce enormous paths in the command-line, obscuring what
# really happens.
actions metrowerks-Link-action
{
    $(METROWERKS_SETUP)
    $(METROWERKS_LINK_SETUP)$(STDLIBS)
    mwld  -maxerrors 20 -maxwarnings 20 -export dllexport -nowraplines $(LINKFLAGS) -L$(LIBPATH) -L$(STDLIBPATH) $(IMPLIB_COMMAND)"$(<[2])" -o "$(<[1])" @"$(>)" -l$(FINDLIBS)
}

#### Cc #####

rule Cc-action
{
    metrowerks-Cc-action $(<) : $(>) ;
}

actions metrowerks-Cc-action
{
    $(METROWERKS_SETUP)
    mwcc -maxerrors 20 -maxwarnings 20 -c -warn on,nounusedexpr,nounused -cwd include -U$(UNDEFS) -D$(DEFINES) -nowraplines $(CFLAGS) -I- -I"$(HDRS)" -I"$(STDHDRS)" -o "$(<)" "$(>)"
}

#### C++ ####
rule C++-action
{
    metrowerks-C++-action $(<) : $(>) ;
}

actions metrowerks-C++-action
{
    $(METROWERKS_SETUP)
    mwcc  -maxerrors 20 -maxwarnings 20 -c -warn on,nounusedexpr,nounused -cwd include -U$(UNDEFS) -D$(DEFINES) -DNOMINMAX -nowraplines -lang c++ $(CFLAGS) $(C++FLAGS) -I- -I"$(HDRS)" -I"$(STDHDRS)" -o "$(<)" "$(>)"
}

#### Archive ####
rule Archive-action
{
    metrowerks-Archive-action $(<) : $(>) ;
}

    actions together metrowerks-Archive-action
{
    $(METROWERKS_SETUP)
    mwld -library -o "$(<)" "$(>)"
}

