# Copyright Vladimir Prus 2004.
# Copyright Toon Knapen 2004.
# Copyright Catherine Morton 2015.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.txt
# or copy at https://www.bfgroup.xyz/b2/LICENSE.txt)

#
# B2 V2 toolset for the IBM XL C++ compiler
#

import toolset : flags ;
import feature ;
import common ;
import generators ;
import os ;

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

# Configure the xlcpp toolset
rule init ( version ? : command * : options * )
{
    local condition = [
        common.check-init-parameters xlcpp : version $(version) ] ;

    command = [ common.get-invocation-command xlcpp : xlC
        : $(command) : "/usr/xlcpp/bin/xlC" ] ;

    common.handle-options xlcpp : $(condition) : $(command) : $(options) ;
}

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

# Allow C++ style comments in C files
flags xlcpp CFLAGS : -qnoxlcompatmacros ;

# Declare flags
flags xlcpp CFLAGS <optimization>off : -qNOOPTimize ;
flags xlcpp CFLAGS <optimization>speed :  ;
flags xlcpp CFLAGS <optimization>space : -O2 -qcompact ;

# Discretionary inlining (not recommended)
flags xlcpp CFLAGS <inlining>off : -qnoinline ;
flags xlcpp CFLAGS <inlining>on : -qinline ;
#flags xlcpp CFLAGS <inlining>full : -qinline ;
flags xlcpp CFLAGS <inlining>full : ;

# Exception handling
flags xlcpp C++FLAGS <exception-handling>off : -qnoeh ;
flags xlcpp C++FLAGS <exception-handling>on : -qeh ;

# Run-time Type Identification
flags xlcpp C++FLAGS <rtti>off : -qnortti ;
flags xlcpp C++FLAGS <rtti>on : -qrtti ;

# Enable 64-bit memory addressing model
flags xlcpp CFLAGS <address-model>64 : -q64 ;
flags xlcpp LINKFLAGS <address-model>64 : -q64 ;
flags xlcpp ARFLAGS <target-os>aix/<address-model>64 : -X 64 ;

# Use absolute path when generating debug information
flags xlcpp CFLAGS <debug-symbols>on : -g -qfullpath ;
flags xlcpp LINKFLAGS <debug-symbols>on : -g -qfullpath ;
flags xlcpp LINKFLAGS <debug-symbols>off : -s ;

if [ os.name ] = AIX
{
    flags xlcpp.compile C++FLAGS : -qfuncsect ;

    # The -bnoipath strips the prepending (relative) path of libraries from
    # the loader section in the target library or executable. Hence, during
    # load-time LIBPATH (identical to LD_LIBRARY_PATH) or a hard-coded
    # -blibpath (*similar* to -lrpath/-lrpath-link) is searched. Without
    # this option, the prepending (relative) path + library name is
    # hard-coded in the loader section, causing *only* this path to be
    # searched during load-time. Note that the AIX linker does not have an
    # -soname equivalent, this is as close as it gets.
    #
    # The above options are definitely for AIX 5.x, and most likely also for
    # AIX 4.x and AIX 6.x. For details about the AIX linker see:
    # http://download.boulder.ibm.com/ibmdl/pub/software/dw/aix/es-aix_ll.pdf
    #
    flags xlcpp.link LINKFLAGS <link>shared : -bnoipath ;

    # Run-time linking
    flags xlcpp.link EXE-LINKFLAGS <link>shared : -brtl ;
}
else
{
    # Linux PPC
    flags xlcpp.compile CFLAGS <link>shared : -qpic=large ;
    flags xlcpp FINDLIBS : rt ;

    flags xlcpp.compile OPTIONS <local-visibility>hidden : -qvisibility=hidden ;
    flags xlcpp.compile OPTIONS <local-visibility>protected : -qvisibility=protected ;
    flags xlcpp.compile OPTIONS <local-visibility>global : -qvisibility=default ;
}

# Profiling
flags xlcpp CFLAGS <profiling>on : -pg ;
flags xlcpp LINKFLAGS <profiling>on : -pg ;

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

flags xlcpp.compile OPTIONS <cflags> ;
flags xlcpp.compile.c++ OPTIONS <cxxflags> ;
flags xlcpp DEFINES <define> ;
flags xlcpp UNDEFS <undef> ;
flags xlcpp HDRS <include> ;
flags xlcpp STDHDRS <sysinclude> ;
flags xlcpp.link OPTIONS <linkflags> ;
flags xlcpp ARFLAGS <arflags> ;

flags xlcpp LIBPATH <library-path> ;
flags xlcpp NEEDLIBS <library-file> ;
flags xlcpp FINDLIBS <find-shared-library> ;
flags xlcpp FINDLIBS <find-static-library> ;

# Select the compiler name according to the threading model.
flags xlcpp VA_C_COMPILER  <threading>single : xlc   ;
flags xlcpp VA_C_COMPILER  <threading>multi : xlc ;
flags xlcpp VA_CXX_COMPILER <threading>single : xlC   ;
flags xlcpp VA_CXX_COMPILER <threading>multi : xlC ;

SPACE = " " ;

flags xlcpp.link.dll HAVE_SONAME <target-os>linux : "" ;

actions xlcpp.link bind NEEDLIBS
{
    $(VA_CXX_COMPILER) $(EXE-LINKFLAGS) $(LINKFLAGS) -o "$(<[1])" -L$(LIBPATH) -L$(STDLIBPATH) "$(>)" "$(NEEDLIBS)" "$(NEEDLIBS)" -l$(FINDLIBS) $(OPTIONS) $(USER_OPTIONS)
}

actions xlcpp.link.dll bind NEEDLIBS
{
    xlC -G $(LINKFLAGS) -o "$(<[1])" $(HAVE_SONAME)-Wl,-soname$(SPACE)-Wl,$(<[-1]:D=) -L$(LIBPATH) -L$(STDLIBPATH) "$(>)" "$(NEEDLIBS)" "$(NEEDLIBS)" -l$(FINDLIBS) $(OPTIONS) $(USER_OPTIONS)
}

actions xlcpp.compile.c
{
    $(VA_C_COMPILER) -c $(OPTIONS) $(USER_OPTIONS) -I$(BOOST_ROOT) -U$(UNDEFS) -D$(DEFINES) $(CFLAGS) -I"$(HDRS)" -I"$(STDHDRS)" -o "$(<)" "$(>)"
}

actions xlcpp.compile.c++
{
    $(VA_CXX_COMPILER) -c $(OPTIONS) $(USER_OPTIONS) -I$(BOOST_ROOT) -U$(UNDEFS) -D$(DEFINES) $(CFLAGS) $(C++FLAGS) -I"$(HDRS)" -I"$(STDHDRS)" -o "$(<)" "$(>)"
}

actions updated together piecemeal xlcpp.archive
{
    ar $(ARFLAGS) ru "$(<)" "$(>)"
}
