Skip to content

Commit

Permalink
Special-case objc2 less in header-translator
Browse files Browse the repository at this point in the history
To prepare for making it optional, which is desired by
objc2-core-foundation and similar crates.
  • Loading branch information
madsmtm committed Dec 19, 2024
1 parent 03e3882 commit 8ba2a96
Show file tree
Hide file tree
Showing 103 changed files with 439 additions and 363 deletions.
13 changes: 13 additions & 0 deletions crates/block2/translation-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
framework = "block"
crate = "block2"
required-crates = []
link = false
skipped = true

macos = "10.6"
maccatalyst = "13.0"
ios = "3.2"
tvos = "9.0"
watchos = "2.0"
visionos = "1.0"
gnustep = true
2 changes: 1 addition & 1 deletion crates/header-translator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ To add a new framework crate, create a new empty crate in [`framework-crates`](.
```toml
framework = "XXX"
crate = "objc2-xxx"
required-dependencies = ["objc2-foundation"]
required-crates = ["objc2", "objc2-foundation"]
macos = "XXX"
maccatalyst = "XXX"
ios = "XXX"
Expand Down
14 changes: 14 additions & 0 deletions crates/header-translator/configs/bitflags.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
framework = "__bitflags__"
crate = "bitflags"
required-crates = []
link = false
skipped = true

# Available everywhere
macos = "10.0"
maccatalyst = "13.0"
ios = "2.0"
tvos = "9.0"
watchos = "2.0"
visionos = "1.0"
gnustep = true
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Fake data
framework = "System"
crate = "objc2-system"
required-dependencies = []
framework = "__builtin__"
crate = "__builtin__"
required-crates = []
link = false
skipped = true

# Available everywhere
macos = "10.0"
maccatalyst = "13.0"
ios = "2.0"
Expand All @@ -10,13 +13,8 @@ watchos = "2.0"
visionos = "1.0"
gnustep = true

# Both a protocol and a class, so we use Swift's naming scheme
protocol.NSObject.renamed = "NSObjectProtocol"

# Return type `oneway void`
class.NSObject.methods.release.skipped = true

# TODO: Move these

# `ns_consumed`, `cf_consumed` and `os_consumed`
fn.IOServiceGetMatchingService.skipped = true
fn.IOServiceGetMatchingServices.skipped = true
Expand Down
14 changes: 14 additions & 0 deletions crates/header-translator/configs/core.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
framework = "__core__"
crate = "core"
required-crates = []
link = false
skipped = true

# Available everywhere
macos = "10.0"
maccatalyst = "13.0"
ios = "2.0"
tvos = "9.0"
watchos = "2.0"
visionos = "1.0"
gnustep = true
14 changes: 14 additions & 0 deletions crates/header-translator/configs/libc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
framework = "__libc__"
crate = "libc"
required-crates = []
link = false
skipped = true

# Available everywhere
macos = "10.0"
maccatalyst = "13.0"
ios = "2.0"
tvos = "9.0"
watchos = "2.0"
visionos = "1.0"
gnustep = true
81 changes: 48 additions & 33 deletions crates/header-translator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,45 @@ use crate::{ItemIdentifier, Location};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
pub libraries: BTreeMap<String, LibraryConfig>,
pub system: LibraryConfig,
}

fn uses_system_config(library_name: &str) -> bool {
matches!(
library_name,
"System" | "bitflags" | "block2" | "libc" | "objc2"
)
libraries: BTreeMap<String, LibraryConfig>,
}

impl Config {
pub fn library(&self, library_name: &str) -> &LibraryConfig {
if uses_system_config(library_name) {
&self.system
} else {
self.libraries.get(library_name).unwrap_or_else(|| {
error!("tried to get library config from {library_name:?}");
&self.system
})
pub fn new(
mut libraries: BTreeMap<String, LibraryConfig>,
) -> Result<Self, Box<dyn Error + Send + Sync>> {
let configs_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("configs");

let builtin_files = ["bitflags.toml", "builtin.toml", "core.toml", "libc.toml"];

for builtin_file in builtin_files {
let path = configs_dir.join(builtin_file);
let config: LibraryConfig = basic_toml::from_str(&fs::read_to_string(path)?)?;
libraries.insert(config.framework.clone(), config);
}

Ok(Self { libraries })
}

pub fn library_from_crate(&self, krate: &str) -> &LibraryConfig {
if uses_system_config(krate) {
&self.system
} else {
pub fn library(&self, library_name: &str) -> &LibraryConfig {
self.libraries.get(library_name).unwrap_or_else(|| {
error!("tried to get library config from {library_name:?}");
self.libraries
.values()
.find(|lib| lib.krate == krate)
.unwrap_or_else(|| {
error!("tried to get library config from krate {krate:?}");
&self.system
})
}
.get("__builtin__")
.expect("could not find builtin library")
})
}

pub fn library_from_crate(&self, krate: &str) -> &LibraryConfig {
self.libraries
.values()
.find(|lib| lib.krate == krate)
.unwrap_or_else(|| {
error!("tried to get library config from krate {krate:?}");
self.libraries
.get("__builtin__")
.expect("could not find builtin library")
})
}

pub fn replace_protocol_name(&self, id: ItemIdentifier) -> ItemIdentifier {
Expand All @@ -61,6 +65,13 @@ impl Config {
.unwrap_or(name)
})
}

pub fn to_parse(&self) -> impl Iterator<Item = (&str, &LibraryConfig)> + Clone {
self.libraries
.iter()
.filter(|(_, data)| !data.skipped)
.map(|(name, data)| (&**name, data))
}
}

fn get_version<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<Version>, D::Error> {
Expand Down Expand Up @@ -118,12 +129,19 @@ pub struct LibraryConfig {
/// want a feature for something as fundamental as `NSString`.
/// Additionally, it is used for things like `MetalKit` always wanting
/// `Metal` enabled.
#[serde(rename = "required-dependencies")]
pub required_dependencies: HashSet<String>,
#[serde(rename = "required-crates")]
pub required_crates: HashSet<String>,
#[serde(rename = "custom-lib-rs")]
#[serde(default)]
pub custom_lib_rs: bool,

#[serde(default = "link_default")]
pub link: bool,
/// Whether we will attempt to parse and emit the library
/// (used for built-in modules).
#[serde(default)]
pub skipped: bool,

#[serde(default)]
#[serde(deserialize_with = "get_version")]
pub macos: Option<Version>,
Expand All @@ -145,9 +163,6 @@ pub struct LibraryConfig {
#[serde(default)]
pub gnustep: bool,

#[serde(default = "link_default")]
pub link: bool,

/// Data about an external class or protocol whose header isn't imported.
///
/// I.e. a bare `@protocol X;` or `@class X;`.
Expand Down
Loading

0 comments on commit 8ba2a96

Please sign in to comment.