Skip to content

Commit 02e64df

Browse files
committed
simplify
1 parent 383b521 commit 02e64df

3 files changed

Lines changed: 25 additions & 50 deletions

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <clang/AST/Mangle.h>
88
#include <clang/AST/ParentMapContext.h>
99
#include <clang/Basic/SourceManager.h>
10+
#include <llvm/Support/Path.h>
1011

1112
#include <algorithm>
1213
#include <array>

‎cpp2rust/cpp_rule_preprocessor.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <clang/AST/RecursiveASTVisitor.h>
1010
#include <clang/ASTMatchers/ASTMatchFinder.h>
1111
#include <clang/ASTMatchers/ASTMatchers.h>
12+
#include <clang/Frontend/ASTUnit.h>
1213
#include <clang/Frontend/FrontendActions.h>
1314
#include <clang/Sema/Initialization.h>
1415
#include <clang/Sema/Lookup.h>

‎rule-preprocessor/src/semantic.rs‎

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,22 @@ fn build_rustc_args(crate_root: &Path) -> Vec<String> {
6060

6161
for dep in &[
6262
"libcc2rs",
63+
"libcc2rs_macros",
6364
"libc",
6465
"brotli_sys",
6566
"rustls_ffi",
6667
"nix",
6768
"jiff",
6869
"xattr",
6970
] {
70-
if let Some(rlib) = find_artifact(&build_dir, dep, ArtifactKind::Rlib) {
71+
if let Some(lib) = find_artifact(&build_dir, dep) {
7172
args.push("--extern".to_string());
72-
args.push(format!("{}={}", dep, rlib.display()));
73-
} else if let Some(ref deps) = legacy_deps {
74-
if let Some(rlib) = find_artifact_in_dir(deps, dep, ArtifactKind::Rlib) {
75-
args.push("--extern".to_string());
76-
args.push(format!("{}={}", dep, rlib.display()));
77-
}
78-
}
79-
}
80-
81-
// Proc-macro crates needed as transitive dependencies
82-
for dep in &["libcc2rs_macros"] {
83-
if let Some(dylib) = find_artifact(&build_dir, dep, ArtifactKind::ProcMacro) {
73+
args.push(format!("{}={}", dep, lib.display()));
74+
} else if let Some(ref deps) = legacy_deps
75+
&& let Some(lib) = find_artifact_in_dir(deps, dep)
76+
{
8477
args.push("--extern".to_string());
85-
args.push(format!("{}={}", dep, dylib.display()));
86-
} else if let Some(ref deps) = legacy_deps {
87-
if let Some(dylib) = find_artifact_in_dir(deps, dep, ArtifactKind::ProcMacro) {
88-
args.push("--extern".to_string());
89-
args.push(format!("{}={}", dep, dylib.display()));
90-
}
78+
args.push(format!("{}={}", dep, lib.display()));
9179
}
9280
}
9381

@@ -133,58 +121,43 @@ fn find_all_out_dirs(build_dir: &Path) -> Vec<PathBuf> {
133121
out_dirs
134122
}
135123

136-
#[derive(Clone, Copy, PartialEq)]
137-
enum ArtifactKind {
138-
Rlib,
139-
ProcMacro,
140-
}
141-
142124
/// Find an artifact in the new Cargo build layout: build/<pkg>/<hash>/out/
143-
fn find_artifact(build_dir: &Path, crate_name: &str, kind: ArtifactKind) -> Option<PathBuf> {
125+
fn find_artifact(build_dir: &Path, crate_name: &str) -> Option<PathBuf> {
144126
// Package directory name uses hyphens where crate name uses underscores
145127
let pkg_name = crate_name.replace('_', "-");
146128
let pkg_dir = build_dir.join(&pkg_name);
147129
if let Ok(entries) = std::fs::read_dir(&pkg_dir) {
148130
for entry in entries.flatten() {
149131
let out_dir = entry.path().join("out");
150-
if out_dir.is_dir() {
151-
if let Some(path) = find_artifact_in_dir(&out_dir, crate_name, kind) {
152-
return Some(path);
153-
}
132+
if out_dir.is_dir()
133+
&& let Some(path) = find_artifact_in_dir(&out_dir, crate_name)
134+
{
135+
return Some(path);
154136
}
155137
}
156138
}
157139
None
158140
}
159141

160142
/// Find an artifact by crate name in a specific directory.
161-
fn find_artifact_in_dir(dir: &Path, crate_name: &str, kind: ArtifactKind) -> Option<PathBuf> {
162-
let prefixes = if crate_name.starts_with("lib") {
163-
vec![format!("{}-", crate_name), format!("lib{}-", crate_name)]
164-
} else {
165-
vec![format!("lib{}-", crate_name)]
166-
};
143+
fn find_artifact_in_dir(dir: &Path, crate_name: &str) -> Option<PathBuf> {
144+
let prefixes = [format!("{}-", crate_name), format!("lib{}-", crate_name)];
167145
let mut fallback = None;
168146
if let Ok(entries) = std::fs::read_dir(dir) {
169147
for entry in entries.flatten() {
170148
let name = entry.file_name().to_string_lossy().to_string();
171149
if !prefixes.iter().any(|p| name.starts_with(p)) {
172150
continue;
173151
}
174-
match kind {
175-
ArtifactKind::Rlib => {
176-
if name.ends_with(".rmeta") {
177-
return Some(entry.path());
178-
}
179-
if name.ends_with(".rlib") {
180-
fallback = Some(entry.path());
181-
}
182-
}
183-
ArtifactKind::ProcMacro => {
184-
if name.ends_with(".so") || name.ends_with(".dylib") || name.ends_with(".dll") {
185-
return Some(entry.path());
186-
}
187-
}
152+
if name.ends_with(".rmeta")
153+
|| name.ends_with(".so")
154+
|| name.ends_with(".dylib")
155+
|| name.ends_with(".dll")
156+
{
157+
return Some(entry.path());
158+
}
159+
if name.ends_with(".rlib") {
160+
fallback = Some(entry.path());
188161
}
189162
}
190163
}

0 commit comments

Comments
 (0)