From 9b32d682b67757393428f849c3d930e08c6b0be4 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Fri, 7 Jun 2019 11:41:57 -0700 Subject: [PATCH] [mtouch] Fix building watch extensions with the watchOS 6.0 SDK. The watchOS 6.0 SDK renames/moves a few symbols, so some magic is required to make things work on both watchOS 6.0 and earlier versions. --- tools/common/CompilerFlags.cs | 18 ++++++++++++++++++ tools/mtouch/Target.cs | 6 ++++++ tools/mtouch/mtouch.cs | 8 ++++++++ 3 files changed, 32 insertions(+) diff --git a/tools/common/CompilerFlags.cs b/tools/common/CompilerFlags.cs index 55b0fcf620..9bc3feb65f 100644 --- a/tools/common/CompilerFlags.cs +++ b/tools/common/CompilerFlags.cs @@ -17,6 +17,7 @@ namespace Xamarin.Utils public HashSet LinkWithLibraries; // X, added to Inputs public HashSet ForceLoadLibraries; // -force_load X, added to Inputs public HashSet OtherFlags; // X + public List InitialOtherFlags; // same as OtherFlags, only that they're the first argument(s) to clang (because order matters!). This is a list to preserve order (fifo). public HashSet Defines; // -DX public HashSet UnresolvedSymbols; // -u X public HashSet SourceFiles; // X, added to Inputs @@ -99,6 +100,13 @@ namespace Xamarin.Utils AddOtherFlag ("-stdlib=libc++"); } + public void AddOtherInitialFlag (string flag) + { + if (InitialOtherFlags == null) + InitialOtherFlags = new List (); + InitialOtherFlags.Add (flag); + } + public void AddOtherFlag (string flag) { if (OtherFlags == null) @@ -221,6 +229,16 @@ namespace Xamarin.Utils { Prepare (); + if (InitialOtherFlags != null) { + var idx = 0; + foreach (var flag in InitialOtherFlags) { + args.Insert (idx, flag); + idx += flag.Length; + args.Insert (idx, " "); + idx++; + } + } + ProcessFrameworksForArguments (args); if (LinkWithLibraries != null) { diff --git a/tools/mtouch/Target.cs b/tools/mtouch/Target.cs index 7bbca6ffa9..41927a90ea 100644 --- a/tools/mtouch/Target.cs +++ b/tools/mtouch/Target.cs @@ -1551,6 +1551,12 @@ namespace Xamarin.Bundler if (App.IsWatchExtension) { mainlib = "libwatchextension.a"; linker_flags.AddOtherFlag (" -e _xamarin_watchextension_main"); + if (App.SdkVersion.Major >= 6) { + // watchOS 6.0's WatchKit contains a WKExtensionMain function, and that's the entry point for Xcode-compiled watch extensions. + // To make watch extensions work on earlier watchOS versions, there's a libWKExtensionMainLegacy.a library with a + // a WKExtensionMain function that does what's needed (Xcode links with this library when deployment target < 6.0). + linker_flags.AddOtherInitialFlag ("-lWKExtensionMainLegacy"); + } } else if (App.IsTVExtension) { mainlib = "libtvextension.a"; } else if (App.IsExtension) { diff --git a/tools/mtouch/mtouch.cs b/tools/mtouch/mtouch.cs index 92329c06d7..0a7198091d 100644 --- a/tools/mtouch/mtouch.cs +++ b/tools/mtouch/mtouch.cs @@ -672,6 +672,14 @@ namespace Xamarin.Bundler sw.WriteLine ("\txamarin_register_modules = xamarin_register_modules_impl;"); sw.WriteLine ("}"); + if (app.Platform == ApplePlatform.WatchOS && app.SdkVersion.Major >= 6) { + sw.WriteLine (); + sw.WriteLine ("extern \"C\" { int WKExtensionMain (int argc, char* argv[]); }"); + sw.WriteLine ("int main (int argc, char *argv[])"); + sw.WriteLine ("{"); + sw.WriteLine ("\treturn WKExtensionMain (argc, argv);"); + sw.WriteLine ("}"); + } } WriteIfDifferent (main_source, sb.ToString (), true); } catch (MonoTouchException) {