I use CMake+ninjabuild as the top level build system
Within CMake:
- there is a C++ static library called
foo
which is built by CMakeadd_library
- There is a C++ executable called
bar
which linksfoo
- There is a Rust execuable called
example-rs
, which is built inside CMake by
add_custom_command(COMMAND cargo build -p example-rs)
This executable need to linkfoo
What is the best way to specify that example-rs
needs to build after foo
?
I need cmake --build . --target all
to build all of foo
, bar
and example-rs
Currently it is done by add_custom_command(COMMAND cargo build -p example-rs DEPENDS foo)
But this makes cargo test
be unware of the dependency, so I have to manually rebuild foo
,
before cargo test
during development
The alternative solution would be using cmake
crate in build.rs
,
which build foo
within build.rs
,
but I do not know how to solve the diamond problem.
The top level cmake and the cmake called within build.rs
uses the same CMAKE_BINARY_DIR,
The top level cmake and build.rs cmake may both compile foo
in parallel,
so this may cause a conflict in disk file.
The problem above may be solved by using both add_custom_command(DEPENDS)
and build.rs
,
but what if there are two Rust executables within CMake project, which both needs to link foo
?
When cargo test
, if both of them rebuilding foo
within build.rs
, there is also a conflict.
NOTE: Creating multiple cmake build directory (CMAKE_BINARY_DIR) is not preferred.
The project is big and CMake configuration takes several minutes.