#!/usr/bin/env python

Import("env")

import core_builders

env.core_sources = []


# Generate AES256 script encryption key
import os

txt = "0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0"
if "SCRIPT_AES256_ENCRYPTION_KEY" in os.environ:
    key = os.environ["SCRIPT_AES256_ENCRYPTION_KEY"]
    ec_valid = True
    if len(key) != 64:
        ec_valid = False
    else:
        txt = ""
        for i in range(len(key) >> 1):
            if i > 0:
                txt += ","
            txts = "0x" + key[i * 2 : i * 2 + 2]
            try:
                int(txts, 16)
            except Exception:
                ec_valid = False
            txt += txts
    if not ec_valid:
        print("Error: Invalid AES256 encryption key, not 64 hexadecimal characters: '" + key + "'.")
        print(
            "Unset 'SCRIPT_AES256_ENCRYPTION_KEY' in your environment "
            "or make sure that it contains exactly 64 hexadecimal characters."
        )
        Exit(255)

# NOTE: It is safe to generate this file here, since this is still executed serially
with open("script_encryption_key.gen.cpp", "w") as f:
    f.write('#include "core/config/project_settings.h"\nuint8_t script_encryption_key[32]={' + txt + "};\n")


# Add required thirdparty code.

thirdparty_obj = []

env_thirdparty = env.Clone()
env_thirdparty.disable_warnings()

# Misc thirdparty code: header paths are hardcoded, we don't need to append
# to the include path (saves a few chars on the compiler invocation for touchy MSVC...)
thirdparty_misc_dir = "#thirdparty/misc/"
thirdparty_misc_sources = [
    # C sources
    "fastlz.c",
    "r128.c",
    "smaz.c",
    # C++ sources
    "pcg.cpp",
    "polypartition.cpp",
    "clipper.cpp",
    "smolv.cpp",
]
thirdparty_misc_sources = [thirdparty_misc_dir + file for file in thirdparty_misc_sources]
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_misc_sources)

# Zlib library, can be unbundled
if env["builtin_zlib"]:
    thirdparty_zlib_dir = "#thirdparty/zlib/"
    thirdparty_zlib_sources = [
        "adler32.c",
        "compress.c",
        "crc32.c",
        "deflate.c",
        "infback.c",
        "inffast.c",
        "inflate.c",
        "inftrees.c",
        "trees.c",
        "uncompr.c",
        "zutil.c",
    ]
    thirdparty_zlib_sources = [thirdparty_zlib_dir + file for file in thirdparty_zlib_sources]

    env_thirdparty.Prepend(CPPPATH=[thirdparty_zlib_dir])
    # Needs to be available in main env too
    env.Prepend(CPPPATH=[thirdparty_zlib_dir])
    if env.dev_build:
        env_thirdparty.Append(CPPDEFINES=["ZLIB_DEBUG"])

    env_thirdparty.add_source_files(thirdparty_obj, thirdparty_zlib_sources)

# Minizip library, could be unbundled in theory
# However, our version has some custom modifications, so it won't compile with the system one
thirdparty_minizip_dir = "#thirdparty/minizip/"
thirdparty_minizip_sources = ["ioapi.c", "unzip.c", "zip.c"]
thirdparty_minizip_sources = [thirdparty_minizip_dir + file for file in thirdparty_minizip_sources]
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_minizip_sources)

# Zstd library, can be unbundled in theory
# though we currently use some private symbols
# https://github.com/godotengine/godot/issues/17374
if env["builtin_zstd"]:
    thirdparty_zstd_dir = "#thirdparty/zstd/"
    thirdparty_zstd_sources = [
        "common/debug.c",
        "common/entropy_common.c",
        "common/error_private.c",
        "common/fse_decompress.c",
        "common/pool.c",
        "common/threading.c",
        "common/xxhash.c",
        "common/zstd_common.c",
        "compress/fse_compress.c",
        "compress/hist.c",
        "compress/huf_compress.c",
        "compress/zstd_compress.c",
        "compress/zstd_double_fast.c",
        "compress/zstd_fast.c",
        "compress/zstd_lazy.c",
        "compress/zstd_ldm.c",
        "compress/zstd_opt.c",
        "compress/zstdmt_compress.c",
        "compress/zstd_compress_literals.c",
        "compress/zstd_compress_sequences.c",
        "compress/zstd_compress_superblock.c",
        "decompress/huf_decompress.c",
        "decompress/zstd_ddict.c",
        "decompress/zstd_decompress_block.c",
        "decompress/zstd_decompress.c",
    ]
    if env["platform"] in ["android", "ios", "linuxbsd", "macos"]:
        # Match platforms with ZSTD_ASM_SUPPORTED in common/portability_macros.h
        thirdparty_zstd_sources.append("decompress/huf_decompress_amd64.S")
    thirdparty_zstd_sources = [thirdparty_zstd_dir + file for file in thirdparty_zstd_sources]

    env_thirdparty.Prepend(CPPPATH=[thirdparty_zstd_dir, thirdparty_zstd_dir + "common"])
    env_thirdparty.Append(CPPDEFINES=["ZSTD_STATIC_LINKING_ONLY"])
    env.Prepend(CPPPATH=thirdparty_zstd_dir)
    # Also needed in main env includes will trigger warnings
    env.Append(CPPDEFINES=["ZSTD_STATIC_LINKING_ONLY"])

    env_thirdparty.add_source_files(thirdparty_obj, thirdparty_zstd_sources)


env.core_sources += thirdparty_obj


# Godot source files

env.add_source_files(env.core_sources, "*.cpp")
env.add_source_files(env.core_sources, "script_encryption_key.gen.cpp")
env.add_source_files(env.core_sources, "version_hash.gen.cpp")

# Certificates
env.Depends(
    "#core/io/certs_compressed.gen.h",
    ["#thirdparty/certs/ca-certificates.crt", env.Value(env["builtin_certs"]), env.Value(env["system_certs_path"])],
)
env.CommandNoCache(
    "#core/io/certs_compressed.gen.h",
    "#thirdparty/certs/ca-certificates.crt",
    env.Run(core_builders.make_certs_header, "Building ca-certificates header."),
)

# Authors
env.Depends("#core/authors.gen.h", "../AUTHORS.md")
env.CommandNoCache(
    "#core/authors.gen.h", "../AUTHORS.md", env.Run(core_builders.make_authors_header, "Generating authors header.")
)

# Donors
env.Depends("#core/donors.gen.h", "../DONORS.md")
env.CommandNoCache(
    "#core/donors.gen.h", "../DONORS.md", env.Run(core_builders.make_donors_header, "Generating donors header.")
)

# License
env.Depends("#core/license.gen.h", ["../COPYRIGHT.txt", "../LICENSE.txt"])
env.CommandNoCache(
    "#core/license.gen.h",
    ["../COPYRIGHT.txt", "../LICENSE.txt"],
    env.Run(core_builders.make_license_header, "Generating license header."),
)

# Chain load SCsubs
SConscript("os/SCsub")
SConscript("math/SCsub")
SConscript("crypto/SCsub")
SConscript("io/SCsub")
SConscript("debugger/SCsub")
SConscript("input/SCsub")
SConscript("variant/SCsub")
SConscript("extension/SCsub")
SConscript("object/SCsub")
SConscript("templates/SCsub")
SConscript("string/SCsub")
SConscript("config/SCsub")
SConscript("error/SCsub")


# Build it all as a library
lib = env.add_library("core", env.core_sources)
env.Prepend(LIBS=[lib])

# Needed to force rebuilding the core files when the thirdparty code is updated.
env.Depends(lib, thirdparty_obj)