Skip to content

Instantly share code, notes, and snippets.

@adamscott
Last active November 19, 2025 14:09
Show Gist options
  • Select an option

  • Save adamscott/82f2c51b73253712fdc7cadf72810899 to your computer and use it in GitHub Desktop.

Select an option

Save adamscott/82f2c51b73253712fdc7cadf72810899 to your computer and use it in GitHub Desktop.
My own Godot "custom.py" (useful default values and caching system for older versions)
#!/usr/bin/env python
from misc.utility.scons_hints import *
USE_BREW = True
import sys
import subprocess
import typing
try:
import distutils
except ModuleNotFoundError as err:
print(err, file=sys.stderr)
print('You should install "setuptools" to fix that issue.', file=sys.stderr)
print(" pip install setuptools", file=sys.stderr)
sys.exit(1)
import os
import platform as platform_module
from SCons.Script import ARGUMENTS
from SCons.Variables.BoolVariable import TRUE_STRINGS
globals()["___allow_list"] = set()
globals()["___script_locals"] = locals()
globals()["___use_brew"] = USE_BREW
def set_param(param, value=None, from_arguments=False, default_value: typing.Any = "no"):
from SCons.Script import ARGUMENTS
globals()["___allow_list"].add(param)
if value is None:
if not from_arguments:
print(f'Could not add param "{param}", no value was given.')
sys.exit(1)
value = ARGUMENTS.get(param, default_value)
globals()["___script_locals"][param] = value
return value
def get_param(param):
global ___allow_list
if param not in globals()["___allow_list"]:
print(f'Could not get param "{param}", not declared yet.')
return globals()["___script_locals"][param]
def has_command_line_tool(name):
from shutil import which
return which(name) is not None
def has_cache_tool_commit():
from subprocess import run
CACHE_TOOL_COMMIT = "0e4a4e3c4d602691ce1b4fc6a721c5c637ead57f"
result = run(["git", "merge-base", "--is-ancestor", CACHE_TOOL_COMMIT, "HEAD"])
return result.returncode == 0
def get_active_branch_name():
from subprocess import run
result = run(["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True)
return result.stdout.strip()
def has_mise_file():
script_path = os.path.abspath(__file__)
script_directory = os.path.dirname(script_path)
return os.path.exists(os.path.join(script_directory, "mise.toml"))
def has_brew():
if not globals()["___use_brew"]:
return False
import subprocess
try:
subprocess.run(["command", "-v", "brew"], shell=True, check=True)
return True
except subprocess.CalledProcessError:
return False
def get_brew_package_prefix(package):
if not globals()["___use_brew"]:
return None
import subprocess
try:
output = subprocess.run(
["brew", "--prefix", package], check=True, capture_output=True, encoding="utf-8", text=True
)
return output.stdout.strip()
except subprocess.CalledProcessError:
return None
if ARGUMENTS.get("custom", "yes") in TRUE_STRINGS:
set_param("system", platform_module.system())
set_param("compiledb", ARGUMENTS.get("compiledb", "yes"))
_current_platform = None
if len(platform_module.mac_ver()[0]) > 0:
_current_platform = "macos"
elif get_param("system") == "Linux" or get_param("system") == "FreeBSD":
_current_platform = "linuxbsd"
elif len(platform_module.win32_ver()[0]) > 0:
_current_platform = "windows"
if _current_platform is not None:
set_param("platform", from_arguments=True, default_value=_current_platform)
set_param("dev_build", from_arguments=True, default_value="yes")
set_param("dev_mode", from_arguments=True, default_value="yes")
set_param("scu_build", from_arguments=True, default_value="yes")
set_param("fast_unsafe", from_arguments=True, default_value="no")
set_param("target", from_arguments=True, default_value="editor")
set_param("module_mono_enabled", from_arguments=True, default_value="no")
set_param("verbose", from_arguments=True, default_value="yes")
# set_param("debug_symbols", from_arguments=True, default_value="yes")
# set_param("separate_debug_symbols", from_arguments=True, default_value="yes")
if get_param("platform") == "web":
set_param("webgpu", from_arguments=True, default_value="yes")
set_param("dawn_libs", "/Users/adamscott/dev/builds/dawn/out/Release")
if get_param("target") == "template_debug" or get_param("target") == "template_release":
set_param("dev_mode", False)
if get_param("target") == "template_release":
set_param("optimize", from_arguments=True, default_value="size")
if get_param("platform") == "web":
set_param("threads", from_arguments=True, default_value="no")
set_param("dev_build", from_arguments=True, default_value="no")
else:
set_param("optimize", from_arguments=True, default_value="none")
if get_param("platform") == "linuxbsd":
# Comment if you want to use clang instead of gcc
set_param("use_llvm", from_arguments=True, default_value="yes")
set_param("linker", from_arguments=True, default_value="mold")
elif get_param("platform") == "macos":
set_param("generate_bundle", from_arguments=True, default_value="yes")
set_param("accesskit", True)
set_param("accesskit_sdk_path", "/Users/adamscott/dev/libs/accesskit-c/accesskit-c")
if has_brew():
moltenvk_path = get_brew_package_prefix("molten-vk")
if moltenvk_path is None:
print(
'"molten-vk" is not installed with "brew", could not get vulkan_sdk_path. Setting \'vulkan=no\'',
file=sys.stderr,
)
print(" brew install molten-vk", file=sys.stderr)
set_param("vulkan", "no")
else:
set_param("vulkan", "yes")
set_param("vulkan_sdk_path", moltenvk_path)
else:
print("brew not installed, could not get vulkan_sdk_path. Setting 'vulkan=no'", file=sys.stderr)
set_param("vulkan", "no")
elif get_param("platform") == "windows":
set_param("d3d12", from_arguments=True, default_value="yes")
if has_command_line_tool("git"):
_cache_dir_name = ".scons_cache"
_active_branch_name = get_active_branch_name()
if not (
_active_branch_name == "master"
and (_current_platform is None or _current_platform == get_param("platform"))
and get_param("target") == "editor"
):
_cache_dir_name = f".scons_cache__{_active_branch_name}__{get_param("platform")}__{get_param("target")}"
_base_cache_path = os.path.join(os.getcwd(), ".scons_cache")
_cache_path = os.path.join(os.getcwd(), _cache_dir_name)
if (_base_cache_path != _cache_path) and (not os.path.isdir(_cache_path)) and (os.path.isdir(_base_cache_path)):
distutils.dir_util.copy_tree(_base_cache_path, _cache_path)
if has_cache_tool_commit():
set_param("cache_path", from_arguments=True, default_value=_cache_dir_name)
set_param("cache_limit", from_arguments=True, default_value=5)
else:
print(
"INFO: This branch don't support `cache_path` and `cache_limit` parameters. Interfacing directly with SCons for cache."
)
if not os.getenv("SCONS_CACHE_LIMIT"):
os.environ["SCONS_CACHE_LIMIT"] = "5120"
if not os.getenv("SCONS_CACHE"):
os.environ["SCONS_CACHE"] = _cache_path
else:
print("WARNING: Skipping caching. custom.py needs `git`.")
if "custom" in ARGUMENTS:
ARGUMENTS.pop("custom")
# Clearing unset params from locals().
globals()["___locals_keys"] = locals().copy().keys()
globals()["___base_keys"] = [
"__name__",
"__doc__",
"__package__",
"__loader__",
"__spec__",
"__annotations__",
"__builtins__",
"__file__",
"__cached__",
]
for local_name in globals()["___locals_keys"]:
if local_name in globals()["___base_keys"]:
continue
if local_name not in globals()["___allow_list"]:
locals().pop(local_name)
locals().pop("local_name")
del globals()["___locals_keys"]
del globals()["___base_keys"]
del globals()["___allow_list"]
del globals()["___script_locals"]
del globals()["___use_brew"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment