Skip to content

Commit 87bea37

Browse files
committed
Add support for libc as a framework crate dependency
Part of #500. Fixes #331.
1 parent ac975b2 commit 87bea37

File tree

13 files changed

+70
-37
lines changed

13 files changed

+70
-37
lines changed

Cargo.lock

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/header-translator/src/context.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::{HashMap, HashSet};
1+
use std::collections::HashMap;
22
use std::ops;
33
use std::path::{Path, PathBuf};
44

@@ -13,7 +13,6 @@ pub struct Context<'a> {
1313
pub macro_invocations: HashMap<Location<'a>, Entity<'a>>,
1414
framework_dir: PathBuf,
1515
include_dir: PathBuf,
16-
system_headers: HashSet<&'static Path>,
1716
}
1817

1918
impl<'a> Context<'a> {
@@ -23,12 +22,6 @@ impl<'a> Context<'a> {
2322
macro_invocations: Default::default(),
2423
framework_dir: sdk.path.join("System/Library/Frameworks"),
2524
include_dir: sdk.path.join("usr/include"),
26-
system_headers: HashSet::from([
27-
Path::new("MacTypes.h"),
28-
Path::new("objc/objc.h"),
29-
Path::new("objc/NSObject.h"),
30-
Path::new("objc/NSObjCRuntime.h"),
31-
]),
3225
}
3326
}
3427

@@ -42,9 +35,12 @@ impl<'a> Context<'a> {
4235
if let Ok(path) = path.strip_prefix(&self.framework_dir) {
4336
return Some(split_path(path));
4437
} else if let Ok(path) = path.strip_prefix(&self.include_dir) {
45-
if self.system_headers.contains(path) {
38+
if path.starts_with("objc") || path == Path::new("MacTypes.h") {
4639
return Some(("System".to_string(), None));
4740
}
41+
if path.starts_with("sys") {
42+
return Some(("libc".to_string(), None));
43+
}
4844
}
4945
}
5046
}

crates/header-translator/src/file.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ impl File {
2828
self.stmts.push(stmt);
2929
}
3030

31+
pub fn imports<'c>(&self, config: &'c Config) -> BTreeSet<&'c str> {
32+
self.stmts
33+
.iter()
34+
.flat_map(|stmt| stmt.required_items_inner())
35+
.filter(|item| item.location().library != self.library_name)
36+
// Ignore crate imports for required items from unknown crates
37+
.filter_map(|item| item.location().import(config))
38+
.collect()
39+
}
40+
3141
pub fn crates<'c>(&self, config: &'c Config) -> BTreeSet<&'c str> {
3242
self.stmts
3343
.iter()
@@ -62,16 +72,16 @@ impl File {
6272

6373
writeln!(f, "use objc2::__framework_prelude::*;")?;
6474

65-
let mut crates = self.crates(config);
75+
let mut imports = self.imports(config);
6676
// TODO: Remove this once MainThreadMarker is moved to objc2
67-
crates.extend(
77+
imports.extend(
6878
config.libraries[&self.library_name]
6979
.required_dependencies
7080
.iter()
7181
.map(|krate| &**krate),
7282
);
7383

74-
for krate in crates {
84+
for krate in imports {
7585
let required = config.libraries[&self.library_name]
7686
.required_dependencies
7787
.contains(krate);

crates/header-translator/src/id.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ impl Location {
6767
if self.library == "block2" {
6868
return Some("block2");
6969
}
70+
if self.library == "libc" {
71+
return Some("libc");
72+
}
73+
Some(&config.libraries.get(&self.library)?.krate)
74+
}
75+
76+
pub fn import<'a>(&self, config: &'a Config) -> Option<&'a str> {
77+
if self.library == "block2" {
78+
return Some("block2");
79+
}
80+
if self.library == "libc" {
81+
return None;
82+
}
7083
Some(&config.libraries.get(&self.library)?.krate)
7184
}
7285

@@ -76,7 +89,10 @@ impl Location {
7689

7790
/// Only the library of the emmision location matters.
7891
pub fn cargo_toml_feature(&self, config: &Config, emission_library: &str) -> Option<String> {
79-
if self.library == "System" || self.library == "block2" || self.library == emission_library
92+
if self.library == "System"
93+
|| self.library == "block2"
94+
|| self.library == "libc"
95+
|| self.library == emission_library
8096
{
8197
None
8298
} else if let Some(krate) = self.krate(config) {
@@ -103,6 +119,8 @@ impl Location {
103119
fn feature(&self, config: &Config, emission_location: &Self) -> Option<String> {
104120
if self.library == "System" {
105121
None
122+
} else if self.library == "libc" {
123+
Some("libc".to_string())
106124
} else if self.library == "block2" {
107125
Some("block2".to_string())
108126
} else if self.library == emission_location.library {
@@ -253,12 +271,11 @@ impl ItemIdentifier {
253271

254272
impl fmt::Display for ItemIdentifierPath<'_> {
255273
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
256-
write!(f, "{}", self.0.name)
257-
// if self.0.is_system() {
258-
// write!(f, "{}", self.0.name)
259-
// } else {
260-
// write!(f, "{}::{}", self.0.library, self.0.name)
261-
// }
274+
if self.0.library() == "libc" {
275+
write!(f, "libc::{}", self.0.name)
276+
} else {
277+
write!(f, "{}", self.0.name)
278+
}
262279
}
263280
}
264281

crates/header-translator/src/library.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,19 @@ see that for related crates.", self.data.krate, self.link_name)?;
200200
.collect();
201201

202202
for (krate, required) in &dependencies {
203-
let (path, version) = match *krate {
204-
"block2" => ("../../crates/block2".to_string(), "0.5.0"),
205-
krate => (format!("../{krate}"), VERSION),
203+
let mut table = match *krate {
204+
"block2" => InlineTable::from_iter([
205+
("path", Value::from("../../crates/block2".to_string())),
206+
("version", Value::from("0.5.0")),
207+
]),
208+
// Use a reasonably new version of libc
209+
"libc" => InlineTable::from_iter([("version", Value::from("0.2.80"))]),
210+
krate => InlineTable::from_iter([
211+
("path", Value::from(format!("../{krate}"))),
212+
("version", Value::from(VERSION)),
213+
]),
206214
};
207-
let mut table = InlineTable::from_iter([
208-
("path", Value::from(path)),
209-
("version", Value::from(version)),
210-
]);
211-
if self.data.gnustep {
215+
if self.data.gnustep && *krate != "libc" {
212216
table.insert("default-features", Value::from(false));
213217
}
214218
if !required {

crates/objc2/src/topics/about_generated/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1313
## Unreleased - YYYY-MM-DD
1414

1515
### Added
16+
* Added `CoreBluetooth` framework as `objc2-core-bluetooth`.
17+
* Added `Virtualization` framework as `objc2-virtualization`.
1618
* Added `objc2_quartz_core::CAMetalDrawable` and
1719
`objc2_quartz_core::CAMetalLayer`.
1820
* Added methods to access `CALayer` from `objc2_app_kit::NSView`.
21+
* Added optional support for a few methods depending on types from `libc`.
1922

2023
### Removed
2124
* `objc2_metal`: Removed internal `__MTLPackedFloat3` and made `MTLPackedFloat3` public.

framework-crates/objc2-app-kit/Cargo.toml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

framework-crates/objc2-app-kit/translation-config.toml

-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ class.NSMovie.methods.QTMovie.skipped = true
137137
class.NSTextLayoutFragment.methods."drawAtPoint:inContext:".skipped = true
138138
class.NSTextLineFragment.methods."drawAtPoint:inContext:".skipped = true
139139
class.NSTextView.methods."quickLookPreviewableItemsInRanges:".skipped = true
140-
class.NSRunningApplication.methods.processIdentifier.skipped = true
141-
class.NSRunningApplication.methods."runningApplicationWithProcessIdentifier:".skipped = true
142140
class.NSSavePanel.methods.allowedContentTypes.skipped = true
143141
class.NSSavePanel.methods."setAllowedContentTypes:".skipped = true
144142
class.NSView.methods.backgroundFilters.skipped = true

framework-crates/objc2-foundation/Cargo.toml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

framework-crates/objc2-foundation/translation-config.toml

-4
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ enum.anonymous.constants.NS_BigEndian.skipped = true
210210
class.NSAppleEventDescriptor.methods."descriptorWithDescriptorType:bytes:length:".skipped = true
211211
class.NSAppleEventDescriptor.methods."descriptorWithDescriptorType:data:".skipped = true
212212
class.NSAppleEventDescriptor.methods."appleEventWithEventClass:eventID:targetDescriptor:returnID:transactionID:".skipped = true
213-
class.NSAppleEventDescriptor.methods."descriptorWithProcessIdentifier:".skipped = true
214213
class.NSAppleEventDescriptor.methods."initWithAEDescNoCopy:".skipped = true
215214
class.NSAppleEventDescriptor.methods."initWithDescriptorType:bytes:length:".skipped = true
216215
class.NSAppleEventDescriptor.methods."initWithDescriptorType:data:".skipped = true
@@ -252,9 +251,6 @@ class.NSURLSessionConfiguration.methods."setTLSMinimumSupportedProtocolVersion:"
252251
class.NSURLSessionConfiguration.methods.TLSMaximumSupportedProtocolVersion.skipped = true
253252
class.NSURLSessionConfiguration.methods."setTLSMaximumSupportedProtocolVersion:".skipped = true
254253
class.NSXPCConnection.methods.auditSessionIdentifier.skipped = true
255-
class.NSXPCConnection.methods.processIdentifier.skipped = true
256-
class.NSXPCConnection.methods.effectiveUserIdentifier.skipped = true
257-
class.NSXPCConnection.methods.effectiveGroupIdentifier.skipped = true
258254
class.NSXPCInterface.methods."setXPCType:forSelector:argumentIndex:ofReply:".skipped = true
259255
class.NSXPCInterface.methods."XPCTypeForSelector:argumentIndex:ofReply:".skipped = true
260256
class.NSXPCCoder.methods."encodeXPCObject:forKey:".skipped = true

framework-crates/objc2-metric-kit/Cargo.toml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

framework-crates/objc2-metric-kit/translation-config.toml

-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ maccatalyst = "13.0"
77
ios = "13.0"
88
visionos = "1.0"
99

10-
# Needs `pid_t`
11-
class.MXMetaData.methods.pid.skipped = true
12-
1310
# fails to strip const from `typedef const NSString *const MXLaunchTaskID;`
1411
typedef.MXLaunchTaskID.skipped = true
1512

0 commit comments

Comments
 (0)