Skip to content

Commit d5dd055

Browse files
committed
Cmake+Rust: Disable test precompile feature
Precompiling the test executable causes `sudo make install` to fail. The issue is that because we don't know the OUTPUT file path for the test executable, CMake can't know if it doesn't need to rerun that command, so it will run it with any call to `make`. The problem is that cargo is will fail to run with `sudo` on many systems. This is generally okay, because we just fixed the issue where we had been compiling the dependencies for each module. Now that we only build the dependencies once, building the test executable is actually very fast. So compiling it during the test isn't so bad.
1 parent 6814f71 commit d5dd055

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

cmake/FindRust.cmake

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,23 @@
8787
# set_property(TEST yourlib PROPERTY ENVIRONMENT ${ENVIRONMENT})
8888
# ```
8989
#
90+
# Experimental: Precompile the Tests Executable
91+
# ~~~~~~~~~~~~
92+
# This feature will cause install failures if you `sudo make install` because
93+
# it will recompile the test executable with sudo and Cargo is likely to fail to
94+
# run with sudo.
95+
# This cannot be fixed unless we can predetermine the test exeecutable OUTPUT
96+
# filepath. See: https://github.com/rust-lang/cargo/issues/1924
97+
#
9098
# If your library has unit tests AND your library DOES depend on your C
9199
# libraries, you can precompile the unit tests application with some extra
92100
# parameters to `add_rust_test()`:
93101
# - `PRECOMPILE_TESTS TRUE`
94-
# - `DEPENDS <the CMake target name for your C library dependency>`
95-
# - `ENVIRONMENT <a linked list of environment vars to build the Rust lib>`
102+
# - `PRECOMPILE_DEPENDS <the CMake target name for your C library dependency>`
103+
# - `PRECOMPILE_ENVIRONMENT <a linked list of environment vars to build the Rust lib>`
96104
#
97-
# The `DEPENDS` option is required so CMake will build the C library first.
98-
# The `ENVIRONMENT` option is required for use in your `build.rs` file so you
105+
# The `PRECOMPILE_DEPENDS` option is required so CMake will build the C library first.
106+
# The `PRECOMPILE_ENVIRONMENT` option is required for use in your `build.rs` file so you
99107
# can tell rustc how to link to your C library.
100108
#
101109
# For example:
@@ -105,8 +113,8 @@
105113
# SOURCE_DIRECTORY "${CMAKE_SOURCE_DIR}/yourlib"
106114
# BINARY_DIRECTORY "${CMAKE_BINARY_DIR}"
107115
# PRECOMPILE_TESTS TRUE
108-
# DEPENDS ClamAV::libclamav
109-
# ENVIRONMENT "${ENVIRONMENT}"
116+
# PRECOMPILE_DEPENDS ClamAV::libclamav
117+
# PRECOMPILE_ENVIRONMENT "${ENVIRONMENT}"
110118
# )
111119
# set_property(TEST yourlib PROPERTY ENVIRONMENT ${ENVIRONMENT})
112120
# ```
@@ -321,8 +329,8 @@ endfunction()
321329

322330
function(add_rust_test)
323331
set(options)
324-
set(oneValueArgs NAME SOURCE_DIRECTORY BINARY_DIRECTORY PRECOMPILE_TESTS DEPENDS)
325-
set(multiValueArgs ENVIRONMENT)
332+
set(oneValueArgs NAME SOURCE_DIRECTORY BINARY_DIRECTORY PRECOMPILE_TESTS PRECOMPILE_DEPENDS)
333+
set(multiValueArgs PRECOMPILE_ENVIRONMENT)
326334
cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
327335

328336
set(MY_CARGO_ARGS "test")
@@ -339,10 +347,10 @@ function(add_rust_test)
339347
list(JOIN MY_CARGO_ARGS " " MY_CARGO_ARGS_STRING)
340348

341349
if(ARGS_PRECOMPILE_TESTS)
342-
list(APPEND ARGS_ENVIRONMENT "CARGO_CMD=test" "CARGO_TARGET_DIR=${ARGS_BINARY_DIRECTORY}")
350+
list(APPEND ARGS_PRECOMPILE_ENVIRONMENT "CARGO_CMD=test" "CARGO_TARGET_DIR=${ARGS_BINARY_DIRECTORY}")
343351
add_custom_target(${ARGS_NAME}_tests ALL
344-
COMMAND ${CMAKE_COMMAND} -E env ${ARGS_ENVIRONMENT} ${cargo_EXECUTABLE} ${MY_CARGO_ARGS} --color always --no-run
345-
DEPENDS ${ARGS_DEPENDS}
352+
COMMAND ${CMAKE_COMMAND} -E env ${ARGS_PRECOMPILE_ENVIRONMENT} ${cargo_EXECUTABLE} ${MY_CARGO_ARGS} --color always --no-run
353+
DEPENDS ${ARGS_PRECOMPILE_DEPENDS}
346354
WORKING_DIRECTORY ${ARGS_SOURCE_DIRECTORY}
347355
)
348356
endif()

unit_tests/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,7 @@ endif()
349349
add_rust_test(NAME libclamav_rust
350350
SOURCE_DIRECTORY "${CMAKE_SOURCE_DIR}/libclamav_rust"
351351
BINARY_DIRECTORY "${CMAKE_BINARY_DIR}"
352-
PRECOMPILE_TESTS TRUE
353-
DEPENDS ClamAV::libclamav
354-
ENVIRONMENT "${ENVIRONMENT}"
352+
PRECOMPILE_TESTS FALSE # Cannot precompile, because `sudo make install` will fail. See notes in FindRust.cmake.
355353
)
356354
set_property(TEST libclamav_rust PROPERTY ENVIRONMENT ${ENVIRONMENT})
357355

0 commit comments

Comments
 (0)