I am struggling with porting a project of mine to CI. I have a third-party dependency written in C++ which is included into my project via the cc crate and bindgen (thanks to the crate authors btw!). Unfortunately, this dependency does only build when I use the x86_64-pc-windows-gnu target on windows.
I set this using config.toml with the following content:
[build]
target = "x86_64-pc-windows-gnu"
So now, when I want to build the project on my Ubuntu CI server I get an error. Of course selecting x86_64-pc-windows-gnu does not make any sense on Ubuntu.
Is there a way to detect the OS in config.toml and only set the target when the project runs on windows? I checked the cargo book, but I am just not getting there..
It can make a lot of sense when you want to cross-compile from a Linux host to a Windows target. The error you mention is probably just telling you that the target x86_64-pc-windows-gnu is not installed. On a "normal" Ubuntu host you could install it with rustup target add x86_64-pc-windows-gnu, I'm not sure how to do that on your CI server.
Not that I know of. The build.target setting chooses the default cargo target, you can always specify an alternative target by passing it as an argument to cargo. E.g. cargo build --release --target x86_64-pc-windows-gnu. Perhaps you could just add the argument in your CI recipe for Windows.
Ah yeah thats for sure, but in my case I really just want to build for the native linux target
Blockquote It can make a lot of sense when you want to cross-compile from a Linux host to a Windows target
Ok, thanks. Yes, I can do that. I was just wondering if I can declare the same thing just in the config.toml directly. In particular it is a library which needs to be built with the gnu flavoured windows target. This library is then used as a dependency of a binary crate. But I guess if I pass x86_64-pc-windows-gnu to cargo build of the binary crate, this setting also transfers down to the library crate. So maybe I remove this setting from config.toml at all.
Perhaps you could just add the argument in your CI recipe for Windows