如何在build.rs脚本中使用本地项目文件?

发布于 2025-01-20 18:53:43 字数 1032 浏览 1 评论 0原文

如何在build.rs中使用本地文件而不包含其绝对路径?我有,例如<​​code>让proto_file_glob_paths:paths = glob(“ ../ proto/**/*。proto”)。unwrap(); 。

我正在使用build.rs中的相对路径,因为我不想编码文件的绝对路径,因此它可以在其他开发人员机器上使用。该构建可以与货物构建货物运行一起工作,但是不是 货物出版。这是因为当运行货物>运行时,工作目录/路径是不同的。

中,货物构建env :: current_dir() is:

project_name/target/debug/build/project_name-70e21ac88134b5a1/build-script-build

in cargo Publishenvy> encry_der_dir()是:

project_name/target/package/project_name-x.y.z/target/debug/build/project_name-xxxxxxx/build-script-build

我不知道我可以使用任何环境变量访问build.rssrc/的目录,以便我可以创建一个相对/绝对路径动态路径。即使cargo_manifest_dir对于构建发布也有所不同。


项目具体细节:我还有其他代码线可以使用其他软件包生成锈蚀代码,并且在货物构建货物运行 >。

How do I use local files without including their absolute paths in build.rs? I have, for example let proto_file_glob_paths: Paths = glob("../proto/**/*.proto").unwrap();.

I am using relative paths in my build.rs, because I don't want to hardcode the absolute path of files, so that it works on other developer machines. The build works fine with cargo build and cargo run, but not cargo publish. This is because the working directory/path is different when cargo publish is run.

In cargo build, the env::current_dir() is:

project_name/target/debug/build/project_name-70e21ac88134b5a1/build-script-build

In cargo publish, the env::current_dir() is:

project_name/target/package/project_name-x.y.z/target/debug/build/project_name-xxxxxxx/build-script-build

I don't know of any environment variables I can use to access the directory of build.rs, or src/, so that I can create a relative/absolute path dynamically. Even CARGO_MANIFEST_DIR is different for build and publish.


Project specifics: I've got other lines of code to generate the rust code using other packages, and this works fine in cargo build and cargo run.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枉心 2025-01-27 18:53:43

在@cerberus和@colonelthirtytwo的一些非常有用的提示之后,我只是:

  • 将本地文件复制到板条箱根中。在运行货物构建之前,我写了一个脚本来执行此操作。当然,我需要更新使用proto/而不是../ proto/的相对路径。
  • proto目录添加到gitignore文件中
  • ,并将proto目录添加到package.include in
[package]
...
include = ["**/*.rs", "Cargo.toml", "Cargo.lock", "config.json", "proto"]

  • 由于板条箱的用户将缺少这些文件。
  • 我没有将proto文件添加到includ> include in 货物中。 toml

After some very helpful prompting from @Cerberus and then from @ColonelThirtyTwo, I just:

  • copied the local files into the crate root. I wrote a script to do this before running cargo build. Of course, I needed to update the relative path to use proto/ instead of ../proto/.
  • added the proto directory to the gitignore file
  • and added the proto directory to package.include in Cargo.toml because it would not be included as it is gitignored:
[package]
...
include = ["**/*.rs", "Cargo.toml", "Cargo.lock", "config.json", "proto"]

My mistakes were that:

  • I assumed ../proto should work, but as Cerberus explained, it won't. Since the files would be missing for the users of the crate.
  • I didn't add the proto file to include in Cargo.toml
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文