summary refs log tree commit diff
path: root/src/test/run-make/sysroot-crates-are-unstable/test.py
blob: 210059e1010df795411396c9eec8351827017840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright 2015 The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

import sys
import os
from os import listdir
from os.path import isfile, join
from subprocess import PIPE, Popen


# This is a whitelist of files which are stable crates or simply are not crates,
# we don't check for the instability of these crates as they're all stable!
STABLE_CRATES = ['std', 'core', 'proc_macro', 'rsbegin.o', 'rsend.o', 'dllcrt2.o', 'crt2.o',
                 'clang_rt']


def convert_to_string(s):
    if s.__class__.__name__ == 'bytes':
        return s.decode('utf-8')
    return s


def exec_command(command, to_input=None):
    child = None
    if to_input is None:
        child = Popen(command, stdout=PIPE, stderr=PIPE)
    else:
        child = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE)
    stdout, stderr = child.communicate(input=to_input)
    return (convert_to_string(stdout), convert_to_string(stderr))


def check_lib(lib):
    if lib['name'] in STABLE_CRATES:
        return True
    print('verifying if {} is an unstable crate'.format(lib['name']))
    stdout, stderr = exec_command([os.environ['RUSTC'], '-', '--crate-type', 'rlib',
                                   '--extern', '{}={}'.format(lib['name'], lib['path'])],
                                  to_input='extern crate {};'.format(lib['name']))
    if not 'use of unstable library feature' in '{}{}'.format(stdout, stderr):
        print('crate {} "{}" is not unstable'.format(lib['name'], lib['path']))
        print('{}{}'.format(stdout, stderr))
        print('')
        return False
    return True

# Generate a list of all crates in the sysroot. To do this we list all files in
# rustc's sysroot, look at the filename, strip everything after the `-`, and
# strip the leading `lib` (if present)
def get_all_libs(dir_path):
    return [{ 'path': join(dir_path, f), 'name': f[3:].split('-')[0] }
            for f in listdir(dir_path)
            if isfile(join(dir_path, f)) and f.endswith('.rlib') and f not in STABLE_CRATES]


sysroot = exec_command([os.environ['RUSTC'], '--print', 'sysroot'])[0].replace('\n', '')
libs = get_all_libs(join(sysroot, 'lib/rustlib/{}/lib'.format(os.environ['TARGET'])))

ret = 0
for lib in libs:
    if not check_lib(lib):
        # We continue so users can see all the not unstable crates.
        ret = 1
sys.exit(ret)