2019-03-01 21:51:20 +01:00
|
|
|
import os
|
2019-05-20 18:34:35 +02:00
|
|
|
import os.path
|
2019-03-01 21:51:20 +01:00
|
|
|
|
|
|
|
|
2019-11-10 17:10:38 +01:00
|
|
|
def is_desktop(platform):
|
2022-08-24 20:05:23 +02:00
|
|
|
return platform in ["windows", "macos", "linuxbsd", "uwp", "haiku"]
|
2019-11-10 17:10:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
def is_unix_like(platform):
|
2022-08-24 20:05:23 +02:00
|
|
|
return platform in ["macos", "linuxbsd", "android", "haiku", "ios"]
|
2019-11-10 17:10:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
def module_supports_tools_on(platform):
|
2021-09-12 20:23:05 +02:00
|
|
|
return is_desktop(platform)
|
2019-11-10 17:10:38 +01:00
|
|
|
|
|
|
|
|
2019-03-01 22:00:39 +01:00
|
|
|
def configure(env, env_mono):
|
2021-09-12 20:23:05 +02:00
|
|
|
# is_android = env["platform"] == "android"
|
2022-08-28 20:27:45 +02:00
|
|
|
# is_web = env["platform"] == "web"
|
2021-09-12 20:23:05 +02:00
|
|
|
# is_ios = env["platform"] == "ios"
|
2021-12-16 02:38:10 +01:00
|
|
|
# is_ios_sim = is_ios and env["arch"] in ["x86_32", "x86_64"]
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
tools_enabled = env["tools"]
|
2019-05-20 18:34:35 +02:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
if tools_enabled and not module_supports_tools_on(env["platform"]):
|
|
|
|
raise RuntimeError("This module does not currently support building for this platform with tools enabled")
|
2019-05-20 18:34:35 +02:00
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
if env["tools"]:
|
2020-03-18 17:40:04 +01:00
|
|
|
env_mono.Append(CPPDEFINES=["GD_MONO_HOT_RELOAD"])
|
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
app_host_dir = find_dotnet_app_host_dir(env)
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
def check_app_host_file_exists(file):
|
|
|
|
file_path = os.path.join(app_host_dir, file)
|
|
|
|
if not os.path.isfile(file_path):
|
|
|
|
raise RuntimeError("File not found: " + file_path)
|
2020-03-18 17:38:53 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
# TODO:
|
|
|
|
# All libnethost does for us is provide a function to find hostfxr.
|
|
|
|
# If we could handle that logic ourselves we could void linking it.
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
# nethost file names:
|
|
|
|
# static: libnethost.a/lib
|
|
|
|
# shared: libnethost.a/dylib and nethost.dll
|
|
|
|
check_app_host_file_exists("libnethost.lib" if os.name == "nt" else "libnethost.a")
|
|
|
|
check_app_host_file_exists("nethost.h")
|
|
|
|
check_app_host_file_exists("hostfxr.h")
|
|
|
|
check_app_host_file_exists("coreclr_delegates.h")
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
env_mono.Prepend(CPPPATH=app_host_dir)
|
2020-03-18 17:38:53 +01:00
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
env.Append(LIBPATH=[app_host_dir])
|
|
|
|
|
|
|
|
# Only the editor build links nethost, which is needed to find hostfxr.
|
|
|
|
# Exported games don't need this logic as hostfxr is bundled with them.
|
|
|
|
if tools_enabled:
|
|
|
|
libnethost_path = os.path.join(app_host_dir, "libnethost.lib" if os.name == "nt" else "libnethost.a")
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
if env["platform"] == "windows":
|
|
|
|
env_mono.Append(CPPDEFINES=["NETHOST_USE_AS_STATIC"])
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
if env.msvc:
|
|
|
|
env.Append(LINKFLAGS="libnethost.lib")
|
|
|
|
else:
|
|
|
|
env.Append(LINKFLAGS=["-Wl,-whole-archive", libnethost_path, "-Wl,-no-whole-archive"])
|
2019-03-01 21:51:20 +01:00
|
|
|
else:
|
2021-12-28 23:25:16 +01:00
|
|
|
is_apple = env["platform"] in ["macos", "ios"]
|
|
|
|
# is_macos = is_apple and not is_ios
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
# if is_ios and not is_ios_sim:
|
|
|
|
# env_mono.Append(CPPDEFINES=["IOS_DEVICE"])
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
if is_apple:
|
|
|
|
env.Append(LINKFLAGS=["-Wl,-force_load," + libnethost_path])
|
|
|
|
else:
|
|
|
|
env.Append(LINKFLAGS=["-Wl,-whole-archive", libnethost_path, "-Wl,-no-whole-archive"])
|
2019-03-01 21:51:20 +01:00
|
|
|
|
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
def find_dotnet_app_host_dir(env):
|
2022-02-27 21:57:50 +01:00
|
|
|
dotnet_version = "6.0"
|
2021-12-28 23:25:16 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
dotnet_root = env["dotnet_root"]
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
if not dotnet_root:
|
2022-08-25 07:29:40 +02:00
|
|
|
dotnet_cmd = find_dotnet_executable(env["arch"])
|
2021-12-28 23:25:16 +01:00
|
|
|
if dotnet_cmd:
|
|
|
|
sdk_path = find_dotnet_sdk(dotnet_cmd, dotnet_version)
|
|
|
|
if sdk_path:
|
|
|
|
dotnet_root = os.path.abspath(os.path.join(sdk_path, os.pardir))
|
|
|
|
|
|
|
|
if not dotnet_root:
|
|
|
|
raise RuntimeError("Cannot find .NET Core Sdk")
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
print("Found .NET Core Sdk root directory: " + dotnet_root)
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
dotnet_cmd = os.path.join(dotnet_root, "dotnet.exe" if os.name == "nt" else "dotnet")
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
runtime_identifier = determine_runtime_identifier(env)
|
2020-03-30 08:28:32 +02:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
# TODO: In the future, if it can't be found this way, we want to obtain it
|
|
|
|
# from the runtime.{runtime_identifier}.Microsoft.NETCore.DotNetAppHost NuGet package.
|
2021-12-28 23:25:16 +01:00
|
|
|
app_host_version = find_app_host_version(dotnet_cmd, dotnet_version)
|
2021-09-12 20:23:05 +02:00
|
|
|
if not app_host_version:
|
2021-12-28 23:25:16 +01:00
|
|
|
raise RuntimeError("Cannot find .NET app host for version: " + dotnet_version)
|
2019-07-03 17:41:07 +02:00
|
|
|
|
2022-08-20 01:48:25 +02:00
|
|
|
def get_runtime_path():
|
|
|
|
return os.path.join(
|
|
|
|
dotnet_root,
|
|
|
|
"packs",
|
|
|
|
"Microsoft.NETCore.App.Host." + runtime_identifier,
|
|
|
|
app_host_version,
|
|
|
|
"runtimes",
|
|
|
|
runtime_identifier,
|
|
|
|
"native",
|
|
|
|
)
|
|
|
|
|
|
|
|
app_host_dir = get_runtime_path()
|
|
|
|
|
|
|
|
# Some Linux distros use their distro name as the RID in these paths.
|
|
|
|
# If the initial generic path doesn't exist, try to get the RID from `dotnet --info`.
|
|
|
|
# The generic RID should still be the first choice. Some platforms like Windows 10
|
|
|
|
# define the RID as `win10-x64` but still use the generic `win-x64` for directory names.
|
|
|
|
if not app_host_dir or not os.path.isdir(app_host_dir):
|
|
|
|
runtime_identifier = find_dotnet_cli_rid(dotnet_cmd)
|
|
|
|
app_host_dir = get_runtime_path()
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
return app_host_dir
|
2019-03-01 21:51:20 +01:00
|
|
|
|
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
def determine_runtime_identifier(env):
|
2021-12-16 02:38:10 +01:00
|
|
|
# The keys are Godot's names, the values are the Microsoft's names.
|
|
|
|
# List: https://docs.microsoft.com/en-us/dotnet/core/rid-catalog
|
2021-09-12 20:23:05 +02:00
|
|
|
names_map = {
|
|
|
|
"windows": "win",
|
|
|
|
"macos": "osx",
|
|
|
|
"linuxbsd": "linux",
|
|
|
|
}
|
2021-12-16 02:38:10 +01:00
|
|
|
arch_map = {
|
|
|
|
"x86_64": "x64",
|
|
|
|
"x86_32": "x86",
|
|
|
|
"arm64": "arm64",
|
|
|
|
"arm32": "arm",
|
|
|
|
}
|
2020-03-30 08:28:32 +02:00
|
|
|
platform = env["platform"]
|
2021-09-12 20:23:05 +02:00
|
|
|
if is_desktop(platform):
|
2021-12-16 02:38:10 +01:00
|
|
|
return "%s-%s" % (names_map[platform], arch_map[env["arch"]])
|
2021-09-12 20:23:05 +02:00
|
|
|
else:
|
|
|
|
raise NotImplementedError()
|
2019-03-01 21:51:20 +01:00
|
|
|
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
def find_app_host_version(dotnet_cmd, search_version_str):
|
2021-09-12 20:23:05 +02:00
|
|
|
import subprocess
|
2021-12-28 23:25:16 +01:00
|
|
|
from distutils.version import LooseVersion
|
|
|
|
|
|
|
|
search_version = LooseVersion(search_version_str)
|
2022-08-26 13:18:52 +02:00
|
|
|
found_match = False
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
try:
|
2021-12-28 23:25:16 +01:00
|
|
|
env = dict(os.environ, DOTNET_CLI_UI_LANGUAGE="en-US")
|
|
|
|
lines = subprocess.check_output([dotnet_cmd, "--list-runtimes"], env=env).splitlines()
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
for line_bytes in lines:
|
|
|
|
line = line_bytes.decode("utf-8")
|
|
|
|
if not line.startswith("Microsoft.NETCore.App "):
|
|
|
|
continue
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
parts = line.split(" ", 2)
|
|
|
|
if len(parts) < 3:
|
|
|
|
continue
|
|
|
|
|
|
|
|
version_str = parts[1]
|
|
|
|
|
|
|
|
version = LooseVersion(version_str)
|
|
|
|
|
|
|
|
if version >= search_version:
|
2022-08-26 13:18:52 +02:00
|
|
|
search_version = version
|
|
|
|
found_match = True
|
|
|
|
if found_match:
|
|
|
|
return str(search_version)
|
2021-12-28 23:25:16 +01:00
|
|
|
except (subprocess.CalledProcessError, OSError) as e:
|
|
|
|
import sys
|
|
|
|
|
|
|
|
print(e, file=sys.stderr)
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
2022-08-25 07:29:40 +02:00
|
|
|
def find_dotnet_arch(dotnet_cmd):
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
try:
|
|
|
|
env = dict(os.environ, DOTNET_CLI_UI_LANGUAGE="en-US")
|
|
|
|
lines = subprocess.check_output([dotnet_cmd, "--info"], env=env).splitlines()
|
|
|
|
|
|
|
|
for line_bytes in lines:
|
|
|
|
line = line_bytes.decode("utf-8")
|
|
|
|
|
|
|
|
parts = line.split(":", 1)
|
|
|
|
if len(parts) < 2:
|
|
|
|
continue
|
|
|
|
|
|
|
|
arch_str = parts[0].strip()
|
|
|
|
if arch_str != "Architecture":
|
|
|
|
continue
|
|
|
|
|
|
|
|
arch_value = parts[1].strip()
|
|
|
|
arch_map = {"x64": "x86_64", "x86": "x86_32", "arm64": "arm64", "arm32": "arm32"}
|
|
|
|
return arch_map[arch_value]
|
|
|
|
except (subprocess.CalledProcessError, OSError) as e:
|
|
|
|
import sys
|
|
|
|
|
|
|
|
print(e, file=sys.stderr)
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
def find_dotnet_sdk(dotnet_cmd, search_version_str):
|
|
|
|
import subprocess
|
|
|
|
from distutils.version import LooseVersion
|
|
|
|
|
|
|
|
search_version = LooseVersion(search_version_str)
|
|
|
|
|
|
|
|
try:
|
2021-12-28 23:25:16 +01:00
|
|
|
env = dict(os.environ, DOTNET_CLI_UI_LANGUAGE="en-US")
|
|
|
|
lines = subprocess.check_output([dotnet_cmd, "--list-sdks"], env=env).splitlines()
|
2021-12-28 23:25:16 +01:00
|
|
|
|
|
|
|
for line_bytes in lines:
|
|
|
|
line = line_bytes.decode("utf-8")
|
|
|
|
|
|
|
|
parts = line.split(" ", 1)
|
2021-09-12 20:23:05 +02:00
|
|
|
if len(parts) < 2:
|
|
|
|
continue
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-12-28 23:25:16 +01:00
|
|
|
version_str = parts[0]
|
|
|
|
|
|
|
|
version = LooseVersion(version_str)
|
|
|
|
|
|
|
|
if version < search_version:
|
|
|
|
continue
|
|
|
|
|
|
|
|
path_part = parts[1]
|
|
|
|
return path_part[1 : path_part.find("]")]
|
|
|
|
except (subprocess.CalledProcessError, OSError) as e:
|
|
|
|
import sys
|
|
|
|
|
|
|
|
print(e, file=sys.stderr)
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
return ""
|
2019-03-01 21:51:20 +01:00
|
|
|
|
|
|
|
|
2022-08-20 01:48:25 +02:00
|
|
|
def find_dotnet_cli_rid(dotnet_cmd):
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
try:
|
|
|
|
env = dict(os.environ, DOTNET_CLI_UI_LANGUAGE="en-US")
|
|
|
|
lines = subprocess.check_output([dotnet_cmd, "--info"], env=env).splitlines()
|
|
|
|
|
|
|
|
for line_bytes in lines:
|
|
|
|
line = line_bytes.decode("utf-8")
|
|
|
|
if not line.startswith(" RID:"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
parts = line.split()
|
|
|
|
if len(parts) < 2:
|
|
|
|
continue
|
|
|
|
|
|
|
|
return parts[1]
|
|
|
|
except (subprocess.CalledProcessError, OSError) as e:
|
|
|
|
import sys
|
|
|
|
|
|
|
|
print(e, file=sys.stderr)
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
ENV_PATH_SEP = ";" if os.name == "nt" else ":"
|
2019-03-01 21:51:20 +01:00
|
|
|
|
|
|
|
|
2022-08-25 07:29:40 +02:00
|
|
|
def find_dotnet_executable(arch):
|
2021-09-12 20:23:05 +02:00
|
|
|
is_windows = os.name == "nt"
|
|
|
|
windows_exts = os.environ["PATHEXT"].split(ENV_PATH_SEP) if is_windows else None
|
|
|
|
path_dirs = os.environ["PATH"].split(ENV_PATH_SEP)
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
search_dirs = path_dirs + [os.getcwd()] # cwd is last in the list
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2022-08-25 07:29:40 +02:00
|
|
|
for dir in path_dirs:
|
|
|
|
search_dirs += [
|
|
|
|
os.path.join(dir, "x64"),
|
|
|
|
os.path.join(dir, "x86"),
|
|
|
|
os.path.join(dir, "arm64"),
|
|
|
|
os.path.join(dir, "arm32"),
|
|
|
|
] # search subfolders for cross compiling
|
|
|
|
|
2022-08-25 20:04:23 +02:00
|
|
|
# `dotnet --info` may not specify architecture. In such cases,
|
|
|
|
# we fallback to the first one we find without architecture.
|
|
|
|
sdk_path_unknown_arch = ""
|
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
for dir in search_dirs:
|
2022-08-25 07:29:40 +02:00
|
|
|
path = os.path.join(dir, "dotnet")
|
2019-03-01 21:51:20 +01:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
if is_windows:
|
|
|
|
for extension in windows_exts:
|
|
|
|
path_with_ext = path + extension
|
2019-05-20 18:34:35 +02:00
|
|
|
|
2021-09-12 20:23:05 +02:00
|
|
|
if os.path.isfile(path_with_ext) and os.access(path_with_ext, os.X_OK):
|
2022-08-25 07:29:40 +02:00
|
|
|
sdk_arch = find_dotnet_arch(path_with_ext)
|
|
|
|
if sdk_arch == arch or arch == "":
|
|
|
|
return path_with_ext
|
2022-08-25 20:04:23 +02:00
|
|
|
elif sdk_arch == "":
|
|
|
|
sdk_path_unknown_arch = path_with_ext
|
2021-09-12 20:23:05 +02:00
|
|
|
else:
|
|
|
|
if os.path.isfile(path) and os.access(path, os.X_OK):
|
2022-08-25 07:29:40 +02:00
|
|
|
sdk_arch = find_dotnet_arch(path)
|
|
|
|
if sdk_arch == arch or arch == "":
|
|
|
|
return path
|
2022-08-25 20:04:23 +02:00
|
|
|
elif sdk_arch == "":
|
|
|
|
sdk_path_unknown_arch = path
|
2019-05-20 18:34:35 +02:00
|
|
|
|
2022-08-25 20:04:23 +02:00
|
|
|
return sdk_path_unknown_arch
|