mono_configure.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import os
  2. import os.path
  3. def is_desktop(platform):
  4. return platform in ["windows", "macos", "linuxbsd", "server", "uwp", "haiku"]
  5. def is_unix_like(platform):
  6. return platform in ["macos", "linuxbsd", "server", "android", "haiku", "ios"]
  7. def module_supports_tools_on(platform):
  8. return is_desktop(platform)
  9. def configure(env, env_mono):
  10. # is_android = env["platform"] == "android"
  11. # is_javascript = env["platform"] == "javascript"
  12. # is_ios = env["platform"] == "ios"
  13. # is_ios_sim = is_ios and env["arch"] in ["x86", "x86_64"]
  14. tools_enabled = env["tools"]
  15. if tools_enabled and not module_supports_tools_on(env["platform"]):
  16. raise RuntimeError("This module does not currently support building for this platform with tools enabled")
  17. if env["tools"] or env["target"] != "release":
  18. env_mono.Append(CPPDEFINES=["GD_MONO_HOT_RELOAD"])
  19. app_host_dir = find_dotnet_app_host_dir(env)
  20. def check_app_host_file_exists(file):
  21. file_path = os.path.join(app_host_dir, file)
  22. if not os.path.isfile(file_path):
  23. raise RuntimeError("File not found: " + file_path)
  24. # TODO:
  25. # All libnethost does for us is provide a function to find hostfxr.
  26. # If we could handle that logic ourselves we could void linking it.
  27. # nethost file names:
  28. # static: libnethost.a/lib
  29. # shared: libnethost.a/dylib and nethost.dll
  30. check_app_host_file_exists("libnethost.lib" if os.name == "nt" else "libnethost.a")
  31. check_app_host_file_exists("nethost.h")
  32. check_app_host_file_exists("hostfxr.h")
  33. check_app_host_file_exists("coreclr_delegates.h")
  34. env.Append(LIBPATH=[app_host_dir])
  35. env_mono.Prepend(CPPPATH=app_host_dir)
  36. libnethost_path = os.path.join(app_host_dir, "libnethost.lib" if os.name == "nt" else "libnethost.a")
  37. if env["platform"] == "windows":
  38. env_mono.Append(CPPDEFINES=["NETHOST_USE_AS_STATIC"])
  39. if env.msvc:
  40. env.Append(LINKFLAGS="libnethost.lib")
  41. else:
  42. env.Append(LINKFLAGS=["-Wl,-whole-archive", libnethost_path, "-Wl,-no-whole-archive"])
  43. else:
  44. is_apple = env["platform"] in ["macos", "ios"]
  45. # is_macos = is_apple and not is_ios
  46. # if is_ios and not is_ios_sim:
  47. # env_mono.Append(CPPDEFINES=["IOS_DEVICE"])
  48. if is_apple:
  49. env.Append(LINKFLAGS=["-Wl,-force_load," + libnethost_path])
  50. else:
  51. env.Append(LINKFLAGS=["-Wl,-whole-archive", libnethost_path, "-Wl,-no-whole-archive"])
  52. def find_dotnet_app_host_dir(env):
  53. dotnet_root = env["dotnet_root"]
  54. if not dotnet_root:
  55. dotnet_exe = find_executable("dotnet")
  56. if dotnet_exe:
  57. dotnet_exe_realpath = os.path.realpath(dotnet_exe) # Eliminate symbolic links
  58. dotnet_root = os.path.abspath(os.path.join(dotnet_exe_realpath, os.pardir))
  59. else:
  60. raise RuntimeError("Cannot find .NET Core Sdk")
  61. print("Found .NET Core Sdk root directory: " + dotnet_root)
  62. dotnet_cmd = os.path.join(dotnet_root, "dotnet.exe" if os.name == "nt" else "dotnet")
  63. runtime_identifier = determine_runtime_identifier(env)
  64. # TODO: In the future, if it can't be found this way, we want to obtain it
  65. # from the runtime.{runtime_identifier}.Microsoft.NETCore.DotNetAppHost NuGet package.
  66. app_host_search_version = "5.0"
  67. app_host_version = find_app_host_version(dotnet_cmd, app_host_search_version)
  68. if not app_host_version:
  69. raise RuntimeError("Cannot find .NET app host for version: " + app_host_search_version)
  70. app_host_dir = os.path.join(
  71. dotnet_root,
  72. "packs",
  73. "Microsoft.NETCore.App.Host." + runtime_identifier,
  74. app_host_version,
  75. "runtimes",
  76. runtime_identifier,
  77. "native",
  78. )
  79. return app_host_dir
  80. def determine_runtime_identifier(env):
  81. names_map = {
  82. "windows": "win",
  83. "macos": "osx",
  84. "linuxbsd": "linux",
  85. "server": "linux", # FIXME: Is server linux only, or also macos?
  86. }
  87. # .NET RID architectures: x86, x64, arm, or arm64
  88. platform = env["platform"]
  89. if is_desktop(platform):
  90. if env["arch"] in ["arm", "arm32"]:
  91. rid = "arm"
  92. elif env["arch"] == "arm64":
  93. rid = "arm64"
  94. else:
  95. bits = env["bits"]
  96. bit_arch_map = {"64": "x64", "32": "x86"}
  97. rid = bit_arch_map[bits]
  98. return "%s-%s" % (names_map[platform], rid)
  99. else:
  100. raise NotImplementedError()
  101. def find_app_host_version(dotnet_cmd, search_version):
  102. import subprocess
  103. try:
  104. lines = subprocess.check_output([dotnet_cmd, "--list-runtimes"]).splitlines()
  105. for line_bytes in lines:
  106. line = line_bytes.decode("utf-8")
  107. if not line.startswith("Microsoft.NETCore.App "):
  108. continue
  109. parts = line.split(" ")
  110. if len(parts) < 2:
  111. continue
  112. version = parts[1]
  113. # Look for 6.0.0 or 6.0.0-*
  114. if version.startswith(search_version + "."):
  115. return version
  116. except (subprocess.CalledProcessError, OSError):
  117. pass
  118. return ""
  119. ENV_PATH_SEP = ";" if os.name == "nt" else ":"
  120. def find_executable(name):
  121. is_windows = os.name == "nt"
  122. windows_exts = os.environ["PATHEXT"].split(ENV_PATH_SEP) if is_windows else None
  123. path_dirs = os.environ["PATH"].split(ENV_PATH_SEP)
  124. search_dirs = path_dirs + [os.getcwd()] # cwd is last in the list
  125. for dir in search_dirs:
  126. path = os.path.join(dir, name)
  127. if is_windows:
  128. for extension in windows_exts:
  129. path_with_ext = path + extension
  130. if os.path.isfile(path_with_ext) and os.access(path_with_ext, os.X_OK):
  131. return path_with_ext
  132. else:
  133. if os.path.isfile(path) and os.access(path, os.X_OK):
  134. return path
  135. return ""