How to add additional commands in target recipes

I’m investigating if we can move from linux make to cmake. One of the issues I’m facing is, in our current makefile, on compiling each source file, we run a home-made tool to inspect the source code for some information:

%.o: %.c
echo “Compiling $<”
python ./scripts/extract_fmt_strs.py $< $*.fmt
$(CC) $(INCLUDES) $(CFLAGS) $(DEPFLAGS) || rm $(@D)/$(*F).d

With cmake, how can I include that “python …” line in the generated recipe? Or is there a different way to achieve the same goal in cmake?

Different approaches can be used:

RULE_LAUNCH_COMPILE is not really meant for this. It is for CTest to capture granular results.

If the script is only needed for after-the-fact analysis then I suggest using either CMAKE_EXPORT_COMPILE_COMMANDS or cmake-file-api to get a look at all sources built by the project. Otherwise I suggest using either a wrapper script as your compiler or using <LANG>_COMPILER_LAUNCHER. Either way you’ll need a script that can run they python tool and then launch the compiler.

Thanks, Brad, Marc.
I read the document of cmake-file-api. Honestly, I don’t understand its usage. Is it an offline tool to retrieve information from the generated buildsystem? I even cannot find the path <build>/.cmake/api/ mentioned in the cmake-file-api document. Is there any examples I can refer to?

The file-api is meant for IDEs and other such tools to ask for more information about the generated build system. It is up to such clients to write a .cmake/api/v1/query/* file with an appropriate query in it so that CMake will write a response. The simplest kind of query that can be created by hand to try it out is a shared stateless query: Just create an empty file called .cmake/api/v1/query/codemodel-v2 in the build tree and run CMake.

For your use case, CMAKE_EXPORT_COMPILE_COMMANDS may be more appropriate.