Debian Codegraph @

meson/query-rustc.py

raw
1 #!/usr/bin/env python3
2
3 from argparse import ArgumentParser
4 from pathlib import Path
5 import os
6 import re
7 import subprocess
8 import sys
9 import tempfile
10
11 parser = ArgumentParser()
12
13 parser.add_argument("RUSTC", type=Path, help="Path to rustc")
14 parser.add_argument("--query", action="store",
15 choices=['native-static-libs', 'default-host-toolchain', 'stable-actual-version'],
16 help="Item to query from RustC ['native-static-libs', 'default-host-toolchain', 'stable-actual-version']",
17 required=True)
18 parser.add_argument("--toolchain-version", action="store",
19 help="Rust Toolchain Version (if needed)")
20 parser.add_argument("--target", help="Target triplet")
21 parser.add_argument("--build-triplet", help="Build machine triplet (for cross builds using specific toolchain version)")
22
23 def removeprefix_fallback(s, pfx):
24 if sys.version_info > (3, 9):
25 return s.removeprefix(pfx)
26 elif s.startswith(pfx):
27 return s[len(pfx):]
28 else:
29 return s
30
31 def removesuffix_fallback(s, sfx):
32 if sys.version_info > (3, 9):
33 return s.removesuffix(sfx)
34 elif s.endswith(sfx):
35 return s[:-len(sfx)]
36 else:
37 return s
38
39 def retrieve_native_static_libs_from_output(output):
40 if re.match(r'^error[:|\[]', output):
41 print(output, file=sys.stderr)
42 sys.exit()
43
44 for i in output.strip().splitlines():
45 match = re.match(r".+native-static-libs: (.+)", i)
46 if match:
47 libs = match.group(1).split()
48 libs = [removesuffix_fallback(lib, ".lib") for lib in libs] # msvc
49 libs = [removeprefix_fallback(lib, "-l") for lib in libs] # msys2
50 print(
51 " ".join(
52 set(
53 libs
54 )
55 )
56 )
57
58 # Get the default target host or actual version of toolchain
59 def retrive_version_info(output, query):
60 for i in output.strip().splitlines():
61 match = re.match(r"%s: (.+)" % query, i)
62 if match:
63 return match.group(1)
64
65 if __name__ == "__main__":
66 args = parser.parse_args()
67 query = args.query
68 query_arg = None
69 rustc_cmd = [Path(args.RUSTC).as_posix()]
70
71 if args.toolchain_version is not None:
72 if args.target is None and args.build_triplet is None:
73 raise ValueError('--target or --build-triplet argument required if --toolchain-version is used')
74 if args.build_triplet is not None:
75 rustc_cmd.extend(['+%s-%s' % (args.toolchain_version, args.build_triplet)])
76 else:
77 rustc_cmd.extend(['+%s-%s' % (args.toolchain_version, args.target)])
78
79 if query == 'native-static-libs':
80 query_arg = ['--print=%s' % query]
81 else:
82 query_arg = ['--version', '--verbose']
83 rustc_cmd.extend(query_arg)
84 if args.target:
85 rustc_cmd.extend(['--target', args.target])
86
87 fd, dummy_out = tempfile.mkstemp()
88 os.close(fd)
89 try:
90 # We need these for '--print=native-static-libs' on Windows
91 if query == 'native-static-libs':
92 rustc_cmd.extend(['--crate-type', 'staticlib'])
93 rustc_cmd.append(os.devnull)
94 rustc_cmd.extend(['-o', dummy_out])
95
96 query_results = subprocess.run(
97 rustc_cmd,
98 capture_output=True,
99 text=True,
100 )
101 finally:
102 os.unlink(dummy_out)
103
104 if query == 'native-static-libs':
105 retrieve_native_static_libs_from_output(query_results.stderr)
106 elif query == 'default-host-toolchain' or query == 'stable-actual-version':
107 if query_results.stderr == '':
108 if query == 'default-host-toolchain':
109 result = retrive_version_info(query_results.stdout, 'host')
110 else:
111 result = retrive_version_info(query_results.stdout, 'release')
112 print(result)
113 else:
114 print(query_results.stderr, file=sys.stderr)