| 1 |
|
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import os |
| 6 |
import os.path |
| 7 |
import shlex |
| 8 |
import subprocess |
| 9 |
from dataclasses import dataclass |
| 10 |
|
| 11 |
|
| 12 |
def find_dotnet_cli(): |
| 13 |
if os.name == "nt": |
| 14 |
for hint_dir in os.environ["PATH"].split(os.pathsep): |
| 15 |
hint_dir = hint_dir.strip('"') |
| 16 |
hint_path = os.path.join(hint_dir, "dotnet") |
| 17 |
if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK): |
| 18 |
return hint_path |
| 19 |
if os.path.isfile(hint_path + ".exe") and os.access(hint_path + ".exe", os.X_OK): |
| 20 |
return hint_path + ".exe" |
| 21 |
else: |
| 22 |
for hint_dir in os.environ["PATH"].split(os.pathsep): |
| 23 |
hint_dir = hint_dir.strip('"') |
| 24 |
hint_path = os.path.join(hint_dir, "dotnet") |
| 25 |
if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK): |
| 26 |
return hint_path |
| 27 |
|
| 28 |
|
| 29 |
def find_msbuild_standalone_windows(): |
| 30 |
msbuild_tools_path = find_msbuild_tools_path_reg() |
| 31 |
|
| 32 |
if msbuild_tools_path: |
| 33 |
return os.path.join(msbuild_tools_path, "MSBuild.exe") |
| 34 |
|
| 35 |
return None |
| 36 |
|
| 37 |
|
| 38 |
def find_msbuild_mono_windows(mono_prefix): |
| 39 |
assert mono_prefix is not None |
| 40 |
|
| 41 |
mono_bin_dir = os.path.join(mono_prefix, "bin") |
| 42 |
msbuild_mono = os.path.join(mono_bin_dir, "msbuild.bat") |
| 43 |
|
| 44 |
if os.path.isfile(msbuild_mono): |
| 45 |
return msbuild_mono |
| 46 |
|
| 47 |
return None |
| 48 |
|
| 49 |
|
| 50 |
def find_msbuild_mono_unix(): |
| 51 |
import sys |
| 52 |
|
| 53 |
hint_dirs = [] |
| 54 |
if sys.platform == "darwin": |
| 55 |
hint_dirs[:0] = [ |
| 56 |
"/Library/Frameworks/Mono.framework/Versions/Current/bin", |
| 57 |
"/usr/local/var/homebrew/linked/mono/bin", |
| 58 |
] |
| 59 |
|
| 60 |
for hint_dir in hint_dirs: |
| 61 |
hint_path = os.path.join(hint_dir, "msbuild") |
| 62 |
if os.path.isfile(hint_path): |
| 63 |
return hint_path |
| 64 |
elif os.path.isfile(hint_path + ".exe"): |
| 65 |
return hint_path + ".exe" |
| 66 |
|
| 67 |
for hint_dir in os.environ["PATH"].split(os.pathsep): |
| 68 |
hint_dir = hint_dir.strip('"') |
| 69 |
hint_path = os.path.join(hint_dir, "msbuild") |
| 70 |
if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK): |
| 71 |
return hint_path |
| 72 |
if os.path.isfile(hint_path + ".exe") and os.access(hint_path + ".exe", os.X_OK): |
| 73 |
return hint_path + ".exe" |
| 74 |
|
| 75 |
return None |
| 76 |
|
| 77 |
|
| 78 |
def find_msbuild_tools_path_reg(): |
| 79 |
import subprocess |
| 80 |
|
| 81 |
program_files = os.getenv("PROGRAMFILES(X86)") |
| 82 |
if not program_files: |
| 83 |
program_files = os.getenv("PROGRAMFILES") |
| 84 |
vswhere = os.path.join(program_files, "Microsoft Visual Studio", "Installer", "vswhere.exe") |
| 85 |
|
| 86 |
vswhere_args = ["-latest", "-products", "*", "-requires", "Microsoft.Component.MSBuild"] |
| 87 |
|
| 88 |
try: |
| 89 |
lines = subprocess.check_output([vswhere] + vswhere_args).splitlines() |
| 90 |
|
| 91 |
for line in lines: |
| 92 |
parts = line.decode("utf-8").split(":", 1) |
| 93 |
|
| 94 |
if len(parts) < 2 or parts[0] != "installationPath": |
| 95 |
continue |
| 96 |
|
| 97 |
val = parts[1].strip() |
| 98 |
|
| 99 |
if not val: |
| 100 |
raise ValueError("Value of `installationPath` entry is empty") |
| 101 |
|
| 102 |
|
| 103 |
msbuild_dir = os.path.join(val, "MSBuild", "Current", "Bin") |
| 104 |
if os.path.isdir(msbuild_dir): |
| 105 |
return msbuild_dir |
| 106 |
|
| 107 |
|
| 108 |
return os.path.join(val, "MSBuild", "15.0", "Bin") |
| 109 |
|
| 110 |
raise ValueError("Cannot find `installationPath` entry") |
| 111 |
except ValueError as e: |
| 112 |
print("Error reading output from vswhere: " + str(e)) |
| 113 |
except OSError: |
| 114 |
pass |
| 115 |
except (subprocess.CalledProcessError, OSError): |
| 116 |
pass |
| 117 |
|
| 118 |
|
| 119 |
@dataclass |
| 120 |
class ToolsLocation: |
| 121 |
dotnet_cli: str = "" |
| 122 |
msbuild_standalone: str = "" |
| 123 |
msbuild_mono: str = "" |
| 124 |
mono_bin_dir: str = "" |
| 125 |
|
| 126 |
|
| 127 |
def find_any_msbuild_tool(mono_prefix): |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
dotnet_cli = find_dotnet_cli() |
| 132 |
if dotnet_cli: |
| 133 |
return ToolsLocation(dotnet_cli=dotnet_cli) |
| 134 |
|
| 135 |
|
| 136 |
if os.name == "nt": |
| 137 |
msbuild_standalone = find_msbuild_standalone_windows() |
| 138 |
if msbuild_standalone: |
| 139 |
return ToolsLocation(msbuild_standalone=msbuild_standalone) |
| 140 |
|
| 141 |
if mono_prefix: |
| 142 |
|
| 143 |
if os.name == "nt": |
| 144 |
msbuild_mono = find_msbuild_mono_windows(mono_prefix) |
| 145 |
if msbuild_mono: |
| 146 |
return ToolsLocation(msbuild_mono=msbuild_mono) |
| 147 |
else: |
| 148 |
msbuild_mono = find_msbuild_mono_unix() |
| 149 |
if msbuild_mono: |
| 150 |
return ToolsLocation(msbuild_mono=msbuild_mono) |
| 151 |
|
| 152 |
return None |
| 153 |
|
| 154 |
|
| 155 |
def run_msbuild(tools: ToolsLocation, sln: str, chdir_to: str, msbuild_args: list[str] | None = None): |
| 156 |
using_msbuild_mono = False |
| 157 |
|
| 158 |
|
| 159 |
if tools.dotnet_cli: |
| 160 |
args = [tools.dotnet_cli, "msbuild"] |
| 161 |
elif tools.msbuild_standalone: |
| 162 |
args = [tools.msbuild_standalone] |
| 163 |
elif tools.msbuild_mono: |
| 164 |
args = [tools.msbuild_mono] |
| 165 |
using_msbuild_mono = True |
| 166 |
else: |
| 167 |
raise RuntimeError("Path to MSBuild or dotnet CLI not provided.") |
| 168 |
|
| 169 |
args += [sln] |
| 170 |
|
| 171 |
if msbuild_args: |
| 172 |
args += msbuild_args |
| 173 |
|
| 174 |
print("Running MSBuild: ", " ".join(shlex.quote(arg) for arg in args), flush=True) |
| 175 |
|
| 176 |
msbuild_env = os.environ.copy() |
| 177 |
|
| 178 |
|
| 179 |
if "PLATFORM" in msbuild_env: |
| 180 |
del msbuild_env["PLATFORM"] |
| 181 |
|
| 182 |
if using_msbuild_mono: |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
msbuild_env.update( |
| 187 |
{ |
| 188 |
"CscToolExe": os.path.join(tools.mono_bin_dir, "csc.bat"), |
| 189 |
"VbcToolExe": os.path.join(tools.mono_bin_dir, "vbc.bat"), |
| 190 |
"FscToolExe": os.path.join(tools.mono_bin_dir, "fsharpc.bat"), |
| 191 |
} |
| 192 |
) |
| 193 |
|
| 194 |
|
| 195 |
return subprocess.call(args, env=msbuild_env, cwd=chdir_to) |
| 196 |
|
| 197 |
|
| 198 |
def build_godot_api(msbuild_tool, module_dir, output_dir, push_nupkgs_local, precision, no_deprecated, werror): |
| 199 |
target_filenames = [ |
| 200 |
"GodotSharp.dll", |
| 201 |
"GodotSharp.pdb", |
| 202 |
"GodotSharp.xml", |
| 203 |
"GodotSharpEditor.dll", |
| 204 |
"GodotSharpEditor.pdb", |
| 205 |
"GodotSharpEditor.xml", |
| 206 |
"GodotPlugins.dll", |
| 207 |
"GodotPlugins.pdb", |
| 208 |
"GodotPlugins.runtimeconfig.json", |
| 209 |
] |
| 210 |
|
| 211 |
for build_config in ["Debug", "Release"]: |
| 212 |
editor_api_dir = os.path.join(output_dir, "GodotSharp", "Api", build_config) |
| 213 |
|
| 214 |
targets = [os.path.join(editor_api_dir, filename) for filename in target_filenames] |
| 215 |
|
| 216 |
args = ["/restore", "/t:Build", "/p:Configuration=" + build_config, "/p:NoWarn=1591"] |
| 217 |
if push_nupkgs_local: |
| 218 |
args += ["/p:ClearNuGetLocalCache=true", "/p:PushNuGetToLocalSource=" + push_nupkgs_local] |
| 219 |
if precision == "double": |
| 220 |
args += ["/p:GodotFloat64=true"] |
| 221 |
if no_deprecated: |
| 222 |
args += ["/p:GodotNoDeprecated=true"] |
| 223 |
if werror: |
| 224 |
args += ["/p:TreatWarningsAsErrors=true"] |
| 225 |
|
| 226 |
sln = os.path.join(module_dir, "glue/GodotSharp/GodotSharp.sln") |
| 227 |
exit_code = run_msbuild(msbuild_tool, sln=sln, chdir_to=module_dir, msbuild_args=args) |
| 228 |
if exit_code != 0: |
| 229 |
return exit_code |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
core_src_dir = os.path.abspath(os.path.join(sln, os.pardir, "GodotSharp", "bin", build_config)) |
| 234 |
editor_src_dir = os.path.abspath(os.path.join(sln, os.pardir, "GodotSharpEditor", "bin", build_config)) |
| 235 |
plugins_src_dir = os.path.abspath(os.path.join(sln, os.pardir, "GodotPlugins", "bin", build_config, "net8.0")) |
| 236 |
|
| 237 |
if not os.path.isdir(editor_api_dir): |
| 238 |
assert not os.path.isfile(editor_api_dir) |
| 239 |
os.makedirs(editor_api_dir) |
| 240 |
|
| 241 |
def copy_target(target_path): |
| 242 |
from shutil import copy |
| 243 |
|
| 244 |
filename = os.path.basename(target_path) |
| 245 |
|
| 246 |
src_path = os.path.join(core_src_dir, filename) |
| 247 |
if not os.path.isfile(src_path): |
| 248 |
src_path = os.path.join(editor_src_dir, filename) |
| 249 |
if not os.path.isfile(src_path): |
| 250 |
src_path = os.path.join(plugins_src_dir, filename) |
| 251 |
|
| 252 |
print(f"Copying assembly to {target_path}...") |
| 253 |
copy(src_path, target_path) |
| 254 |
|
| 255 |
for scons_target in targets: |
| 256 |
copy_target(scons_target) |
| 257 |
|
| 258 |
return 0 |
| 259 |
|
| 260 |
|
| 261 |
def generate_sdk_package_versions(): |
| 262 |
|
| 263 |
|
| 264 |
import os |
| 265 |
import sys |
| 266 |
from os.path import dirname |
| 267 |
|
| 268 |
|
| 269 |
script_path = dirname(os.path.abspath(__file__)) |
| 270 |
root_path = dirname(dirname(dirname(script_path))) |
| 271 |
|
| 272 |
sys.path.insert(0, root_path) |
| 273 |
from methods import get_version_info |
| 274 |
|
| 275 |
version_info = get_version_info("") |
| 276 |
sys.path.remove(root_path) |
| 277 |
|
| 278 |
version_str = "{major}.{minor}.{patch}".format(**version_info) |
| 279 |
version_status = version_info["status"] |
| 280 |
if version_status != "stable": |
| 281 |
|
| 282 |
|
| 283 |
import re |
| 284 |
|
| 285 |
match = re.search(r"[\d]+$", version_status) |
| 286 |
if match: |
| 287 |
pos = match.start() |
| 288 |
version_status = version_status[:pos] + "." + version_status[pos:] |
| 289 |
version_str += "-" + version_status |
| 290 |
|
| 291 |
import version |
| 292 |
|
| 293 |
version_defines = ( |
| 294 |
[ |
| 295 |
f"GODOT{version.major}", |
| 296 |
f"GODOT{version.major}_{version.minor}", |
| 297 |
f"GODOT{version.major}_{version.minor}_{version.patch}", |
| 298 |
] |
| 299 |
+ [f"GODOT{v}_OR_GREATER" for v in range(4, version.major + 1)] |
| 300 |
+ [f"GODOT{version.major}_{v}_OR_GREATER" for v in range(0, version.minor + 1)] |
| 301 |
+ [f"GODOT{version.major}_{version.minor}_{v}_OR_GREATER" for v in range(0, version.patch + 1)] |
| 302 |
) |
| 303 |
|
| 304 |
props = """<Project> |
| 305 |
<PropertyGroup> |
| 306 |
<PackageVersion_GodotSharp>{0}</PackageVersion_GodotSharp> |
| 307 |
<PackageVersion_Godot_NET_Sdk>{0}</PackageVersion_Godot_NET_Sdk> |
| 308 |
<PackageVersion_Godot_SourceGenerators>{0}</PackageVersion_Godot_SourceGenerators> |
| 309 |
<GodotVersionConstants>{1}</GodotVersionConstants> |
| 310 |
</PropertyGroup> |
| 311 |
</Project> |
| 312 |
""".format(version_str, ";".join(version_defines)) |
| 313 |
|
| 314 |
|
| 315 |
with open(os.path.join(dirname(script_path), "SdkPackageVersions.props"), "w", encoding="utf-8", newline="\n") as f: |
| 316 |
f.write(props) |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
constants = """namespace Godot.SourceGenerators |
| 321 |
{{ |
| 322 |
// TODO: This is currently disabled because of https://github.com/dotnet/roslyn/issues/52904 |
| 323 |
#pragma warning disable IDE0040 // Add accessibility modifiers. |
| 324 |
partial class Common |
| 325 |
{{ |
| 326 |
public const string VersionDocsUrl = "https://docs.godotengine.org/en/{docs_branch}"; |
| 327 |
}} |
| 328 |
}} |
| 329 |
""".format(**version_info) |
| 330 |
|
| 331 |
generators_dir = os.path.join( |
| 332 |
dirname(script_path), |
| 333 |
"editor", |
| 334 |
"Godot.NET.Sdk", |
| 335 |
"Godot.SourceGenerators", |
| 336 |
"Generated", |
| 337 |
) |
| 338 |
os.makedirs(generators_dir, exist_ok=True) |
| 339 |
|
| 340 |
with open(os.path.join(generators_dir, "Common.Constants.cs"), "w", encoding="utf-8", newline="\n") as f: |
| 341 |
f.write(constants) |
| 342 |
|
| 343 |
|
| 344 |
def build_all( |
| 345 |
msbuild_tool, module_dir, output_dir, godot_platform, dev_debug, push_nupkgs_local, precision, no_deprecated, werror |
| 346 |
): |
| 347 |
|
| 348 |
generate_sdk_package_versions() |
| 349 |
|
| 350 |
|
| 351 |
exit_code = build_godot_api( |
| 352 |
msbuild_tool, module_dir, output_dir, push_nupkgs_local, precision, no_deprecated, werror |
| 353 |
) |
| 354 |
if exit_code != 0: |
| 355 |
return exit_code |
| 356 |
|
| 357 |
|
| 358 |
sln = os.path.join(module_dir, "editor/GodotTools/GodotTools.sln") |
| 359 |
args = ["/restore", "/t:Build", "/p:Configuration=" + ("Debug" if dev_debug else "Release")] + ( |
| 360 |
["/p:GodotPlatform=" + godot_platform] if godot_platform else [] |
| 361 |
) |
| 362 |
if push_nupkgs_local: |
| 363 |
args += ["/p:ClearNuGetLocalCache=true", "/p:PushNuGetToLocalSource=" + push_nupkgs_local] |
| 364 |
if precision == "double": |
| 365 |
args += ["/p:GodotFloat64=true"] |
| 366 |
exit_code = run_msbuild(msbuild_tool, sln=sln, chdir_to=module_dir, msbuild_args=args) |
| 367 |
if exit_code != 0: |
| 368 |
return exit_code |
| 369 |
|
| 370 |
|
| 371 |
args = ["/restore", "/t:Build", "/p:Configuration=Release"] |
| 372 |
if push_nupkgs_local: |
| 373 |
args += ["/p:ClearNuGetLocalCache=true", "/p:PushNuGetToLocalSource=" + push_nupkgs_local] |
| 374 |
if precision == "double": |
| 375 |
args += ["/p:GodotFloat64=true"] |
| 376 |
if no_deprecated: |
| 377 |
args += ["/p:GodotNoDeprecated=true"] |
| 378 |
sln = os.path.join(module_dir, "editor/Godot.NET.Sdk/Godot.NET.Sdk.sln") |
| 379 |
exit_code = run_msbuild(msbuild_tool, sln=sln, chdir_to=module_dir, msbuild_args=args) |
| 380 |
if exit_code != 0: |
| 381 |
return exit_code |
| 382 |
|
| 383 |
return 0 |
| 384 |
|
| 385 |
|
| 386 |
def main(): |
| 387 |
import argparse |
| 388 |
import sys |
| 389 |
|
| 390 |
parser = argparse.ArgumentParser(description="Builds all Godot .NET solutions") |
| 391 |
parser.add_argument("--godot-output-dir", type=str, required=True) |
| 392 |
parser.add_argument( |
| 393 |
"--dev-debug", |
| 394 |
action="store_true", |
| 395 |
default=False, |
| 396 |
help="Build GodotTools and Godot.NET.Sdk with 'Configuration=Debug'", |
| 397 |
) |
| 398 |
parser.add_argument("--godot-platform", type=str, default="") |
| 399 |
parser.add_argument("--mono-prefix", type=str, default="") |
| 400 |
parser.add_argument("--push-nupkgs-local", type=str, default="") |
| 401 |
parser.add_argument( |
| 402 |
"--precision", type=str, default="single", choices=["single", "double"], help="Floating-point precision level" |
| 403 |
) |
| 404 |
parser.add_argument( |
| 405 |
"--no-deprecated", |
| 406 |
action="store_true", |
| 407 |
default=False, |
| 408 |
help="Build GodotSharp without using deprecated features. This is required, if the engine was built with 'deprecated=no'.", |
| 409 |
) |
| 410 |
parser.add_argument("--werror", action="store_true", default=False, help="Treat compiler warnings as errors.") |
| 411 |
|
| 412 |
args = parser.parse_args() |
| 413 |
|
| 414 |
this_script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 415 |
module_dir = os.path.abspath(os.path.join(this_script_dir, os.pardir)) |
| 416 |
|
| 417 |
output_dir = os.path.abspath(args.godot_output_dir) |
| 418 |
|
| 419 |
push_nupkgs_local = os.path.abspath(args.push_nupkgs_local) if args.push_nupkgs_local else None |
| 420 |
|
| 421 |
msbuild_tool = find_any_msbuild_tool(args.mono_prefix) |
| 422 |
|
| 423 |
if msbuild_tool is None: |
| 424 |
print("Unable to find MSBuild") |
| 425 |
sys.exit(1) |
| 426 |
|
| 427 |
exit_code = build_all( |
| 428 |
msbuild_tool, |
| 429 |
module_dir, |
| 430 |
output_dir, |
| 431 |
args.godot_platform, |
| 432 |
args.dev_debug, |
| 433 |
push_nupkgs_local, |
| 434 |
args.precision, |
| 435 |
args.no_deprecated, |
| 436 |
args.werror, |
| 437 |
) |
| 438 |
sys.exit(exit_code) |
| 439 |
|
| 440 |
|
| 441 |
if __name__ == "__main__": |
| 442 |
main() |