Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial optional Rust support #5427

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" />
superm1 marked this conversation as resolved.
Show resolved Hide resolved
</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])
hughsie marked this conversation as resolved.
Show resolved Hide resolved
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you just have this one ifndef wrap all 3? Or is the concern the documentation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was worried about the docs. I'll play -- I didn't like the idea of duplicating them in two source files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible I would suggest moving them to the rust files, and then making sure the job that builds docs does it from the rust files too. Then that makes sure that we can scale better too as more rust stuff is added.


/**
* 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>
hughsie marked this conversation as resolved.
Show resolved Hide resolved
//
// SPDX-License-Identifier: LGPL-2.1+
hughsie marked this conversation as resolved.
Show resolved Hide resolved

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is probably missing comments about the function documentation and parameters. Not sure if the documentation generator will parse the rust files, but another thing worth checking.

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',
],
superm1 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading