# -*- Python -*- vim: set ft=python ts=4 sw=4 expandtab tw=79:

#
# Copyright (c) 2018-2019, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Configuration file for the 'lit' test runner.

import ctypes
import lit.formats
import os
import re
import subprocess

# Tell pylint that we know config and lit_config exist somewhere.
if 'PYLINT_IMPORT' in os.environ:
    config = object()
    lit_config = object()

def append_dynamic_library_path(path):
    if config.operating_system == 'Windows':
        name = 'PATH'
        sep = ';'
    elif config.operating_system == 'Darwin':
        name = 'DYLD_LIBRARY_PATH'
        sep = ':'
    else:
        name = 'LD_LIBRARY_PATH'
        sep = ':'
    if name in config.environment:
        config.environment[name] = path + sep + config.environment[name]
    else:
        config.environment[name] = path

if(config.architecture == "x86_64"):
    lib = ctypes.cdll.LoadLibrary(config.libpgmath_getarchlib_dir + "/" + config.libpgmath_getarchlib)
    lib.get_arch.restype = ctypes.c_char_p
    arch = ctypes.c_char_p(lib.get_arch()).value.decode("utf-8")
    config.available_features.add(arch)

config.available_features.add(config.architecture)

# host_triple = getattr(config, 'host_triple', None)
# target_triple = getattr(config, 'target_triple', None)
# if host_triple and host_triple == target_triple:
config.available_features.add('native')

# name: The name of this test suite.
config.name = 'libpgmath'

# suffixes: A list of file extensions to treat as test files.
config.suffixes = ['.c']

# test_source_root: The root path where tests are located.
config.test_source_root = os.path.dirname(__file__)

# test_exec_root: The root object directory where output is placed
config.test_exec_root = config.libpgmath_obj_root

# test format
config.test_format = lit.formats.ShTest()

if config.operating_system == 'Windows':
    append_dynamic_library_path(config.libpgmath_runtime_dir)

# compiler flags
config.test_cflags = ""
if config.operating_system != 'Windows':
    config.test_cflags += " -g"

if config.architecture == "x86_64":
    if config.operating_system == 'Darwin':
        config.test_cflags += " -DTARGET_OSX_X8664"
    elif config.operating_system == 'Windows':
        config.test_cflags += " -DTARGET_WIN_X8664"
    elif config.operating_system == 'Linux':
        config.test_cflags += " -DTARGET_LINUX_X8664"

config.test_cflags += " -O1 " \
                     "-I" + config.test_source_root

libs = ""
if config.has_libpgmath_runtime:
    if config.operating_system != 'Windows':
        libs += " " + os.path.join(config.libpgmath_runtime_dir,
            config.libpgmath_runtime) + \
            " -Wl,-rpath," + config.libpgmath_runtime_dir
    else:
        libs += " " + os.path.join(config.libpgmath_runtime_dir,
            config.libpgmath_runtime.replace(".dll", "") + ".lib")

if config.has_libm and config.operating_system != 'Windows':
    libs += " -lm"

config.ompt_test_compiler = config.test_compiler

if(config.architecture == "x86_64"):
    config.test_cflags += " -march=native"
elif(config.architecture == "ppc64le"):
    config.test_cflags += " -mcpu=native"

config.substitutions.append(("%libpgmath-compile", \
    "%compiler %cflags %s -o %t " + libs))
config.substitutions.append(("%libpgmath-run", "%t"))
config.substitutions.append(("%compiler", config.test_compiler))
config.substitutions.append(("%cflags", config.test_cflags))
