Automatically generating intermediate files by intervening compilation process.

Hello folks,

This is with respect to one of my previous questions (custom command not getting executed while building.) where I have manually changed files to generate intermediate llvm code for analysis and it seems to work. But the issue is the project is too large for making manual changes all the time and the structure does evolve as well based on the requirements. Thus, manual change to every CMakeList file is out of option.

So I generated the compile_command JSON file and observed that there are many header files a single C file is depending on (obviously because of the large project). Below is one such example

/usr/bin/clang -Dconfig=1 -I/path/to/header1 -I/path/to/header2 -O3 -DNDEBUG -fPIC -Wall -Wextra -Wconversion -Wunused -Wmissing-prototypes -Winfinite-recursion -Wassign-enum -Wcomma -Wdocumentation -Wstrict-prototypes -Wconditional-uninitialized -Wshadow -std=gnu99 -o CMakeFiles/folder.dir/src/eval.c.o -c /path_dir/folder.dir/src/eval.c

My initial idea was since it’s a json file. Use Python to loop through the JSON data and invoke the command present in the JSON. But as you can see there are CMakeFiles and folder.dir which if I guess are variables generated by the CMake build process (I am not completely sure). I also see that in the JSON there is a directory specified, but I am not sure how much to trust the directory and to which level of path should I consider.

So, I came up with the idea of creating my own binary which will act as an intermediary compiler. I will be exporting it to env into CC and CXX which will then make its way to CMake so that this binary will be invoked and since this is during run-time, I will be having the JSON data inside the argv of my binary. So now I can add my respect flags and other information and run that command inside my binary so that the entire compile process is not disturbed.

The issue with the above approach is the main CMakeLists file checks for the CMAKE_C_COMPILER_ID and a few other things to add some warning and error flags accordingly based on the compiler that is passed. Since my binary is not a compiler on its own it’s failing those checks. Any idea how to circumvent it? I tried changing the compiler_id to clang by comparing it with an empty string but it’s now failing at openssl path for this binary.

am I thinking in the wrong direction altogether to achieve this?

There is CMAKE_<LANG>_COMPILER_LAUNCHER which will add your tool in front of the compiler. You should still run the compiler too, but you can do whatever you want with the command line at that point too.