# Any copyright is dedicated to the Public Domain.
# http://creativecommons.org/publicdomain/zero/1.0/
import os

Import("CC environment")

# Find out what the target architecture is.
target = environment["TARGET_ARCH"]
env = os.environ
extra_path = os.getcwd() + os.pathsep + os.path.normpath(os.path.join(os.getcwd(), ".."))
if "PATH" in env:
    env["PATH"] += os.pathsep + extra_path
else:
    env["PATH"] = extra_path

asflags=None
print("Building for " + str(target))
if target == "x86" or target == "amd64":
    if os.name == "nt":
        if target == "amd64":
            asflags = "-f win64 -D REJOY_WINDOWS"
        elif target == "x86":
            asflags = "-f win32"
    elif "darwin" in os.name.lower():
        asflags = "-f macho"
    elif target == "x86":
        asflags = "-f elf32"
    elif target == "amd64":
        asflags = "-f elf64"
    util_env = Environment(ENV = os.environ, TOOLS=["default", "nasm"], TARGET_ARCH=target)
    # Try to find yasm or nasm.
    conf = util_env.Configure()
    asm = None
    for try_asm in ["nasm", "yasm"]:
        if conf.CheckProg(try_asm):
            asm = try_asm
            break
    conf.Finish()
else:
    asm = None

if asm:
    # It's possible we used a tool (like nasm) but are using an alternative assembler (like yasm)
    util_env["AS"] = asm
    if asflags:
        util_env.Append(ASFLAGS=asflags)
    source = ["rejoy_util." + target + ".s"]
else:
    util_env = environment.Clone()
    source = ["rejoy_util.c"]

rejoy_util = util_env.StaticLibrary("rejoy_util", source)

Return("rejoy_util")
