Build scripts place their output in a folder created by Cargo. The path to it is passed in the environment variable OUT_DIR
both to the build script and the main code. Since the file is placed in the directory, we need to add the path separator and then the name of the file. There seems to be no way to call include_bytes!
with a platform-agnostic path separator, inserting /
or \
depending on the host OS.
This works but is not portable: include_bytes!(concat!(env!("OUT_DIR"), "/myfile"));
This doesn't work: include_bytes!(concat!(env!("OUT_DIR"), std::path::MAIN_SEPARATOR, "myfile"));
because MAIN_SEPARATOR
is not a literal, and concat!
only eats literals.
I've tried to to assemble a String
in const fn
, but that doesn't work either because String::push()
requires a mutable borrow which are unsable in const fn
.
#[cfg(unix)]
and #[cfg(windows)]
sort of work, but break on cross-compilation because these cfg
attributes look at the _target_ OS, and we need to add a separator that works on the _host_ OS.
It's weird that this is either impossible to accomplish or the way to do so is very obscure. I'd expect this to be a relatively common operation.
rustc --version --verbose
:
rustc 1.46.0-beta.2 (6f959902b 2020-07-23)
binary: rustc
commit-hash: 6f959902b3103c49ca981fbc01871589c3498489
commit-date: 2020-07-23
host: x86_64-unknown-linux-gnu
release: 1.46.0-beta.2
LLVM version: 10.0
Can you use a build script to use cargo:rustc-env
to set your own environment variable that contains the full path using the host specific path separators and whatnot?
I'll try it. Thanks for pointing this out!
You can use cfg-ed macros to pass conditional string literals to concat!
(I can't say if this code specifically is good enough for cross-platform support)
#[cfg(not(windows))]
macro_rules! main_separator{
()=>{"/"}
}
#[cfg(windows)]
macro_rules! main_separator{
()=>{r#"\"#}
}
fn main(){
println!("{}", include_str!(concat!(".", main_separator!(), "main.rs" )));
}
/
should work on Windows, if it doesn't then that is the issue.
The build.rs
trick worked, thanks!
You can use cfg-ed macros to pass conditional string literals to concat!
I did that, but that only look at the _target_ platform and should break when the host platform is different (i.e. when cross-compiling).
/
should work on Windows, if it doesn't then that is the issue.
include_bytes!
is explicitly documented to use platform-dependent separators. The documentation doesn't say anything about /
working on Windows. If include_bytes!
allows /
as the cross-platform separator, it would be great to document that.
/
should work on Windows, if it doesn't then that is the issue.
On Windows if OUT_DIR
for whatever reason uses \\?\
paths then using /
as the separator will break. While it is nice to support /
in paths as much as we can, we should be pushing to ensure code always uses the correct separator for the platform.
Oh that's a bummer, I thought the same parsing here would apply as for std::path::Path
which allows to use both separators on Windows.
Well it is the same parsing. Concatenating foo/bar
onto a Path
that starts with \\?\
on Windows also runs into the exact same issue.
Would it maybe make sense for the include_*! macros to allow syntax like include_bytes!(env!("OUT_DIR") / "myfile")
?
I did that, but that only look at the target platform and should break when the host platform is different (i.e. when cross-compiling).
//! `build.rs`
#[cfg(windows)]
const HOST_FAMILY: &str = "windows";
#[cfg(unix)]
const HOST_FAMILY: &str = "unix";
fn main ()
{
#[cfg(any(windows, unix))] {
println!("cargo:rust-cfg=host_family={}", HOST_FAMILY);
}
}
cfg
)and then:
#[cfg(host_family = "windows")]
macro_rules! PATH_SEPARATOR {() => (
r"\"
)}
#[cfg(not(host_family = "windows"))]
macro_rules! PATH_SEPARATOR {() => (
r"/"
)}
include_bytes!(concat!(
env!("OUT_DIR"), PATH_SEPARATOR!(), "my_file"
));
Most helpful comment
Would it maybe make sense for the include_*! macros to allow syntax like
include_bytes!(env!("OUT_DIR") / "myfile")
?