未验证 提交 89794d5d 编写于 作者: B Bert Belder

build: make it possible to pass arbitrary env vars to rustc

上级 590463bd
#!/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.
# This file just executes its arguments, except that it allows overriding
# environment variables using command-line arguments.
import subprocess
import sys
import os
import re
args = sys.argv[1:]
args = []
env = os.environ.copy()
if sys.platform == 'win32':
......@@ -27,24 +29,19 @@ if sys.platform == 'win32':
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)
# Environment variables can be specified on the command line using
# '--env=variable=value' flags. These flags are not passed through to rustc.
# This is useful to set env vars that are normally automatically set by Cargo,
# e.g. CARGO_PKG_NAME, CARGO_PKG_VERSION, OUT_DIR, etc.
for arg in sys.argv[1:]:
match = re.search('--env=([^=]+)=(.*)', arg)
if match:
env["CARGO_PKG_VERSION"] = match.group(1)
del args[i]
break
key, value = match.groups()
if key == "OUT_DIR":
# OUT_DIR needs to contain an absolute path.
value = os.path.abspath(value)
env[key] = value
else:
args.append(arg)
sys.exit(subprocess.call(args, env=env))
......@@ -63,6 +63,7 @@ template("_rust_crate") {
"crate_version",
"deps",
"edition",
"env",
"features",
"generated_source_dir",
"inputs",
......@@ -234,24 +235,6 @@ template("_rust_crate") {
"--color=always",
]
if (defined(generated_source_dir)) {
args += [
# 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. This flag tells run.py to set 'OUT_DIR' to
# the path where the current crate can find its generated sources.
"--generated-source-dir=" +
rebase_path(generated_source_dir, root_build_dir),
]
}
if (defined(crate_version)) {
args += [
# This is used to set env variables for Cargo build compatibility
"--cargo-pkg-version=$crate_version",
]
}
if (is_win) {
# Proc-macro crates need to be linked by rustc itself, because rustc
# doesn't expose all the information necessary to produce the correct
......@@ -318,6 +301,22 @@ template("_rust_crate") {
sources += [ info.out_path ]
deps += [ info.label ]
}
if (defined(generated_source_dir)) {
args += [
# 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. This flag tells run.py to set 'OUT_DIR' to
# the path where the current crate can find its generated sources.
"--env=OUT_DIR=" + rebase_path(generated_source_dir, root_build_dir),
]
}
if (defined(env)) {
foreach(e, env) {
args += [ "--env=$e" ]
}
}
}
}
......
......@@ -179,10 +179,10 @@ rust_executable("deno") {
# Extract version from Cargo.toml
# TODO integrate this into rust.gni by allowing the rust_executable template
# to specify a cargo.toml from which it will extract a version.
crate_version = deno_cargo_info.version
inputs = [
"Cargo.toml",
]
env = [ "CARGO_PKG_VERSION=${deno_cargo_info.version}" ]
}
rust_test("cli_test") {
......@@ -194,10 +194,10 @@ rust_test("cli_test") {
]
# Extract version from Cargo.toml
crate_version = deno_cargo_info.version
inputs = [
"Cargo.toml",
]
env = [ "CARGO_PKG_VERSION=${deno_cargo_info.version}" ]
}
# Generates the core TypeScript type library for deno that will be
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册