Skip to content

Commit

Permalink
Refactor translation config files
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed May 2, 2024
1 parent 13b81fc commit 5553f58
Show file tree
Hide file tree
Showing 67 changed files with 1,511 additions and 1,940 deletions.
104 changes: 63 additions & 41 deletions crates/header-translator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,41 @@ use std::fmt;
use std::fs;
use std::path::Path;

use heck::ToTrainCase;
use semver::Version;
use serde::Deserialize;

use crate::data;
use crate::id::Location;
use crate::stmt::{Derives, Mutability};
use crate::{data, ItemIdentifier};

#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
#[serde(rename = "class")]
#[serde(default)]
pub class_data: HashMap<String, ClassData>,
#[serde(rename = "protocol")]
#[serde(default)]
pub protocol_data: HashMap<String, ProtocolData>,
#[serde(rename = "struct")]
#[serde(default)]
pub struct_data: HashMap<String, StructData>,
#[serde(rename = "enum")]
#[serde(default)]
pub enum_data: HashMap<String, EnumData>,
#[serde(rename = "fn")]
#[serde(default)]
pub fns: HashMap<String, FnData>,
#[serde(rename = "static")]
#[serde(default)]
pub statics: HashMap<String, StaticData>,
#[serde(rename = "typedef")]
#[serde(default)]
pub typedef_data: HashMap<String, TypedefData>,
#[serde(rename = "library")]
#[serde(default)]
pub libraries: BTreeMap<String, LibraryData>,
pub libraries: BTreeMap<String, LibraryConfig>,
pub system: LibraryConfig,
}

impl Config {
pub fn replace_protocol_name(&self, name: String) -> String {
self.protocol_data
.get(&name)
.and_then(|data| data.renamed.clone())
.unwrap_or(name)
pub fn library(&self, location: impl AsRef<Location>) -> &LibraryConfig {
let location = location.as_ref();
if location.library == "System" {
return &self.system;
}
self.libraries.get(&location.library).unwrap_or_else(|| {
error!("tried to get library config from {location:?}");
&self.system
})
}

pub fn replace_protocol_name(&self, id: ItemIdentifier) -> ItemIdentifier {
let library_config = self.library(&id);
id.map_name(|name| {
library_config
.protocol_data
.get(&name)
.and_then(|data| data.renamed.clone())
.unwrap_or(name)
})
}
}

Expand Down Expand Up @@ -84,7 +78,9 @@ fn get_version<'de, D: serde::Deserializer<'de>>(

#[derive(Deserialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct LibraryData {
pub struct LibraryConfig {
#[serde(rename = "framework")]
pub framework: String,
#[serde(rename = "crate")]
pub krate: String,
/// Dependencies are optional by default, this can be used to make a
Expand All @@ -99,6 +95,7 @@ pub struct LibraryData {
#[serde(rename = "custom-lib-rs")]
#[serde(default)]
pub custom_lib_rs: bool,

#[serde(default)]
#[serde(deserialize_with = "get_version")]
pub macos: Option<Version>,
Expand All @@ -119,6 +116,28 @@ pub struct LibraryData {
pub visionos: Option<Version>,
#[serde(default)]
pub gnustep: bool,

#[serde(rename = "class")]
#[serde(default)]
pub class_data: HashMap<String, ClassData>,
#[serde(rename = "protocol")]
#[serde(default)]
pub protocol_data: HashMap<String, ProtocolData>,
#[serde(rename = "struct")]
#[serde(default)]
pub struct_data: HashMap<String, StructData>,
#[serde(rename = "enum")]
#[serde(default)]
pub enum_data: HashMap<String, EnumData>,
#[serde(rename = "fn")]
#[serde(default)]
pub fns: HashMap<String, FnData>,
#[serde(rename = "static")]
#[serde(default)]
pub statics: HashMap<String, StaticData>,
#[serde(rename = "typedef")]
#[serde(default)]
pub typedef_data: HashMap<String, TypedefData>,
}

#[derive(Deserialize, Debug, Default, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -260,19 +279,22 @@ impl Default for MethodData {
}
}

impl Config {
impl LibraryConfig {
pub fn from_file(file: &Path) -> Result<Self, Box<dyn Error>> {
let s = fs::read_to_string(file)?;

let mut config: Self = basic_toml::from_str(&s)?;

for (name, data) in &config.libraries {
assert_eq!(
name.to_lowercase(),
data.krate.replace("objc2-", "").replace('-', ""),
"crate name had an unexpected format",
);
}
assert_eq!(
config.framework.to_lowercase(),
config.krate.replace("objc2-", "").replace('-', ""),
"crate name had an unexpected format",
);
assert_eq!(
Some(&*config.framework.to_train_case().to_lowercase()),
config.krate.strip_prefix("objc2-"),
"crate name had an unexpected format",
);

data::apply_tweaks(&mut config);

Expand Down
File renamed without changes.
42 changes: 14 additions & 28 deletions crates/header-translator/src/data/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,33 @@
macro_rules! data {
// Parse module declarations
(
@($($parsed_module:ident)*)

mod $module:ident;

$($rest:tt)*
) => {
data! {
@($($parsed_module)* $module)

$($rest)*
}
};
(
@($($module:ident)*)

$($rest:tt)*
$(
mod $module:ident;
)+
) => {
$(
#[allow(non_snake_case)]
mod $module;
)*
)+

#[allow(unused_variables)]
pub(crate) fn apply_tweaks(config: &mut $crate::config::Config) {
pub(crate) fn apply_tweaks(data: &mut $crate::config::LibraryConfig) {
$(
$module::apply_tweaks(config);
)*

if data.framework == stringify!($module) {
$module::apply_tweaks(data);
}
)+
}
};
($($rest:tt)*) => {
#[allow(unused_variables)]
pub(crate) fn apply_tweaks(config: &mut $crate::config::LibraryConfig) {
__data_inner! {
@(config)
$($rest)*
}
}
};
($($rest:tt)*) => {
data! {
@()

$($rest)*
}
};
}

macro_rules! __set_mutability {
Expand Down
4 changes: 2 additions & 2 deletions crates/header-translator/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data! {
mod ClassKit;
mod CloudKit;
mod Contacts;
mod CoreAnimation;
mod QuartzCore;
mod CoreData;
mod CoreLocation;
mod CoreWLAN;
Expand Down Expand Up @@ -47,7 +47,7 @@ data! {
mod MetalKit;
mod MetricKit;
mod OSAKit;
mod PhotoKit;
mod Photos;
mod ServiceManagement;
mod SoundAnalysis;
mod Speech;
Expand Down
4 changes: 2 additions & 2 deletions crates/header-translator/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,13 @@ impl AsRef<Self> for Location {
}
}

impl AsRef<Self> for ItemIdentifier {
impl<N> AsRef<Self> for ItemIdentifier<N> {
fn as_ref(&self) -> &Self {
self
}
}

impl AsRef<Location> for ItemIdentifier {
impl<N> AsRef<Location> for ItemIdentifier<N> {
fn as_ref(&self) -> &Location {
&self.location
}
Expand Down
2 changes: 1 addition & 1 deletion crates/header-translator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod stmt;
mod thread_safety;
mod unexposed_attr;

pub use self::config::{Config, LibraryData};
pub use self::config::{Config, LibraryConfig};
pub use self::context::Context;
pub use self::file::File;
pub use self::global_analysis::global_analysis;
Expand Down
6 changes: 3 additions & 3 deletions crates/header-translator/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::path::Path;
use toml_edit::InlineTable;
use toml_edit::{value, Array, DocumentMut, Formatted, Item, Table, Value};

use crate::config::LibraryData;
use crate::config::LibraryConfig;
use crate::display_helper::FormatterFn;
use crate::file::clean_name;
use crate::file::File;
Expand All @@ -22,11 +22,11 @@ use crate::VERSION;
pub struct Library {
pub files: BTreeMap<String, File>,
link_name: String,
pub data: LibraryData,
pub data: LibraryConfig,
}

impl Library {
pub fn new(name: &str, data: &LibraryData) -> Self {
pub fn new(name: &str, data: &LibraryConfig) -> Self {
Self {
files: BTreeMap::new(),
link_name: name.to_string(),
Expand Down
32 changes: 26 additions & 6 deletions crates/header-translator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tracing_subscriber::util::SubscriberInitExt;
use tracing_tree::HierarchicalLayer;

use header_translator::{
global_analysis, run_cargo_fmt, Config, Context, File, Library, LibraryData, Stmt,
global_analysis, run_cargo_fmt, Config, Context, File, Library, LibraryConfig, Stmt,
};

type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
Expand Down Expand Up @@ -47,7 +47,7 @@ fn main() -> Result<(), BoxError> {
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let workspace_dir = manifest_dir.parent().unwrap().parent().unwrap();

let config = load_config(manifest_dir);
let config = load_config(workspace_dir)?;

clang_sys::load()?;
info!(clang_version = clang::get_version());
Expand Down Expand Up @@ -188,10 +188,30 @@ fn main() -> Result<(), BoxError> {
Ok(())
}

fn load_config(manifest_dir: &Path) -> Config {
let _span = info_span!("loading config").entered();
fn load_config(workspace_dir: &Path) -> Result<Config, BoxError> {
let _span = info_span!("loading configs").entered();

Config::from_file(&manifest_dir.join("translation-config.toml")).expect("read config")
let mut libraries = BTreeMap::default();

for dir in fs::read_dir(workspace_dir.join("framework-crates"))? {
let dir = dir?;
if !dir.file_type()?.is_dir() {
continue;
}
let path = dir.path().join("translation-config.toml");
let config =
LibraryConfig::from_file(&path).unwrap_or_else(|e| panic!("read {path:?} config: {e}"));
assert_eq!(*config.krate, *dir.file_name());
libraries.insert(config.framework.to_string(), config);
}

let path = workspace_dir
.join("crates")
.join("header-translator")
.join("system-config.toml");
let system = LibraryConfig::from_file(&path).expect("read system config");

Ok(Config { libraries, system })
}

fn parse_sdk(
Expand Down Expand Up @@ -386,7 +406,7 @@ fn update_ci(workspace_dir: &Path, config: &Config) -> io::Result<()> {
mut ci: impl Write,
config: &Config,
env_name: &str,
check: impl Fn(&LibraryData) -> bool,
check: impl Fn(&LibraryConfig) -> bool,
) -> io::Result<()> {
// Use a BTreeSet to sort the libraries
let mut frameworks = BTreeSet::new();
Expand Down
1 change: 1 addition & 0 deletions crates/header-translator/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ impl Method {
| EntityKind::ParmDecl => {
// Ignore
}
// `ns_consumed`, `cf_consumed` and `os_consumed`
EntityKind::NSConsumed => {
error!("found NSConsumed, which requires manual handling");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/header-translator/src/rust_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ impl Ty {
.get_definition()
.expect("objc protocol declaration definition");
let mut decl = ItemRef::new(&definition, context);
decl.id.name = context.replace_protocol_name(decl.id.name);
decl.id = context.replace_protocol_name(decl.id);
decl
})
.collect();
Expand Down
Loading

0 comments on commit 5553f58

Please sign in to comment.