0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00

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

This commit is contained in:
Bert Belder 2019-08-28 22:19:07 -07:00
parent 590463bd4a
commit 89794d5d34
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
3 changed files with 37 additions and 41 deletions

View file

@ -1,13 +1,15 @@
#!/usr/bin/env python #!/usr/bin/env python
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. # 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 subprocess
import sys import sys
import os import os
import re import re
args = sys.argv[1:] args = []
env = os.environ.copy() env = os.environ.copy()
if sys.platform == 'win32': if sys.platform == 'win32':
@ -27,24 +29,19 @@ if sys.platform == 'win32':
env["GN_OUT_DIR"] = os.path.abspath(".") env["GN_OUT_DIR"] = os.path.abspath(".")
assert os.path.isdir(env["GN_OUT_DIR"]) assert os.path.isdir(env["GN_OUT_DIR"])
# Some crates (e.g. 'typenum') generate source files and place them in the # Environment variables can be specified on the command line using
# directory indicated by the 'OUT_DIR' environment variable, which is normally # '--env=variable=value' flags. These flags are not passed through to rustc.
# set by Cargo. We pre-generate these files and store them in the source repo. # This is useful to set env vars that are normally automatically set by Cargo,
# Therefore, set 'OUT_DIR' so these crates can find their generated sources. # e.g. CARGO_PKG_NAME, CARGO_PKG_VERSION, OUT_DIR, etc.
for i, arg in enumerate(args): for arg in sys.argv[1:]:
match = re.search('--generated-source-dir=(.*)', arg) match = re.search('--env=([^=]+)=(.*)', arg)
if match: if match:
env["OUT_DIR"] = os.path.abspath(match.group(1)) key, value = match.groups()
del args[i] if key == "OUT_DIR":
break # OUT_DIR needs to contain an absolute path.
value = os.path.abspath(value)
# Set the CARGO_PKG_VERSION env variable if provided as an argument env[key] = value
# When building with Cargo this variable is set automatically else:
for i, arg in enumerate(args): args.append(arg)
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)) sys.exit(subprocess.call(args, env=env))

View file

@ -63,6 +63,7 @@ template("_rust_crate") {
"crate_version", "crate_version",
"deps", "deps",
"edition", "edition",
"env",
"features", "features",
"generated_source_dir", "generated_source_dir",
"inputs", "inputs",
@ -234,24 +235,6 @@ template("_rust_crate") {
"--color=always", "--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) { if (is_win) {
# Proc-macro crates need to be linked by rustc itself, because rustc # Proc-macro crates need to be linked by rustc itself, because rustc
# doesn't expose all the information necessary to produce the correct # doesn't expose all the information necessary to produce the correct
@ -318,6 +301,22 @@ template("_rust_crate") {
sources += [ info.out_path ] sources += [ info.out_path ]
deps += [ info.label ] 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" ]
}
}
} }
} }

View file

@ -179,10 +179,10 @@ rust_executable("deno") {
# Extract version from Cargo.toml # Extract version from Cargo.toml
# TODO integrate this into rust.gni by allowing the rust_executable template # TODO integrate this into rust.gni by allowing the rust_executable template
# to specify a cargo.toml from which it will extract a version. # to specify a cargo.toml from which it will extract a version.
crate_version = deno_cargo_info.version
inputs = [ inputs = [
"Cargo.toml", "Cargo.toml",
] ]
env = [ "CARGO_PKG_VERSION=${deno_cargo_info.version}" ]
} }
rust_test("cli_test") { rust_test("cli_test") {
@ -194,10 +194,10 @@ rust_test("cli_test") {
] ]
# Extract version from Cargo.toml # Extract version from Cargo.toml
crate_version = deno_cargo_info.version
inputs = [ inputs = [
"Cargo.toml", "Cargo.toml",
] ]
env = [ "CARGO_PKG_VERSION=${deno_cargo_info.version}" ]
} }
# Generates the core TypeScript type library for deno that will be # Generates the core TypeScript type library for deno that will be