# -*- Python -*-

# Common configuration for running rerand

import os

def get_required_attr(config, attr_name):
  attr_value = getattr(config, attr_name, None)
  if attr_value == None:
    lit_config.fatal(
      "No attribute %r in test configuration! You may need to run "
      "tests from your build directory or add this attribute "
      "to lit.site.cfg " % attr_name)
  return attr_value

# Setup source root.
config.test_source_root = os.path.dirname(__file__)

config.name = "Rerand"

rerand_cflags = ["--static", "-D__RERAND__", "-fsanitize=rerand"]

rerand_common_cflags = ["--static", "-fsanitize=rerand"]
rerand_data_cflags = rerand_common_cflags + ["-mllvm","-rerand-inst-data=1",
                                             "-mllvm","-rerand-inst-code=0",
                                             "-D__RERAND_DATA__"]

rerand_code_cflags = rerand_common_cflags + ["-mllvm","-rerand-inst-data=0",
                                             "-mllvm","-rerand-inst-code=1",
                                             "-D__RERAND_CODE__"]

rerand_cflags = rerand_common_cflags + ["-mllvm","-rerand-inst-data=1",
                                        "-mllvm","-rerand-inst-code=1",
                                        "-D__RERAND_DATA__","-D__RERAND_CODE__"]

config.name += config.name_suffix

clang_cflags = ["-O0", config.target_cflags] + config.debug_info_flags + ["-Wl,-emit-relocs"]

clang_cxxflags = config.cxx_mode_flags + clang_cflags
clang_rerand_cxxflags = clang_cxxflags + rerand_cflags
clang_rerand_data_cxxflags = clang_cxxflags + rerand_data_cflags
clang_rerand_code_cxxflags = clang_cxxflags + rerand_code_cflags

config.clang_cxxflags = clang_cxxflags

config.substitutions.append( ('%env_rerand_opts', "env RERAND_OPTIONS='verbosity=1'") )

def build_invocation(compile_flags):
  return " " + " ".join([config.clang] + compile_flags) + " "

config.substitutions.append( ("%clangxx ", build_invocation(clang_cxxflags)) )
config.substitutions.append( ("%clangxx_rerand ", build_invocation(clang_rerand_cxxflags)) )
config.substitutions.append( ("%clangxx_rerand_data ", build_invocation(clang_rerand_data_cxxflags)) )
config.substitutions.append( ("%clangxx_rerand_code ", build_invocation(clang_rerand_code_cxxflags)) )

# Rerand only supports Linux platform atm.
if config.host_os not in ['Linux']:
  config.unsupported = True

config.suffixes = ['.c', '.cc', '.cpp']
