1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#![allow(
clippy::inherent_to_string,
clippy::needless_doctest_main,
clippy::new_without_default,
clippy::or_fun_call,
clippy::toplevel_ref_arg
)]
mod cargo;
mod error;
mod gen;
mod out;
mod paths;
mod syntax;
use crate::error::Result;
use crate::gen::error::report;
use crate::gen::{fs, Opt};
use crate::paths::{PathExt, TargetDir};
use cc::Build;
use std::io::{self, Write};
use std::iter;
use std::path::{Path, PathBuf};
use std::process;
#[must_use]
pub fn bridge(rust_source_file: impl AsRef<Path>) -> Build {
bridges(iter::once(rust_source_file))
}
#[must_use]
pub fn bridges(rust_source_files: impl IntoIterator<Item = impl AsRef<Path>>) -> Build {
let ref mut rust_source_files = rust_source_files.into_iter();
build(rust_source_files).unwrap_or_else(|err| {
let _ = writeln!(io::stderr(), "\n\ncxxbridge error: {}\n\n", report(err));
process::exit(1);
})
}
struct Project {
out_dir: PathBuf,
target_dir: TargetDir,
}
impl Project {
fn init() -> Result<Self> {
let out_dir = paths::out_dir()?;
let target_dir = match cargo::target_dir(&out_dir) {
target_dir @ TargetDir::Path(_) => target_dir,
TargetDir::Unknown => paths::search_parents_for_target_dir(&out_dir),
};
Ok(Project {
out_dir,
target_dir,
})
}
}
fn build(rust_source_files: &mut dyn Iterator<Item = impl AsRef<Path>>) -> Result<Build> {
let ref prj = Project::init()?;
let include_dir = paths::include_dir(prj);
let mut build = Build::new();
build.cpp(true);
build.cpp_link_stdlib(None);
build.include(&include_dir);
write_header(prj);
let crate_dir = symlink_crate(prj, &mut build);
for path in rust_source_files {
generate_bridge(prj, &mut build, path.as_ref())?;
}
eprintln!("\nCXX include path:");
eprintln!(" {}", include_dir.display());
if let Some(crate_dir) = crate_dir {
eprintln!(" {}", crate_dir.display());
}
Ok(build)
}
fn write_header(prj: &Project) {
let ref cxx_h = prj.out_dir.join("cxxbridge").join("rust").join("cxx.h");
let _ = out::write(cxx_h, gen::include::HEADER.as_bytes());
if let TargetDir::Path(target_dir) = &prj.target_dir {
let ref header_dir = target_dir.join("cxxbridge").join("rust");
let _ = fs::create_dir_all(header_dir);
let ref cxx_h = header_dir.join("cxx.h");
let _ = out::write(cxx_h, gen::include::HEADER.as_bytes());
}
}
fn symlink_crate(prj: &Project, build: &mut Build) -> Option<PathBuf> {
let manifest_dir = match paths::manifest_dir() {
Some(manifest_dir) => manifest_dir,
None => return None,
};
let package_name = match paths::package_name() {
Some(package_name) => package_name,
None => return None,
};
let mut link = paths::include_dir(prj);
link.push("CRATE");
let _ = out::symlink_dir(manifest_dir, link.join(package_name));
build.include(&link);
Some(link)
}
fn generate_bridge(prj: &Project, build: &mut Build, rust_source_file: &Path) -> Result<()> {
let opt = Opt::default();
let generated = gen::generate_from_path(rust_source_file, &opt);
let ref rel_path = paths::local_relative_path(rust_source_file);
let ref rel_path_h = rel_path.with_appended_extension(".h");
let ref header_path = paths::namespaced(&prj.out_dir, rel_path_h);
out::write(header_path, &generated.header)?;
let ref link_path = paths::namespaced(&prj.out_dir, rel_path);
let _ = out::symlink_file(header_path, link_path);
if let TargetDir::Path(target_dir) = &prj.target_dir {
let ref link_path = paths::namespaced(target_dir, rel_path);
let _ = out::symlink_file(header_path, link_path);
let _ = out::symlink_file(header_path, link_path.with_appended_extension(".h"));
}
let ref rel_path_cc = rel_path.with_appended_extension(".cc");
let ref implementation_path = paths::namespaced(&prj.out_dir, rel_path_cc);
out::write(implementation_path, &generated.implementation)?;
build.file(implementation_path);
Ok(())
}