Rust bindgen: Only generate if CMake MAINTAINER_MODE is ON

In testing, I found that libclang.so/clang.dll is required by bindgen
and was not found on all of our test machines. To resolve this we will
only generate sys.rs bindings when CMake MAINTAINER_MODE option is "ON".

This is unfortunate that we have to commit generated source to version
control. But as a benefit it makes rust-analyzer happier when editing a
workspace that hasn't yet been compiled. And it makes it more reasonable
that the generated sys.rs file generated to the source directory and not
the build directory (something we hadn't resolved yet).
This commit is contained in:
micasnyd 2022-01-10 16:49:44 -08:00 committed by Micah Snyder
parent ba0a21c31f
commit 4d34bc10b8
6 changed files with 7132 additions and 10 deletions

4
.gitignore vendored
View file

@ -234,7 +234,3 @@ Cargo.lock
# These are backup files generated by rustfmt # These are backup files generated by rustfmt
**/*.rs.bk **/*.rs.bk
# The name of our generated ClamAV-Rust bindings file.
libclamav_rust/src/sys.rs

View file

@ -80,6 +80,7 @@ The Windows Visual Studio and Autotools build systems have been removed.
You will need: You will need:
- CMake (3.14+ for Unix/Linux; 3.16+ for Windows) - CMake (3.14+ for Unix/Linux; 3.16+ for Windows)
- A C compiler toolchain such as `gcc`, `clang`, or Microsoft Visual Studio. - A C compiler toolchain such as `gcc`, `clang`, or Microsoft Visual Studio.
- The Rust compiler toolchain.
Recommended tools: Recommended tools:
- pkg-config - pkg-config
@ -89,6 +90,7 @@ For Maintainer-mode only (not recommended):
- Flex - Flex
- Bison - Bison
- Gperf - Gperf
- Rust bindgen
### External Library Dependencies ### External Library Dependencies
@ -496,6 +498,7 @@ The following is a complete list of CMake options unique to configuring ClamAV:
_Default: `ON`_ _Default: `ON`_
- `MAINTAINER_MODE`: Generate Yara lexer and grammar C source with Flex & Bison. - `MAINTAINER_MODE`: Generate Yara lexer and grammar C source with Flex & Bison.
Generate Rust bindings (`libclamav_rust/src/sys.rs`).
*To-do*: Also generate JS parse source with Gperf. *To-do*: Also generate JS parse source with Gperf.
_Default: `OFF`_ _Default: `OFF`_

View file

@ -165,8 +165,8 @@ function(add_rust_library)
if ("${CMAKE_OSX_ARCHITECTURES}" MATCHES "^arm64;x86_64$") if ("${CMAKE_OSX_ARCHITECTURES}" MATCHES "^arm64;x86_64$")
add_custom_command( add_custom_command(
OUTPUT "${OUTPUT}" OUTPUT "${OUTPUT}"
COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=build" "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS} --target=x86_64-apple-darwin COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=build" "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" "MAINTAINER_MODE=${MAINTAINER_MODE}" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS} --target=x86_64-apple-darwin
COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=build" "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS} --target=aarch64-apple-darwin COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=build" "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" "MAINTAINER_MODE=${MAINTAINER_MODE}" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS} --target=aarch64-apple-darwin
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/${LIB_TARGET}/${LIB_BUILD_TYPE}" COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/${LIB_TARGET}/${LIB_BUILD_TYPE}"
COMMAND lipo ARGS -create ${CMAKE_CURRENT_BINARY_DIR}/x86_64-apple-darwin/${LIB_BUILD_TYPE}/lib${ARGS_TARGET}.a ${CMAKE_CURRENT_BINARY_DIR}/aarch64-apple-darwin/${LIB_BUILD_TYPE}/lib${ARGS_TARGET}.a -output "${OUTPUT}" COMMAND lipo ARGS -create ${CMAKE_CURRENT_BINARY_DIR}/x86_64-apple-darwin/${LIB_BUILD_TYPE}/lib${ARGS_TARGET}.a ${CMAKE_CURRENT_BINARY_DIR}/aarch64-apple-darwin/${LIB_BUILD_TYPE}/lib${ARGS_TARGET}.a -output "${OUTPUT}"
WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}" WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}"
@ -175,7 +175,7 @@ function(add_rust_library)
else() else()
add_custom_command( add_custom_command(
OUTPUT "${OUTPUT}" OUTPUT "${OUTPUT}"
COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=build" "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS} COMMAND ${CMAKE_COMMAND} -E env "CARGO_CMD=build" "CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" "MAINTAINER_MODE=${MAINTAINER_MODE}" ${cargo_EXECUTABLE} ARGS ${MY_CARGO_ARGS}
WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}" WORKING_DIRECTORY "${ARGS_WORKING_DIRECTORY}"
DEPENDS ${LIB_SOURCES} DEPENDS ${LIB_SOURCES}
COMMENT "Building ${ARGS_TARGET} in ${ARGS_WORKING_DIRECTORY} with: ${cargo_EXECUTABLE} ${MY_CARGO_ARGS_STRING}") COMMENT "Building ${ARGS_TARGET} in ${ARGS_WORKING_DIRECTORY} with: ${cargo_EXECUTABLE} ${MY_CARGO_ARGS_STRING}")

View file

@ -628,7 +628,7 @@ static const flex_int32_t yy_rule_can_match_eol[76] =
/* /*
* YARA rule lexer for ClamAV * YARA rule lexer for ClamAV
* *
* Copyright (C) 2014-2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved. * Copyright (C) 2014-2022 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
* *
* Authors: Steven Morgan * Authors: Steven Morgan
* *

View file

@ -92,12 +92,24 @@ fn main() -> Result<(), &'static str> {
detect_clamav_build()?; detect_clamav_build()?;
// We only want to execute cbindgen for `cargo build`, not `cargo test`. // We only want to generate bindings for `cargo build`, not `cargo test`.
// FindRust.cmake defines $CARGO_CMD so we can differentiate. // FindRust.cmake defines $CARGO_CMD so we can differentiate.
let cargo_cmd = env::var("CARGO_CMD").unwrap_or("".into()); let cargo_cmd = env::var("CARGO_CMD").unwrap_or("".into());
if cargo_cmd == "build" { if cargo_cmd == "build" {
execute_bindgen()?; // Always generate the C-headers when CMake kicks off a build.
execute_cbindgen()?; execute_cbindgen()?;
// Only generate the `.rs` bindings when maintainer-mode is enabled.
//
// Bindgen requires libclang, which may not readily available, so we
// will commit the bindings to version control and use maintainer-mode
// to update them, as needed.
// On the plus-side, this means that our `.rs` file is present before our
// first build, so at least rust-analyzer will be happy.
let maintainer_mode = env::var("MAINTAINER_MODE").unwrap_or("".into());
if maintainer_mode == "ON" {
execute_bindgen()?;
}
} else { } else {
eprintln!("NOTE: Not generating bindings because CARGO_CMD != build"); eprintln!("NOTE: Not generating bindings because CARGO_CMD != build");
} }

7111
libclamav_rust/src/sys.rs Normal file

File diff suppressed because it is too large Load diff