Skip to content

Specify source AND binary dirs in add_rust_* fun. Breaks API!#9

Merged
val-ms merged 4 commits intomainfrom
rust-source-binary-directories
Sep 7, 2022
Merged

Specify source AND binary dirs in add_rust_* fun. Breaks API!#9
val-ms merged 4 commits intomainfrom
rust-source-binary-directories

Conversation

@val-ms
Copy link
Owner

@val-ms val-ms commented Sep 6, 2022

Changed the add_rust_* functions to have SOURCE_DIRECTORY and BINARY_DIRECTORY parameters instead of WORKING_DIRECTORY. This allows you to build and run the Rust unit tests from the same BINARY_DIRECTORy as you do for the Rust library build. That means it won't recompile all of the dependencies when building the unit test executable.

A side-effect of this change is that the CMakeLists.txt file that uses add_rust_library() must be in the same directory as the Rust module. I.e. you should co-locate the CMakeLists.txt with the Cargo.toml file. You can no longer have a parent directory's CMakeFiles.txt add a library for a subdirectory.
The reason is that CMake will set the INTERFACE_INCLUDE_DIRECTORIES property for your library's CMake target use both the SOURCE_DIRECTORY and BINARY_DIRECTORY. This ensures that generated headers will be placed in the include path for any downstream CMake targets. CMake requires both directories to exist. When CMake does the build, it will create subdirectories in the build path for each CMakeLists.txt file. Co-locating CMakeLists.txt with Cargo.toml ensures that the BUILD_DIRECTORY is automatically created by CMake.

Important: This commit breaks the API for the FindRust.cmake module!

Changed the `add_rust_*` functions to have SOURCE_DIRECTORY and
BINARY_DIRECTORY parameters instead of WORKING_DIRECTORY.
This allows you to build and run the Rust unit tests from the same
BINARY_DIRECTORy as you do for the Rust library build. That means it
won't recompile all of the dependencies when building the unit test
executable.

A side-effect of this change is that the CMakeLists.txt file that uses
`add_rust_library()` must be in the same directory as the Rust module.
I.e. you should co-locate the CMakeLists.txt with the Cargo.toml file.
You can no longer have a parent directory's CMakeFiles.txt add a
library for a subdirectory.
The reason is that CMake will set the INTERFACE_INCLUDE_DIRECTORIES
property for your library's CMake target use both the SOURCE_DIRECTORY
and BINARY_DIRECTORY. This ensures that generated headers will be placed
in the include path for any downstream CMake targets.
CMake requires both directories to exist. When CMake does the build, it
will create subdirectories in the build path for each CMakeLists.txt
file. Co-locating CMakeLists.txt with Cargo.toml ensures that the
BUILD_DIRECTORY is automatically created by CMake.

Important: This commit breaks the API for the FindRust.cmake module!
If you use the same BINARY_DIRECTORY for all Rust targets, then you will
not have to rebuild the dependencies that they have in common. This
significantly improves compile time for projects with multiple Rust
components.
Unlike the sys.rs Rust beindings, the C bindings are always generated
and do not need to be committed.
It appears that when MAINTAINER_MODE=ON, the change in that environment
variable for the demorust ( lib/rust ) module causes the build.rs script
to be re-run.  I think that's because the MAINTAINER_MODE variable may
not be passed to `cargo test` from `ctest`, while it is passed to `cargo
build` from `cmake`.  However, when MAINTAINER_MODE=OFF there is no
change in the environment variables passed and so `build.rs` is not
re-run for `cargo test`.  As a consequence, it never links the test
executable with the required librar(ies).

The primary change in this commit to fix that is to move:
```rust
println!("cargo:rerun-if-env-changed=LIBDEMO");
```
out of the "test" match case so it applies in general.
That seems to fix it. I'm ... not 100% sure why, but it works for me!
@val-ms val-ms force-pushed the rust-source-binary-directories branch from ca29544 to a3ad255 Compare September 7, 2022 20:32
@val-ms val-ms merged commit fd293fa into main Sep 7, 2022
@val-ms val-ms deleted the rust-source-binary-directories branch September 8, 2022 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant