#
#     Copyright (c) 2026, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
#

# Variables

variable FORTRANINTEGER64BITSUFFIX is
    help("Suffix for math libraries 'ilp64' or 'lp64' version.")
    default($if($ISFORTRANINTEGER64BIT,ilp64,lp64));

variable NEEDLIBBLAS is
    help("Enable or disable linking of OpenBLAS or NVPL BLAS library.")
    default(0);

variable NEEDLIBLAPACK is
    help("Enable or disable linking of OpenBLAS or NVPL LAPACK library.")
    default(0);

variable NEEDLIBSCALAPACK is
    help("Set to append Scalapack libraries to the link line.")
    default(0);

variable LIBBLAS is
    help("OpenBLAS BLAS libraries.")
    default($if($NEEDLIBBLAS,-lblas_$FORTRANINTEGER64BITSUFFIX));

variable LIBLAPACK is
    help("OpenBLAS LAPACK libraries.")
    default($if($NEEDLIBLAPACK,-llapack_$FORTRANINTEGER64BITSUFFIX));

variable LIBSCALAPACK is
    help("Scalapack libraries.")
    default($if($NEEDLIBSCALAPACK,-lscalapack_$FORTRANINTEGER64BITSUFFIX));

append LDLIB1=--as-needed $ifn($NEEDLIBNVPL,$LIBSCALAPACK $LIBLAPACK $LIBBLAS,$LIBNVPL) --no-as-needed;

# Switches

switch -Mmathlib is
    help("Link OpenBLAS"$if($index($TARGET,linuxarm64)," or NVPL")" libraries.")
    helpgroup(linker)
    keyword(
        blas(
            set(NEEDLIBBLAS=1)
        )
        fftw(
            if($index($TARGET,linuxarm64))
            set(NEEDLIBNVPLFFTW=1)
        )
        lapack(
            set(NEEDLIBBLAS=1)
            set(NEEDLIBLAPACK=1)
        )
        openblas(
            set(NEEDLIBNVPL=0)
            set(NEEDLIBBLAS=1)
            set(NEEDLIBLAPACK=1)
        )
        rand(
            if($index($TARGET,linuxarm64))
            set(NEEDLIBNVPLRAND=1)
        )
        sparse(
            if($index($TARGET,linuxarm64))
            set(NEEDLIBNVPLSPARSE=1)
        )
        tensor(
            if($index($TARGET,linuxarm64))
            set(NEEDLIBNVPLTENSOR=1)
        )
    )
    nokeyword(
        help("Link all OpenBLAS"$if($index($TARGET,linuxarm64)," or all NVPL")" libraries.")
        set(NEEDLIBBLAS=1)
        set(NEEDLIBNVPLFFTW=1)
        set(NEEDLIBLAPACK=1)
        set(NEEDLIBNVPLRAND=1)
        set(NEEDLIBNVPLSPARSE=1)
        set(NEEDLIBNVPLTENSOR=1)
    );

switch -Mscalapack is
    help("Link Scalapack libraries.")
    helpgroup(linker)
    set(NEEDLIBSCALAPACK=1)
    set(NEEDLIBLAPACK=1)
    set(NEEDLIBBLAS=1);
