#!/usr/bin/env python
from misc.utility.scons_hints import *

Import("env")


def _escape_define(env, config):
    return f"<{config}>" if env["ninja"] and env.msvc else f'\\"{config}\\"'


env_crypto = env.Clone()
env_crypto.AddMethod(_escape_define, "EscapeDefine")

is_builtin = env["builtin_mbedtls"]
has_module = env["module_mbedtls_enabled"]
thirdparty_obj = []
configs = []

if is_builtin or not has_module:
    # Use our headers for builtin or if the module is not going to be compiled.
    # We decided not to depend on system mbedtls and bundle a minimal version
    # when building without the module.
    env_crypto.Prepend(
        CPPPATH=[
            "#thirdparty/mbedtls/godot/",
            "#thirdparty/mbedtls/include/",
            "#thirdparty/mbedtls/tf-psa-crypto/include/",
            "#thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/include/",
        ]
    )
    env_crypto.Append(
        CPPDEFINES=[
            ("TF_PSA_CRYPTO_CONFIG_FILE", env_crypto.EscapeDefine("godot_psa_config.h")),
            ("MBEDTLS_CONFIG_FILE", env_crypto.EscapeDefine("godot_mbedtls_config.h")),
        ]
    )
    configs = ["#thirdparty/mbedtls/godot/godot_psa_config.h", "#thirdparty/mbedtls/godot/godot_mbedtls_config.h"]
    if not has_module:
        env_crypto.Append(CPPDEFINES=["GODOT_MBEDTLS_LIGHT"])
    # Our custom mbedTLS platform functions
    platform_obj = []
    env_crypto.add_source_files(platform_obj, ["#thirdparty/mbedtls/godot/godot_mbedtls_platform.cpp"])
    env.core_sources += platform_obj
    env.Depends(platform_obj, configs)

# MbedTLS core functions (for CryptoCore).
# If the mbedtls module is compiled we don't need to add the .c files with our
# custom config since they will be built by the module itself.
# Only if the module is not enabled, we must compile here the required sources
# to make a "light" build with only the necessary mbedtls files.
if not has_module:
    # Build minimal mbedTLS library (MD5/SHA/Base64/AES).
    env_thirdparty = env_crypto.Clone()
    env_thirdparty.disable_warnings()
    psa_dir = "#thirdparty/mbedtls/tf-psa-crypto/"
    env_thirdparty.Prepend(
        CPPPATH=[
            psa_dir + d
            for d in ["include/", "core/", "dispatch/", "extras", "platform/", "utilities/", "drivers/builtin/src/"]
        ]
    )
    env_thirdparty.Prepend(CPPPATH=["#thirdparty/mbedtls/tf-psa-crypto/drivers/builtin/include/"])

    thirdparty_mbedtls_dir = "#thirdparty/mbedtls/"
    thirdparty_mbedtls_sources = [
        "tf-psa-crypto/core/psa_crypto.c",
        "tf-psa-crypto/core/psa_crypto_client.c",
        "tf-psa-crypto/core/psa_crypto_driver_wrappers_no_static.c",
        "tf-psa-crypto/core/psa_crypto_random.c",
        "tf-psa-crypto/core/psa_crypto_slot_management.c",
        "tf-psa-crypto/utilities/base64.c",
        "tf-psa-crypto/utilities/constant_time.c",
        "tf-psa-crypto/platform/threading.c",
        "tf-psa-crypto/extras/md.c",
        "tf-psa-crypto/drivers/builtin/src/aes.c",
        "tf-psa-crypto/drivers/builtin/src/ctr_drbg.c",
        "tf-psa-crypto/drivers/builtin/src/cipher.c",
        "tf-psa-crypto/drivers/builtin/src/cipher_wrap.c",
        "tf-psa-crypto/drivers/builtin/src/entropy.c",
        "tf-psa-crypto/drivers/builtin/src/entropy_poll.c",
        "tf-psa-crypto/drivers/builtin/src/md5.c",
        "tf-psa-crypto/drivers/builtin/src/psa_crypto_cipher.c",
        "tf-psa-crypto/drivers/builtin/src/psa_crypto_hash.c",
        "tf-psa-crypto/drivers/builtin/src/psa_crypto_rsa.c",
        "tf-psa-crypto/drivers/builtin/src/psa_util_internal.c",
        "tf-psa-crypto/drivers/builtin/src/sha1.c",
        "tf-psa-crypto/drivers/builtin/src/sha256.c",
    ]
    thirdparty_mbedtls_sources = [thirdparty_mbedtls_dir + file for file in thirdparty_mbedtls_sources]
    env_thirdparty.add_source_files(thirdparty_obj, thirdparty_mbedtls_sources)
    # Needed to force rebuilding the library when the configuration file is updated.
    env_thirdparty.Depends(thirdparty_obj, configs)
    env.core_sources += thirdparty_obj

# Godot source files
core_obj = []

env_crypto.add_source_files(core_obj, "*.cpp")
env.core_sources += core_obj

# Needed to force rebuilding the core files when the thirdparty library is updated.
env.Depends(core_obj, thirdparty_obj + configs)
