From 89794d5d347d1358ec32ed42e1f0faa6e0ac6abe Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Wed, 28 Aug 2019 22:19:07 -0700 Subject: [PATCH] build: make it possible to pass arbitrary env vars to rustc --- build_extra/rust/run.py | 39 ++++++++++++++++++--------------------- build_extra/rust/rust.gni | 35 +++++++++++++++++------------------ cli/BUILD.gn | 4 ++-- 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/build_extra/rust/run.py b/build_extra/rust/run.py index 36d1ab83..7c6bea08 100644 --- a/build_extra/rust/run.py +++ b/build_extra/rust/run.py @@ -1,13 +1,15 @@ #!/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)) diff --git a/build_extra/rust/rust.gni b/build_extra/rust/rust.gni index 2af3b2b1..01f1b8a4 100644 --- a/build_extra/rust/rust.gni +++ b/build_extra/rust/rust.gni @@ -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" ] + } + } } } diff --git a/cli/BUILD.gn b/cli/BUILD.gn index 7fd8ba7d..23bc48fd 100644 --- a/cli/BUILD.gn +++ b/cli/BUILD.gn @@ -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 -- GitLab