summaryrefslogtreecommitdiff
path: root/build_extra/rust/run.py
blob: 36d1ab83a8485fb05225adb91d0b7a4bb3f90899 (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
#!/usr/bin/env python
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
# This file just executes its arguments, except that also adds GN_OUT_DIR and
# CARGO_PKG_VERSION to the environ. This is for compatibility with cargo.
import subprocess
import sys
import os
import re

args = sys.argv[1:]
env = os.environ.copy()

if sys.platform == 'win32':
    # On Windows, when gn is setting up the build toolchain, it produces a set
    # of environment variables that are required to invoke the right build
    # toolchain. We need to load those environment variables here too in order
    # for rustc to be able to successfully invoke the linker tool.
    # The file is in 'windows environment block' format, which contains
    # multiple 'key=value' pairs, separated by '\0' bytes, and terminated by
    # two '\0' bytes at the end.
    gn_env_pairs = open("environment.x64").read()[:-2].split('\0')
    gn_env = dict([pair.split('=', 1) for pair in gn_env_pairs])
    env.update(gn_env)

# This is for src/msg.rs to know where to find msg_generated.rs.
# When building with Cargo this variable is set by build.rs.
env["GN_OUT_DIR"] = os.path.abspath(".")
assert os.path.isdir(env["GN_OUT_DIR"])

# Some crates (e.g. 'typenum') generate source files and place them in the
# directory indicated by the 'OUT_DIR' environment variable, which is normally
# set by Cargo. We pre-generate these files and store them in the source repo.
# Therefore, set 'OUT_DIR' so these crates can find their generated sources.
for i, arg in enumerate(args):
    match = re.search('--generated-source-dir=(.*)', arg)
    if match:
        env["OUT_DIR"] = os.path.abspath(match.group(1))
        del args[i]
        break

# Set the CARGO_PKG_VERSION env variable if provided as an argument
# When building with Cargo this variable is set automatically
for i, arg in enumerate(args):
    match = re.search('--cargo-pkg-version="?([^"]*)"?', arg)
    if match:
        env["CARGO_PKG_VERSION"] = match.group(1)
        del args[i]
        break

sys.exit(subprocess.call(args, env=env))