# This file was originally based on gcc-tools.jam by David Abrahams.

# singleton variables...
set-as-singleton MIPSPRO_ROOT_DIRECTORY MIPSPRO_BIN_DIRECTORY MIPSPRO_INCLUDE_DIRECTORY MIPSPRO_C++INCLUDE_DIRECTORY MIPSPRO_STDLIB_DIRECTORY ;

# compute directories for invoking MIPSPRO
MIPSPRO_ROOT_DIRECTORY ?= "/usr" ;
MIPSPRO_BIN_DIRECTORY ?= "$(MIPSPRO_ROOT_DIRECTORY)/bin" ;

MIPSPRO_INCLUDE_DIRECTORY ?= "$(MIPSPRO_ROOT_DIRECTORY)/include" ;
MIPSPRO_C++INCLUDE_DIRECTORY ?= "$(MIPSPRO_INCLUDE_DIRECTORY)/CC" ;

# FIXME: this hardcodes N32 with mips3
MIPSPRO_STDLIB_DIRECTORY ?= "$(MIPSPRO_ROOT_DIRECTORY)/lib32/mips3" "$(MIPSPRO_ROOT_DIRECTORY)/lib32" ;

flags mipspro LINKFLAGS : -nostdlib -LANG:std ;
flags mipspro LINKFLAGS <target-type>DLL : -shared ;
flags mipspro LINKFLAGS <runtime-link>static : -non_shared ;
flags mipspro LINKFLAGS <debug-symbols>on : -g ;

# FIXME: this hardcodes N32 with mips3
flags mipspro CFLAGS : -n32 -mips3 -nostdinc -c -LANG:std -OPT:Olimit=0 -OPT:IEEE_NaN_inf=ON ;

flags mipspro CFLAGS <debug-symbols>on : -g ;

flags mipspro CFLAGS <optimization>off : -O0 ;
flags mipspro CFLAGS <optimization>speed : -O3 ;
flags mipspro CFLAGS <optimization>space : -O2 ;

flags mipspro CFLAGS <inlining>off : -INLINE:none ;
flags mipspro CFLAGS <inlining>full : -INLINE:all ;

flags mipspro C++FLAGS <exception-handling>off : -LANG:exceptions=OFF ;

flags mipspro STDHDRS : "$(MIPSPRO_INCLUDE_DIRECTORY)" ;
flags mipspro STDC++HDRS : "$(MIPSPRO_C++INCLUDE_DIRECTORY)" "$(BOOST_ROOT)/boost/compatibility/cpp_c_headers" ;
flags mipspro STDLIBPATH : "$(MIPSPRO_STDLIB_DIRECTORY)" ;

flags mipspro HDRS <include> ;
flags mipspro UNDEFS <undef> ;
flags mipspro DEFINES <define> ;
flags mipspro CFLAGS <cflags> ;
flags mipspro C++FLAGS <cxxflags> ;
flags mipspro LIBPATH <library-path> ;
flags mipspro NEEDLIBS <library-file> ;
flags mipspro FINDLIBS <find-library> ;
flags mipspro STDHDRS <sysinclude> ;
flags mipspro LINKFLAGS <linkflags> ;
flags mipspro ARFLAGS <arflags> ;

if ! $(ARFLAGS)
{
    flags mipspro ARFLAGS : "" ;
}

#### Link (for C++) ####

rule Link-action
{
    mipspro-Link-action "$(<)" : "$(>)" ;
}

# for mipspro, we repeat all libraries so that dependencies are always resolved
actions mipspro-Link-action bind NEEDLIBS
{
    "$(MIPSPRO_BIN_DIRECTORY)/CC" "$(LINKFLAGS)" -o "$(<[1])" "-L$(LIBPATH)" "-L$(STDLIBPATH)" "$(>)" "$(NEEDLIBS)" "$(NEEDLIBS)" "-l$(FINDLIBS)"
}

#### Cc #####

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

actions mipspro-Cc-action
{
    "$(MIPSPRO_BIN_DIRECTORY)/cc" "-U$(UNDEFS)" "-D$(DEFINES)" "$(CFLAGS)" "-I$(HDRS)" "-I$(STDHDRS)" -o "$(<)" "$(>)"
}

#### C++ ####

rule C++-action
{
    mipspro-C++-action "$(<)" : "$(>)" ;
}

actions mipspro-C++-action
{
    "$(MIPSPRO_BIN_DIRECTORY)/CC" "-U$(UNDEFS)" "-D$(DEFINES)" "$(CFLAGS)" "$(C++FLAGS)" "-I$(HDRS)" "-I$(STDC++HDRS)" "-I$(STDHDRS)" -o "$(<)" "$(>)"
}

#### Archive ####

rule Archive-action
{
    mipspro-Archive-action "$(<)" : "$(>)" ;
}

actions updated together piecemeal mipspro-Archive-action
{
    "$(MIPSPRO_BIN_DIRECTORY)/CC" -ar $(CFLAGS) $(C++FLAGS) -o "$(<:T)" "$(>:T)"
}
