Specify source AND binary dirs in add_rust_* fun. Breaks API!#9
Merged
Conversation
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!
ca29544 to
a3ad255
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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!