Commit Graph

12 Commits

Author SHA1 Message Date
Micah Snyder
027e5b6144 CMake, CTest: Decrease Rust test verbosity
Now that we have a fair number of Rust crate dependencies,
`cargo test -vv` is way too verbose.
2022-03-02 13:12:59 -07:00
Micah Snyder
0556550b23 CMake, Rust: Improve toolchain (target triple) selection
The current method of trying to determine the target triple based
the architecture, operating system, etc. is difficult to get right
and maintain. In particular, I found that some installations like the
Alpine package will use "alpine" in the name of the triple instead of
"unknown".
E.g. "aarch64-alpine-linux-musl" instead of "aarch64-unknown-linux-musl".
This makes it nearly impossible to figure out the exact target name
based on target system specifications.

I believe it will be better to use the default target 99% of the time
and require users that which to cross-compile to use a new CMake
variable `-D RUST_TARGET_TRIPLE=<target>` to choose the target.
This is basically how it works for C/C++ anyways.
2022-03-01 16:18:03 -07:00
micasnyd
0fe0b79b94 CMake: Rust RelWithDebInfo and MinSizeRel builds
RelWithDebInfo is our preferred build type. It has optimizations and
should run faster, but includes debugging symbols for better profiling,
stack traces, etc.

The Rust MinSizeRel support is just Release mode for now. There are
optimizations we can do to shrink it further, but for now at least it
won't actually be Debug (aka slow).
2022-01-19 16:12:25 -07:00
micasnyd
4d34bc10b8 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).
2022-01-14 11:50:07 -07:00
micasnyd
5698e3285c CMake-Rust: Only execute cbindgen for build, not tests
Generate c-bindings header in build (binary) directory,
not source directory (if different).
2022-01-10 12:18:33 -07:00
micasnyd
140c88aa4e Bump copyright for 2022
Includes minor format corrections.
2022-01-09 14:23:25 -07:00
Scott Hutton
278ba2923e Integrate cbindgen via build.rs 2021-12-08 13:15:46 -07:00
Scott Hutton
37e5b573aa Add Rust logging module unit test & integrate with CMake / CTest
Add a basic unit test for the new libclamav_rust `logging.rs` module.
This test simply initializes logging and then prints out a message with
each of the `log` macros.

Also set the Rust edition to 2018 because the default is the 2015
edition in which using external crates is very clunky.

For the Rust test support in CMake this commit adds support for
cross-compiling the Rust tests.
Rust tests must be built for the same LLVM triple (target platform) as
the rest of the project. In particular this is needed to build both
x64 and x86 packages on a 64bit Windows host.

For Alpine, we observed that the LLVM triple for the host platform tools
may be either:
- x86_64-unknown-linux-musl, or
- x86_64-alpine-linux-musl
To support it either way, we look up the host triple with `rustc -vV`
and use that if the musl libc exists. This is a big hacky and
unfortunately means that we probably can't cross-compile to other
platforms when running on a musl libc host. There are probably
improvements to be made to improve cross compiling support.

The Rust test programs must link with libclamav, libclammspack, and
possibly libclamunrar_iface and libclamunrar plus all of the library
dependencies for those libraries.
To do this, we pass the path of each library in environment variables
when building the libclamav_rust unit test program.
Within `libclamav_rust/build.rs`, we read those environment variables.
If set, we parse each into library path and name components to use
as directives for how to build the unit test program.
See: https://doc.rust-lang.org/cargo/reference/build-scripts.html

Our `build.rs` file ignores the library path environment variables if
thye're not set, which is necessary when building the libclamav_rust
library and when libclamunrar isn't static and for when not linking with
a libiconv external to libc.

Rust test programs are built and executed in subdirectory under:
  <target>/<llvm triple>/<config>/deps
where "target" for libclamav_rust tests is set to <build>/unit_tests
For example:
  clamav/build/unit_tests/x86_64-pc-windows-msvc/debug/deps/clamav_rust-7e1343f8a2bff1cc.exe
Since this program isn't co-located with the rest of the libraries
we also have to set environment variables so the test program can find and
load the shared libraries:
- Windows: PATH
- macOS: DYLD_LIBRARY_PATH
We already set LD_LIBRARY_PATH when not Windows for similar reasons.

Note: In build.rs, we iterate references to LIB_ENV_LINK & Co because
older Rust versions do implement Iterator for [&str].
2021-11-23 10:43:14 -08:00
Micah Snyder
679883fdb6 CMake: Improve Rust platform support
Add support for Rust FreeBSD, OpenBSD targets

Add support for Rust on GNU aarch64.

Add support for Rust on Alpine (musl x86_64).
Note: Current trick of checking for musl libc.so doesn't work when
cross compiling. TODO: Find a better way to check if the target is
MUSL.

Add Rust toolchain to fix Dockerfile build.
2021-11-15 12:33:05 -08:00
Micah Snyder
6f4bbe2d8b CMake: Add support for Rust (Cargo) universal binary builds on macOS 2021-11-15 12:33:05 -08:00
Micah Snyder
147502dcf6 Add cargo vendor feature
Vendoring crate dependencies is required for offline builds.
Some packaging systems require that builds can be performed offline.
This feature enabled vendoring crates at configure time which are
then included in in CPack source packaging.
2021-11-15 12:33:05 -08:00
Micah Snyder
a53eeec275 CMake: Add Rust toolchain detection 2021-11-15 12:33:05 -08:00