0

Cmake has the configure_file function to perform an operation similar to autotools' configure script processing foo.h.in into a foo.h, that contains macros describing relevant parameters of the toolchain and run-time. configure_file though is a very general purpose mechanism that replaces place-holders that reference a Cmake variable with a macro that defines the information for the program.

I'm trying to use Cmake to do a simple build configuration of an existing open source library (it's libwww from the W3). By default the library uses autotools, but even so, the authors see fit to do some "special" customizations of the basic autotools methodology. I'm working with a large-ish, multi-part project that cross-compiles onto some platforms, mundane and exotic. It uses Cmake to organize the build, and I wish to produce the cmake code to express (to some level of approximation) the intent of the autotools inputs. That means the code is expecting something called config.h based on config.h.in. My plan is to translate the autotools template language into that used by Cmake.

What I'm struggling with is that the space of inputs that autotools uses to populate their config.h-type files corresponds the all the results of the tests performed by the configure script to determine these values, and it's fairly well-defined in the autotools documentation (sort of). configure_file on the other hand is a very general, cmake-style mechanism that will instantiate the value of any Cmake variable into a properly-formatted template version. What I'm struggling to find is set of Cmake variables created by the equivalent configuration tests done when Cmake creates a makefile or IDE project. I'm looking for equivalencies values that the specific autotools wwwconf.h.in cares about and the name of the Cmake variable expresses that result.

So to make it concrete, the config.h.h - the autotools input template - has a large set of entries that look like this -

/* Define if you have the strerror function.  */
#undef HAVE_STRERROR

So HAVE_STRERROR by default is undefined and if configure knows the answer, it replaces the corresponding line in config.h with #define HAVE_STRERROR 1 or something equally affirmative. config.h.in is generated by autoconf based on the user-specified requirements express in configure.ac (as of course is the configure script itself).

So the binding between the macro name HAVE_STRERRO and the result of the test performed by configure to determine the existence of strerror in the standard libraries could be standardized or it could be user-specified in configure.ac, but for Cmake's configure_file, it is all very open-ended. It will replace #cmakedefine FOO in your input template with either #undef FOO or #define FOO <foo-value>. Its mission is simple. It doesn't care what the semantics of FOO is. It just wants to create an include file with C macros expressing that name and value. My mission is a bit more specific. I want to emulate autoconf's mechanism for creating a config.h file with the answer to the questions asked by configure at build time. That means I have to know the names of the Cmake variables that record the results of its configure-time tests, do some kind of mapping if they are not the same as the symbols used by autoconf and use the configure_file function to produce a config.h that is the same (hopefully) as would be produced by autoconf.

So my problem is that the documentation for configure_file only covers the substitution language for this very basic function. I need to find where the results of Cmake's configure-time tests are, make a mapping to the expected names in the library's config.h and use configure_file to instantiate them within my cmake-style input template.

I've been using Cmake for a long time, but these are relatively new features, and I can't seem to find the Cmake variables for the results of the configure tests or any similar mechanism to find out those data. I figured they may be stored in the .cache file, and there are some name/values like that, but the list seems too short. I have looked for, but not found, documentation on such variables or name-value query mechanism, and I'm hoping the community has some pointers on how one is supposed

cycollins
  • 415
  • 4
  • 11
  • A statement like `#cmakedefine HAVE_STRERROR` will be replaced by either `#define HAVE_STRERROR` if the CMake variable `HAVE_STRERROR` is set, or `#undef HAVE_STRERROR` if the CMake variable is not set. [The documentation](https://cmake.org/cmake/help/latest/command/configure_file.html) is rather clear I think. And you can always do some experiments to see what happens. – Some programmer dude Jun 30 '23 at 15:26
  • You could check whether a specific C function exists in the target environment by using [CheckFunctionExists](https://cmake.org/cmake/help/latest/module/CheckFunctionExists.html) macro. That macro accepts a name of the variable, where result of the check will be stored. As for the "environment tests" performed by CMake automatically, their results are stored in [CMake variables](https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html). Some of these variables are cached one, some other are normal variables. – Tsyvarev Jun 30 '23 at 16:22
  • @Someprogrammerdude - that part of the documentation is very clear. The part I’m looking for is the actual names of the variables that correspond to the results of the all those checks Cmake does automatically. As another comment points out, the information is in among the Cmake variables, as I assumed, but that space is hard to search by content, such that when the original config.h.in asks for has_, it’s easy to look up the name of the corresponding Cmake variable. CheckFunctionExists is a good pointer, though. The source for that might have some ideas. – cycollins Jul 03 '23 at 07:00
  • In almost all cases you're supposed to explicitly define your own variables for tests. Take for example the mentioned CheckFunctionExists command, the second argument is the variable to set. This is the same variable that can be used in the `.in` file used by the `configure_file` command. – Some programmer dude Jul 03 '23 at 07:05

0 Answers0