# Ninja code generator does not build in correct order when using protobuf and custom\_command

**URL:** https://discourse.cmake.org/t/ninja-code-generator-does-not-build-in-correct-order-when-using-protobuf-and-custom-command/3730
**Category:** Code
**Tags:** os:linux, gen:ninja
**Created:** [July 11, 2021, 12:08pm UTC](https://discourse.cmake.org/t/ninja-code-generator-does-not-build-in-correct-order-when-using-protobuf-and-custom-command/3730 "2021-07-11T12:08:21Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Acki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@Acki](https://discourse.cmake.org/u/Acki)
#### Post date: [July 11, 2021, 12:08pm UTC](https://discourse.cmake.org/t/ninja-code-generator-does-not-build-in-correct-order-when-using-protobuf-and-custom-command/3730/1 "2021-07-11T12:08:21Z")

</div>

I have a project which is using protobuf and a custom command.  
However I get a compile error, because my custom\_command is not executed in the correct order.  
The problem occurs only with the ninja code generator. With make the code compiles fine.

Steps to reproduce:

```auto
cmake -S . -GNinja -B build
cmake --build build

```

```auto
../Main.cpp:2:10: fatal error: some_header.h: No such file or directory
    2 | #include "some_header.h"

```

For unknown reason I cannot upload my test case code in the forum…  
So I uploaded it to a third party site. It is a minimal test case.

[protobuf\_test\_case.tar.xz](https://www.file-upload.net/download-14633703/protobuf_test_case.tar.xz.html)

Can anybody tell me how I can get my code working also with ninja?

---

<div class="post-metadata">

### Author: ![Acki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@Acki](https://discourse.cmake.org/u/Acki)
#### Post date: [July 23, 2021, 7:43pm UTC](https://discourse.cmake.org/t/ninja-code-generator-does-not-build-in-correct-order-when-using-protobuf-and-custom-command/3730/2 "2021-07-23T19:43:22Z")

</div>

can nobody help?

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [July 23, 2021, 11:15pm UTC](https://discourse.cmake.org/t/ninja-code-generator-does-not-build-in-correct-order-when-using-protobuf-and-custom-command/3730/3 "2021-07-23T23:15:14Z")

</div>

Hi,

It is easier if you show the code plain somewhere instead of an archive.  
Your custom command looks like:

```auto
add_custom_command(
  TARGET MyProtoLib
  PRE_LINK
  COMMAND PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR} python3
          ${CMAKE_CURRENT_SOURCE_DIR}/test.py)

```

This cannot work when creating the header file like this. Ninja does not know what creates some\_header.h (or if it exists somewhere in the include search path). And it is free to compile Main.cpp before create MyProtoLib static library.  
You should prefer a custom command that is not bound to a target:

```auto
add_custom_command ( OUTPUT some_header.h
  COMMAND "${CMAKE_COMMAND}" -E env PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}
          python3 ${CMAKE_CURRENT_SOURCE_DIR}/test.py
  DEPENDS test.py
)

```

and then add some\_header.h to the MyProtoLib or ConsumingService sources

```auto
target_sources ( MyProtoLib PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/some_header.h )

```

---

<div class="post-metadata">

### Author: ![Acki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@Acki](https://discourse.cmake.org/u/Acki)
#### Post date: [August 1, 2021, 2:49pm UTC](https://discourse.cmake.org/t/ninja-code-generator-does-not-build-in-correct-order-when-using-protobuf-and-custom-command/3730/4 "2021-08-01T14:49:15Z")

</div>

Hi Hendrik, thanks for your reply, this gives me a hint in the right direction.  
Unfortunately I missed some important part of the test case. So the `MyProtoLib` creates some python files during compilation and this python file should be used when running test.py  
so I added the missing import statement `import foo_pb2.py`  
Thats why there is the dependency from the `test.py` to `MyProtoLib`

So the dependency tree is like that:

1. Compile `MyProtoLib`
2. Run `test.py`
3. Compile `ConsumingService`

I adjusted the test case to reflect this, now the compilation fails again, also with make.

[https://www.file-upload.net/download-14650441/protobuf\_test\_case2.tar.xz.html](https://www.file-upload.net/download-14650441/protobuf_test_case2.tar.xz.html)  
PS: Why Can I still not upload file in the forum?

---

<div class="post-metadata">

### Author: ![Acki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@Acki](https://discourse.cmake.org/u/Acki)
#### Post date: [August 5, 2021, 8:09am UTC](https://discourse.cmake.org/t/ninja-code-generator-does-not-build-in-correct-order-when-using-protobuf-and-custom-command/3730/5 "2021-08-05T08:09:34Z")

</div>

Hi @hsattler did you saw my answer?

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [August 5, 2021, 9:08pm UTC](https://discourse.cmake.org/t/ninja-code-generator-does-not-build-in-correct-order-when-using-protobuf-and-custom-command/3730/6 "2021-08-05T21:08:05Z")

</div>

some\_header.h should be added as PRIVATE to MyProtoLib, not ConsumingService.  
After that the dependency chain between Main.cpp.obj and some\_header.h should be working

The add\_custom\_command for some\_header.h needs an additions DEPENDS on ${CMAKE\_CURRENT\_BINARY\_DIR}/foo\_pb2.py to trigger it’s creating prior to running test.py.

---

<div class="post-metadata">

### Author: ![Acki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@Acki](https://discourse.cmake.org/u/Acki)
#### Post date: [August 5, 2021, 10:38pm UTC](https://discourse.cmake.org/t/ninja-code-generator-does-not-build-in-correct-order-when-using-protobuf-and-custom-command/3730/7 "2021-08-05T22:38:32Z")

</div>

@hsattler  
I added:

```auto
target_sources ( MyProtoLib PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/some_header.h )

add_custom_command ( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/some_header.h
  COMMAND "${CMAKE_COMMAND}" -E env PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}
          python3 ${CMAKE_CURRENT_SOURCE_DIR}/test.py
  DEPENDS test.py
  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/foo_pb2.py
)

```

I get:

```auto
../src/Main.cpp:2:10: fatal error: some_header.h: No such file or directory
    2 | #include "some_header.h"

```

The python code itself will not be executed…so there is even no `some_header.h` created at all

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [August 6, 2021, 6:41am UTC](https://discourse.cmake.org/t/ninja-code-generator-does-not-build-in-correct-order-when-using-protobuf-and-custom-command/3730/8 "2021-08-06T06:41:53Z")

</div>

I added  
find\_package(Python3 REQUIRED)  
in the top-level CMakeLists.txt and used “${Python3\_EXECUTABLE}” instead of plain python to make it work in Windows with python not in PATH.

Then I fixed the test.py to “import foo\_pb2” (remove the .py suffix).  
I then build with Ninja generator and it worked.

Since the test.py could not be run, there was not some\_header.h. Always look at the first error message, not the last.

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [August 6, 2021, 6:45am UTC](https://discourse.cmake.org/t/ninja-code-generator-does-not-build-in-correct-order-when-using-protobuf-and-custom-command/3730/9 "2021-08-06T06:45:55Z")

</div>

```auto
  DEPENDS test.py
  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/foo_pb2.py

```

should be

```auto
 DEPENDS test.py ${CMAKE_CURRENT_BINARY_DIR}/foo_pb2.py

```

see syntax of add\_custom\_command()
