Skip to content

Commit ffd85c9

Browse files
authored
[generator] Throw PlatformNotSupportException in 32-bit mode for 64-bit-only iOS API. Fixes #4689. (#4954)
Throw PlatformNotSupportedException for iOS API that was introduced in iOS 11+ in 32-bit mode, since that API is clearly not available in any 32-bit capable iOS version. This makes the 32-bit version of Xamarin.iOS.dll smaller (from 15.282.176 bytes to 14.575.616 bytes, ~700kb smaller - small enough that this makes the dontlink test work in 32-bit mode again on device). Fixes #4689.
1 parent f96faaa commit ffd85c9

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

src/foundation.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13492,10 +13492,9 @@ interface NSUnitTemperature : NSSecureCoding {
1349213492
NSDimension BaseUnit { get; }
1349313493
}
1349413494

13495-
#if !WATCH && !TVOS
13496-
[Mac (10,8), iOS (11,0), NoWatch, NoTV]
1349713495
partial interface NSFileManager {
1349813496

13497+
[iOS (11, 0), NoTV, NoWatch]
1349913498
[Mac (10, 8), Export ("trashItemAtURL:resultingItemURL:error:")]
1350013499
bool TrashItem (NSUrl url, out NSUrl resultingItemUrl, out NSError error);
1350113500

@@ -13515,7 +13514,6 @@ interface NSFileProviderService
1351513514
[Export ("name")]
1351613515
string Name { get; }
1351713516
}
13518-
#endif
1351913517

1352013518
#if MONOMAC
1352113519
partial interface NSFilePresenter {

src/generator.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3942,6 +3942,22 @@ AvailabilityBaseAttribute GetIntroduced (MethodInfo mi, PropertyInfo pi)
39423942
return mi.GetAvailability (AvailabilityKind.Introduced) ?? pi.GetAvailability (AvailabilityKind.Introduced);
39433943
}
39443944

3945+
bool Is64BitiOSOnly (ICustomAttributeProvider provider)
3946+
{
3947+
if (BindThirdPartyLibrary)
3948+
return false;
3949+
if (BindingTouch.CurrentPlatform != PlatformName.iOS)
3950+
return false;
3951+
var attrib = provider.GetAvailability (AvailabilityKind.Introduced);
3952+
if (attrib == null) {
3953+
var minfo = provider as MemberInfo;
3954+
if (minfo != null && minfo.DeclaringType != null)
3955+
return Is64BitiOSOnly (minfo.DeclaringType);
3956+
return false;
3957+
}
3958+
return attrib.Version.Major >= 11;
3959+
}
3960+
39453961
//
39463962
// Generates the code necessary to lower the MonoTouch-APIs to something suitable
39473963
// to be passed to Objective-C.
@@ -4754,6 +4770,12 @@ void GenerateProperty (Type type, PropertyInfo pi, List<string> instance_fields_
47544770
print ("get; ");
47554771
} else {
47564772
print ("get {");
4773+
var is32BitNotSupported = Is64BitiOSOnly (pi);
4774+
if (is32BitNotSupported) {
4775+
print ("#if ARCH_32");
4776+
print ("\tthrow new PlatformNotSupportedException (\"This API is not supported on this version of iOS\");");
4777+
print ("#else");
4778+
}
47574779
if (debug)
47584780
print ("Console.WriteLine (\"In {0}\");", pi.GetGetMethod ());
47594781
if (is_model)
@@ -4778,6 +4800,8 @@ void GenerateProperty (Type type, PropertyInfo pi, List<string> instance_fields_
47784800
indent--;
47794801
}
47804802
}
4803+
if (is32BitNotSupported)
4804+
print ("#endif");
47814805
print ("}\n");
47824806
}
47834807
}
@@ -4805,6 +4829,12 @@ void GenerateProperty (Type type, PropertyInfo pi, List<string> instance_fields_
48054829
print ("set; ");
48064830
} else {
48074831
print ("set {");
4832+
var is32BitNotSupported = Is64BitiOSOnly (pi);
4833+
if (is32BitNotSupported) {
4834+
print ("#if ARCH_32");
4835+
print ("\tthrow new PlatformNotSupportedException (\"This API is not supported on this version of iOS\");");
4836+
print ("#else");
4837+
}
48084838
if (debug)
48094839
print ("Console.WriteLine (\"In {0}\");", pi.GetSetMethod ());
48104840

@@ -4830,6 +4860,8 @@ void GenerateProperty (Type type, PropertyInfo pi, List<string> instance_fields_
48304860
}
48314861
}
48324862
}
4863+
if (is32BitNotSupported)
4864+
print ("#endif");
48334865
print ("}");
48344866
}
48354867
}
@@ -5213,6 +5245,13 @@ void GenerateMethod (MemberInformation minfo)
52135245
}
52145246

52155247
print ("{");
5248+
5249+
var is32BitNotSupported = Is64BitiOSOnly ((ICustomAttributeProvider) minfo.method ?? minfo.property);
5250+
if (is32BitNotSupported) {
5251+
print ("#if ARCH_32");
5252+
print ("\tthrow new PlatformNotSupportedException (\"This API is not supported on this version of iOS\");");
5253+
print ("#else");
5254+
}
52165255
if (debug)
52175256
print ("\tConsole.WriteLine (\"In {0}\");", mi);
52185257

@@ -5237,6 +5276,8 @@ void GenerateMethod (MemberInformation minfo)
52375276
indent--;
52385277
}
52395278
}
5279+
if (is32BitNotSupported)
5280+
print ("#endif");
52405281
print ("}\n");
52415282
}
52425283

@@ -6177,6 +6218,7 @@ public void Generate (Type type)
61776218
var initSelector = (InlineSelectors || BindThirdPartyLibrary) ? "Selector.GetHandle (\"init\")" : "Selector.Init";
61786219
var initWithCoderSelector = (InlineSelectors || BindThirdPartyLibrary) ? "Selector.GetHandle (\"initWithCoder:\")" : "Selector.InitWithCoder";
61796220
string v = UnifiedAPI && class_mod == "abstract " ? "protected" : ctor_visibility;
6221+
var is32BitNotSupported = Is64BitiOSOnly (type);
61806222
if (external) {
61816223
if (!disable_default_ctor) {
61826224
GeneratedCode (sw, 2);
@@ -6188,12 +6230,19 @@ public void Generate (Type type)
61886230
sw.WriteLine ("\t\t[Export (\"init\")]");
61896231
sw.WriteLine ("\t\t{0} {1} () : base (NSObjectFlag.Empty)", v, TypeName);
61906232
sw.WriteLine ("\t\t{");
6233+
if (is32BitNotSupported) {
6234+
sw.WriteLine ("\t\t#if ARCH_32");
6235+
sw.WriteLine ("\tthrow new PlatformNotSupportedException (\"This API is not supported on this version of iOS\");");
6236+
sw.WriteLine ("\t\t#else");
6237+
}
61916238
if (is_direct_binding_value != null)
61926239
sw.WriteLine ("\t\t\tIsDirectBinding = {0};", is_direct_binding_value);
61936240
if (debug)
61946241
sw.WriteLine ("\t\t\tConsole.WriteLine (\"{0}.ctor ()\");", TypeName);
61956242
sw.WriteLine ("\t\t\tInitializeHandle (global::{1}.IntPtr_objc_msgSend (this.Handle, global::{2}.{0}), \"init\");", initSelector, ns.Messaging, ns.CoreObjCRuntime);
61966243
sw.WriteLine ("\t\t\t");
6244+
if (is32BitNotSupported)
6245+
sw.WriteLine ("\t\t#endif");
61976246
sw.WriteLine ("\t\t}");
61986247
}
61996248
} else {
@@ -6207,12 +6256,19 @@ public void Generate (Type type)
62076256
sw.WriteLine ("\t\t[Export (\"init\")]");
62086257
sw.WriteLine ("\t\t{0} {1} () : base (NSObjectFlag.Empty)", v, TypeName);
62096258
sw.WriteLine ("\t\t{");
6259+
if (is32BitNotSupported) {
6260+
sw.WriteLine ("\t\t#if ARCH_32");
6261+
sw.WriteLine ("\tthrow new PlatformNotSupportedException (\"This API is not supported on this version of iOS\");");
6262+
sw.WriteLine ("\t\t#else");
6263+
}
62106264
var indentation = 3;
62116265
WriteIsDirectBindingCondition (sw, ref indentation, is_direct_binding, is_direct_binding_value,
62126266
() => string.Format ("InitializeHandle (global::{1}.IntPtr_objc_msgSend (this.Handle, global::{2}.{0}), \"init\");", initSelector, ns.Messaging, ns.CoreObjCRuntime),
62136267
() => string.Format ("InitializeHandle (global::{1}.IntPtr_objc_msgSendSuper (this.SuperHandle, global::{2}.{0}), \"init\");", initSelector, ns.Messaging, ns.CoreObjCRuntime));
62146268

62156269
WriteMarkDirtyIfDerived (sw, type);
6270+
if (is32BitNotSupported)
6271+
sw.WriteLine ("\t\t#endif");
62166272
sw.WriteLine ("\t\t}");
62176273
sw.WriteLine ();
62186274
}
@@ -6229,6 +6285,11 @@ public void Generate (Type type)
62296285
sw.WriteLine ("\t\t[Export (\"initWithCoder:\")]");
62306286
sw.WriteLine ("\t\t{0} {1} (NSCoder coder) : base (NSObjectFlag.Empty)", UnifiedAPI ? v : "public", TypeName);
62316287
sw.WriteLine ("\t\t{");
6288+
if (is32BitNotSupported) {
6289+
sw.WriteLine ("\t\t#if ARCH_32");
6290+
sw.WriteLine ("\tthrow new PlatformNotSupportedException (\"This API is not supported on this version of iOS\");");
6291+
sw.WriteLine ("\t\t#else");
6292+
}
62326293
if (nscoding) {
62336294
if (debug)
62346295
sw.WriteLine ("\t\t\tConsole.WriteLine (\"{0}.ctor (NSCoder)\");", TypeName);
@@ -6241,6 +6302,8 @@ public void Generate (Type type)
62416302
} else {
62426303
sw.WriteLine ("\t\t\tthrow new InvalidOperationException (\"Type does not conform to NSCoding\");");
62436304
}
6305+
if (is32BitNotSupported)
6306+
sw.WriteLine ("\t\t#endif");
62446307
sw.WriteLine ("\t\t}");
62456308
sw.WriteLine ();
62466309
}
@@ -6250,18 +6313,32 @@ public void Generate (Type type)
62506313
sw.WriteLine ("\t\t[EditorBrowsable (EditorBrowsableState.Advanced)]");
62516314
sw.WriteLine ("\t\t{0} {1} (NSObjectFlag t) : base (t)", UnifiedAPI ? "protected" : "public", TypeName);
62526315
sw.WriteLine ("\t\t{");
6316+
if (is32BitNotSupported) {
6317+
sw.WriteLine ("\t\t#if ARCH_32");
6318+
sw.WriteLine ("\t\t\tthrow new PlatformNotSupportedException (\"This API is not supported on this version of iOS\");");
6319+
sw.WriteLine ("\t\t#else");
6320+
}
62536321
if (is_direct_binding_value != null)
62546322
sw.WriteLine ("\t\t\tIsDirectBinding = {0};", is_direct_binding_value);
62556323
WriteMarkDirtyIfDerived (sw, type);
6324+
if (is32BitNotSupported)
6325+
sw.WriteLine ("\t\t#endif");
62566326
sw.WriteLine ("\t\t}");
62576327
sw.WriteLine ();
62586328
}
62596329
GeneratedCode (sw, 2);
62606330
sw.WriteLine ("\t\t[EditorBrowsable (EditorBrowsableState.Advanced)]");
62616331
sw.WriteLine ("\t\t{0} {1} (IntPtr handle) : base (handle)", UnifiedAPI ? "protected internal" : "public", TypeName);
62626332
sw.WriteLine ("\t\t{");
6333+
if (is32BitNotSupported) {
6334+
sw.WriteLine ("\t\t#if ARCH_32");
6335+
sw.WriteLine ("\t\t\tthrow new PlatformNotSupportedException (\"This API is not supported on this version of iOS\");");
6336+
sw.WriteLine ("\t\t#else");
6337+
}
62636338
if (is_direct_binding_value != null)
62646339
sw.WriteLine ("\t\t\tIsDirectBinding = {0};", is_direct_binding_value);
6340+
if (is32BitNotSupported)
6341+
sw.WriteLine ("\t\t#endif");
62656342
WriteMarkDirtyIfDerived (sw, type);
62666343
sw.WriteLine ("\t\t}");
62676344
sw.WriteLine ();

0 commit comments

Comments
 (0)