# (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.

# singleton variables...
set-as-singleton MINGW_ROOT_DIRECTORY MINGW_BIN_DIRECTORY MINGW_INCLUDE_DIRECTORY MINGW_STDLIB_DIRECTORY ;

# compute directories for invoking MINGW
MINGW_BIN_DIRECTORY ?= $(MINGW_ROOT_DIRECTORY)$(SLASH)bin$(SLASH) ;
MINGW_BIN_DIRECTORY ?= "" ; # Don't clobber tool names if MINGW_ROOT_DIRECTORY not set
MINGW_INCLUDE_DIRECTORY ?= $(MINGW_ROOT_DIRECTORY)$(SLASH)include ;
MINGW_STDLIB_DIRECTORY ?= $(MINGW_ROOT_DIRECTORY)$(SLASH)lib ;

{
    local GCC_INCLUDE_DIRECTORY = $(MINGW_INCLUDE_DIRECTORY) ;
    local GCC_STDLIB_DIRECTORY = $(MINGW_STDLIB_DIRECTORY) ;
    extends-toolset gcc ;
}

flags mingw CFLAGS <threading>multi : -mthreads ;
flags mingw LINKFLAGS <threading>multi : -mthreads ;

#### Link ####

rule Link-action
{
    # This will appear before the import library name when building a DLL, but
    # will be "multiplied away" otherwise. The --exclude-symbols directive
    # proved to be neccessary with some versions of Cygwin.
    IMPLIB_COMMAND on $(<) = "-Wl,--exclude-symbols,_bss_end__:_bss_start__:_data_end__:_data_start__ -Wl,--out-implib," ;
    mingw-Link-action $(<) : $(>) ;
}

# for mingw, we repeat all libraries so that dependencies are always resolved
actions mingw-Link-action bind NEEDLIBS
{
    $(MINGW_BIN_DIRECTORY)g++ $(IMPLIB_COMMAND)$(<[2]) $(LINKFLAGS) -o "$(<[1])" -L$(LIBPATH) -L$(STDLIBPATH) "$(>)" "$(NEEDLIBS)" "$(NEEDLIBS)" -l$(FINDLIBS) -Wl,-rpath-link,.
}

#### Cc #####

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

actions mingw-Cc-action
{
    $(MINGW_BIN_DIRECTORY)mingw -c -Wall -U$(UNDEFS) -D$(DEFINES) $(CFLAGS) -I"$(HDRS)" -I"$(STDHDRS)" -o "$(<)" "$(>)"
}

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

actions mingw-C++-action
{
    $(MINGW_BIN_DIRECTORY)g++ -c -Wall -ftemplate-depth-100 -U$(UNDEFS) -D$(DEFINES) $(CFLAGS) $(C++FLAGS) -I"$(HDRS)" -I"$(STDHDRS)" -o "$(<)" "$(>)"
}

#### Archive ####

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

actions updated together piecemeal mingw-Archive-action
{
    ar ru$(ARFLAGS) "$(<:T)" "$(>:T)"
}

