Examples found in repository?
src/lib.rs (line 46)
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 198
pub fn frb_codegen(config: &config::Opts, all_symbols: &[String]) -> anyhow::Result<()> {
let dart_root = config.dart_root_or_default();
ensure_tools_available(&dart_root, config.skip_deps_check)?;
info!("Picked config: {:?}", config);
let rust_output_dir = Path::new(&config.rust_output_path).parent().unwrap();
let dart_output_dir = Path::new(&config.dart_output_path).parent().unwrap();
info!("Phase: Parse source code to AST, then to IR");
let raw_ir_file = config.get_ir_file()?;
info!("Phase: Transform IR");
let ir_file = transformer::transform(raw_ir_file);
info!("Phase: Generate Rust code");
fs::create_dir_all(rust_output_dir)?;
let generated_rust = ir_file.generate_rust(config);
write_rust_modules(config, &generated_rust)?;
info!("Phase: Generate Dart code");
let generated_dart = ir_file.generate_dart(config, &generated_rust.wasm_exports);
run!(
commands::format_rust,
&config.rust_output_path,
(
config.wasm_enabled && !config.inline_rust,
config.rust_io_output_path(),
config.rust_wasm_output_path(),
)
)?;
if !config.skip_add_mod_to_lib {
others::try_add_mod_to_lib(&config.rust_crate_dir, &config.rust_output_path);
}
info!("Phase: Generating Dart bindings for Rust");
let temp_dart_wire_file = tempfile::NamedTempFile::new()?;
let temp_bindgen_c_output_file = tempfile::Builder::new().suffix(".h").tempfile()?;
let exclude_symbols = generated_rust.get_exclude_symbols(all_symbols);
with_changed_file(
&config.rust_output_path,
DUMMY_WIRE_CODE_FOR_BINDGEN,
|| {
commands::bindgen_rust_to_dart(
BindgenRustToDartArg {
rust_crate_dir: &config.rust_crate_dir,
c_output_path: temp_bindgen_c_output_file
.path()
.as_os_str()
.to_str()
.unwrap(),
dart_output_path: temp_dart_wire_file.path().as_os_str().to_str().unwrap(),
dart_class_name: &config.dart_wire_class_name(),
c_struct_names: ir_file.get_c_struct_names(),
exclude_symbols,
llvm_install_path: &config.llvm_path[..],
llvm_compiler_opts: &config.llvm_compiler_opts,
},
&dart_root,
)
},
)?;
let effective_func_names = [
generated_rust.extern_func_names,
EXTRA_EXTERN_FUNC_NAMES.to_vec(),
]
.concat();
let c_dummy_code = generator::c::generate_dummy(&effective_func_names);
for output in &config.c_output_path {
fs::create_dir_all(Path::new(output).parent().unwrap())?;
fs::write(
output,
fs::read_to_string(&temp_bindgen_c_output_file)? + "\n" + &c_dummy_code,
)?;
}
fs::create_dir_all(dart_output_dir)?;
let generated_dart_wire_code_raw = fs::read_to_string(temp_dart_wire_file)?;
let generated_dart_wire = extract_dart_wire_content(&modify_dart_wire_content(
&generated_dart_wire_code_raw,
&config.dart_wire_class_name(),
));
sanity_check(&generated_dart_wire.body, &config.dart_wire_class_name())?;
let generated_dart_decl_all = &generated_dart.decl_code;
let generated_dart_impl_io_wire = &generated_dart.impl_code.io + &generated_dart_wire;
if let Some(dart_decl_output_path) = &config.dart_decl_output_path {
write_dart_decls(
config,
dart_decl_output_path,
dart_output_dir,
&generated_dart,
generated_dart_decl_all,
&generated_dart_impl_io_wire,
)?;
} else if config.wasm_enabled {
fs::write(
&config.dart_output_path,
(&generated_dart.file_prelude
+ generated_dart_decl_all
+ &generated_dart.impl_code.common)
.to_text(),
)?;
fs::write(
config.dart_io_output_path(),
(&generated_dart.file_prelude + &generated_dart_impl_io_wire).to_text(),
)?;
fs::write(
config.dart_wasm_output_path(),
(&generated_dart.file_prelude + &generated_dart.impl_code.wasm).to_text(),
)?;
} else {
let mut out = generated_dart.file_prelude
+ generated_dart_decl_all
+ &generated_dart.impl_code.common
+ &generated_dart_impl_io_wire;
out.import = out.import.lines().unique().join("\n");
fs::write(&config.dart_output_path, out.to_text())?;
}
info!("Phase: Running build_runner");
let dart_root = &config.dart_root;
if generated_dart.needs_freezed && config.build_runner {
let dart_root = dart_root.as_ref().ok_or_else(|| {
Error::str(
"build_runner configured to run, but Dart root could not be inferred.
Please specify --dart-root, or disable build_runner with --no-build-runner.",
)
})?;
commands::build_runner(dart_root)?;
}
info!("Phase: Formatting Dart code");
run!(
commands::format_dart[config.dart_format_line_length],
&config.dart_output_path,
?config.dart_decl_output_path,
(
config.wasm_enabled,
config.dart_wasm_output_path(),
config.dart_io_output_path(),
),
(
generated_dart.needs_freezed && config.build_runner,
config.dart_freezed_path(),
)
)?;
info!("Success!");
Ok(())
}