]> git.lizzy.rs Git - rust.git/blob - src/test/run-make-fulldeps/sysroot-crates-are-unstable/test.py
Rollup merge of #104849 - GuillaumeGomez:source-code-sidebar-css-migration, r=notriddle
[rust.git] / src / test / run-make-fulldeps / sysroot-crates-are-unstable / test.py
1 import sys
2 import os
3 from os import listdir
4 from os.path import isfile, join
5 from subprocess import PIPE, Popen
6
7
8 # This is n list of files which are stable crates or simply are not crates,
9 # we don't check for the instability of these crates as they're all stable!
10 STABLE_CRATES = ['std', 'alloc', 'core', 'proc_macro',
11                  'rsbegin.o', 'rsend.o', 'dllcrt2.o', 'crt2.o', 'clang_rt']
12
13
14 def convert_to_string(s):
15     if s.__class__.__name__ == 'bytes':
16         return s.decode('utf-8')
17     return s
18
19
20 def set_ld_lib_path():
21     var = os.environ.get("LD_LIB_PATH_ENVVAR")
22     rpath = os.environ.get("HOST_RPATH_DIR")
23     if var and rpath:
24         path = os.environ.get(var)
25         if path:
26             os.environ[var] = rpath + os.pathsep + path
27         else:
28             os.environ[var] = rpath
29
30
31 def exec_command(command, to_input=None):
32     child = None
33     if to_input is None:
34         child = Popen(command, stdout=PIPE, stderr=PIPE)
35     else:
36         child = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE)
37     stdout, stderr = child.communicate(input=to_input)
38     return (convert_to_string(stdout), convert_to_string(stderr))
39
40
41 def check_lib(lib):
42     if lib['name'] in STABLE_CRATES:
43         return True
44     print('verifying if {} is an unstable crate'.format(lib['name']))
45     stdout, stderr = exec_command([os.environ['RUSTC'], '-', '--crate-type', 'rlib',
46                                    '--extern', '{}={}'.format(lib['name'], lib['path'])],
47                                   to_input=('extern crate {};'.format(lib['name'])).encode('utf-8'))
48     if not 'use of unstable library feature' in '{}{}'.format(stdout, stderr):
49         print('crate {} "{}" is not unstable'.format(lib['name'], lib['path']))
50         print('{}{}'.format(stdout, stderr))
51         print('')
52         return False
53     return True
54
55 # Generate a list of all crates in the sysroot. To do this we list all files in
56 # rustc's sysroot, look at the filename, strip everything after the `-`, and
57 # strip the leading `lib` (if present)
58 def get_all_libs(dir_path):
59     return [{ 'path': join(dir_path, f), 'name': f[3:].split('-')[0] }
60             for f in listdir(dir_path)
61             if isfile(join(dir_path, f)) and f.endswith('.rlib') and f not in STABLE_CRATES]
62
63
64 set_ld_lib_path()
65 sysroot = exec_command([os.environ['RUSTC'], '--print', 'sysroot'])[0].replace('\n', '')
66 assert sysroot, "Could not read the rustc sysroot!"
67 libs = get_all_libs(join(sysroot, 'lib/rustlib/{}/lib'.format(os.environ['TARGET'])))
68
69 ret = 0
70 for lib in libs:
71     if not check_lib(lib):
72         # We continue so users can see all the not unstable crates.
73         ret = 1
74 sys.exit(ret)