Skip to content

Commit

Permalink
Make IntelliJ workaround compatible with 'run watch --crate' (#3355)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazcw committed Mar 23, 2022
1 parent 85c09e7 commit edadca1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
23 changes: 13 additions & 10 deletions build/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]`.
Expand All @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions lib/rust/profiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@ enso-web = { path = "../web" }

[dev-dependencies]
futures = "0.3"

[features]
line-numbers = ["enso-profiler-macros/lineno"]
3 changes: 0 additions & 3 deletions lib/rust/profiler/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@ proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0", features = [ "full", "visit-mut" ] }
Inflector = "0.11"

[features]
lineno = []
28 changes: 21 additions & 7 deletions lib/rust/profiler/macros/build.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -16,8 +15,23 @@


fn main() {
println!("cargo:rerun-if-env-changed=ENSO_MAX_PROFILING_LEVEL");
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("ENSO_MAX_PROFILING_LEVEL").unwrap_or_default();
println!("cargo:rustc-env=ENSO_MAX_PROFILING_LEVEL={}", value);
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);
}
}
6 changes: 3 additions & 3 deletions lib/rust/profiler/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<L: fmt::Display>(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<L: fmt::Display>(name: L) -> String {
let span = proc_macro::Span::call_site();
Expand Down

0 comments on commit edadca1

Please sign in to comment.