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=[] if system == "Darwin" else ['-fsanitize=undefined', '-fno-sanitize-recover=undefined'],
  CPPPATH=["#", "#iqdbc_repo"],
  tools=["default", "compilation_db"],
)

# short colored build output, if the top-level pretty tool is present (main-repo build)
_pretty = Dir('#tools/scons/site_tools').File('pretty.py')
if not _pretty.exists():
  _pretty = Dir('#site_scons/site_tools').File('pretty.py')
if _pretty.exists():
  env.Tool('pretty', toolpath=[_pretty.dir.abspath])

# 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)
