Skip to content

env!("OUT_DIR") macro#1967

Closed
eupn wants to merge 7 commits into
rust-lang:masterfrom
eupn:env_macro
Closed

env!("OUT_DIR") macro#1967
eupn wants to merge 7 commits into
rust-lang:masterfrom
eupn:env_macro

Conversation

@eupn

@eupn eupn commented Oct 8, 2019

Copy link
Copy Markdown
Contributor

Hello,

This PR is related to the #1964.

@matklad pls give a hint on how do we search for the OUT_DIR in the workspace given that we have a target_path from cargo metadata and can use that.

@matklad matklad left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls give a hint on how do we search for the OUT_DIR in the workspace given that we have

I don't have any better suggestions here than "look at what Cargo does, and do the same" :)

Comment thread crates/ra_db/src/input.rs Outdated
@eupn eupn changed the title env!() macro env!("OUT_DIR") macro Oct 8, 2019
Comment thread crates/ra_db/src/input.rs Outdated
@CAD97

CAD97 commented Oct 28, 2019

Copy link
Copy Markdown
Contributor

"look at what Cargo does, and do the same"

Highly relevant:

https://github.com/rust-lang/cargo/blob/b03182a8ff0117ffa1c426119ae2b5a418776bbd/src/cargo/core/compiler/context/compilation_files.rs#L216-L220
https://github.com/rust-lang/cargo/blob/b03182a8ff0117ffa1c426119ae2b5a418776bbd/src/cargo/core/compiler/context/compilation_files.rs#L197-L204
https://github.com/rust-lang/cargo/blob/b03182a8ff0117ffa1c426119ae2b5a418776bbd/src/cargo/core/compiler/context/compilation_files.rs#L164-L170

    /// Returns the "OUT_DIR" directory for running a build script.
    /// `/path/to/target/{debug,release}/build/PKG-HASH/out`
    pub fn build_script_out_dir(&self, unit: &Unit<'a>) -> PathBuf {
        self.build_script_run_dir(unit).join("out")
    }
    /// Returns the directory where a compiled build script is stored.
    /// `/path/to/target/{debug,release}/build/PKG-HASH`
    pub fn build_script_dir(&self, unit: &Unit<'a>) -> PathBuf {
        assert!(unit.target.is_custom_build());
        assert!(!unit.mode.is_run_custom_build());
        let dir = self.pkg_dir(unit);
        self.layout(CompileKind::Host).build().join(dir)
    }
    pub fn pkg_dir(&self, unit: &Unit<'a>) -> String {
        let name = unit.pkg.package_id().name();
        match self.metas[unit] {
            Some(ref meta) => format!("{}-{}", name, meta),
            None => format!("{}-{}", name, self.target_short_hash(unit)),
        }
    }

cargo metadata's target_directory is just the path to the %workspace/target directory, so it seems the only way to get OUT_DIR would be to guess the package meta disambiguator 😞

If nightly cargo is used, cargo +nightly build -Z unstable-options --build-plan outputs a huge JSON blob that includes

{ "invocations": [
  { "package_name": "this-package",
    "target_kind": [ "custom-build" ],
    "compile_mode": "run-custom-build",
    "env": { "OUT_DIR": "/absolute/path/to/target/debug/build/this-package-hash/out" }
  }
]}

This is present even if the build is fresh and would not be rerun, but does seem to make the build stale and queue a new full rebuild 😞

I think the "best" option would to be adding OUT_DIR to the cargo metadata information, if the package meta is set at that point. cc rust-lang/cargo#7546

@CAD97

CAD97 commented Oct 28, 2019

Copy link
Copy Markdown
Contributor

Also related: intellij-rust's implementation of env("OUT_DIR"): intellij-rust/intellij-rust#4542

They use cargo build --build-plan.

@edwin0cheng

Copy link
Copy Markdown
Contributor

@eupn are you still interested in this patch? I'd be happy to continue to implement it.

@eupn

eupn commented Mar 6, 2020

Copy link
Copy Markdown
Contributor Author

@edwin0cheng go ahead!

@CAD97

CAD97 commented Mar 6, 2020

Copy link
Copy Markdown
Contributor

Current status:

OUT_DIR for a given build script is reported in the build script's build-script-executed message. rust-lang/cargo#7622

Here's IntelliJ Rust's (currently off-by-default) support for OUT_DIR: intellij-rust/intellij-rust#4734 (unless they've done more recently, ofc).

@edwin0cheng

edwin0cheng commented Mar 6, 2020

Copy link
Copy Markdown
Contributor

@CAD97

Yeah, and the current status of RA part :

We have implemented basic include and concat eager builtin macro. So this is the final puzzle for supporting include!(concat!(env!(“OUT_DIR”), “xxx.rs”))!!

@edwin0cheng

Copy link
Copy Markdown
Contributor

Another update :

#3549 is landed ! Although it didn't implement fetching OUT_DIR from cargo check yet, but I think it works functionally.

I will close this PR now and feel free to reopen it if needed.
Thanks @eupn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants