From 315636e77c1cd9d39f5ca9c1eed7c60a4653afa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20Rold=C3=A1n=20Etcheverry?= Date: Sat, 20 Aug 2022 01:48:25 +0200 Subject: [PATCH] C#: Fallback to `dotnet --info` to determine .NET RID Some Linux distros use their distro name as the RID for directory names. If the .NET Host directory cannot be found with the generic RID, 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. Co-authored-by: Lewis James --- modules/mono/build_scripts/mono_configure.py | 54 ++++++++++++++++---- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/modules/mono/build_scripts/mono_configure.py b/modules/mono/build_scripts/mono_configure.py index 071693d7587c..ca2fd9ba3cdd 100644 --- a/modules/mono/build_scripts/mono_configure.py +++ b/modules/mono/build_scripts/mono_configure.py @@ -96,15 +96,26 @@ def find_dotnet_app_host_dir(env): if not app_host_version: raise RuntimeError("Cannot find .NET app host for version: " + app_host_search_version) - app_host_dir = os.path.join( - dotnet_root, - "packs", - "Microsoft.NETCore.App.Host." + runtime_identifier, - app_host_version, - "runtimes", - runtime_identifier, - "native", - ) + 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() return app_host_dir @@ -160,6 +171,31 @@ def find_app_host_version(dotnet_cmd, search_version): return "" +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 "" + + ENV_PATH_SEP = ";" if os.name == "nt" else ":"