#  Copyright Noel Belcourt 2007.
#  Copyright 2017, NVIDIA CORPORATION.
#  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 property ;
import generators ;
import os ;
import toolset : flags ;
import feature ;
import fortran ;
import type ;
import common ;
import gcc ;
import regex ;

feature.extend toolset : pgi ;
toolset.inherit pgi : unix ;

# Documentation and toolchain description located
# http://www.pgroup.com/resources/docs.htm

rule init ( version ? : command * : options * )
{
  local condition = [ common.check-init-parameters pgi : version $(version) ] ;

  local l_command = [ common.get-invocation-command pgi : pgc++ : $(command) ] ;

  common.handle-options pgi : $(condition) : $(l_command) : $(options) ;

  command_c = $(command_c[1--2]) $(l_command[-1]:B=pgcc) ;

  toolset.flags pgi CONFIG_C_COMMAND $(condition) : $(command_c) ;

  flags pgi.compile DEFINES $(condition) :
    [ feature.get-values <define> : $(options) ] : unchecked ;

  # set link flags
  flags pgi.link FINDLIBS-ST : [
    feature.get-values <find-static-library> : $(options) ] : unchecked ;
}

# Declare generators
generators.register-c-compiler pgi.compile.c : C : OBJ : <toolset>pgi ;
generators.register-c-compiler pgi.compile.c++ : CPP : OBJ : <toolset>pgi ;
generators.register-fortran-compiler pgi.compile.fortran : FORTRAN : OBJ : <toolset>pgi ;

# Declare flags and actions for compilation
flags pgi.compile.c++ OPTIONS <cxxstd>98 : -std=c++03 ;
flags pgi.compile.c++ OPTIONS <cxxstd>03 : -std=c++03 ;
flags pgi.compile.c++ OPTIONS <cxxstd>0x : -std=c++11 ;
flags pgi.compile.c++ OPTIONS <cxxstd>11 : -std=c++11 ;
flags pgi.compile.c++ OPTIONS <cxxstd>1y : -std=c++14 ;
flags pgi.compile.c++ OPTIONS <cxxstd>14 : -std=c++14 ;
flags pgi.compile.c++ OPTIONS <cxxstd>1z : -std=c++17 ;
flags pgi.compile.c++ OPTIONS <cxxstd>17 : -std=c++17 ;
flags pgi.compile.c++ OPTIONS <cxxstd>2a : -std=c++17 ;
flags pgi.compile.c++ OPTIONS <cxxstd>20 : -std=c++17 ;
flags pgi.compile.c++ OPTIONS <cxxstd>latest : -std=c++17 ;

flags pgi.compile OPTIONS <link>shared : -fpic ;
flags pgi.compile OPTIONS <debug-symbols>on : -gopt ;
flags pgi.compile OPTIONS <optimization>off : -O0 ;
flags pgi.compile OPTIONS <optimization>speed : -fast ;
flags pgi.compile OPTIONS <optimization>space : -fast ;

flags pgi.compile OPTIONS <warnings>off : -Minform=severe ;
flags pgi.compile OPTIONS <warnings>on : -Minform=warn ;
flags pgi.compile OPTIONS <warnings>all : -Minform=warn ;
flags pgi.compile OPTIONS <warnings>extra : -Minform=inform ;
flags pgi.compile OPTIONS <warnings>pedantic : -Minform=inform ;
flags pgi.compile OPTIONS <warnings-as-errors>on : -Werror ;

flags pgi.compile.c++ OPTIONS <rtti>off : --no_rtti ;
flags pgi.compile.c++ OPTIONS <exception-handling>off : --no_exceptions ;

flags pgi.compile OPTIONS <cflags> ;
flags pgi.compile.c++ OPTIONS <cxxflags> ;
flags pgi.compile DEFINES <define> ;
flags pgi.compile INCLUDES <include> ;

flags pgi.compile.fortran OPTIONS <fflags> ;

rule compile.c ( targets * : sources * : properties * )
{
    local config_command = [ regex.escape
        [ on $(targets[1]) return $(CONFIG_C_COMMAND:E="") ]
        : "()[]\\+.*^$\"" : "\\" ] ;
    COMMAND_DATABASE on $(targets) = "(\"$(config_command)\"[^\n]*)" ;
}

rule compile.c++ ( targets * : sources * : properties * )
{
    local config_command = [ regex.escape
        [ on $(targets[1]) return $(CONFIG_COMMAND:E="") ]
        : "()[]\\+.*^$\"" : "\\" ] ;
    COMMAND_DATABASE on $(targets) = "(\"$(config_command)\"[^\n]*)" ;
}

actions compile.c
{
    "$(CONFIG_C_COMMAND)" $(OPTIONS) -D$(DEFINES) -I"$(INCLUDES)" -c -o "$(<)" "$(>)"
}

actions compile.c++
{
    "$(CONFIG_COMMAND)" $(OPTIONS) -D$(DEFINES) -I"$(INCLUDES)" -c -o "$(<)" "$(>)"
}

actions compile.fortran
{
    "$(CONFIG_F_COMMAND)" $(OPTIONS) -D$(DEFINES) -I"$(INCLUDES)" -c -o "$(<)" "$(>)"
}

# Declare flags and actions for linking
flags pgi.link OPTIONS <debug-symbols>on : -gopt ;
# Strip the binary when no debugging is needed
flags pgi.link OPTIONS <debug-symbols>off : -s ;
flags pgi.link OPTIONS <linkflags> ;
flags pgi.link OPTIONS <link>shared : -fpic ;
flags pgi.link LINKPATH <library-path> ;
flags pgi.link FINDLIBS-ST <find-static-library> ;
flags pgi.link FINDLIBS-SA <find-shared-library> ;
flags pgi.link FINDLIBS-SA <threading>multi : pthread rt ;
flags pgi.link LIBRARIES <library-file> ;
flags pgi.link LINK-RUNTIME <runtime-link>static : static ;
flags pgi.link LINK-RUNTIME <runtime-link>shared : dynamic ;
flags pgi.link RPATH <dll-path> ;

rule link ( targets * : sources * : properties * )
{
    SPACE on $(targets) = " " ;
}

actions link bind LIBRARIES
{
    "$(CONFIG_COMMAND)" $(OPTIONS) -L"$(LINKPATH)" -R"$(RPATH)" -o "$(<)" "$(>)" "$(LIBRARIES)" -l$(FINDLIBS-ST) -l$(FINDLIBS-SA)
}

# Slight mods for dlls
rule link.dll ( targets * : sources * : properties * )
{
    SPACE on $(targets) = " " ;
}

actions link.dll bind LIBRARIES
{
    "$(CONFIG_COMMAND)" $(OPTIONS) -shared -L"$(LINKPATH)" -R"$(RPATH)" -soname $(<[-1]:D=) -o "$(<)" "$(>)" "$(LIBRARIES)" -l$(FINDLIBS-SA) -l$(FINDLIBS-ST)
}

actions updated together piecemeal pgi.archive
{
    ar -rc$(ARFLAGS:E=) "$(<)" "$(>)"
}

