Can I define "unix" feature in Cargo.toml or cargo build options?


#1

I think that “unix” or “windows” feature of cfg is usually defined automatically by rustc.
Can I define them by myself?

Here is a background.

I am making wasm in Rust code. My library depends the crate “aligned_alloc” and it describes some module for both “unix” feature and “windows” feature.

Since wasm is neither “unix” nor “windows”, I got an error “Use of undeclared type or module”.

It may work if “unix”, because aligned_alloc uses libc in this case and there is a library for libc in wasm.

I saw explanations of features section in Cargo.toml, but it seems for creating new features, not for adding existing features.

Thanks.


#2

A feature in Cargo.toml would be used like #[cfg(feature = "unix")], whereas the builtin definition is #[cfg(unix)]. Tip: you can list the builtin ones using rustc --print cfg, even with a --target if needed.

So you could make your own “unix” feature, but I suspect what you really want is a platform specific dependency.


#3

Thank you, @cuviper san.

Since aligned_alloc is not my library, I am looking for how to enable the builtin definition “unix”.


#4

Well, technically setting RUSTFLAGS=--cfg=unix would do this, but I don’t think you should. That will enable code paths that really are only expected to work for true unix targets. It looks like aligned_alloc will try to use an external fn posix_memalign, which I doubt will work in wasm.

You can either approach the aligned_alloc author to ask for true wasm support, or find a different way to do it on wasm and use the platform dependencies to alternate your code.


#5

I have never known the detail of “unix” feature. Thank you!

You are right. I should ask the aligned_alloc author at first.