Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env!("OUT_DIR") macro #1967

Closed
wants to merge 7 commits into from
Closed

env!("OUT_DIR") macro #1967

wants to merge 7 commits into from

Conversation

eupn
Copy link
Contributor

@eupn eupn commented Oct 8, 2019

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.

Copy link
Member

@matklad matklad left a comment

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" :)

crates/ra_db/src/input.rs Outdated Show resolved Hide resolved
@eupn eupn changed the title env!() macro env!("OUT_DIR") macro Oct 8, 2019
crates/ra_db/src/input.rs Outdated Show resolved Hide resolved
@CAD97
Copy link
Contributor

CAD97 commented Oct 28, 2019

"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
Copy link
Contributor

CAD97 commented Oct 28, 2019

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

They use cargo build --build-plan.

@edwin0cheng
Copy link
Member

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

@eupn
Copy link
Contributor Author

eupn commented Mar 6, 2020

@edwin0cheng go ahead!

@CAD97
Copy link
Contributor

CAD97 commented Mar 6, 2020

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
Copy link
Member

edwin0cheng commented Mar 6, 2020

@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
Member

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