Autogenerate config.h for -sys module

Straight to the point. Many build systems provide the ability to analyze the target platform by compiling small code chunks with the asking header or with a call to the asking function (eample: Release 0.47).

Is there in Rust any analogue of such a system and maybe someone is already writing or is going to write this?

If you have a config.h, that likely means the library in question is already using the GNU autotools, meaning that it is probably expected to be built by ./configure; make; make install. In particular, the configure script generates the correct config.h file which you can run bindgen on.

@H2CO3 thanks for clarification.

How about re-implementation part of GNU autotools on Rust to generate config.h like it doing meson in link at my original post?

For example pkg-config crate does not determine ncursesw include directory.

P.S. GNU autotools is already outdate solution and does not want to return back in 2021 year to bunch of Bash scripts for include directory searching as I say earley meson well complete with that issue better.

There are a couple of crates you could try.

If your library uses GNU autotools, you can use the autotools crate in your build script:

https://crates.io/crates/autotools

Likewise if your library uses CMake, there's the cmake crate:

https://crates.io/crates/cmake

1 Like

I usually generate my own minimal config.h, because most of the time most of these config files are full of useless junk that doesn't do anything.

  • Very often the C code doesn't even use these defines. Grep the codebase for them to make sure. They may be leftovers from older versions of the project, or even a copy-paste from build system's template that has no relevance for the project.

  • Even when some of the defines are technically used, they're often workarounds for prehistoric buggy C compilers and dead platforms that Rust never supported. It's fine to assume a compiler as modern as 1989, with support for 8-bit bytes, more than 64KB of RAM, and function prototypes.

  • You can approximate HAS_FOO_H headers without checking for the headers themselves just by target platform. Usually they're either Linux-only or available everywhere except Windows.

2 Likes

@kornel maybe you will make a crate in future with a well API of your solution, I just did not find anything except autocfg, but it was made for Rust, not for C. For example create cautocfg or if you allow I create this one?

If you want to make a crate for this, go ahead. Personally I haven't gone that far, because technically it's just a matter of writing a text file to OUT_DIR, but the details of each #define are very dependent on what each C project is doing, and they're all snowflakes.

1 Like

@kornel ok. please welcome feedback in issue at https://github.com/vit1251/cautocfg

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.