插入式编译器替换找不到 std crate

Drop-in compiler replacement can't find std crate

我正在尝试制作 drop-in compiler replacement。这是我的源代码。

#![feature(rustc_private)]
#![feature(link_args)]

extern crate rustc_driver;

fn main() {
    rustc_driver::set_sigpipe_handler();
    rustc_driver::main();
}

这实际上是 rustc 源代码的精确副本。 我使用环境变量构建、安装和导出了这个工具。

cargo install
export RUSTC=tool1    # `tool1` is name of binary

然后我尝试构建另一个项目 example1。 这是 example1.

的源代码
fn main() {}

构建失败并出现错误。

error[E0463]: can't find crate for `std`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: Could not compile `foo2`.

To learn more, run the command again with --verbose.

我确认 example1 使用正常 cargo 构建良好。它仅在 tool1 时被破坏。 (export RUSTC=tool1) 如果我 unset RUSTC,它再次工作。

看来我犯了一些错误,但我想不通是什么。我怎样才能让它发挥作用?


这是我的工具信息。

rustc -V
rustc 1.28.0-nightly (a1d4a9503 2018-05-20)

cargo -V
cargo 1.28.0-nightly (f352115d5 2018-05-15)

Here's full example source code.

这里只是猜测,但我认为您的 tool1 没有安装在与 rustc 相同的文件夹中。请注意,在您的 tool1 旁边的 cargo bin 文件夹中,您可能有一个名为 rustc 的可执行文件,但是这个 rustc 可能是一个 rustup 包装器,它重定向到某个地方的真实编译器您的工具链文件夹(可能是 $HOME/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc)。

您需要将 tool1 安装在工具链文件夹中,或者使用指向工具链库(可能 $HOME/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib)的 -L 参数调用它。

检查 tool1 共享库要求显示系统无法找到 Rust 共享库(我在 Linux 系统上,所以我使用 ldd):

> ldd /home/adona/.cargo/bin/tool1
    linux-vdso.so.1 =>  (0x00007ffed5938000)
    librustc_driver-aabc67f1ff8e0e97.so => not found
    libstd-46fff00efefae8a8.so => not found
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa2d6f54000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fa2d7521000)

如果要通过 cargo 构建,请使用 -L 选项设置 RUSTFLAGS,例如:

export RUSTFLAGS="-L $HOME/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib"

如果您想直接从命令行使用 tool1,您必须使用 ldconfig 命令或 LD_LIBRARY_PATH 环境变量配置链接器库路径:

export LD_LIBRARY_PATH=$HOME/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib:$LD_LIBRARY_PATH