tcod-rs icon indicating copy to clipboard operation
tcod-rs copied to clipboard

Can't override build script to use existing libtcod

Open ThibaultLemaire opened this issue 5 years ago • 8 comments

I've tried to use the libtcod provided by my distro,

[target.x86_64-unknown-linux-gnu.tcod]
rustc-flags = "-L $LIBTCOD -l tcod"

but unfortunately cargo gives me the following warning

warning: unused manifest key: target.x86_64-unknown-linux-gnu.tcod

and compilation fails when pkg-config is not found.

ThibaultLemaire avatar Oct 10 '19 10:10 ThibaultLemaire

Judging by the warning, it looks like you put it in Cargo.toml. The overrides should go to .cargo/config instead.

I'm not able to actually test any of this now, but FWIW when I was playing with local overrides in a different project, I used these options instead of rustc-flags:

[target.x86_64-unknown-linux-gnu.SDL2]
rustc-link-search = ["lib/x86_64-linux-gnu"]
rustc-link-lib = ["SDL2"]

(and I had a lib/x86_64-linux-gnu/ directory in the root of my repo with all the so files)

So try something like this:

[target.x86_64-unknown-linux-gnu.tcod]
rustc-link-search = ["/usr/lib/tcod"]
rustc-link-lib = ["tcod"]

Putting in the absolute path to the directory where the libtcod.so* files are. I'm not sure what the path resolution rules are so I'd start with absolute ones and once that works switch over to whatever you actually want.

tomassedovic avatar Oct 10 '19 11:10 tomassedovic

Thank you, I'm still learning about cargo and didn't know about its configuration (for the record, the Rust Book chapter on configuration).

I can also confirm successful compilation with rustc-link-search and rustc-link-lib instead of rustc-flags. I could update the .cargo/config if you'd like.

Also I'm actually trying to compile on NixOS, which means the path to the library changes with every update so I'd like to rely on an environment variable but I don't know how to do that with cargo.

ThibaultLemaire avatar Oct 10 '19 14:10 ThibaultLemaire

I see rust-sdl2 uses pkg-config to figure out where SDL2 is, maybe we could do the same for libtcod?

ThibaultLemaire avatar Oct 10 '19 15:10 ThibaultLemaire

That's great to hear!

Have you tried with rust-flags in the right .cargo/config? I'm definitely open to updating it (honestly, I forgot we shipped a sample) but your original issue was probably due to using the wrong file. I just suggested the other options as something that did work for me in the past.

tcod_sys already uses pkg-config so adding an option to do it for libtcod sounds like a great idea to me!

https://github.com/tomassedovic/tcod-rs/blob/master/tcod_sys/build.rs

I'll be happy to check the PR if you put it up (though it may take some time, sorry about that). I won't have the time to write it myself unfortunately :-(.

tomassedovic avatar Oct 10 '19 16:10 tomassedovic

(we should still support building libtcod as a fallback, but yeah if pkg-config finds it, let's just use that instead)

tomassedovic avatar Oct 10 '19 16:10 tomassedovic

Have you tried with rust-flags in the right .cargo/config?

Not exactly, but I bet it would work as well.

I just suggested the other options as something that did work for me in the past

I know, but I think using link-search and link-lib is cleaner (since it's also what's recommended in the rust doc).

tcod_sys already uses pkg-config so adding an option to do it for libtcod sounds like a great idea to me!

Great, I'll open a new issue and start working on a PR.

we should still support building libtcod as a fallback

Ofc, that was a given.

So, if I manage to make the build detect libtcod through pgk-config, should we remove the manual override section of the README and the config example or would they still have some use ? (which then means I should update them)

ThibaultLemaire avatar Oct 10 '19 16:10 ThibaultLemaire

(Btw, just confirmed working with rustc-flags)

ThibaultLemaire avatar Oct 10 '19 17:10 ThibaultLemaire

Actually, the libtcod package on NixOS is missing the pgkconfig declaration so pkg-config won't help me for what I'm trying to accomplish there...

I was thinking on maybe reading an env var in the build script to allow easy override? (dynamically writing the path to .cargo/config is not something I want to do)

ThibaultLemaire avatar Oct 12 '19 21:10 ThibaultLemaire