mono_configure.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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_version = "5.0"
  54. dotnet_root = env["dotnet_root"]
  55. if not dotnet_root:
  56. dotnet_cmd = find_executable("dotnet")
  57. if dotnet_cmd:
  58. sdk_path = find_dotnet_sdk(dotnet_cmd, dotnet_version)
  59. if sdk_path:
  60. dotnet_root = os.path.abspath(os.path.join(sdk_path, os.pardir))
  61. if not dotnet_root:
  62. raise RuntimeError("Cannot find .NET Core Sdk")
  63. print("Found .NET Core Sdk root directory: " + dotnet_root)
  64. dotnet_cmd = os.path.join(dotnet_root, "dotnet.exe" if os.name == "nt" else "dotnet")
  65. runtime_identifier = determine_runtime_identifier(env)
  66. # TODO: In the future, if it can't be found this way, we want to obtain it
  67. # from the runtime.{runtime_identifier}.Microsoft.NETCore.DotNetAppHost NuGet package.
  68. app_host_version = find_app_host_version(dotnet_cmd, dotnet_version)
  69. if not app_host_version:
  70. raise RuntimeError("Cannot find .NET app host for version: " + dotnet_version)
  71. def get_runtime_path():
  72. return os.path.join(
  73. dotnet_root,
  74. "packs",
  75. "Microsoft.NETCore.App.Host." + runtime_identifier,
  76. app_host_version,
  77. "runtimes",
  78. runtime_identifier,
  79. "native",
  80. )
  81. app_host_dir = get_runtime_path()
  82. # Some Linux distros use their distro name as the RID in these paths.
  83. # If the initial generic path doesn't exist, try to get the RID from `dotnet --info`.
  84. # The generic RID should still be the first choice. Some platforms like Windows 10
  85. # define the RID as `win10-x64` but still use the generic `win-x64` for directory names.
  86. if not app_host_dir or not os.path.isdir(app_host_dir):
  87. runtime_identifier = find_dotnet_cli_rid(dotnet_cmd)
  88. app_host_dir = get_runtime_path()
  89. return app_host_dir
  90. def determine_runtime_identifier(env):
  91. names_map = {
  92. "windows": "win",
  93. "macos": "osx",
  94. "linuxbsd": "linux",
  95. "server": "linux", # FIXME: Is server linux only, or also macos?
  96. }
  97. # .NET RID architectures: x86, x64, arm, or arm64
  98. platform = env["platform"]
  99. if is_desktop(platform):
  100. if env["arch"] in ["arm", "arm32"]:
  101. rid = "arm"
  102. elif env["arch"] == "arm64":
  103. rid = "arm64"
  104. else:
  105. bits = env["bits"]
  106. bit_arch_map = {"64": "x64", "32": "x86"}
  107. rid = bit_arch_map[bits]
  108. return "%s-%s" % (names_map[platform], rid)
  109. else:
  110. raise NotImplementedError()
  111. def find_app_host_version(dotnet_cmd, search_version_str):
  112. import subprocess
  113. from distutils.version import LooseVersion
  114. search_version = LooseVersion(search_version_str)
  115. try:
  116. lines = subprocess.check_output([dotnet_cmd, "--list-runtimes"]).splitlines()
  117. for line_bytes in lines:
  118. line = line_bytes.decode("utf-8")
  119. if not line.startswith("Microsoft.NETCore.App "):
  120. continue
  121. parts = line.split(" ", 2)
  122. if len(parts) < 3:
  123. continue
  124. version_str = parts[1]
  125. version = LooseVersion(version_str)
  126. if version >= search_version:
  127. return version_str
  128. except (subprocess.CalledProcessError, OSError) as e:
  129. import sys
  130. print(e, file=sys.stderr)
  131. return ""
  132. def find_dotnet_sdk(dotnet_cmd, search_version_str):
  133. import subprocess
  134. from distutils.version import LooseVersion
  135. search_version = LooseVersion(search_version_str)
  136. try:
  137. lines = subprocess.check_output([dotnet_cmd, "--list-sdks"]).splitlines()
  138. for line_bytes in lines:
  139. line = line_bytes.decode("utf-8")
  140. parts = line.split(" ", 1)
  141. if len(parts) < 2:
  142. continue
  143. version_str = parts[0]
  144. version = LooseVersion(version_str)
  145. if version < search_version:
  146. continue
  147. path_part = parts[1]
  148. return path_part[1 : path_part.find("]")]
  149. except (subprocess.CalledProcessError, OSError) as e:
  150. import sys
  151. print(e, file=sys.stderr)
  152. return ""
  153. def find_dotnet_cli_rid(dotnet_cmd):
  154. import subprocess
  155. try:
  156. env = dict(os.environ, DOTNET_CLI_UI_LANGUAGE="en-US")
  157. lines = subprocess.check_output([dotnet_cmd, "--info"], env=env).splitlines()
  158. for line_bytes in lines:
  159. line = line_bytes.decode("utf-8")
  160. if not line.startswith(" RID:"):
  161. continue
  162. parts = line.split()
  163. if len(parts) < 2:
  164. continue
  165. return parts[1]
  166. except (subprocess.CalledProcessError, OSError) as e:
  167. import sys
  168. print(e, file=sys.stderr)
  169. return ""
  170. ENV_PATH_SEP = ";" if os.name == "nt" else ":"
  171. def find_executable(name):
  172. is_windows = os.name == "nt"
  173. windows_exts = os.environ["PATHEXT"].split(ENV_PATH_SEP) if is_windows else None
  174. path_dirs = os.environ["PATH"].split(ENV_PATH_SEP)
  175. search_dirs = path_dirs + [os.getcwd()] # cwd is last in the list
  176. for dir in search_dirs:
  177. path = os.path.join(dir, name)
  178. if is_windows:
  179. for extension in windows_exts:
  180. path_with_ext = path + extension
  181. if os.path.isfile(path_with_ext) and os.access(path_with_ext, os.X_OK):
  182. return path_with_ext
  183. else:
  184. if os.path.isfile(path) and os.access(path, os.X_OK):
  185. return path
  186. return ""