Repository: godotengine/godot
File: platform/windows/detect.py (Python)

Code before:
    if env["bits"] != "default":
        print(
            """
            Bits argument is not supported for MSVC compilation. Architecture depends on the Native/Cross Compile Tools Prompt/Developer Console
            (or Visual Studio settings) that is being used to run SCons. As a consequence, bits argument is disabled. Run scons again without bits
            argument (example: scons p=windows) and SCons will attempt to detect what MSVC compiler will be executed and inform you.
            """
        )
        raise SCons.Errors.UserError("Bits argument should not be used when using VCINSTALLDIR")

    # Force bits arg
    # (Actually msys2 mingw can support 64-bit, we could detect that)
    env["bits"] = "32"
    env["x86_libtheora_opt_vc"] = True

    # find compiler manually
    compiler_version_str = methods.detect_visual_c_compiler_version(env["ENV"])
    print("Found MSVC compiler: " + compiler_version_str)

    # If building for 64bit architecture, disable assembly optimisations for 32 bit builds (theora as of writing)... vc compiler for 64bit can not compile _asm
    if compiler_version_str == "amd64" or compiler_version_str == "x86_amd64":
        env["bits"] = "64"
        env["x86_libtheora_opt_vc"] = False
        print("Compiled program architecture will be a 64 bit executable (forcing bits=64).")
    elif compiler_version_str == "x86" or compiler_version_str == "amd64_x86":
        print("Compiled program architecture will be a 32 bit executable. (forcing bits=32).")
    else:
        print(
            "Failed to manually detect MSVC compiler architecture version... Defaulting to 32bit executable settings"
            " (forcing bits=32). Compilation attempt will continue, but SCons can not detect for what architecture this"
            " build is compiled for. You should check your settings/compilation setup, or avoid setting VCINSTALLDIR."
        )


def setup_msvc_auto(env):
    """Set up MSVC using SCons's auto-detection logic"""

    # If MSVC_VERSION is set by SCons, we know MSVC is installed.
    # But we may want a different version or target arch.

    # The env may have already been set up with default MSVC tools, so
    # reset a few things so we can set it up with the tools we want.
    # (Ideally we'd decide on the tool config before configuring any
    # environment, and just set the env up once, but this function runs
    # on an existing env so this is the simplest way.)
    env["MSVC_SETUP_RUN"] = False  # Need to set this to re-run the tool
    env["MSVS_VERSION"] = None
    env["MSVC_VERSION"] = None
    env["TARGET_ARCH"] = None
    if env["bits"] != "default":
        env["TARGET_ARCH"] = {"32": "x86", "64": "x86_64"}[env["bits"]]

Review comment (suggestion):
This should be fine, this part of code is used when you are running scons from a developer console for a specific architecture. Architecture is detected by `detect_visual_c_compiler_version`(which is looking in the `PATH` to get compiler name). But I would move `env["arch"] = "x86_32"` to the `elif compiler_version_str == "x86" ...` branch below.

Code after:
    return 0


def setup_msvc_manual(env):
    # FIXME: This is super hacky, and probably obsolete.
    # Won't work with detect_arch() used for `arch` by default.

    """Set up env to use MSVC manually, using VCINSTALLDIR"""
    if env["arch"] != "auto":
        print(
            """
            Arch argument is not supported for MSVC manual configuration (VCINSTALLDIR configured).
            Architecture depends on the Native/Cross Compile Tools Prompt/Developer Console (or Visual Studio settings) that is being used to run SCons.
            As a consequence, the arch argument is disabled. Run scons again without arch argument (example: scons p=windows)
            and SCons will attempt to detect what MSVC compiler will be executed and inform you.
            """
        )
        raise SCons.Errors.UserError("Arch argument should not be used when using VCINSTALLDIR")

    # Force ARCH arg
    # (Actually msys2 mingw can support 64-bit, we could detect that)
    # TODO: This is wrong, but not sure what to do about it.
    # We want to determine the arch and bitness in the SConstruct only.
    # We can check if it's correct in here, but if it's not, it should
    # just fail with an error message instead of trying to force it.

    # find compiler manually
    compiler_version_str = methods.detect_visual_c_compiler_version(env["ENV"])
    print("Found MSVC compiler: " + compiler_version_str)

    # If building for 64bit architecture, disable assembly optimisations for 32 bit builds (theora as of writing)... vc compiler for 64bit can not compile _asm
    if compiler_version_str == "amd64" or compiler_version_str == "x86_amd64":
        env["arch"] = "x86_64"
        env["x86_libtheora_opt_vc"] = False
        print("Compiled program architecture will be a 64 bit executable (forcing arch=x86_64).")
    elif compiler_version_str == "x86" or compiler_version_str == "amd64_x86":
        env["arch"] = "x86_32"
        env["x86_libtheora_opt_vc"] = True
        print("Compiled program architecture will be a 32 bit executable (forcing arch=x86_32).")
    else:
        print(
            "Failed to manually detect MSVC compiler architecture version.\n"
            "You should check your settings/compilation setup, or avoid setting VCINSTALLDIR."
        )
        sys.exit()


def setup_msvc_auto(env):
    """Set up MSVC using SCons's auto-detection logic"""

    # If MSVC_VERSION is set by SCons, we know MSVC is installed.