1

I'm working on a code base that uses quite a bit of conditional compilation via macros passed as arguments to the compiler (i.e. gcc -DMACRO_HERE file.cpp). I would like to have a way to get a list of all the macros defined this way within the code so that I can write out all the used macros to the console and save files when the application is run, that way we know exactly what build was used.

I also need to do the same thing with the git hash but I think I can do that easily with a macro.

Edit 1: Note that this is not the same question as GCC dump preprocessor defines since I want the list available within the program and I only want the macros that are declared by being passed to the compiler with the -D argument

Edit 2: I also need it be cross compiler compatible since we use GCC, XL, CCE, CLANG, NVCC, HIP, and the MPI versions of those. Note that we're building with Make

bcaddy
  • 73
  • 5
  • 1
    Using standard C++ it's not possible to get a list of all macros, much less a list of macros passed to the compiler on the command-line. GCC might have some extension function to retrieve all macros (have you read its documentation?) but I doubt it would be possible to differentiate when and where a macro was defined. – Some programmer dude Mar 17 '22 at 18:52
  • GCC might but that doesn't help since I need something that works with GCC, XL, CLANG, NVCC, and ROCm – bcaddy Mar 17 '22 at 19:07
  • Can you check if the linked answer works on the other compilers? – HolyBlackCat Mar 17 '22 at 19:19
  • The linked answer doesn’t answer my question. It dumps all the preprocessor defines and I only want the ones that are defined during the actual compiler call with the -D argument – bcaddy Mar 17 '22 at 20:16

1 Answers1

5

Here's an outline of a possible solution.

The request is not well-specified because there is no guarantee that all object files will be built with the same conditional macros. So let's say that you want to capture the conditional macros specified for some designated source file.

On that basis, we can play a build trick, easy to do with make: the build recipe for that designated source file actually invokes a script, by inserting the path to the script at the beginning of the compile line.

The script runs through its arguments, selects the ones which start -D, and uses them to create a simple C source file which defines an array const char* build_options[], populating it with stringified versions of the command line arguments. (Unless you're a perfectionist, you don't need to do heroics to correctly escape the strings, because no sane build configuration would use -D arguments which require heroic escaping.)

Once the source file is built, the script saves it and either uses the command-line it was passed as its arguments to compile it, or leaves it to be compiled by some later build step.

rici
  • 234,347
  • 28
  • 237
  • 341