How to extract constant strings from object files in a cmake script?
Detecting features often requires a chain of check_cxx_source_compiles
, which is slow on some platforms (MSVC/Windows).
It would be interesting to combine multiple checks and encode these in constant strings that are embedded in the compiled object file or executable.
As an example, this CMake script detects the CPU architecture using a chain of check_c_source_compiles
s.
The number of checks grows linearly by the number of supported architectures, or checks that need to be done.
I think it would be useful for CMake to do these checks "in parallel" by constructing const string literals that are greppable.
As an example, this c source detects the compiler target architecture, and also shows example usage for other tests.
Compiling this source, and running strings a.out | grep INFO
gives an output that can be easily post processed for CMake.
INFO[ARCH=X64]
INFO[STDATOMIC=YES]
INFO[SYS_STAT_H=YES]
Compiling this single source is considerably faster than test compiling 3 (or more) sources in series.
C++ experts can probably improve on this by using SFINAE, templates and other magic.
I hope these examples are sufficient motivation to add support for extracting strings from compiled objects and/or executables in cmake scripts.