From edadca1c883e1ad3c93adc3224871afb0f752465 Mon Sep 17 00:00:00 2001 From: Kaz Wesley Date: Wed, 23 Mar 2022 11:42:45 -0700 Subject: [PATCH] Make IntelliJ workaround compatible with 'run watch --crate' (#3355) --- build/run.js | 23 ++++++++++++---------- lib/rust/profiler/Cargo.toml | 3 --- lib/rust/profiler/macros/Cargo.toml | 3 --- lib/rust/profiler/macros/build.rs | 30 +++++++++++++++++++++-------- lib/rust/profiler/macros/src/lib.rs | 6 +++--- 5 files changed, 38 insertions(+), 27 deletions(-) diff --git a/build/run.js b/build/run.js index ff4300ef37e..17489091157 100755 --- a/build/run.js +++ b/build/run.js @@ -164,11 +164,8 @@ commands.build.rust = async function (argv) { args.push('--dev') } args.push('--') - // Enable source-file and line number information in the data generated by the `#[profile]` - // macro. - // - // The `profiler` library requires use of a Rust unstable feature, `proc_macro_span`, to be - // able to obtain this information. + // Enable a Rust unstable feature that the `#[profile]` macro uses to obtain source-file and line + // number information to include in generated profile files. // // The IntelliJ Rust plugin does not support the `proc_macro_span` Rust feature; using it causes // JetBrains IDEs to become entirely unaware of the items produced by `#[profile]`. @@ -177,11 +174,17 @@ commands.build.rust = async function (argv) { // In order to have line number information in actual usage, but keep everything understandable // by JetBrains IDEs, we need IntelliJ/CLion to build crates differently from how they are // built for the application to be run. This is accomplished by gating the use of the unstable - // functionality by a Cargo feature. Cargo features are disabled by default, so when a Rust IDE - // builds crates internally in order to determine macro expansions, it will do so without line - // numbers. When this script is used to build the application, it is not for the purpose of IDE - // macro expansion, so we can safely enable line numbers. - args.push('--features=enso-profiler/line-numbers') + // functionality by a `cfg` flag. A `cfg` flag is disabled by default, so when a Rust IDE builds + // crates internally in order to determine macro expansions, it will do so without line numbers. + // When this script is used to build the application, it is not for the purpose of IDE macro + // expansion, so we can safely enable line numbers. + // + // The reason we don't use a Cargo feature for this is because this script can build different + // crates, and we'd like to enable this feature when building any crate that depends on the + // `profiler` crates. We cannot do something like '--feature=enso_profiler/line-numbers' without + // causing build to fail when building a crate that doesn't have `enso_profiler` in its + // dependency tree. + process.env.ENSO_ENABLE_PROC_MACRO_SPAN = 1 await run_cargo('wasm-pack', args) await patch_file(paths.wasm.glue, js_workaround_patcher) await fs.rename(paths.wasm.mainRaw, paths.wasm.main) diff --git a/lib/rust/profiler/Cargo.toml b/lib/rust/profiler/Cargo.toml index 7768dc6083d..5e3de1682b3 100644 --- a/lib/rust/profiler/Cargo.toml +++ b/lib/rust/profiler/Cargo.toml @@ -13,6 +13,3 @@ enso-web = { path = "../web" } [dev-dependencies] futures = "0.3" - -[features] -line-numbers = ["enso-profiler-macros/lineno"] diff --git a/lib/rust/profiler/macros/Cargo.toml b/lib/rust/profiler/macros/Cargo.toml index 5c9e93cb22d..7e3a7635689 100644 --- a/lib/rust/profiler/macros/Cargo.toml +++ b/lib/rust/profiler/macros/Cargo.toml @@ -12,6 +12,3 @@ proc-macro2 = "1.0" quote = "1.0" syn = { version = "1.0", features = [ "full", "visit-mut" ] } Inflector = "0.11" - -[features] -lineno = [] diff --git a/lib/rust/profiler/macros/build.rs b/lib/rust/profiler/macros/build.rs index a68c81a58df..c06c6110d1d 100644 --- a/lib/rust/profiler/macros/build.rs +++ b/lib/rust/profiler/macros/build.rs @@ -1,7 +1,6 @@ -//! Build script for [`enso_profiler_macros`]. This is needed because `profiler_macros` has a -//! profiling level controlled by the value of an environment variable at compile time, and cargo -//! needs to be made aware that changes to the env can invalidate the result of compiling this -//! crate and any dependents. +//! Build script for [`enso_profiler_macros`]. This is needed to make cargo aware that +//! the crate depends on the values of environment variables at compile time, and changes to those +//! variables should result in recompiling this crate and its dependents. // === Non-Standard Linter Configuration === #![warn(missing_copy_implementations)] @@ -16,8 +15,23 @@ fn main() { - println!("cargo:rerun-if-env-changed=ENSO_MAX_PROFILING_LEVEL"); - // This is a no-op assignment, except it makes cargo aware that the output depends on the env. - let value = std::env::var("ENSO_MAX_PROFILING_LEVEL").unwrap_or_default(); - println!("cargo:rustc-env=ENSO_MAX_PROFILING_LEVEL={}", value); + declare_env_dependence("ENSO_MAX_PROFILING_LEVEL"); + declare_env_cfg_flag("ENSO_ENABLE_PROC_MACRO_SPAN", "enso_enable=\"proc_macro_span\""); +} + +/// Make cargo aware that the result of compiling this crate depends on an environment variable. +fn declare_env_dependence(env: &str) { + println!("cargo:rerun-if-env-changed={}", env); + // This is a no-op assignment, except it makes cargo aware that the output depends on the env. + let value = std::env::var(env).unwrap_or_default(); + println!("cargo:rustc-env={}={}", env, value); +} + +/// Make cargo aware that the result of compiling this crate depends on an environment variable; +/// convert that variable to a `cfg` flag so that it can be used for conditional compilation. +fn declare_env_cfg_flag(env: &str, cfg: &str) { + println!("cargo:rerun-if-env-changed={}", env); + if std::env::var(env).is_ok() { + println!("cargo:rustc-cfg={}", cfg); + } } diff --git a/lib/rust/profiler/macros/src/lib.rs b/lib/rust/profiler/macros/src/lib.rs index fde0f020456..88b03e63b2f 100644 --- a/lib/rust/profiler/macros/src/lib.rs +++ b/lib/rust/profiler/macros/src/lib.rs @@ -7,7 +7,7 @@ //! implementing this without proc macros would be complex and repetitious. //! - To implement the [`#[profile]`](macro@profile) attribute macro. -#![cfg_attr(feature = "lineno", feature(proc_macro_span))] +#![cfg_attr(enso_enable = "proc_macro_span", feature(proc_macro_span))] // === Standard Linter Configuration === #![deny(non_ascii_idents)] #![warn(unsafe_code)] @@ -290,13 +290,13 @@ pub fn profile( func.into_token_stream().into() } -#[cfg(not(feature = "lineno"))] +#[cfg(not(enso_enable = "proc_macro_span"))] /// Decorate the input with file:line info determined by the proc_macro's call site. fn make_label(name: L) -> String { format!("{} (?:?)", name) } -#[cfg(feature = "lineno")] +#[cfg(enso_enable = "proc_macro_span")] /// Decorate the input with file:line info determined by the proc_macro's call site. fn make_label(name: L) -> String { let span = proc_macro::Span::call_site();