Skip to content

Commit 565dc3c

Browse files
committed
Handle rs crate dependencies.
Signed-off-by: Tricster <[email protected]>
1 parent d13d5bc commit 565dc3c

File tree

5 files changed

+62
-12
lines changed

5 files changed

+62
-12
lines changed

source/loaders/rs_loader/rust/compiler/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ rustc_private = true
1010
dlopen = "0.1.8"
1111
libffi = "3.0.0"
1212
cargo_toml = "0.11.5"
13-
lazy_static = "1.4.0"
13+
lazy_static = "1.4.0"
14+
itertools = "0.10.3"

source/loaders/rs_loader/rust/compiler/src/lib.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#![feature(box_patterns)]
55
#![feature(let_else)]
66
#![feature(iter_zip)]
7+
// allow us to get file prefix
8+
#![feature(path_file_prefix)]
79
extern crate rustc_ast;
810
extern crate rustc_ast_pretty;
911
extern crate rustc_attr;
@@ -19,13 +21,17 @@ extern crate rustc_session;
1921
extern crate rustc_span;
2022

2123
use dlopen;
24+
use itertools::Itertools;
2225
use rustc_ast::{visit, Impl, Item, ItemKind, VariantData};
2326
use rustc_hir::def::{DefKind, Res};
2427
use rustc_hir::def_id::DefId;
2528
use rustc_interface::{interface::Compiler, Config, Queries};
2629
use rustc_middle::hir::exports::Export;
2730
use rustc_middle::ty::Visibility;
28-
use rustc_session::config::{self, CrateType, ExternEntry, ExternLocation, Externs, Input};
31+
use rustc_session::config::{
32+
self, CrateType, ErrorOutputType, ExternEntry, ExternLocation, Externs, Input,
33+
};
34+
use rustc_session::search_paths::SearchPath;
2935
use rustc_session::utils::CanonicalizedPath;
3036
use rustc_span::source_map;
3137
use std::io::Write;
@@ -393,25 +399,36 @@ impl CompilerCallbacks {
393399
}
394400
fn analyze_metadata<'tcx>(&mut self, queries: &'tcx Queries<'tcx>) {
395401
let mut class_map: HashMap<DefId, Class> = HashMap::new();
396-
let crate_num = queries
402+
let krates = queries
397403
.expansion()
398404
.expect("Unable to get Expansion")
399405
.peek_mut()
400406
.1
401407
.borrow_mut()
402408
.access(|resolver| {
403409
let c_store = resolver.cstore().clone();
404-
c_store
405-
.crates_untracked()
406-
.last()
407-
.cloned()
408-
.expect("Unable to get last element of crates.")
410+
c_store.crates_untracked()
409411
});
410412
queries
411413
.global_ctxt()
412414
.expect("Unable to get global ctxt")
413415
.peek_mut()
414416
.enter(|ctxt| {
417+
// since we are loading a package, input_path should be lib<crate_name>.rlib
418+
let crate_name = &self
419+
.source
420+
.input_path
421+
.file_prefix()
422+
.expect("Unable to get file prefix.")
423+
.to_str()
424+
.expect("Unable to cast OsStr to str")[3..];
425+
// find our krate
426+
let crate_num = krates
427+
.iter()
428+
.find_or_first(|&&x| {
429+
ctxt.crate_name(x) == rustc_span::Symbol::intern(crate_name)
430+
})
431+
.expect("unable to find crate");
415432
// parse public functions and structs
416433
for child in ctxt.item_children(crate_num.as_def_id()) {
417434
let Export {
@@ -466,7 +483,7 @@ impl CompilerCallbacks {
466483
}
467484
}
468485
// after parsing all structs, parse tarit implementations.
469-
for trait_impl in ctxt.all_trait_implementations(crate_num) {
486+
for trait_impl in ctxt.all_trait_implementations(*crate_num) {
470487
use rustc_middle::ty::fast_reject::SimplifiedTypeGen::AdtSimplifiedType;
471488
if let Some(AdtSimplifiedType(def_id)) = trait_impl.1 {
472489
if let Some(class) = class_map.get_mut(&def_id) {
@@ -519,6 +536,19 @@ impl rustc_driver::Callbacks for CompilerCallbacks {
519536
}
520537

521538
config.opts.externs = Externs::new(externs);
539+
// we hardcode the dependency path for now.
540+
let dep_path = self
541+
.source
542+
.input_path
543+
.clone()
544+
.parent()
545+
.expect("Unable to get parent dir")
546+
.join("deps");
547+
// println!("include dep: {}", dep_path.display());
548+
config.opts.search_paths.push(SearchPath::from_cli_opt(
549+
format!("dependency={}", dep_path.display()).as_str(),
550+
ErrorOutputType::default(),
551+
));
522552
// Set up inputs
523553
let wrapped_script_path = self
524554
.source

source/loaders/rs_loader/rust/compiler/src/wrapper/class.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,12 @@ impl ToMetaResult for String {
550550
}
551551
}
552552

553+
impl ToMetaResult for &str {
554+
fn to_meta_result(self) -> Result<MetacallValue> {
555+
Ok(unsafe { metacall_value_create_string(self.as_ptr() as *const i8, self.len()) })
556+
}
557+
}
558+
553559
impl<T> ToMetaResult for Vec<T>
554560
where
555561
T: Clone + ToMetaResult,
@@ -717,6 +723,17 @@ impl FromMeta for String {
717723
}
718724
}
719725

726+
impl FromMeta for &str {
727+
fn from_meta(val: MetacallValue) -> Result<Self> {
728+
Ok(unsafe {
729+
let s = metacall_value_to_string(val);
730+
CStr::from_ptr(s)
731+
.to_str()
732+
.expect("Unable to cast Cstr to str")
733+
})
734+
}
735+
}
736+
720737
impl<T> FromMeta for Vec<T>
721738
where
722739
T: Clone + FromMeta,

source/loaders/rs_loader/rust/compiler/src/wrapper/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ pub fn generate_wrapper(callbacks: CompilerCallbacks) -> std::io::Result<Compile
140140
let source_dir = path.parent().expect("input path has no parent");
141141

142142
// create metacall_class file
143-
println!("create: {:?}", source_dir.join("metacall_class.rs"));
143+
// println!("create: {:?}", source_dir.join("metacall_class.rs"));
144144
let mut class_file = File::create(source_dir.join("metacall_class.rs"))?;
145145
let bytes = include_bytes!("class.rs");
146146
class_file.write_all(bytes)?;
147-
println!("open: {:?}", source_dir.join("metacall_wrapped_package.rs"));
147+
// println!("open: {:?}", source_dir.join("metacall_wrapped_package.rs"));
148148
let mut wrapper_file = std::fs::OpenOptions::new()
149149
.append(true)
150150
.open(source_dir.join("metacall_wrapped_package.rs"))?;

source/loaders/rs_loader/rust/src/lifecycle/initialize.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ pub extern "C" fn rs_loader_impl_initialize(
77
loader_impl: *mut c_void,
88
_config: *mut c_void,
99
) -> *mut c_void {
10-
let boxed_loader_lifecycle_state = Box::new(api::LoaderLifecycleState::new(Vec::new()));
10+
// add current_dir to execution path to allow relative search path
11+
let search_paths = vec![std::env::current_dir().expect("Unable to get current dir")];
12+
let boxed_loader_lifecycle_state = Box::new(api::LoaderLifecycleState::new(search_paths));
1113
compiler::initialize();
1214

1315
api::define_type(

0 commit comments

Comments
 (0)