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 Oct 30, 2023
1 parent 06afa2a commit 9f5dc86
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 2 deletions.
2 changes: 1 addition & 1 deletion contrib/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license=('GPL2')
depends=('libgusb' 'modemmanager' 'tpm2-tss')
makedepends=('meson' 'valgrind' 'gobject-introspection' 'gi-docgen' 'git'
'python-cairo' 'noto-fonts' 'noto-fonts-cjk' 'python-gobject' 'vala'
'libdrm' 'curl' 'polkit' 'xz')
'libdrm' 'curl' 'polkit' 'rust' 'xz')

pkgver() {
cd ${pkgname}
Expand Down
15 changes: 15 additions & 0 deletions contrib/ci/dependencies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1755,4 +1755,19 @@
<package variant="mingw64">msitools</package>
</distro>
</dependency>
<!-- optional -->
<dependency id="rustc">
<distro id="arch">
<package variant="x86_64">rust</package>
</distro>
<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
78 changes: 78 additions & 0 deletions libfwupdplugin/fu-crc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (C) 2022 Richard Hughes <richard@hughsie.com>
//
// SPDX-License-Identifier: LGPL-2.1+

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) };
crc8_full(&slice, crc_init, polynomial)
}

#[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) };
crc16_full(&slice, crc_init, polynomial)
}

#[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) };
crc32_full(&slice, crc_init, polynomial)
}

#[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 @@ -298,6 +298,20 @@ library_deps = [
platform_deps,
]

if rust_support
fwupdpluginrs = static_library(
'fwupdpluginrs',
sources: [
'fu-crc.rs',
],
rust_crate_type: 'staticlib',
)
rust = import('rust')
rust.test('fwupdpluginrs', fwupdpluginrs)
else
fwupdpluginrs = []
endif

fwupdplugin = library(
'fwupdplugin',
fwupdplugin_rs_targets,
Expand All @@ -314,6 +328,7 @@ fwupdplugin = library(
],
link_with: [
fwupd,
fwupdpluginrs,
],
install_dir: libdir_pkg,
install: true
Expand Down
10 changes: 9 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project('fwupd', 'c',
version: '1.9.7',
license: 'LGPL-2.1-or-later',
meson_version: '>=0.61.0',
meson_version: '>=1.0.0',
default_options: ['warning_level=2', 'c_std=c11'],
)

Expand Down Expand Up @@ -143,6 +143,14 @@ add_project_link_arguments(
language: 'c'
)

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

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

# Needed for realpath(), syscall(), cfmakeraw(), etc.
Expand Down
4 changes: 4 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ option('build',
value: 'all',
description: 'build type',
)
option('rust',
type: 'feature',
description: 'Rust support',
)
option('consolekit',
type: 'feature',
description: 'ConsoleKit support',
Expand Down
1 change: 1 addition & 0 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ parts:
- git
- gettext
- gnu-efi
- rustc
- fwupd-unsigned-dev
- libcurl4-openssl-dev
- libarchive-dev
Expand Down

0 comments on commit 9f5dc86

Please sign in to comment.