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

How to include generated file written under OUT_DIR #673

Closed
egrimley-arm opened this issue May 31, 2023 · 3 comments
Closed

How to include generated file written under OUT_DIR #673

egrimley-arm opened this issue May 31, 2023 · 3 comments

Comments

@egrimley-arm
Copy link

Recent versions of protobuf-codegen seem to enable putting the generated .rs file under OUT_DIR (using cargo_out_dir), but it's unclear to me how to use the .rs file when it's under OUT_DIR. It does not seem to be possible to include! the file because of:

  1. the inner attributes (#!allow(...), etc.): see include! macro fails if included file has top-level inner attributes rust-lang/rfcs#752

  2. the documentation comment (//! Generated file from ...)

Sorry if this is in the documentation somewhere and I've missed it or if I'm just using the wrong combination of versions.

@egrimley-arm
Copy link
Author

I haven't explicitly said how this could be "fixed". I suppose there are two things that could be done:

  • Have an option for rust-protobuf to generate a .rs file without the inner attributes and without the documentation comment, so that it can be include!-ed.
  • Find and document some work-around so that the .rs file as currently generated can be used after it has been written to OUT_DIR.

@matejcik
Copy link

faced the same problem. The solution seems to be to create your own mod.rs whose contents are:

include!(concat!(env!("OUT_DIR"), "/mod.rs"));

(this requires gen_mod_rs to be enabled, which is the default since version 3)

Rust somehow magically picks the rest of the modules from the outdir.

related original issues:
#117
#523
#525

@egrimley-arm
Copy link
Author

Thank you, @matejcik . That's very helpful. I also found this useful example: https://github.com/jgarzik/rust-protobuf-example

However, there's one thing I don't understand yet: how cargo dependencies are tracked. With that rust-protobuf-example I found that any change to any file (even rust-protobuf-example/newdir/newfile, for example) results in a rebuild. But when I had a crate that was a dependency of a larger project I found I had to add my own println!("cargo:rerun-if-changed=..."):
https://github.com/veracruz-project/veracruz/blob/870db97945f9880cdbc8d3c4002fda64f2bb969d/transport-protocol/build.rs#L13-L15

But that's a different issue. I'll close this one.

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

No branches or pull requests

2 participants