# How to add additional commands in target recipes

**URL:** https://discourse.cmake.org/t/how-to-add-additional-commands-in-target-recipes/138
**Category:** Usage
**Created:** [November 6, 2019, 8:34am UTC](https://discourse.cmake.org/t/how-to-add-additional-commands-in-target-recipes/138 "2019-11-06T08:34:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![oscarh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/aca169/32.png) [@oscarh](https://discourse.cmake.org/u/oscarh)
#### Post date: [November 6, 2019, 8:34am UTC](https://discourse.cmake.org/t/how-to-add-additional-commands-in-target-recipes/138/1 "2019-11-06T08:34:33Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [November 6, 2019, 10:05am UTC](https://discourse.cmake.org/t/how-to-add-additional-commands-in-target-recipes/138/2 "2019-11-06T10:05:14Z")

</div>

Different approaches can be used:

- Use [global](https://cmake.org/cmake/help/latest/prop_gbl/RULE_LAUNCH_COMPILE.html) or [target](https://cmake.org/cmake/help/latest/prop_gbl/RULE_LAUNCH_COMPILE.html) RULE\_LAUNCH\_COMPILE property or use target property [\<LANG\>\_COMPILER\_LAUNCHER](https://cmake.org/cmake/help/latest/prop_tgt/LANG_COMPILER_LAUNCHER.html#prop_tgt:%3CLANG%3E_COMPILER_LAUNCHER).
- Set variable [CMAKE\_EXPORT\_COMPILE\_COMMANDS](https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html) to `TRUE` to retrieve compile commands.
- Possibility to specify, as compiler (through [environment variables](https://cmake.org/cmake/help/latest/manual/cmake-env-variables.7.html#id3)`CC` or `CXX`), a script which will handle both actions. May be more tricky than other solutions.
- You can also have a look at [cmake-file-api](https://cmake.org/cmake/help/latest/manual/cmake-file-api.7.html).

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [November 6, 2019, 3:03pm UTC](https://discourse.cmake.org/t/how-to-add-additional-commands-in-target-recipes/138/3 "2019-11-06T15:03:08Z")

</div>

`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.

---

<div class="post-metadata">

### Author: ![oscarh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/aca169/32.png) [@oscarh](https://discourse.cmake.org/u/oscarh)
#### Post date: [November 7, 2019, 12:28am UTC](https://discourse.cmake.org/t/how-to-add-additional-commands-in-target-recipes/138/4 "2019-11-07T00:28:51Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [November 7, 2019, 2:11pm UTC](https://discourse.cmake.org/t/how-to-add-additional-commands-in-target-recipes/138/5 "2019-11-07T14:11:08Z")

</div>

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](https://cmake.org/cmake/help/v3.16/manual/cmake-file-api.7.html#v1-shared-stateless-query-files): 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.
