How to avoid race for two `cmake --build` in parallel?

How to avoid race when two cmake --build are run in parallel, for the same cmake build directory?

For example, if I want to have cmake --build . --target Alice and cmake --build . --target Bob in parallel,
and both of the targets depends on Foo.

How to ensure Foo is built only once without race?

Why I need to call two cmake --build in parallel?
I need to call CMake from another build system, in order to specify dependency.

I am using Linux with ninja-build generator

No, this is not supported. Though ninja might perform some locking making these effectively serial, there is no guarantee CMake provides itself.