aboutsummaryrefslogtreecommitdiff
path: root/rustrunner.py
blob: 6c20bb301af4e7e94c09203d291e355234d18710 (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
#!/usr/bin/env python3

# Copyright 2014 The Meson development team

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This is a wrapper script to run the Rust compiler. It is needed
because:

- output file name of Rust compilation is not knowable at command
  execution time (no, --crate-name can't be used)
- need to delete old crates so nobody uses them by accident
"""

import sys, os, subprocess, glob

def delete_old_crates(target_name, target_type):
    if target_type == 'dylib':
        (base, suffix) = os.path.splitext(target_name)
        crates = glob.glob(base + '-*' + suffix)
        crates = [(os.stat(i).st_mtime, i) for i in crates]
        [os.unlink(c[1]) for c in sorted(crates)[:-1]]
    if target_type == 'lib':
        (base, suffix) = os.path.splitext(target_name)
        crates = glob.glob(base + '-*' + '.rlib') # Rust does not use .a
        crates = [(os.stat(i).st_mtime, i) for i in crates]
        [os.unlink(c[1]) for c in sorted(crates)[:-1]]

def invoke_rust(rustc_command):
    return subprocess.call(rustc_command, shell=False)

def touch_file(fname):
    try:
        os.unlink(fname)
    except FileNotFoundError:
        pass
    open(fname, 'w').close()

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print('This script is internal to Meson. Do not run it on its own.')
        print("%s <target name> <target type> <rustc invokation cmd line>")
        sys.exit(1)
    target_name = sys.argv[1]
    target_type = sys.argv[2]
    rustc_command = sys.argv[3:]
    retval = invoke_rust(rustc_command)
    if retval != 0:
        sys.exit(retval)
    if target_type != "bin":
        delete_old_crates(target_name, target_type)
        touch_file(target_name)