# Copyright Vladimir Prus 2004.
# Copyright Peter Dimov 2018
#
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt
# or copy at http://www.boost.org/LICENSE_1_0.txt)

import common ;
import errors ;
import feature ;
import clang ;
import msvc ;
import os ;
import toolset ;
import generators ;
import path ;
import regex ;

feature.extend-subfeature toolset clang : platform : win ;

toolset.inherit-generators clang-win <toolset>clang <toolset-clang:platform>win : msvc : msvc.compile.c.pch msvc.compile.c++.pch ;
toolset.inherit-flags clang-win : msvc ;
toolset.inherit-rules clang-win : msvc ;

# Override default do-nothing generators.
generators.override clang-win.compile.rc : rc.compile.resource ;
generators.override clang-win.compile.mc : mc.compile ;

if [ MATCH (--debug-(clang-(win-)?)?configuration) : [ modules.peek : ARGV ] ]
{
    local rule .notice ( messages * )
    {
        ECHO "notice: [clang-win]" $(messages) ;
    }
}
else
{
    local rule .notice ( messages * )
    {
    }
}

# [ get-option archiver : 32 : $(options) ]
#
# returns <archiver-32>, or <archiver>

local rule get-option ( option : addr : options * )
{
    local r = [ feature.get-values "<$(option)-$(addr)>" : $(options) ] ;
    r ?= [ feature.get-values "<$(option)>" : $(options) ] ;
    return $(r) ;
}

# init
#
# options:
#
# <assembler>ml.exe (or <assembler-32>, or <assembler-64>)
# <archiver>lib.exe
# <manifest-tool>mt.exe
# <resource-compiler>rc.exe
# <mc-compiler>mc.exe
# <idl-compiler>midl.exe

rule init ( version ? : command * : options * )
{
    command = [ common.get-invocation-command-nodefault clang-win : clang-cl.exe : $(command) ] ;

    if ! $(command)
    {
        errors.error "Cannot configure toolset clang-win: no 'clang-cl.exe' command found or given" ;
    }

    local compiler = "\"$(command)\"" ;
    compiler = "$(compiler:J= )" ;

    version ?= [ MATCH "version ([0-9.]+)" : [ SHELL "$(compiler) -v 2>&1" ] ] ;

    .notice "using compiler '$(compiler)', version '$(version)'" ;

    local condition = [  common.check-init-parameters clang-win : version $(version) ] ;

    common.handle-options clang-win : $(condition) : $(command) : $(options) ;
    
    for local addr in 32 64
    {
        local config = [ SPLIT_BY_CHARACTERS [ SHELL "$(compiler) -m$(addr) -### foo.obj /link 2>&1" ] : "\n" ] ;

        local match = 1 ;
        local items ;

        while $(match)
        {
            match = [ MATCH "^ *(\"[^\"]*\")(.*)" : $(config) ] ;

            if $(match)
            {
                items += $(match[1]) ;
                config = $(match[2]) ;
            }
        }

        local ml = [ regex.replace $(items[1]) "x64\\\\+link\\.exe" "x64\\ml64.exe" ] ;
        ml = [ regex.replace $(ml) "x86\\\\+link\\.exe" "x86\\ml.exe" ] ;

        if ! [ MATCH "(ml\\.exe)" "(ml64\\.exe)" : $(ml) ]
        {
            ml = ;
        }

        local assembler = [ get-option "assembler" : $(addr) : $(options) ] ;
        assembler ?= $(ml) ;
        if $(addr) = 32 { assembler ?= ml.exe ; } else { assembler ?= ml64.exe ; }

        local link = [ regex.replace $(items[1]) "\\\\+HostX64\\\\+x86\\\\+" "\\HostX86\\x86\\" ] ;

        local archiver = [ get-option "archiver" : $(addr) : $(options) ] ;
        archiver ?= "$(link) /lib" ;
        archiver ?= lib.exe ;

        .notice "$(addr):" "using assembler '$(assembler)'" ;
        .notice "$(addr):" "using archiver '$(archiver)'" ;

        local manifest-tool = [ get-option "manifest-tool" : $(addr) : $(options) ] ;
        local resource-compiler = [ get-option "resource-compiler" : $(addr) : $(options) ] ;
        local mc-compiler = [ get-option "mc-compiler" : $(addr) : $(options) ] ;
        local idl-compiler = [ get-option "idl-compiler" : $(addr) : $(options) ] ;

        for local item in $(items)
        {
            match = [ MATCH "\"-libpath:(.*)\\\\+Lib\\\\.*\\\\um\\\\+x(.*)\"" : $(item) ] ;

            if $(match)
            {
                local sdk-path = "$(match[1])\\bin\\x$(match[2])" ;
                .notice "$(addr):" "using SDK path '$(sdk-path)'" ;

                manifest-tool ?= "\"$(sdk-path)\\mt.exe\"" ;
                resource-compiler ?= "\"$(sdk-path)\\rc.exe\"" ;
                mc-compiler ?= "\"$(sdk-path)\\mc.exe\"" ;
                idl-compiler ?= "\"$(sdk-path)\\midl.exe\"" ;
            }
        }

        manifest-tool ?= mt.exe ;
        resource-compiler ?= rc.exe ;
        mc-compiler ?= mc.exe ;
        idl-compiler ?= midl.exe ;

        .notice "$(addr):" "using manifest-tool '$(manifest-tool)'" ;
        .notice "$(addr):" "using resource-compiler '$(resource-compiler)'" ;
        .notice "$(addr):" "using mc-compiler '$(mc-compiler)'" ;
        .notice "$(addr):" "using idl-compiler '$(idl-compiler)'" ;

        local cond = "$(condition)/<architecture>/<address-model>$(addr)" "$(condition)/<architecture>x86/<address-model>$(addr)" ;
        if $(addr) = 32 { cond += "$(condition)/<architecture>/<address-model>" ; }

        toolset.flags clang-win.compile .CC $(cond) : $(compiler) -m$(addr) ;
        toolset.flags clang-win.link .LD $(cond) : $(compiler) -m$(addr) /link "/incremental:no" ;
        toolset.flags clang-win.compile .ASM $(cond) : $(assembler) -nologo ;
        toolset.flags clang-win.archive .LD $(cond) : $(archiver) /nologo ;
        toolset.flags clang-win.link .MT $(cond) : $(manifest-tool) -nologo ;
        toolset.flags clang-win.compile .MC $(cond) : $(mc-compiler) ;
        toolset.flags clang-win.compile .RC $(cond) : $(resource-compiler) /nologo ;
        toolset.flags clang-win.compile .IDL $(cond) : $(idl-compiler) ;
    }
}
