# Copyright Brian Kuhl 2017.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.txt
# or copy at https://www.bfgroup.xyz/b2/LICENSE.txt)

import args ;
import clang ;
import feature : feature get-values ;
import os ;
import toolset ;
import toolset : flags ;
import gcc ;
import common ;
import errors ;
import generators ;
import numbers ;


feature.extend-subfeature toolset clang : platform : vxworks ;
# FIXME: import order affects default value for 'platform' subfeature
import clang-linux ;

toolset.inherit-generators clang-vxworks
  <toolset>clang <toolset-clang:platform>vxworks
  : clang-linux
  ;

toolset.inherit-rules clang-vxworks : clang-linux ;
toolset.inherit-flags clang-vxworks : clang-linux ;

.debug-configuration = [ args.get-arg debug-configuration ] ;

# Initializes the clang-vxworks toolset
#   version in optional
#   compile and link options allow you to specify addition command line options for each version
rule init ( version ? :  command * : options * )
{
    command = [ common.get-invocation-command clang-vxworks : ccllvm
        : $(command) ] ;

    linker = [ get-values <linker> : $(options) ] ;

    # Determine the version
    local command-string = [ common.make-command-string $(command) ] ;
    if $(command)
    {
        version ?= [ MATCH "^([0-9.]+)"
            : [ SHELL "$(command-string) -dumpversion" ] ] ;
    }

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

    common.handle-options clang-vxworks : $(condition) : $(command) : $(options) ;

    if ! $(linker) {
        # use default linker
    }
    else if ! [ numbers.less [ MATCH "^([0-9]+)" : 0$(version:E=12) ] 12 ] {
        toolset.flags clang-vxworks.link OPTIONS $(condition) : --ld-path=$(linker) ;
    }
    else {
        toolset.flags clang-vxworks.link OPTIONS $(condition) : -fuse-ld=$(linker) ;
    }

    # - Archive builder.
    local archiver = [ feature.get-values <archiver> : $(options) ] ;
    toolset.flags clang-vxworks.archive .AR $(condition) : $(archiver[1]) ;
}
