Skip to content

Commit

Permalink
Add initial optional Rust support
Browse files Browse the repository at this point in the history
Do not too excited, this is only to see what breaks.
  • Loading branch information
hughsie committed Jul 15, 2023
1 parent fa5be90 commit 9a12f9a
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ repos:
types_or: [shell, c, python]
language: script
entry: ./contrib/ci/check-license.py
- id: rustfmt
name: Check rust format
types_or: [rust]
language: script
entry: ./contrib/ci/rustfmt.py
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.33.0
hooks:
Expand Down
12 changes: 12 additions & 0 deletions contrib/ci/dependencies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1780,4 +1780,16 @@
<package variant="mingw64">msitools</package>
</distro>
</dependency>
<!-- optional -->
<dependency id="rustc">
<distro id="fedora">
<package variant="x86_64" />
</distro>
<distro id="debian">
<package variant="x86_64" />
</distro>
<distro id="ubuntu">
<package variant="x86_64" />
</distro>
</dependency>
</dependencies>
46 changes: 46 additions & 0 deletions contrib/ci/rustfmt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/python3
#
# Copyright (C) 2023 Richard Hughes <richard@hughsie.com>
#
# SPDX-License-Identifier: LGPL-2.1+

import glob
import os
import sys
import subprocess


def __get_license(fn: str) -> str:
with open(fn, "r") as f:
for line in f.read().split("\n"):
if line.find("SPDX-License-Identifier:") > 0:
return line.split(":")[1]
return ""


def test_files() -> int:

rc: int = 0
for fn in glob.glob("**/*.rs", recursive=True):
if "meson-private" in fn:
continue
if "venv" in fn:
continue
if fn.startswith("subprojects"):
continue
if fn.startswith("dist"):
continue
try:
subprocess.check_output(["rustfmt", "--quiet", "--check", fn])
except FileNotFoundError as e:
break
except subprocess.CalledProcessError as e:
print(str(e))
rc = 1
return rc


if __name__ == "__main__":

# all done!
sys.exit(test_files())
6 changes: 6 additions & 0 deletions libfwupdplugin/fu-crc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*
* Since: 1.8.2
**/
#ifndef HAVE_RUST
guint8
fu_crc8_full(const guint8 *buf, gsize bufsz, guint8 crc_init, guint8 polynomial)
{
Expand All @@ -37,6 +38,7 @@ fu_crc8_full(const guint8 *buf, gsize bufsz, guint8 crc_init, guint8 polynomial)
}
return ~((guint8)(crc >> 8));
}
#endif

/**
* fu_crc8:
Expand Down Expand Up @@ -68,6 +70,7 @@ fu_crc8(const guint8 *buf, gsize bufsz)
*
* Since: 1.8.2
**/
#ifndef HAVE_RUST
guint16
fu_crc16_full(const guint8 *buf, gsize bufsz, guint16 crc, guint16 polynomial)
{
Expand All @@ -83,6 +86,7 @@ fu_crc16_full(const guint8 *buf, gsize bufsz, guint16 crc, guint16 polynomial)
}
return ~crc;
}
#endif

/**
* fu_crc16:
Expand Down Expand Up @@ -114,6 +118,7 @@ fu_crc16(const guint8 *buf, gsize bufsz)
*
* Since: 1.8.2
**/
#ifndef HAVE_RUST
guint32
fu_crc32_full(const guint8 *buf, gsize bufsz, guint32 crc, guint32 polynomial)
{
Expand All @@ -127,6 +132,7 @@ fu_crc32_full(const guint8 *buf, gsize bufsz, guint32 crc, guint32 polynomial)
}
return ~crc;
}
#endif

/**
* fu_crc32:
Expand Down
85 changes: 85 additions & 0 deletions libfwupdplugin/fu-crc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (C) 2022 Richard Hughes <richard@hughsie.com>
//
// SPDX-License-Identifier: LGPL-2.1+

use std::mem;
use std::slice;

fn crc8_full(buf: &[u8], crc_init: u8, polynomial: u8) -> u8 {
let mut crc = crc_init as u32;
for tmp in buf.iter() {
crc ^= (*tmp as u32) << 8;
for _ in 0..8 {
if crc & 0x8000 > 0 {
crc ^= ((polynomial as u32 | 0x100) << 7) as u32;
}
crc <<= 1;
}
}
!(crc >> 8) as u8
}

fn crc16_full(buf: &[u8], crc_init: u16, polynomial: u16) -> u16 {
let mut crc = crc_init;
for tmp in buf.iter() {
crc = crc ^ *tmp as u16;
for _ in 0..8 {
if crc & 0x1 > 0 {
crc = (crc >> 1) ^ polynomial;
} else {
crc >>= 1;
}
}
}
!crc
}

fn crc32_full(buf: &[u8], crc_init: u32, polynomial: u32) -> u32 {
let mut crc = crc_init;
for tmp in buf.iter() {
crc = crc ^ *tmp as u32;
for _ in 0..8 {
let mask = {
if crc & 0b1 > 0 {
0xFFFFFFFF
} else {
0x0
}
};
crc = (crc >> 1) ^ (polynomial & mask);
}
}
!crc
}

#[no_mangle]
pub extern "C" fn fu_crc8_full(buf: *mut u8, bufsz: usize, crc_init: u8, polynomial: u8) -> u8 {
let slice = unsafe { slice::from_raw_parts(buf, bufsz) };
let ret = crc8_full(&slice, crc_init, polynomial);
mem::forget(slice);
ret
}

#[no_mangle]
pub extern "C" fn fu_crc16_full(buf: *mut u8, bufsz: usize, crc_init: u16, polynomial: u16) -> u16 {
let slice = unsafe { slice::from_raw_parts(buf, bufsz) };
let ret = crc16_full(&slice, crc_init, polynomial);
mem::forget(slice);
ret
}

#[no_mangle]
pub extern "C" fn fu_crc32_full(buf: *mut u8, bufsz: usize, crc_init: u32, polynomial: u32) -> u32 {
let slice = unsafe { slice::from_raw_parts(buf, bufsz) };
let ret = crc32_full(&slice, crc_init, polynomial);
mem::forget(slice);
ret
}

#[test]
fn fu_crc() {
let buf = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09];
assert_eq!(crc8_full(&buf, 0x0, 0x07), 0x7A);
assert_eq!(crc16_full(&buf, 0xFFFF, 0xA001), 0x4DF1);
assert_eq!(crc32_full(&buf, 0xFFFFFFFF, 0xEDB88320), 0x40EFAB9E);
}
15 changes: 15 additions & 0 deletions libfwupdplugin/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,20 @@ library_deps = [
platform_deps,
]

if get_option('rust').disable_auto_if(not rustc.found()).allowed()
fwupdpluginrs = static_library(
'fwupdpluginrs',
sources: [
'fu-crc.rs',
],
rust_crate_type: 'staticlib',
)
rust = import('unstable-rust')
rust.test('fwupdpluginrs', fwupdpluginrs)
else
fwupdpluginrs = []
endif

fwupdplugin = library(
'fwupdplugin',
fwupdplugin_struct_targets,
Expand All @@ -310,6 +324,7 @@ fwupdplugin = library(
],
link_with: [
fwupd,
fwupdpluginrs,
],
install_dir: libdir_pkg,
install: true
Expand Down
6 changes: 6 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ add_project_link_arguments(
language: 'c'
)

# rust is optional
rustc = find_program('rustc', required: get_option('rust'))
if add_languages('rust', native: false, required: rustc.found())
add_project_arguments('-DHAVE_RUST', language: 'c')
endif

add_project_arguments('-DFWUPD_COMPILATION', language: 'c')

# Needed for realpath(), syscall(), cfmakeraw(), etc.
Expand Down
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
option('build', type : 'combo', choices : ['all', 'standalone', 'library'], value : 'all', description : 'build type')
option('rust', type : 'feature', description : 'Rust support')
option('consolekit', type : 'feature', description : 'ConsoleKit support', deprecated: {'true': 'enabled', 'false': 'disabled'})
option('static_analysis', type : 'boolean', value : false, description : 'enable GCC static analysis support')
option('firmware-packager', type : 'boolean', value : true, description : 'enable firmware-packager installation')
Expand Down

0 comments on commit 9a12f9a

Please sign in to comment.