Using tui-rs on multiple OS

To help learn Rust I made logterm using tui-rs. tui-rs has several 'features' including a backend create 'termion' and the backend crate 'crossterm'. To build for Windows you must use 'crossterm' whereas for Linux you can use either. For example:

cargo build logtail --features="crossterm" --no-default-features

I have the above building on Windows but can't figure out how to create a Cargo.toml that will work for both Linux or Windows. The following extract from my Cargo.toml works on Windows:

But to build on Linux I have to comment out the second tui dependency line and uncomment the first. If I don't do that, the termion backend isn't included. And when I try to build on Windows with Cargo.toml using the 'Target Linux on Linux' tui dependency line, then the build fails because it tries building termion.

I'm not sure how to solve this, any ideas? Thanks.

Is just using crossterm in both cases not an option for some reason?

It might be, but I want to learn! Until I've done a lot more work on my app I won't know which backend features I want so I'd like to resolve this rather than have it force my choice of backend.

Sounds like you want Platform specific dependencies, example from the page:

[target.'cfg(windows)'.dependencies]
winhttp = "0.4.0"

[target.'cfg(unix)'.dependencies]
openssl = "1.0.1"

or specific triples:

[target.x86_64-pc-windows-gnu.dependencies]
winhttp = "0.4.0"

[target.i686-unknown-linux-gnu.dependencies]
openssl = "1.0.1"
1 Like

Thanks, this seems like it should do the trick but I can't get it to work.

I'm building on Linux with and without --target=x86_64-pc-windows-gnu to simulate building on Linux and Windows, but I can't get my Cargo.toml to pick up the Windows dependencies and ignore the Linux ones. It always ends up trying to compile 'termion', unless I manually comment it out as I explained in my earlier post.

Here's one way I tried based on your example:

[dependencies]
# snip
termion = { version = "1.5", optional = true }
crossterm = { version = "0.17", optional = true }

[target.'cfg(not(windows))'.dependencies]
tui = { version = "0.10.0", features = ["termion", "crossterm"], default-features = false }

[target.'cfg(windows)'.dependencies]
tui = { version = "0.10.0", features = ["crossterm"], default-features = false }

I also found another way to specify this here, but the following doesn't change the behaviour:

[dependencies]
# snip
crossterm = { version = "0.17", optional = true }

[target.'cfg(not(target_os = "windows"))'.dependencies]
termion = { version = "1.5", optional = true }
tui = { version = "0.10.0", features = ["termion", "crossterm"], default-features = false }

[target.'cfg(target_os = "windows")'.dependencies]
tui = { version = "0.10.0", features = ["crossterm"], default-features = false }

I can't see what I'm doing wrong. Here are the build commands I'm using:

For Linux, which works:

cargo build --bin logtail-crossterm --features="crossterm" --no-default-features

For Windows, which fails because it still tries to build 'termion':

cargo build --bin logtail-crossterm --features="crossterm" --target=x86_64-pc-windows-gnu --no-default-features

Here's a bit more of my Cargo.toml. Any hints appreciated.

[dependencies]
rand = "0.7.3"
structopt = "~0.3.15"
linemux = "0.1.1"
tokio = "0.2.22"
futures = "0.3.5"
crossterm = { version = "0.17", optional = true }

[target.'cfg(not(target_os = "windows"))'.dependencies]
termion = { version = "1.5", optional = true }
tui = { version = "0.10.0", features = ["termion", "crossterm"], default-features = false }

[target.'cfg(target_os = "windows")'.dependencies]
tui = { version = "0.10.0", features = ["crossterm"], default-features = false }

[[bin]]
name = "logtail"
required-features = ["termion"]
path = "src/bin/logtail-termion.rs"

[[bin]]
name = "logtail-crossterm"
required-features = ["crossterm"]

I've tried a few more experiments but am still stuck, so have spun this off into a different topic:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.