# Automatically generating intermediate files by intervening compilation process.

**URL:** <https://discourse.cmake.org/t/automatically-generating-intermediate-files-by-intervening-compilation-process/8458>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [July 4, 2023, 9:56pm UTC](https://discourse.cmake.org/t/automatically-generating-intermediate-files-by-intervening-compilation-process/8458 "2023-07-04T21:56:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![insight](https://discourse.cmake.org/user_avatar/discourse.cmake.org/insight/32/3468_2.png) [@insight](https://discourse.cmake.org/u/insight)\
**Post date:** [July 4, 2023, 9:56pm UTC](https://discourse.cmake.org/t/automatically-generating-intermediate-files-by-intervening-compilation-process/8458/1 "2023-07-04T21:56:05Z")

</div>

Hello folks,

This is with respect to one of my previous questions ([custom command not getting executed while building.](https://discourse.cmake.org/t/custom-command-not-getting-executed-while-building/8190)) 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?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [July 4, 2023, 10:03pm UTC](https://discourse.cmake.org/t/automatically-generating-intermediate-files-by-intervening-compilation-process/8458/2 "2023-07-04T22:03:03Z")

</div>

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.
