2019-03-26 23:51:51 +00:00
|
|
|
#!/usr/bin/env python
|
2017-04-18 00:16:57 +00:00
|
|
|
|
2019-03-26 23:51:51 +00:00
|
|
|
import os
|
2022-05-29 08:51:33 +00:00
|
|
|
import platform
|
2019-03-26 23:51:51 +00:00
|
|
|
import sys
|
2019-11-26 19:26:06 +00:00
|
|
|
import subprocess
|
2022-04-30 13:49:51 +00:00
|
|
|
from binding_generator import scons_generate_bindings, scons_emit_files
|
2022-12-19 08:46:00 +00:00
|
|
|
from SCons.Errors import UserError
|
2019-11-26 19:26:06 +00:00
|
|
|
|
2022-07-04 15:50:54 +00:00
|
|
|
EnsureSConsVersion(4, 0)
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2018-02-22 22:16:25 +00:00
|
|
|
def add_sources(sources, dir, extension):
|
2019-03-26 23:51:51 +00:00
|
|
|
for f in os.listdir(dir):
|
2021-08-18 14:03:52 +00:00
|
|
|
if f.endswith("." + extension):
|
|
|
|
sources.append(dir + "/" + f)
|
2018-02-22 22:16:25 +00:00
|
|
|
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2022-12-19 08:46:00 +00:00
|
|
|
def normalize_path(val):
|
|
|
|
return val if os.path.isabs(val) else os.path.join(env.Dir("#").abspath, val)
|
|
|
|
|
|
|
|
|
|
|
|
def validate_api_file(key, val, env):
|
|
|
|
if not os.path.isfile(normalize_path(val)):
|
|
|
|
raise UserError("GDExtension API file ('%s') does not exist: %s" % (key, val))
|
|
|
|
|
|
|
|
|
|
|
|
def validate_gdextension_dir(key, val, env):
|
|
|
|
if not os.path.isdir(normalize_path(val)):
|
|
|
|
raise UserError("GDExtension directory ('%s') does not exist: %s" % (key, val))
|
|
|
|
|
|
|
|
|
|
|
|
def get_gdextension_dir(env):
|
|
|
|
return normalize_path(env.get("gdextension_dir", env.Dir("gdextension").abspath))
|
|
|
|
|
|
|
|
|
|
|
|
def get_api_file(env):
|
|
|
|
return normalize_path(env.get("custom_api_file", os.path.join(get_gdextension_dir(env), "extension_api.json")))
|
|
|
|
|
|
|
|
|
2019-03-26 23:51:51 +00:00
|
|
|
# Try to detect the host platform automatically.
|
2018-08-16 14:14:35 +00:00
|
|
|
# This is used if no `platform` argument is passed
|
2021-08-18 14:03:52 +00:00
|
|
|
if sys.platform.startswith("linux"):
|
2022-06-06 13:09:32 +00:00
|
|
|
default_platform = "linux"
|
2021-08-18 14:03:52 +00:00
|
|
|
elif sys.platform == "darwin":
|
2022-07-20 08:01:47 +00:00
|
|
|
default_platform = "macos"
|
2021-08-18 14:03:52 +00:00
|
|
|
elif sys.platform == "win32" or sys.platform == "msys":
|
2022-06-06 13:09:32 +00:00
|
|
|
default_platform = "windows"
|
|
|
|
elif ARGUMENTS.get("platform", ""):
|
|
|
|
default_platform = ARGUMENTS.get("platform")
|
2018-08-16 14:14:35 +00:00
|
|
|
else:
|
2021-09-29 20:19:36 +00:00
|
|
|
raise ValueError("Could not detect platform automatically, please specify with platform=<platform>")
|
2018-02-22 22:16:25 +00:00
|
|
|
|
2023-07-09 13:27:31 +00:00
|
|
|
try:
|
|
|
|
Import("env")
|
|
|
|
except:
|
|
|
|
# Default tools with no platform defaults to gnu toolchain.
|
|
|
|
# We apply platform specific toolchains via our custom tools.
|
|
|
|
env = Environment(tools=["default"], PLATFORM="")
|
|
|
|
|
2023-06-14 15:30:24 +00:00
|
|
|
env.PrependENVPath("PATH", os.getenv("PATH"))
|
2020-03-30 21:58:20 +00:00
|
|
|
|
2022-07-17 10:34:42 +00:00
|
|
|
# Default num_jobs to local cpu count if not user specified.
|
|
|
|
# SCons has a peculiarity where user-specified options won't be overridden
|
|
|
|
# by SetOption, so we can rely on this to know if we should use our default.
|
|
|
|
initial_num_jobs = env.GetOption("num_jobs")
|
|
|
|
altered_num_jobs = initial_num_jobs + 1
|
|
|
|
env.SetOption("num_jobs", altered_num_jobs)
|
|
|
|
if env.GetOption("num_jobs") == altered_num_jobs:
|
|
|
|
cpu_count = os.cpu_count()
|
|
|
|
if cpu_count is None:
|
|
|
|
print("Couldn't auto-detect CPU count to configure build parallelism. Specify it with the -j argument.")
|
|
|
|
else:
|
|
|
|
safer_cpu_count = cpu_count if cpu_count <= 4 else cpu_count - 1
|
|
|
|
print(
|
|
|
|
"Auto-detected %d CPU cores available for build parallelism. Using %d cores by default. You can override it with the -j argument."
|
|
|
|
% (cpu_count, safer_cpu_count)
|
|
|
|
)
|
|
|
|
env.SetOption("num_jobs", safer_cpu_count)
|
|
|
|
|
2022-09-24 16:44:25 +00:00
|
|
|
# Custom options and profile flags.
|
|
|
|
customs = ["custom.py"]
|
|
|
|
profile = ARGUMENTS.get("profile", "")
|
|
|
|
if profile:
|
|
|
|
if os.path.isfile(profile):
|
|
|
|
customs.append(profile)
|
|
|
|
elif os.path.isfile(profile + ".py"):
|
|
|
|
customs.append(profile + ".py")
|
|
|
|
opts = Variables(customs, ARGUMENTS)
|
|
|
|
|
2022-07-20 08:01:47 +00:00
|
|
|
platforms = ("linux", "macos", "windows", "android", "ios", "javascript")
|
2021-08-18 14:03:52 +00:00
|
|
|
opts.Add(
|
|
|
|
EnumVariable(
|
2023-07-09 13:27:31 +00:00
|
|
|
key="platform",
|
|
|
|
help="Target platform",
|
|
|
|
default=env.get("platform", default_platform),
|
2022-06-06 13:09:32 +00:00
|
|
|
allowed_values=platforms,
|
2021-08-18 14:03:52 +00:00
|
|
|
ignorecase=2,
|
|
|
|
)
|
|
|
|
)
|
2022-05-29 08:51:33 +00:00
|
|
|
|
2022-09-24 16:44:25 +00:00
|
|
|
# Editor and template_debug are compatible (i.e. you can use the same binary for Godot editor builds and Godot debug templates).
|
|
|
|
# Godot release templates are only compatible with "template_release" builds.
|
|
|
|
# For this reason, we default to template_debug builds, unlike Godot which defaults to editor builds.
|
|
|
|
opts.Add(
|
2023-07-09 13:27:31 +00:00
|
|
|
EnumVariable(
|
|
|
|
key="target",
|
|
|
|
help="Compilation target",
|
|
|
|
default=env.get("target", "template_debug"),
|
|
|
|
allowed_values=("editor", "template_release", "template_debug"),
|
|
|
|
)
|
2022-09-24 16:44:25 +00:00
|
|
|
)
|
2020-12-03 20:30:59 +00:00
|
|
|
opts.Add(
|
2021-08-18 14:03:52 +00:00
|
|
|
PathVariable(
|
2023-07-09 13:27:31 +00:00
|
|
|
key="gdextension_dir",
|
|
|
|
help="Path to a custom directory containing GDExtension interface header and API JSON file",
|
|
|
|
default=env.get("gdextension_dir", None),
|
|
|
|
validator=validate_gdextension_dir,
|
2022-12-13 23:40:17 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
opts.Add(
|
|
|
|
PathVariable(
|
2023-07-09 13:27:31 +00:00
|
|
|
key="custom_api_file",
|
|
|
|
help="Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`)",
|
|
|
|
default=env.get("custom_api_file", None),
|
|
|
|
validator=validate_api_file,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
opts.Add(
|
|
|
|
BoolVariable(
|
|
|
|
key="generate_bindings",
|
|
|
|
help="Force GDExtension API bindings generation. Auto-detected by default.",
|
|
|
|
default=env.get("generate_bindings", False),
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
2020-12-03 20:30:59 +00:00
|
|
|
)
|
2021-07-05 14:07:55 +00:00
|
|
|
opts.Add(
|
2023-07-09 13:27:31 +00:00
|
|
|
BoolVariable(
|
|
|
|
key="generate_template_get_node",
|
|
|
|
help="Generate a template version of the Node class's get_node.",
|
|
|
|
default=env.get("generate_template_get_node", True),
|
|
|
|
)
|
2021-07-05 14:07:55 +00:00
|
|
|
)
|
2017-04-18 00:16:57 +00:00
|
|
|
|
2023-07-09 13:27:31 +00:00
|
|
|
opts.Add(BoolVariable(key="build_library", help="Build the godot-cpp library.", default=env.get("build_library", True)))
|
|
|
|
opts.Add(
|
|
|
|
EnumVariable(
|
|
|
|
key="precision",
|
|
|
|
help="Set the floating-point precision level",
|
|
|
|
default=env.get("precision", "single"),
|
|
|
|
allowed_values=("single", "double"),
|
|
|
|
)
|
|
|
|
)
|
2021-09-30 02:29:42 +00:00
|
|
|
|
2022-06-06 13:09:32 +00:00
|
|
|
# Add platform options
|
|
|
|
tools = {}
|
|
|
|
for pl in platforms:
|
|
|
|
tool = Tool(pl, toolpath=["tools"])
|
|
|
|
if hasattr(tool, "options"):
|
|
|
|
tool.options(opts)
|
|
|
|
tools[pl] = tool
|
|
|
|
|
2022-05-29 08:51:33 +00:00
|
|
|
# CPU architecture options.
|
|
|
|
architecture_array = ["", "universal", "x86_32", "x86_64", "arm32", "arm64", "rv64", "ppc32", "ppc64", "wasm32"]
|
|
|
|
architecture_aliases = {
|
|
|
|
"x64": "x86_64",
|
|
|
|
"amd64": "x86_64",
|
|
|
|
"armv7": "arm32",
|
|
|
|
"armv8": "arm64",
|
|
|
|
"arm64v8": "arm64",
|
|
|
|
"aarch64": "arm64",
|
|
|
|
"rv": "rv64",
|
|
|
|
"riscv": "rv64",
|
|
|
|
"riscv64": "rv64",
|
|
|
|
"ppcle": "ppc32",
|
|
|
|
"ppc": "ppc32",
|
|
|
|
"ppc64le": "ppc64",
|
|
|
|
}
|
2023-07-09 13:27:31 +00:00
|
|
|
opts.Add(
|
|
|
|
EnumVariable(
|
|
|
|
key="arch",
|
|
|
|
help="CPU architecture",
|
|
|
|
default=env.get("arch", ""),
|
|
|
|
allowed_values=architecture_array,
|
|
|
|
map=architecture_aliases,
|
|
|
|
)
|
|
|
|
)
|
2022-05-29 08:51:33 +00:00
|
|
|
|
2022-09-11 17:25:22 +00:00
|
|
|
# Targets flags tool (optimizations, debug symbols)
|
|
|
|
target_tool = Tool("targets", toolpath=["tools"])
|
|
|
|
target_tool.options(opts)
|
|
|
|
|
2018-08-16 14:14:35 +00:00
|
|
|
opts.Update(env)
|
|
|
|
Help(opts.GenerateHelpText(env))
|
2017-04-18 00:16:57 +00:00
|
|
|
|
2022-05-29 08:51:33 +00:00
|
|
|
# Process CPU architecture argument.
|
|
|
|
if env["arch"] == "":
|
|
|
|
# No architecture specified. Default to arm64 if building for Android,
|
|
|
|
# universal if building for macOS or iOS, wasm32 if building for web,
|
|
|
|
# otherwise default to the host architecture.
|
2022-07-20 08:01:47 +00:00
|
|
|
if env["platform"] in ["macos", "ios"]:
|
2022-05-29 08:51:33 +00:00
|
|
|
env["arch"] = "universal"
|
|
|
|
elif env["platform"] == "android":
|
|
|
|
env["arch"] = "arm64"
|
|
|
|
elif env["platform"] == "javascript":
|
|
|
|
env["arch"] = "wasm32"
|
|
|
|
else:
|
|
|
|
host_machine = platform.machine().lower()
|
|
|
|
if host_machine in architecture_array:
|
|
|
|
env["arch"] = host_machine
|
|
|
|
elif host_machine in architecture_aliases.keys():
|
|
|
|
env["arch"] = architecture_aliases[host_machine]
|
|
|
|
elif "86" in host_machine:
|
|
|
|
# Catches x86, i386, i486, i586, i686, etc.
|
|
|
|
env["arch"] = "x86_32"
|
|
|
|
else:
|
|
|
|
print("Unsupported CPU architecture: " + host_machine)
|
|
|
|
Exit()
|
|
|
|
|
2022-06-06 13:09:32 +00:00
|
|
|
tool = Tool(env["platform"], toolpath=["tools"])
|
|
|
|
|
|
|
|
if tool is None or not tool.exists(env):
|
|
|
|
raise ValueError("Required toolchain not found for platform " + env["platform"])
|
|
|
|
|
|
|
|
tool.generate(env)
|
2022-09-11 17:25:22 +00:00
|
|
|
target_tool.generate(env)
|
2022-05-29 08:51:33 +00:00
|
|
|
|
2021-11-23 21:41:52 +00:00
|
|
|
# Detect and print a warning listing unknown SCons variables to ease troubleshooting.
|
|
|
|
unknown = opts.UnknownVariables()
|
|
|
|
if unknown:
|
|
|
|
print("WARNING: Unknown SCons variables were passed and will be ignored:")
|
|
|
|
for item in unknown.items():
|
|
|
|
print(" " + item[0] + "=" + item[1])
|
|
|
|
|
2022-05-29 08:51:33 +00:00
|
|
|
print("Building for architecture " + env["arch"] + " on platform " + env["platform"])
|
|
|
|
|
2021-09-29 21:00:24 +00:00
|
|
|
# Require C++17
|
2022-06-06 13:09:32 +00:00
|
|
|
if env.get("is_msvc", False):
|
2022-02-16 11:12:10 +00:00
|
|
|
env.Append(CXXFLAGS=["/std:c++17"])
|
2021-09-29 21:00:24 +00:00
|
|
|
else:
|
2022-02-16 11:12:10 +00:00
|
|
|
env.Append(CXXFLAGS=["-std=c++17"])
|
2021-09-29 21:00:24 +00:00
|
|
|
|
2023-01-09 10:03:07 +00:00
|
|
|
if env["precision"] == "double":
|
2022-03-20 16:19:27 +00:00
|
|
|
env.Append(CPPDEFINES=["REAL_T_IS_DOUBLE"])
|
|
|
|
|
2022-04-30 13:49:51 +00:00
|
|
|
# Generate bindings
|
|
|
|
env.Append(BUILDERS={"GenerateBindings": Builder(action=scons_generate_bindings, emitter=scons_emit_files)})
|
2017-03-06 07:49:24 +00:00
|
|
|
|
2022-04-30 13:49:51 +00:00
|
|
|
bindings = env.GenerateBindings(
|
2022-09-13 08:37:58 +00:00
|
|
|
env.Dir("."),
|
2022-12-19 08:46:00 +00:00
|
|
|
[get_api_file(env), os.path.join(get_gdextension_dir(env), "gdextension_interface.h"), "binding_generator.py"],
|
2022-04-30 13:49:51 +00:00
|
|
|
)
|
2021-02-03 22:48:58 +00:00
|
|
|
|
2022-09-13 08:37:58 +00:00
|
|
|
scons_cache_path = os.environ.get("SCONS_CACHE")
|
|
|
|
if scons_cache_path is not None:
|
|
|
|
CacheDir(scons_cache_path)
|
|
|
|
Decider("MD5")
|
|
|
|
|
2022-04-30 13:49:51 +00:00
|
|
|
# Forces bindings regeneration.
|
|
|
|
if env["generate_bindings"]:
|
|
|
|
AlwaysBuild(bindings)
|
2022-09-13 08:37:58 +00:00
|
|
|
NoCache(bindings)
|
2017-04-18 00:16:57 +00:00
|
|
|
|
2022-04-30 13:49:51 +00:00
|
|
|
# Includes
|
2022-12-19 08:46:00 +00:00
|
|
|
env.Append(CPPPATH=[[env.Dir(d) for d in [get_gdextension_dir(env), "include", os.path.join("gen", "include")]]])
|
2017-03-06 07:49:24 +00:00
|
|
|
|
2019-03-26 23:51:51 +00:00
|
|
|
# Sources to compile
|
2018-02-22 22:16:25 +00:00
|
|
|
sources = []
|
2021-08-19 17:47:56 +00:00
|
|
|
add_sources(sources, "src", "cpp")
|
2021-11-30 13:00:13 +00:00
|
|
|
add_sources(sources, "src/classes", "cpp")
|
2021-08-18 14:03:52 +00:00
|
|
|
add_sources(sources, "src/core", "cpp")
|
|
|
|
add_sources(sources, "src/variant", "cpp")
|
2022-04-30 13:49:51 +00:00
|
|
|
sources.extend([f for f in bindings if str(f).endswith(".cpp")])
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2022-09-24 16:44:25 +00:00
|
|
|
suffix = ".{}.{}".format(env["platform"], env["target"])
|
|
|
|
if env.dev_build:
|
|
|
|
suffix += ".dev"
|
2023-01-09 10:03:07 +00:00
|
|
|
if env["precision"] == "double":
|
2022-09-24 16:44:25 +00:00
|
|
|
suffix += ".double"
|
|
|
|
suffix += "." + env["arch"]
|
2022-05-29 08:51:33 +00:00
|
|
|
if env["ios_simulator"]:
|
2022-09-24 16:44:25 +00:00
|
|
|
suffix += ".simulator"
|
|
|
|
|
|
|
|
# Expose it when included from another project
|
|
|
|
env["suffix"] = suffix
|
2019-11-26 19:26:06 +00:00
|
|
|
|
2021-09-30 02:29:42 +00:00
|
|
|
library = None
|
2022-09-24 16:44:25 +00:00
|
|
|
env["OBJSUFFIX"] = suffix + env["OBJSUFFIX"]
|
|
|
|
library_name = "libgodot-cpp{}{}".format(suffix, env["LIBSUFFIX"])
|
2021-09-30 02:29:42 +00:00
|
|
|
|
|
|
|
if env["build_library"]:
|
|
|
|
library = env.StaticLibrary(target=env.File("bin/%s" % library_name), source=sources)
|
|
|
|
Default(library)
|
|
|
|
|
|
|
|
env.Append(LIBPATH=[env.Dir("bin")])
|
|
|
|
env.Append(LIBS=library_name)
|
|
|
|
Return("env")
|