# How to parse CMakeLists.txt files?

**URL:** https://discourse.cmake.org/t/how-to-parse-cmakelists-txt-files/4417
**Category:** Usage
**Created:** [November 3, 2021, 11:57pm UTC](https://discourse.cmake.org/t/how-to-parse-cmakelists-txt-files/4417 "2021-11-03T23:57:12Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Giorgi\_Didberidze](https://discourse.cmake.org/user_avatar/discourse.cmake.org/giorgi_didberidze/32/1935_2.png) [@Giorgi\_Didberidze](https://discourse.cmake.org/u/Giorgi_Didberidze)
#### Post date: [November 3, 2021, 11:57pm UTC](https://discourse.cmake.org/t/how-to-parse-cmakelists-txt-files/4417/1 "2021-11-03T23:57:12Z")

</div>

I would like to know if there’s a way to parse the content of CMakeLists.txt files and print out the targets and its content. I want to print out the content of set(), add\_executable() and add\_library(). For example:  
…\_add\_executable(…  
LINUX SSC  
SRC  
…c  
PUBLIC  
…  
…  
LABEL  
…  
…  
INCLUDE  
…  
)  
Is it possible to simply print out what’s inside of the target?

---

<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: [November 4, 2021, 10:59am UTC](https://discourse.cmake.org/t/how-to-parse-cmakelists-txt-files/4417/2 "2021-11-04T10:59:55Z")

</div>

When do you want this information? If at configure time, I would suggest making your own wrapper for `add_*` that tracks the arguments to it:

```cmake
function (my_add_executable)
  # analyze ${ARGN} as needed
  add_executable(${ARGN})
endfunction ()

```

If you just want to do some one-off processing, `cmake --trace-expand` can work. If you want more structured information, I would recommend the [`file-api`](https://cmake.org/cmake/help/latest/manual/cmake-file-api.7.html) mechanism.

---

<div class="post-metadata">

### Author: ![Giorgi\_Didberidze](https://discourse.cmake.org/user_avatar/discourse.cmake.org/giorgi_didberidze/32/1935_2.png) [@Giorgi\_Didberidze](https://discourse.cmake.org/u/Giorgi_Didberidze)
#### Post date: [November 17, 2021, 1:05am UTC](https://discourse.cmake.org/t/how-to-parse-cmakelists-txt-files/4417/3 "2021-11-17T01:05:37Z")

</div>

Thank you for your response. At this point I would like to have this information for one-off processing. I tried cmake --trace-expand, but I believe file-api is what I need as I did take a look into it. As I understood I should use codemodel object in order to retrieve this information as a json file. That’s exactly what I want, to have the target information in a machine readable format. Perfect. However, I am stuck with the implementation part. I read that I should create a query dir and after the reruning cmake the reply dir will be created with all the json files. I tried it but didn’t really get an output. So, could you please help me with this part? Thanks.

---

<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: [November 17, 2021, 2:28pm UTC](https://discourse.cmake.org/t/how-to-parse-cmakelists-txt-files/4417/4 "2021-11-17T14:28:16Z")

</div>

I’m not familiar with the actual use of file-api. @brad.king?

---

<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 17, 2021, 2:44pm UTC](https://discourse.cmake.org/t/how-to-parse-cmakelists-txt-files/4417/5 "2021-11-17T14:44:18Z")

</div>

The simplest usage is:

```auto
$ mkdir build
$ cd build
$ mkdir -p .cmake/api/v1/query
$ touch .cmake/api/v1/query/codemodel-v2
$ cmake /path/to/src ...
$ ls .cmake/api/v1/reply

```

This is an example of “v1 Shared Stateless Query Files” explained in the [cmake-file-api(7)](https://cmake.org/cmake/help/latest/manual/cmake-file-api.7.html#api-v1) documentation. This style query is mainly for manual testing. A real application or IDE should use one of the “v1 Client” style queries, whose lifetime and ownership by a particular client are well-defined.
