import os
import platform

system = platform.system()

env = Environment(
  CC='clang',
  CFLAGS=[
    '-Wall',
    "-Wextra",
    '-Werror',
    '-nostdlib',
    '-fno-builtin',
    '-std=gnu11',
    '-Wfatal-errors',
    '-Wno-pointer-to-int-cast',
    '-g',
    '-O0',
    '-fno-omit-frame-pointer',
    '-grecord-command-line',
    '-DALLOW_DEBUG',
  ],
  LINKFLAGS=[
    '-fsanitize=undefined',
    '-fno-sanitize-recover=undefined',
  ],
  CPPPATH=["#"],
  tools=["default", "compilation_db"],
)

# The Mull plugin injects mutations that are dormant unless run with mull-runner
if system == "Darwin":
  mull_plugin = Dir('#').abspath + '/.mull/lib/mull-ir-frontend-18'
else:
  mull_plugin = '/usr/lib/mull-ir-frontend-18'
if os.path.exists(mull_plugin):
  # Only use mull plugin if it exists
  env['CC'] = 'clang-18'
  env.Append(CFLAGS=['-fprofile-arcs', '-ftest-coverage', f'-fpass-plugin={mull_plugin}'])
  env.Append(LINKFLAGS=['-fprofile-arcs', '-ftest-coverage'])
  if system == "Darwin":
    env.PrependENVPath('PATH', '/opt/homebrew/opt/llvm@18/bin')

safety = env.SharedObject("safety.os", "safety.c")
libsafety = env.SharedLibrary("libsafety.so", [safety])

# GCC-style note file is generated by compiler, allow scons to clean it up
env.SideEffect("safety.gcno", safety)
