These are chat archives for rust-lang/rust

4th
Feb 2017
Robyn Speer
@rspeer
Feb 04 2017 21:22
I have some questions about code generation. In particular, I'm using phf_codegen (https://docs.rs/phf_codegen/0.7.21/phf_codegen/) to generate a hash table at compile time, and maybe I should be asking its author for clearer documentation, but maybe it's just that I don't understand the general Rust context.
Denis Lisov
@tanriol
Feb 04 2017 21:23
So, what's the question?
Robyn Speer
@rspeer
Feb 04 2017 21:23
When it generates code, it generates literals that it constructs with things like ::phf::Slice::Static.
I tried putting extern crate phf in the generated code, and that doesn't let it know what ::phf is.
What's with the leading double colon?
Denis Lisov
@tanriol
Feb 04 2017 21:25
"from the crate root", i.e. it expects you to have extern crate phf; in your lib.rs or main.rs
Robyn Speer
@rspeer
Feb 04 2017 21:25
Aha, so not in the generated file.
Denis Lisov
@tanriol
Feb 04 2017 21:26
Not in the generated file and not in the module if you're doing it in some module.
Robyn Speer
@rspeer
Feb 04 2017 21:26
okay.
The other question is, it suggests generating the code in a path generated with something like Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs").
This ends up as something like target/debug/build/language-codes-f4edc1d947f3002f/out/codegen.rs.
which I had to kind of guess around to find. And I can't use code from there, only include! it using the same path.
What is this directory for? Why wouldn't I put it somewhere more findable?
Denis Lisov
@tanriol
Feb 04 2017 21:31
You usually don't need to find a generated file manually :-)
Robyn Speer
@rspeer
Feb 04 2017 21:33
...I kind of do when the code-generator is under-documented and I'm using it by trial and error.
My question is, what's the meaning of that directory to my Rust environment? Why am I putting it in target/debug/build/language-codes-f4edc1d947f3002f/out/ instead of, say, codegen/?
Denis Lisov
@tanriol
Feb 04 2017 21:36
OUT_DIR - the folder in which all output should be placed. This folder is inside the build directory for the package being built, and it is unique for the package in question.
So a special place for build script output.
Robyn Speer
@rspeer
Feb 04 2017 21:37
Ah, so it would probably matter if it's being built as part of another crate, right?
Denis Lisov
@tanriol
Feb 04 2017 21:38
It may matter if it's being build as a dependency, yes.
Robyn Speer
@rspeer
Feb 04 2017 21:38
Alright, thanks.
Denis Lisov
@tanriol
Feb 04 2017 21:39
It may also matter if the generated files differ between debug and release and/or between architectures if you ever cross-compile.
I don't know the extents of Cargo's build script caching, but would not be surprised if they are not rerun every time.