main.rs (5153B)
1 //! Source-owned native generation; no hand-edited generated bindings. 2 use anyhow::{Context, Result, ensure}; 3 use camino::Utf8PathBuf; 4 use clap::{Parser, Subcommand, ValueEnum}; 5 use std::fs; 6 use uniffi_bindgen::bindings::{GenerateOptions, TargetLanguage}; 7 use uniffi_bindgen::interface::AsType; 8 9 mod kotlin; 10 mod swift; 11 12 #[derive(Parser)] 13 #[command(version = "0.32.2+radroots-native-v2")] 14 struct Cli { 15 #[command(subcommand)] 16 command: Command, 17 } 18 #[derive(Subcommand)] 19 enum Command { 20 /// Generate from the actual multi-component native library. 21 Generate { 22 #[arg(long)] 23 library: bool, 24 source: Utf8PathBuf, 25 #[arg(long, value_enum)] 26 language: Language, 27 #[arg(long)] 28 out_dir: Utf8PathBuf, 29 /// Stable unformatted output is required by the source-owned policy. 30 #[arg(long)] 31 no_format: bool, 32 }, 33 } 34 #[derive(Clone, Copy, ValueEnum)] 35 enum Language { 36 Swift, 37 Kotlin, 38 } 39 40 fn main() -> Result<()> { 41 let Command::Generate { 42 library: _, 43 source, 44 language, 45 out_dir, 46 no_format, 47 } = Cli::parse().command; 48 ensure!(no_format, "source-owned generation requires --no-format"); 49 ensure!(!out_dir.exists(), "output directory must be new"); 50 let staging = tempfile::tempdir().context("create generation staging")?; 51 let stage = Utf8PathBuf::from_path_buf(staging.path().to_owned()) 52 .map_err(|_| anyhow::anyhow!("non-UTF8 generation staging"))?; 53 uniffi_bindgen::bindings::generate(GenerateOptions { 54 languages: vec![match language { 55 Language::Swift => TargetLanguage::Swift, 56 Language::Kotlin => TargetLanguage::Kotlin, 57 }], 58 source: source.clone(), 59 out_dir: stage.clone(), 60 format: false, 61 ..Default::default() 62 })?; 63 { 64 let components = uniffi_bindgen::library_mode::find_cis( 65 &source, 66 &uniffi_bindgen::EmptyCrateConfigSupplier, 67 )?; 68 for ci in components { 69 if matches!(language, Language::Swift) { 70 if ci.has_async_fns() { 71 let path = stage.join(format!("{}.swift", ci.namespace())); 72 let header = 73 fs::read_to_string(stage.join(format!("{}FFI.h", ci.namespace())))?; 74 let generated = swift::cancellation(&fs::read_to_string(&path)?, &header)?; 75 fs::write(path, generated)?; 76 } 77 continue; 78 } 79 let methods: Vec<_> = ci 80 .object_definitions() 81 .iter() 82 .filter_map(|object| { 83 object 84 .methods() 85 .into_iter() 86 .find(|method| method.name() == "close") 87 .map(|method| (object.name().to_owned(), method.is_async())) 88 }) 89 .collect(); 90 let mut messages = Vec::new(); 91 for error in ci 92 .enum_definitions() 93 .iter() 94 .filter(|e| ci.is_name_used_as_error(e.name())) 95 { 96 for variant in error.variants() { 97 if let Some(field) = variant.fields().iter().find(|v| v.name() == "message") { 98 ensure!( 99 field.as_type() == uniffi_bindgen::interface::Type::String, 100 "unsupported native error message type" 101 ); 102 messages.push((error.name().to_owned(), variant.name().to_owned())); 103 } 104 } 105 } 106 if methods.is_empty() && messages.is_empty() { 107 continue; 108 } 109 // This producer has no foreign renaming/package configuration. Fail 110 // closed if its source-owned canonical package layout ever changes. 111 let path = stage.join(format!("uniffi/{0}/{0}.kt", ci.namespace())); 112 let original = fs::read_to_string(&path).context("read Kotlin component")?; 113 let generated = kotlin::preserve_close(&original, &methods)?; 114 let generated = kotlin::preserve_error_messages(&generated, &messages)?; 115 fs::write(path, generated)?; 116 } 117 } 118 copy_tree(&stage, &out_dir)?; 119 Ok(()) 120 } 121 122 fn copy_tree(source: &camino::Utf8Path, output: &camino::Utf8Path) -> Result<()> { 123 fs::create_dir(output)?; 124 for entry in fs::read_dir(source)? { 125 let entry = entry?; 126 let destination = output.join( 127 entry 128 .file_name() 129 .to_str() 130 .context("non-UTF8 generated path")?, 131 ); 132 let kind = entry.file_type()?; 133 if kind.is_dir() { 134 let path = Utf8PathBuf::from_path_buf(entry.path()) 135 .map_err(|_| anyhow::anyhow!("non-UTF8 generated path"))?; 136 copy_tree(&path, &destination)?; 137 } else { 138 ensure!(kind.is_file(), "unexpected generated entry type"); 139 fs::copy(entry.path(), destination)?; 140 } 141 } 142 Ok(()) 143 }