rust support in libutil via rust-monocrate
Co-authored-by: Jade Lovelace <jadel@mercury.com> Change-Id: I026f271b07c9e27012f9ee1c16a2a1f4ba7f6ba3
This commit is contained in:
Generated
+4
@@ -39,6 +39,10 @@ dependencies = [
|
||||
"rowan",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lixutil-rs"
|
||||
version = "0.0.0"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.19.0"
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
members = ["lix/lix-doc"]
|
||||
members = ["lix/lix-doc", "lix/libutil"]
|
||||
|
||||
[workspace.package]
|
||||
edition = "2021"
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
# Note: this is not really intended to actually do anything with Cargo, it
|
||||
# exists to make Cargo manage dependencies in a workspace across lix-doc and
|
||||
# liblixrust
|
||||
#
|
||||
# It is highly likely it will not actually pass build, especially as the build
|
||||
# system gets more anti-Cargo.
|
||||
|
||||
[package]
|
||||
name = "lixutil-rs"
|
||||
edition.workspace = true
|
||||
|
||||
[lib]
|
||||
path = "lib.rs"
|
||||
@@ -0,0 +1,2 @@
|
||||
language = "C++"
|
||||
namespace = "nix"
|
||||
@@ -0,0 +1,14 @@
|
||||
#[repr(C)]
|
||||
pub struct TestMultiplyArgs {
|
||||
pub a: u64,
|
||||
pub b: u64,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn test_multiply(args: TestMultiplyArgs) -> u64 {
|
||||
args.a * args.b
|
||||
}
|
||||
|
||||
pub mod exports {
|
||||
pub use super::test_multiply;
|
||||
}
|
||||
@@ -344,6 +344,10 @@ dependencies = [
|
||||
nlohmann_json,
|
||||
openssl,
|
||||
libatomic,
|
||||
|
||||
# NOTE: this is secretly rust-monocrate in a trenchcoat.
|
||||
# See that directory's README for more details.
|
||||
liblixutil_rs,
|
||||
]
|
||||
|
||||
libutil_temp = library(
|
||||
@@ -422,6 +426,8 @@ liblixutil = declare_dependency(
|
||||
libarchive,
|
||||
nlohmann_json,
|
||||
libatomic,
|
||||
# This is secretly rust-monocrate + the generated header.
|
||||
liblixutil_rs,
|
||||
],
|
||||
link_with : libutil
|
||||
)
|
||||
|
||||
@@ -25,8 +25,54 @@ install_headers(config_h, subdir : 'lix')
|
||||
# Subcomponents: these link into artifacts themselves, and have interdependencies.
|
||||
subdir('lix-doc')
|
||||
|
||||
# We manage libutil's Rust components here, because subdir(rust-monocrate)
|
||||
# needs libutil's Rust components, and subdir(libutil) needs rust-monocrate.
|
||||
# FIXME?: Should libutil-rs just be in a separate directory from libutil, then?
|
||||
libutil_rs_sources = files(
|
||||
'libutil/lib.rs',
|
||||
)
|
||||
libutil_rs = static_library(
|
||||
'lixutil_rs',
|
||||
sources : libutil_rs_sources,
|
||||
rust_args : [
|
||||
# Will be empty if we're linking statically.
|
||||
rust_dynamic_args,
|
||||
# Will no-op if we're linking statically.
|
||||
'-C', f'link-arg=-Wl,@soname_arg@,liblixutil_rs.@dylib_suffix@',
|
||||
],
|
||||
)
|
||||
libutil_rs_gen_h = custom_target(
|
||||
'libutil-rs.gen.hh',
|
||||
input : [
|
||||
files('libutil/cbindgen.toml'),
|
||||
libutil_rs_sources,
|
||||
],
|
||||
output : ['libutil-rs.gen.hh'],
|
||||
command : [
|
||||
cbindgen,
|
||||
'--config',
|
||||
'@INPUT0@',
|
||||
'--output',
|
||||
'@OUTPUT0@',
|
||||
'--',
|
||||
'@INPUT1@',
|
||||
],
|
||||
)
|
||||
# For rust-monocrate.
|
||||
liblixutil_rs_internal = declare_dependency(
|
||||
link_with : libutil_rs,
|
||||
)
|
||||
|
||||
subdir('rust-monocrate')
|
||||
|
||||
# NOW we can create the dependency object anything that actually wants to use liblixutil_rs,
|
||||
# because liblixutil_rs is secretly rust-monocrate in a trenchcoat.
|
||||
# `library('lixutil')` and `executable('liblixutil-tests')` depend on this.
|
||||
liblixutil_rs = declare_dependency(
|
||||
link_with : rust_monocrate,
|
||||
sources : [libutil_rs_gen_h],
|
||||
)
|
||||
|
||||
# liblix_doc is actually rust-monocrate in a trenchcoat.
|
||||
liblix_doc = declare_dependency(
|
||||
link_with : rust_monocrate,
|
||||
|
||||
@@ -6,3 +6,4 @@ edition.workspace = true
|
||||
[lib]
|
||||
crate-type = ["staticlib"]
|
||||
lix-doc = { path = "../lix-doc" }
|
||||
lixutil_rs = { path = "../libutil_rs" }
|
||||
|
||||
@@ -10,6 +10,7 @@ rust_monocrate = static_library(
|
||||
sources : rust_monocrate_sources,
|
||||
dependencies : [
|
||||
liblix_doc_internal,
|
||||
liblixutil_rs_internal,
|
||||
rnix,
|
||||
rowan,
|
||||
],
|
||||
|
||||
@@ -10,3 +10,4 @@
|
||||
//!
|
||||
//! It re-exports all the symbols that should be exported to C++.
|
||||
pub use lix_doc::*;
|
||||
pub use lixutil_rs::*;
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "lix/libutil-rs.gen.hh"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace nix {
|
||||
TEST(rustSupport, testMultiplyArgs) {
|
||||
TestMultiplyArgs const args{
|
||||
.a = 20,
|
||||
.b = 5,
|
||||
};
|
||||
|
||||
uint64_t const product = test_multiply(args);
|
||||
|
||||
ASSERT_EQ(product, 20 * 5);
|
||||
}
|
||||
}
|
||||
@@ -69,6 +69,7 @@ libutil_tests_sources = files(
|
||||
'libutil/paths-setting.cc',
|
||||
'libutil/pool.cc',
|
||||
'libutil/references.cc',
|
||||
'libutil/rust.cc',
|
||||
'libutil/serialise.cc',
|
||||
'libutil/suggestions.cc',
|
||||
'libutil/tarfile.cc',
|
||||
|
||||
Reference in New Issue
Block a user