protected override string GenerateCommandLineCommands() { var args = new ProcessArgumentBuilder(); TargetArchitecture architectures; bool msym; if (string.IsNullOrEmpty(Architectures) || !Enum.TryParse(Architectures, out architectures)) { architectures = TargetArchitecture.Default; } if (architectures == TargetArchitecture.ARMv6) { Log.LogError("Target architecture ARMv6 is no longer supported in Xamarin.iOS. Please select a supported architecture."); return(null); } if (IsClassic && minimumOSVersion < IPhoneSdkVersion.V3_1 && architectures.HasFlag(TargetArchitecture.ARMv7)) { Log.LogWarning(null, null, null, AppManifest.ItemSpec, 0, 0, 0, 0, "Deployment Target changed from iOS {0} to iOS 3.1 (minimum requirement for ARMv7)", minimumOSVersion); minimumOSVersion = IPhoneSdkVersion.V3_1; } if (!string.IsNullOrEmpty(IntermediateOutputPath)) { Directory.CreateDirectory(IntermediateOutputPath); args.Add("--cache"); args.AddQuoted(Path.GetFullPath(IntermediateOutputPath)); } if (IsClassic || IPhoneSdks.MonoTouch.Version < new IPhoneSdkVersion(8, 5, 0)) { args.Add("--nomanifest"); args.Add("--nosign"); } args.Add(SdkIsSimulator ? "--sim" : "--dev"); args.AddQuoted(Path.GetFullPath(AppBundleDir)); if (AppleSdkSettings.XcodeVersion.Major >= 5 && IPhoneSdks.MonoTouch.Version.CompareTo(new IPhoneSdkVersion(6, 3, 7)) < 0) { args.Add("--compiler", "clang"); } args.Add("--executable"); args.AddQuoted(ExecutableName); if (IsAppExtension) { args.Add("--extension"); } if (Debug) { if (FastDev && IPhoneSdks.MonoTouch.SupportsFastDev) { args.Add("--fastdev"); } args.Add("--debug"); } if (Profiling) { args.Add("--profiling"); } if (LinkerDumpDependencies) { args.Add("--linkerdumpdependencies"); } switch (LinkMode.ToLowerInvariant()) { case "sdkonly": args.Add("--linksdkonly"); break; case "none": args.Add("--nolink"); break; } if (!string.IsNullOrEmpty(I18n)) { args.Add("--i18n"); args.AddQuotedFormat(I18n); } args.Add("--sdkroot"); args.AddQuoted(SdkRoot); args.Add("--sdk"); args.AddQuoted(SdkVersion); if (!minimumOSVersion.IsUseDefault) { args.Add("--targetver"); args.AddQuoted(minimumOSVersion.ToString()); } if (UseFloat32 /* We want to compile 32-bit floating point code to use 32-bit floating point operations */) { args.Add("--aot-options=-O=float32"); } if (IPhoneSdks.MonoTouch.SupportsGenericValueTypeSharing) { if (!EnableGenericValueTypeSharing) { args.Add("--gsharedvt=false"); } } if (LinkDescriptions != null) { foreach (var desc in LinkDescriptions) { args.AddQuoted(string.Format("--xml={0}", desc.ItemSpec)); } } if (EnableBitcode) { switch (Framework) { case PlatformFramework.WatchOS: args.Add("--bitcode=full"); break; case PlatformFramework.TVOS: args.Add("--bitcode=asmonly"); break; default: throw new InvalidOperationException(string.Format("Bitcode is currently not supported on {0}.", Framework)); } } if (!string.IsNullOrEmpty(HttpClientHandler)) { args.Add(string.Format("--http-message-handler={0}", HttpClientHandler)); } if (!string.IsNullOrEmpty(TLSProvider)) { args.Add(string.Format("--tls-provider={0}", TLSProvider.ToLowerInvariant())); } string thumb = UseThumb && UseLlvm ? "+thumb2" : ""; string llvm = UseLlvm ? "+llvm" : ""; string abi = ""; if (SdkIsSimulator) { if (architectures.HasFlag(TargetArchitecture.i386)) { abi += (abi.Length > 0 ? "," : "") + "i386"; } if (architectures.HasFlag(TargetArchitecture.x86_64)) { abi += (abi.Length > 0 ? "," : "") + "x86_64"; } if (string.IsNullOrEmpty(abi)) { architectures = TargetArchitecture.i386; abi = "i386"; } } else { if (architectures == TargetArchitecture.Default) { architectures = TargetArchitecture.ARMv7; } if (architectures.HasFlag(TargetArchitecture.ARMv7)) { abi += (abi.Length > 0 ? "," : "") + "armv7" + llvm + thumb; } if (architectures.HasFlag(TargetArchitecture.ARMv7s)) { abi += (abi.Length > 0 ? "," : "") + "armv7s" + llvm + thumb; } if (architectures.HasFlag(TargetArchitecture.ARM64)) { // Note: ARM64 does not have thumb. abi += (abi.Length > 0 ? "," : "") + "arm64" + llvm; } if (architectures.HasFlag(TargetArchitecture.ARMv7k)) { abi += (abi.Length > 0 ? "," : "") + "armv7k" + llvm; } if (string.IsNullOrEmpty(abi)) { abi = "armv7" + llvm + thumb; } } // Output the CompiledArchitectures CompiledArchitectures = architectures.ToString(); args.Add("--abi=" + abi); // output symbols to preserve when stripping args.Add("--symbollist"); args.AddQuoted(Path.GetFullPath(SymbolsList)); // don't have mtouch generate the dsyms... args.Add("--dsym=no"); if (!string.IsNullOrEmpty(ArchiveSymbols) && bool.TryParse(ArchiveSymbols.Trim(), out msym)) { args.Add("--msym=" + (msym ? "yes" : "no")); } var gcc = new GccOptions(); if (!string.IsNullOrEmpty(ExtraArgs)) { var extraArgs = ProcessArgumentBuilder.Parse(ExtraArgs); var target = MainAssembly.ItemSpec; string projectDir; if (ProjectDir.StartsWith("~/", StringComparison.Ordinal)) { // Note: Since the Visual Studio plugin doesn't know the user's home directory on the Mac build host, // it simply uses paths relative to "~/". Expand these paths to their full path equivalents. var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); projectDir = Path.Combine(home, ProjectDir.Substring(2)); } else { projectDir = ProjectDir; } var customTags = new Dictionary <string, string> (StringComparer.OrdinalIgnoreCase) { { "projectdir", projectDir }, // Apparently msbuild doesn't propagate the solution path, so we can't get it. // { "solutiondir", proj.ParentSolution != null ? proj.ParentSolution.BaseDirectory : proj.BaseDirectory }, { "appbundledir", AppBundleDir }, { "targetpath", Path.Combine(Path.GetDirectoryName(target), Path.GetFileName(target)) }, { "targetdir", Path.GetDirectoryName(target) }, { "targetname", Path.GetFileName(target) }, { "targetext", Path.GetExtension(target) }, }; for (int i = 0; i < extraArgs.Length; i++) { if (extraArgs[i] == "-gcc_flags" || extraArgs[i] == "--gcc_flags") { // user-defined -gcc_flags argument if (i + 1 < extraArgs.Length && !string.IsNullOrEmpty(extraArgs[i + 1])) { var gccArgs = ProcessArgumentBuilder.Parse(extraArgs[i + 1]); for (int j = 0; j < gccArgs.Length; j++) { gcc.Arguments.Add(StringParserService.Parse(gccArgs[j], customTags)); } } i++; } else { // other user-defined mtouch arguments args.AddQuoted(StringParserService.Parse(extraArgs[i], customTags)); } } } BuildNativeReferenceFlags(gcc); BuildEntitlementFlags(gcc); foreach (var framework in gcc.Frameworks) { args.Add("-framework"); args.AddQuoted(framework); } foreach (var framework in gcc.WeakFrameworks) { args.Add("-weak-framework"); args.AddQuoted(framework); } if (gcc.Cxx) { args.Add("--cxx"); } if (gcc.Arguments.Length > 0) { args.Add("--gcc_flags"); args.AddQuoted(gcc.Arguments.ToString()); } foreach (var asm in References) { args.Add("-r"); if (IsFrameworkItem(asm)) { args.AddQuoted(ResolveFrameworkFile(asm.ItemSpec)); } else { args.AddQuoted(Path.GetFullPath(asm.ItemSpec)); } } foreach (var ext in AppExtensionReferences) { args.Add("--app-extension"); args.AddQuoted(Path.GetFullPath(ext.ItemSpec)); } args.Add("--target-framework"); args.Add(TargetFrameworkIdentifier + "," + TargetFrameworkVersion); args.AddQuoted(MainAssembly.ItemSpec); // We give the priority to the ExtraArgs to set the mtouch verbosity. if (string.IsNullOrEmpty(ExtraArgs) || (!string.IsNullOrEmpty(ExtraArgs) && !ExtraArgs.Contains("-q") && !ExtraArgs.Contains("-v"))) { args.Add(GetVerbosityLevel(Verbosity)); } if (!string.IsNullOrWhiteSpace(License)) { args.Add(string.Format("--license={0}", License)); } return(args.ToString()); }
IEnumerable <Config> GetAotConfigs() { if (!Directory.Exists(AotOutputDirectory)) { Directory.CreateDirectory(AotOutputDirectory); } // Check that we have a compatible NDK version for the targeted ABIs. NdkUtil.NdkVersion ndkVersion; bool hasNdkVersion = NdkUtil.GetNdkToolchainRelease(AndroidNdkDirectory, out ndkVersion); var sdkBinDirectory = MonoAndroidHelper.GetOSBinPath(); var abis = SupportedAbis.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (var abi in abis) { string aotCompiler = ""; string outdir = ""; string mtriple = ""; AndroidTargetArch arch; switch (abi) { case "armeabi": aotCompiler = Path.Combine(sdkBinDirectory, "cross-arm"); outdir = Path.Combine(AotOutputDirectory, "armeabi"); mtriple = "armv5-linux-gnueabi"; arch = AndroidTargetArch.Arm; break; case "armeabi-v7a": aotCompiler = Path.Combine(sdkBinDirectory, "cross-arm"); outdir = Path.Combine(AotOutputDirectory, "armeabi-v7a"); mtriple = "armv7-linux-gnueabi"; arch = AndroidTargetArch.Arm; break; case "arm64": case "arm64-v8a": case "aarch64": aotCompiler = Path.Combine(sdkBinDirectory, "cross-arm64"); outdir = Path.Combine(AotOutputDirectory, "arm64-v8a"); mtriple = "aarch64-linux-android"; arch = AndroidTargetArch.Arm64; break; case "x86": aotCompiler = Path.Combine(sdkBinDirectory, "cross-x86"); outdir = Path.Combine(AotOutputDirectory, "x86"); mtriple = "i686-linux-android"; arch = AndroidTargetArch.X86; break; case "x86_64": aotCompiler = Path.Combine(sdkBinDirectory, "cross-x86_64"); outdir = Path.Combine(AotOutputDirectory, "x86_64"); mtriple = "x86_64-linux-android"; arch = AndroidTargetArch.X86_64; break; // case "mips": default: throw new Exception("Unsupported Android target architecture ABI: " + abi); } if (!NdkUtil.ValidateNdkPlatform(Log, AndroidNdkDirectory, arch, enableLLVM:EnableLLVM)) { yield return(Config.Invalid); yield break; } if (!ValidateAotConfiguration(Log, arch, EnableLLVM)) { yield return(Config.Invalid); yield break; } outdir = Path.GetFullPath(outdir); if (!Directory.Exists(outdir)) { Directory.CreateDirectory(outdir); } var toolPrefix = NdkUtil.GetNdkToolPrefix(AndroidNdkDirectory, arch); var toolchainPath = toolPrefix.Substring(0, toolPrefix.LastIndexOf(Path.DirectorySeparatorChar)); var ldFlags = string.Empty; if (EnableLLVM) { int level = GetNdkApiLevel(AndroidNdkDirectory, AndroidApiLevel, arch); string androidLibPath = string.Empty; try { androidLibPath = NdkUtil.GetNdkPlatformLibPath(AndroidNdkDirectory, arch, level); } catch (InvalidOperationException ex) { Diagnostic.Error(5101, ex.Message); } var libs = new List <string>() { GetShortPath(Path.Combine(GetNdkToolchainLibraryDir(toolchainPath), "libgcc.a")), GetShortPath(Path.Combine(androidLibPath, "libc.so")), GetShortPath(Path.Combine(androidLibPath, "libm.so")) }; ldFlags = string.Join(";", libs); } foreach (var assembly in ResolvedAssemblies) { string outputFile = Path.Combine(outdir, string.Format("libaot-{0}.so", Path.GetFileName(assembly.ItemSpec))); string seqpointsFile = Path.Combine(outdir, string.Format("{0}.msym", Path.GetFileName(assembly.ItemSpec))); string tempDir = Path.Combine(outdir, Path.GetFileName(assembly.ItemSpec)); if (!Directory.Exists(tempDir)) { Directory.CreateDirectory(tempDir); } List <string> aotOptions = new List <string> (); if (!string.IsNullOrEmpty(AotAdditionalArguments)) { aotOptions.Add(AotAdditionalArguments); } if (sequencePointsMode == SequencePointsMode.Offline) { aotOptions.Add("msym-dir=" + GetShortPath(outdir)); } if (AotMode != AotMode.Normal) { aotOptions.Add(AotMode.ToString().ToLowerInvariant()); } aotOptions.Add("outfile=" + GetShortPath(outputFile)); aotOptions.Add("asmwriter"); aotOptions.Add("mtriple=" + mtriple); aotOptions.Add("tool-prefix=" + GetShortPath(toolPrefix)); aotOptions.Add("ld-flags=" + ldFlags); aotOptions.Add("llvm-path=" + GetShortPath(sdkBinDirectory)); aotOptions.Add("temp-path=" + GetShortPath(tempDir)); string aotOptionsStr = (EnableLLVM ? "--llvm " : "") + "--aot=" + string.Join(",", aotOptions); if (!string.IsNullOrEmpty(ExtraAotOptions)) { aotOptionsStr += (aotOptions.Count > 0 ? "," : "") + ExtraAotOptions; } // Due to a Monodroid MSBuild bug we can end up with paths to assemblies that are not in the intermediate // assembly directory (typically obj/assemblies). This can lead to problems with the Mono loader not being // able to find their dependency laters, since framework assemblies are stored in different directories. // This can happen when linking is disabled (AndroidLinkMode=None). Workaround this problem by resolving // the paths to the right assemblies manually. var resolvedPath = Path.GetFullPath(assembly.ItemSpec); var intermediateAssemblyPath = Path.Combine(IntermediateAssemblyDir, Path.GetFileName(assembly.ItemSpec)); if (LinkMode.ToLowerInvariant() == "none") { if (!resolvedPath.Contains(IntermediateAssemblyDir) && File.Exists(intermediateAssemblyPath)) { resolvedPath = intermediateAssemblyPath; } } var assembliesPath = Path.GetFullPath(Path.GetDirectoryName(resolvedPath)); var assemblyPath = QuoteFileName(Path.GetFullPath(resolvedPath)); yield return(new Config(assembliesPath, QuoteFileName(aotCompiler), aotOptionsStr, assemblyPath, outputFile)); } } }
protected override string GenerateCommandLineCommands() { var args = GenerateCommandLineArguments(); List <string> unescapedArgs = new List <string> (); TargetArchitecture architectures; if (string.IsNullOrEmpty(Architectures) || !Enum.TryParse(Architectures, out architectures)) { architectures = TargetArchitecture.Default; } if (architectures == TargetArchitecture.ARMv6) { Log.LogError(MSBStrings.E0053); return(null); } args.AddQuotedLine((SdkIsSimulator ? "--sim=" : "--dev=") + Path.GetFullPath(AppBundleDir)); if (AppleSdkSettings.XcodeVersion.Major >= 5 && IPhoneSdks.MonoTouch.Version.CompareTo(new IPhoneSdkVersion(6, 3, 7)) < 0) { args.AddLine("--compiler=clang"); } args.AddQuotedLine($"--executable={ExecutableName}"); if (IsAppExtension) { args.AddLine("--extension"); } if (Debug) { if (FastDev && !SdkIsSimulator) { args.AddLine("--fastdev"); } } if (LinkerDumpDependencies) { args.AddLine("--linkerdumpdependencies"); } if (!string.IsNullOrEmpty(Interpreter)) { args.AddLine($"--interpreter={Interpreter}"); } switch (LinkMode.ToLowerInvariant()) { case "sdkonly": args.AddLine("--linksdkonly"); break; case "none": args.AddLine("--nolink"); break; } args.AddQuotedLine($"--sdk={SdkVersion}"); if (!minimumOSVersion.IsUseDefault) { args.AddQuotedLine($"--targetver={minimumOSVersion.ToString ()}"); } if (UseFloat32 /* We want to compile 32-bit floating point code to use 32-bit floating point operations */) { args.AddLine("--aot-options=-O=float32"); } else { args.AddLine("--aot-options=-O=-float32"); } if (LinkDescriptions != null) { foreach (var desc in LinkDescriptions) { args.AddQuotedLine($"--xml={desc.ItemSpec}"); } } if (EnableBitcode) { switch (Platform) { case ApplePlatform.WatchOS: args.AddLine("--bitcode=full"); break; case ApplePlatform.TVOS: args.AddLine("--bitcode=asmonly"); break; default: throw new InvalidOperationException(string.Format("Bitcode is currently not supported on {0}.", Platform)); } } string thumb = UseThumb && UseLlvm ? "+thumb2" : ""; string llvm = UseLlvm ? "+llvm" : ""; string abi = ""; if (SdkIsSimulator) { if (architectures.HasFlag(TargetArchitecture.i386)) { abi += (abi.Length > 0 ? "," : "") + "i386"; } if (architectures.HasFlag(TargetArchitecture.x86_64)) { abi += (abi.Length > 0 ? "," : "") + "x86_64"; } if (string.IsNullOrEmpty(abi)) { architectures = TargetArchitecture.i386; abi = "i386"; } } else { if (architectures == TargetArchitecture.Default) { architectures = TargetArchitecture.ARMv7; } if (architectures.HasFlag(TargetArchitecture.ARMv7)) { abi += (abi.Length > 0 ? "," : "") + "armv7" + llvm + thumb; } if (architectures.HasFlag(TargetArchitecture.ARMv7s)) { abi += (abi.Length > 0 ? "," : "") + "armv7s" + llvm + thumb; } if (architectures.HasFlag(TargetArchitecture.ARM64)) { // Note: ARM64 does not have thumb. abi += (abi.Length > 0 ? "," : "") + "arm64" + llvm; } if (architectures.HasFlag(TargetArchitecture.ARMv7k)) { abi += (abi.Length > 0 ? "," : "") + "armv7k" + llvm; } if (architectures.HasFlag(TargetArchitecture.ARM64_32)) { abi += (abi.Length > 0 ? "," : "") + "arm64_32" + llvm; } if (string.IsNullOrEmpty(abi)) { abi = "armv7" + llvm + thumb; } } // Output the CompiledArchitectures CompiledArchitectures = architectures.ToString(); args.AddLine($"--abi={abi}"); // output symbols to preserve when stripping args.AddQuotedLine($"--symbollist={Path.GetFullPath (SymbolsList)}"); // don't have mtouch generate the dsyms... args.AddLine("--dsym=no"); var gcc = new GccOptions(); if (!string.IsNullOrEmpty(ExtraArgs)) { var extraArgs = CommandLineArgumentBuilder.Parse(ExtraArgs); var target = MainAssembly.ItemSpec; string projectDir; if (ProjectDir.StartsWith("~/", StringComparison.Ordinal)) { // Note: Since the Visual Studio plugin doesn't know the user's home directory on the Mac build host, // it simply uses paths relative to "~/". Expand these paths to their full path equivalents. var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); projectDir = Path.Combine(home, ProjectDir.Substring(2)); } else { projectDir = ProjectDir; } var customTags = new Dictionary <string, string> (StringComparer.OrdinalIgnoreCase) { { "projectdir", projectDir }, // Apparently msbuild doesn't propagate the solution path, so we can't get it. // { "solutiondir", proj.ParentSolution != null ? proj.ParentSolution.BaseDirectory : proj.BaseDirectory }, { "appbundledir", AppBundleDir }, { "targetpath", Path.Combine(Path.GetDirectoryName(target), Path.GetFileName(target)) }, { "targetdir", Path.GetDirectoryName(target) }, { "targetname", Path.GetFileName(target) }, { "targetext", Path.GetExtension(target) }, }; for (int i = 0; i < extraArgs.Length; i++) { var argument = extraArgs[i]; int startIndex = 0; while (argument.Length > startIndex && argument[startIndex] == '-') { startIndex++; } int endIndex = startIndex; while (endIndex < argument.Length && argument[endIndex] != '=') { endIndex++; } int length = endIndex - startIndex; if (length == 9 && string.CompareOrdinal(argument, startIndex, "gcc_flags", 0, 9) == 0) { // user-defined -gcc_flags argument string flags = null; if (endIndex < extraArgs[i].Length) { flags = Unquote(argument, endIndex + 1); } else if (i + 1 < extraArgs.Length) { flags = extraArgs[++i]; } if (!string.IsNullOrEmpty(flags)) { var gccArgs = CommandLineArgumentBuilder.Parse(flags); for (int j = 0; j < gccArgs.Length; j++) { gcc.Arguments.Add(StringParserService.Parse(gccArgs[j], customTags)); } } } else { // other user-defined mtouch arguments unescapedArgs.Add(StringParserService.Parse(argument, customTags)); } } } BuildNativeReferenceFlags(gcc); BuildEntitlementFlags(gcc); foreach (var framework in gcc.Frameworks) { args.AddQuotedLine($"--framework={framework}"); } foreach (var framework in gcc.WeakFrameworks) { args.AddQuotedLine($"--weak-framework={framework}"); } if (gcc.Cxx) { args.AddLine("--cxx"); } if (gcc.Arguments.Length > 0) { unescapedArgs.Add($"--gcc_flags={gcc.Arguments.ToString ()}"); } foreach (var asm in References) { if (IsFrameworkItem(asm)) { args.AddQuotedLine($"--reference={ResolveFrameworkFile (asm.ItemSpec)}"); } else { args.AddQuotedLine($"--reference={Path.GetFullPath (asm.ItemSpec)}"); } } foreach (var ext in AppExtensionReferences) { args.AddQuotedLine($"--app-extension={Path.GetFullPath (ext.ItemSpec)}"); } if (!string.IsNullOrWhiteSpace(License)) { args.AddLine($"--license={License}"); } return(CreateResponseFile(args, unescapedArgs)); }
protected override string GenerateCommandLineCommands() { var args = new CommandLineArgumentBuilder(); List <string> unescapedArgs = new List <string> (); TargetArchitecture architectures; bool msym; if (string.IsNullOrEmpty(Architectures) || !Enum.TryParse(Architectures, out architectures)) { architectures = TargetArchitecture.Default; } if (architectures == TargetArchitecture.ARMv6) { Log.LogError("Target architecture ARMv6 is no longer supported in Xamarin.iOS. Please select a supported architecture."); return(null); } if (!string.IsNullOrEmpty(IntermediateOutputPath)) { Directory.CreateDirectory(IntermediateOutputPath); args.AddQuotedLine($"--cache={Path.GetFullPath (IntermediateOutputPath)}"); } args.AddQuotedLine((SdkIsSimulator ? "--sim=" : "--dev=") + Path.GetFullPath(AppBundleDir)); if (AppleSdkSettings.XcodeVersion.Major >= 5 && IPhoneSdks.MonoTouch.Version.CompareTo(new IPhoneSdkVersion(6, 3, 7)) < 0) { args.AddLine("--compiler=clang"); } args.AddQuotedLine($"--executable={ExecutableName}"); if (IsAppExtension) { args.AddLine("--extension"); } if (Debug) { if (FastDev && !SdkIsSimulator) { args.AddLine("--fastdev"); } args.AddLine("--debug"); } if (Profiling) { args.AddLine("--profiling"); } if (LinkerDumpDependencies) { args.AddLine("--linkerdumpdependencies"); } if (EnableSGenConc) { args.AddLine("--sgen-conc"); } if (!string.IsNullOrEmpty(Interpreter)) { args.Add($"--interpreter={Interpreter}"); } switch (LinkMode.ToLowerInvariant()) { case "sdkonly": args.AddLine("--linksdkonly"); break; case "none": args.AddLine("--nolink"); break; } if (!string.IsNullOrEmpty(I18n)) { args.AddQuotedLine($"--i18n={I18n}"); } args.AddQuotedLine($"--sdkroot={SdkRoot}"); args.AddQuotedLine($"--sdk={SdkVersion}"); if (!minimumOSVersion.IsUseDefault) { args.AddQuotedLine($"--targetver={minimumOSVersion.ToString ()}"); } if (UseFloat32 /* We want to compile 32-bit floating point code to use 32-bit floating point operations */) { args.AddLine("--aot-options=-O=float32"); } else { args.AddLine("--aot-options=-O=-float32"); } if (!EnableGenericValueTypeSharing) { args.AddLine("--gsharedvt=false"); } if (LinkDescriptions != null) { foreach (var desc in LinkDescriptions) { args.AddQuotedLine($"--xml={desc.ItemSpec}"); } } if (EnableBitcode) { switch (Framework) { case PlatformFramework.WatchOS: args.AddLine("--bitcode=full"); break; case PlatformFramework.TVOS: args.AddLine("--bitcode=asmonly"); break; default: throw new InvalidOperationException(string.Format("Bitcode is currently not supported on {0}.", Framework)); } } if (!string.IsNullOrEmpty(HttpClientHandler)) { args.AddLine($"--http-message-handler={HttpClientHandler}"); } string thumb = UseThumb && UseLlvm ? "+thumb2" : ""; string llvm = UseLlvm ? "+llvm" : ""; string abi = ""; if (SdkIsSimulator) { if (architectures.HasFlag(TargetArchitecture.i386)) { abi += (abi.Length > 0 ? "," : "") + "i386"; } if (architectures.HasFlag(TargetArchitecture.x86_64)) { abi += (abi.Length > 0 ? "," : "") + "x86_64"; } if (string.IsNullOrEmpty(abi)) { architectures = TargetArchitecture.i386; abi = "i386"; } } else { if (architectures == TargetArchitecture.Default) { architectures = TargetArchitecture.ARMv7; } if (architectures.HasFlag(TargetArchitecture.ARMv7)) { abi += (abi.Length > 0 ? "," : "") + "armv7" + llvm + thumb; } if (architectures.HasFlag(TargetArchitecture.ARMv7s)) { abi += (abi.Length > 0 ? "," : "") + "armv7s" + llvm + thumb; } if (architectures.HasFlag(TargetArchitecture.ARM64)) { // Note: ARM64 does not have thumb. abi += (abi.Length > 0 ? "," : "") + "arm64" + llvm; } if (architectures.HasFlag(TargetArchitecture.ARMv7k)) { abi += (abi.Length > 0 ? "," : "") + "armv7k" + llvm; } if (string.IsNullOrEmpty(abi)) { abi = "armv7" + llvm + thumb; } } // Output the CompiledArchitectures CompiledArchitectures = architectures.ToString(); args.AddLine($"--abi={abi}"); // output symbols to preserve when stripping args.AddQuotedLine($"--symbollist={Path.GetFullPath (SymbolsList)}"); // don't have mtouch generate the dsyms... args.AddLine("--dsym=no"); if (!string.IsNullOrEmpty(ArchiveSymbols) && bool.TryParse(ArchiveSymbols.Trim(), out msym)) { args.AddLine($"--msym={(msym ? "yes" : "no")}"); } var gcc = new GccOptions(); if (!string.IsNullOrEmpty(ExtraArgs)) { var extraArgs = CommandLineArgumentBuilder.Parse(ExtraArgs); var target = MainAssembly.ItemSpec; string projectDir; if (ProjectDir.StartsWith("~/", StringComparison.Ordinal)) { // Note: Since the Visual Studio plugin doesn't know the user's home directory on the Mac build host, // it simply uses paths relative to "~/". Expand these paths to their full path equivalents. var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); projectDir = Path.Combine(home, ProjectDir.Substring(2)); } else { projectDir = ProjectDir; } var customTags = new Dictionary <string, string> (StringComparer.OrdinalIgnoreCase) { { "projectdir", projectDir }, // Apparently msbuild doesn't propagate the solution path, so we can't get it. // { "solutiondir", proj.ParentSolution != null ? proj.ParentSolution.BaseDirectory : proj.BaseDirectory }, { "appbundledir", AppBundleDir }, { "targetpath", Path.Combine(Path.GetDirectoryName(target), Path.GetFileName(target)) }, { "targetdir", Path.GetDirectoryName(target) }, { "targetname", Path.GetFileName(target) }, { "targetext", Path.GetExtension(target) }, }; for (int i = 0; i < extraArgs.Length; i++) { var argument = extraArgs[i]; int startIndex = 0; while (argument.Length > startIndex && argument[startIndex] == '-') { startIndex++; } int endIndex = startIndex; while (endIndex < argument.Length && argument[endIndex] != '=') { endIndex++; } int length = endIndex - startIndex; if (length == 9 && string.CompareOrdinal(argument, startIndex, "gcc_flags", 0, 9) == 0) { // user-defined -gcc_flags argument string flags = null; if (endIndex < extraArgs[i].Length) { flags = Unquote(argument, endIndex + 1); } else if (i + 1 < extraArgs.Length) { flags = extraArgs[++i]; } if (!string.IsNullOrEmpty(flags)) { var gccArgs = CommandLineArgumentBuilder.Parse(flags); for (int j = 0; j < gccArgs.Length; j++) { gcc.Arguments.Add(StringParserService.Parse(gccArgs[j], customTags)); } } } else { // other user-defined mtouch arguments unescapedArgs.Add(StringParserService.Parse(argument, customTags)); } } } BuildNativeReferenceFlags(gcc); BuildEntitlementFlags(gcc); foreach (var framework in gcc.Frameworks) { args.AddQuotedLine($"--framework={framework}"); } foreach (var framework in gcc.WeakFrameworks) { args.AddQuotedLine($"--weak-framework={framework}"); } if (gcc.Cxx) { args.AddLine("--cxx"); } if (gcc.Arguments.Length > 0) { unescapedArgs.Add($"--gcc_flags={gcc.Arguments.ToString ()}"); } foreach (var asm in References) { if (IsFrameworkItem(asm)) { args.AddQuotedLine($"-r={ResolveFrameworkFile (asm.ItemSpec)}"); } else { args.AddQuotedLine($"-r={Path.GetFullPath (asm.ItemSpec)}"); } } foreach (var ext in AppExtensionReferences) { args.AddQuotedLine($"--app-extension={Path.GetFullPath (ext.ItemSpec)}"); } args.AddLine($"--target-framework={TargetFrameworkIdentifier},{TargetFrameworkVersion}"); args.AddQuotedLine($"--root-assembly={Path.GetFullPath (MainAssembly.ItemSpec)}"); // We give the priority to the ExtraArgs to set the mtouch verbosity. if (string.IsNullOrEmpty(ExtraArgs) || (!string.IsNullOrEmpty(ExtraArgs) && !ExtraArgs.Contains("-q") && !ExtraArgs.Contains("-v"))) { args.AddLine(GetVerbosityLevel(Verbosity)); } if (!string.IsNullOrWhiteSpace(License)) { args.AddLine($"--license={License}"); } // Generate a response file var responseFile = Path.GetFullPath(ResponseFilePath); if (File.Exists(responseFile)) { File.Delete(responseFile); } try { using (var fs = File.Create(responseFile)) { using (var writer = new StreamWriter(fs)) writer.Write(args); } } catch (Exception ex) { Log.LogWarning("Failed to create response file '{0}': {1}", responseFile, ex); } // Some arguments can not safely go in the response file and are // added separately. They must go _after_ the response file // as they may override options passed in the response file var actualArgs = new CommandLineArgumentBuilder(); actualArgs.AddQuoted($"@{responseFile}"); foreach (var arg in unescapedArgs) { actualArgs.AddQuoted(arg); } return(actualArgs.ToString()); }
IEnumerable <Config> GetAotConfigs() { if (!Directory.Exists(AotOutputDirectory)) { Directory.CreateDirectory(AotOutputDirectory); } var sdkBinDirectory = MonoAndroidHelper.GetOSBinPath(); foreach (var abi in SupportedAbis) { string aotCompiler = ""; string outdir = ""; string mtriple = ""; AndroidTargetArch arch; switch (abi) { case "armeabi-v7a": aotCompiler = Path.Combine(sdkBinDirectory, "cross-arm"); outdir = Path.Combine(AotOutputDirectory, "armeabi-v7a"); mtriple = "armv7-linux-gnueabi"; arch = AndroidTargetArch.Arm; break; case "arm64": case "arm64-v8a": case "aarch64": aotCompiler = Path.Combine(sdkBinDirectory, "cross-arm64"); outdir = Path.Combine(AotOutputDirectory, "arm64-v8a"); mtriple = "aarch64-linux-android"; arch = AndroidTargetArch.Arm64; break; case "x86": aotCompiler = Path.Combine(sdkBinDirectory, "cross-x86"); outdir = Path.Combine(AotOutputDirectory, "x86"); mtriple = "i686-linux-android"; arch = AndroidTargetArch.X86; break; case "x86_64": aotCompiler = Path.Combine(sdkBinDirectory, "cross-x86_64"); outdir = Path.Combine(AotOutputDirectory, "x86_64"); mtriple = "x86_64-linux-android"; arch = AndroidTargetArch.X86_64; break; // case "mips": default: throw new Exception("Unsupported Android target architecture ABI: " + abi); } if (EnableLLVM && !NdkUtil.ValidateNdkPlatform(LogMessage, LogCodedError, AndroidNdkDirectory, arch, enableLLVM:EnableLLVM)) { yield return(Config.Invalid); yield break; } outdir = Path.GetFullPath(outdir); if (!Directory.Exists(outdir)) { Directory.CreateDirectory(outdir); } // dont use a full path if the outdir is withing the WorkingDirectory. if (outdir.StartsWith(WorkingDirectory, StringComparison.InvariantCultureIgnoreCase)) { outdir = outdir.Replace(WorkingDirectory + Path.DirectorySeparatorChar, string.Empty); } int level = 0; string toolPrefix = EnableLLVM ? NdkUtil.GetNdkToolPrefix(AndroidNdkDirectory, arch, level = GetNdkApiLevel(AndroidNdkDirectory, AndroidApiLevel, arch)) : Path.Combine(AndroidBinUtilsDirectory, $"{NdkUtil.GetArchDirName (arch)}-"); var toolchainPath = toolPrefix.Substring(0, toolPrefix.LastIndexOf(Path.DirectorySeparatorChar)); var ldFlags = string.Empty; if (EnableLLVM) { if (string.IsNullOrEmpty(AndroidNdkDirectory)) { yield return(Config.Invalid); yield break; } string androidLibPath = string.Empty; try { androidLibPath = NdkUtil.GetNdkPlatformLibPath(AndroidNdkDirectory, arch, level); } catch (InvalidOperationException ex) { Diagnostic.Error(5101, ex.Message); } string toolchainLibDir; if (NdkUtil.UsingClangNDK) { toolchainLibDir = GetNdkToolchainLibraryDir(toolchainPath, arch); } else { toolchainLibDir = GetNdkToolchainLibraryDir(toolchainPath); } var libs = new List <string>(); if (NdkUtil.UsingClangNDK) { libs.Add($"-L{toolchainLibDir}"); libs.Add($"-L{androidLibPath}"); if (arch == AndroidTargetArch.Arm) { // Needed for -lunwind to work string compilerLibDir = Path.Combine(toolchainPath, "..", "sysroot", "usr", "lib", NdkUtil.GetArchDirName(arch)); libs.Add($"-L{compilerLibDir}"); } } libs.Add($"\\\"{Path.Combine (toolchainLibDir, "libgcc.a")}\\\""); libs.Add($"\\\"{Path.Combine (androidLibPath, "libc.so")}\\\""); libs.Add($"\\\"{Path.Combine (androidLibPath, "libm.so")}\\\""); ldFlags = string.Join(";", libs); } foreach (var assembly in ResolvedAssemblies) { string outputFile = Path.Combine(outdir, string.Format("libaot-{0}.so", Path.GetFileName(assembly.ItemSpec))); string seqpointsFile = Path.Combine(outdir, string.Format("{0}.msym", Path.GetFileName(assembly.ItemSpec))); string tempDir = Path.Combine(outdir, Path.GetFileName(assembly.ItemSpec)); if (!Directory.Exists(tempDir)) { Directory.CreateDirectory(tempDir); } List <string> aotOptions = new List <string> (); if (Profiles != null && Profiles.Length > 0) { aotOptions.Add("profile-only"); foreach (var p in Profiles) { var fp = Path.GetFullPath(p.ItemSpec); aotOptions.Add($"profile={fp}"); } } if (!string.IsNullOrEmpty(AotAdditionalArguments)) { aotOptions.Add(AotAdditionalArguments); } if (sequencePointsMode == SequencePointsMode.Offline) { aotOptions.Add($"msym-dir={outdir}"); } if (AotMode != AotMode.Normal) { aotOptions.Add(AotMode.ToString().ToLowerInvariant()); } aotOptions.Add($"outfile={outputFile}"); aotOptions.Add("asmwriter"); aotOptions.Add($"mtriple={mtriple}"); aotOptions.Add($"tool-prefix={toolPrefix}"); aotOptions.Add($"llvm-path={sdkBinDirectory}"); aotOptions.Add($"temp-path={tempDir}"); aotOptions.Add($"ld-flags={ldFlags}"); // we need to quote the entire --aot arguments here to make sure it is parsed // on windows as one argument. Otherwise it will be split up into multiple // values, which wont work. string aotOptionsStr = (EnableLLVM ? "--llvm " : "") + $"\"--aot={string.Join (",", aotOptions)}\""; if (!string.IsNullOrEmpty(ExtraAotOptions)) { aotOptionsStr += (aotOptions.Count > 0 ? " " : "") + ExtraAotOptions; } // Due to a Monodroid MSBuild bug we can end up with paths to assemblies that are not in the intermediate // assembly directory (typically obj/assemblies). This can lead to problems with the Mono loader not being // able to find their dependency laters, since framework assemblies are stored in different directories. // This can happen when linking is disabled (AndroidLinkMode=None). Workaround this problem by resolving // the paths to the right assemblies manually. var resolvedPath = Path.GetFullPath(assembly.ItemSpec); var intermediateAssemblyPath = Path.Combine(IntermediateAssemblyDir, Path.GetFileName(assembly.ItemSpec)); if (LinkMode.ToLowerInvariant() == "none") { if (!resolvedPath.Contains(IntermediateAssemblyDir) && File.Exists(intermediateAssemblyPath)) { resolvedPath = intermediateAssemblyPath; } } var assembliesPath = Path.GetFullPath(Path.GetDirectoryName(resolvedPath)); var assemblyPath = Path.GetFullPath(resolvedPath); yield return(new Config(assembliesPath, aotCompiler, aotOptionsStr, assemblyPath, outputFile, Path.Combine(tempDir, "response.txt"))); } } }
bool DoExecute() { Log.LogDebugMessage("Aot:", AndroidAotMode); Log.LogDebugMessage(" AndroidApiLevel: {0}", AndroidApiLevel); Log.LogDebugMessage(" AndroidAotMode: {0}", AndroidAotMode); Log.LogDebugMessage(" AndroidSequencePointsMode: {0}", AndroidSequencePointsMode); Log.LogDebugMessage(" AndroidNdkDirectory: {0}", AndroidNdkDirectory); Log.LogDebugMessage(" AotOutputDirectory: {0}", AotOutputDirectory); Log.LogDebugMessage(" EnableLLVM: {0}", EnableLLVM); Log.LogDebugMessage(" IntermediateAssemblyDir: {0}", IntermediateAssemblyDir); Log.LogDebugMessage(" LinkMode: {0}", LinkMode); Log.LogDebugMessage(" SdkBinDirectory: {0}", SdkBinDirectory); Log.LogDebugMessage(" SupportedAbis: {0}", SupportedAbis); Log.LogDebugTaskItems(" ResolvedAssemblies:", ResolvedAssemblies); Log.LogDebugTaskItems(" AdditionalNativeLibraryReferences:", AdditionalNativeLibraryReferences); bool hasValidAotMode = GetAndroidAotMode(AndroidAotMode, out AotMode); if (!hasValidAotMode) { Log.LogCodedError("XA3001", "Invalid AOT mode: {0}", AndroidAotMode); return(false); } TryGetSequencePointsMode(AndroidSequencePointsMode, out sequencePointsMode); var nativeLibs = new List <string> (); if (!Directory.Exists(AotOutputDirectory)) { Directory.CreateDirectory(AotOutputDirectory); } // Check that we have a compatible NDK version for the targeted ABIs. NdkUtil.NdkVersion ndkVersion; bool hasNdkVersion = NdkUtil.GetNdkToolchainRelease(AndroidNdkDirectory, out ndkVersion); var abis = SupportedAbis.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (var abi in abis) { string aotCompiler = ""; string outdir = ""; string mtriple = ""; AndroidTargetArch arch; switch (abi) { case "armeabi": aotCompiler = Path.Combine(SdkBinDirectory, "cross-arm"); outdir = Path.Combine(AotOutputDirectory, "armeabi"); mtriple = "armv5-linux-gnueabi"; arch = AndroidTargetArch.Arm; break; case "armeabi-v7a": aotCompiler = Path.Combine(SdkBinDirectory, "cross-arm"); outdir = Path.Combine(AotOutputDirectory, "armeabi-v7a"); mtriple = "armv7-linux-gnueabi"; arch = AndroidTargetArch.Arm; break; case "arm64": case "arm64-v8a": case "aarch64": aotCompiler = Path.Combine(SdkBinDirectory, "cross-arm64"); outdir = Path.Combine(AotOutputDirectory, "arm64-v8a"); mtriple = "aarch64-linux-android"; arch = AndroidTargetArch.Arm64; break; case "x86": aotCompiler = Path.Combine(SdkBinDirectory, "cross-x86"); outdir = Path.Combine(AotOutputDirectory, "x86"); mtriple = "i686-linux-android"; arch = AndroidTargetArch.X86; break; case "x86_64": aotCompiler = Path.Combine(SdkBinDirectory, "cross-x86_64"); outdir = Path.Combine(AotOutputDirectory, "x86_64"); mtriple = "x86_64-linux-android"; arch = AndroidTargetArch.X86_64; break; // case "mips": default: throw new Exception("Unsupported Android target architecture ABI: " + abi); } if (!NdkUtil.ValidateNdkPlatform(Log, AndroidNdkDirectory, arch, enableLLVM:EnableLLVM)) { return(false); } if (!ValidateAotConfiguration(Log, arch, EnableLLVM)) { return(false); } outdir = Path.GetFullPath(outdir); if (!Directory.Exists(outdir)) { Directory.CreateDirectory(outdir); } var toolPrefix = NdkUtil.GetNdkToolPrefix(AndroidNdkDirectory, arch); var toolchainPath = toolPrefix.Substring(0, toolPrefix.LastIndexOf(Path.DirectorySeparatorChar)); var ldFlags = string.Empty; if (EnableLLVM) { int level = GetNdkApiLevel(AndroidNdkDirectory, AndroidApiLevel, arch); string androidLibPath = string.Empty; try { androidLibPath = NdkUtil.GetNdkPlatformLibPath(AndroidNdkDirectory, arch, level); } catch (InvalidOperationException ex) { Diagnostic.Error(5101, ex.Message); } var libs = new List <string>() { QuoteFileName(Path.Combine(GetNdkToolchainLibraryDir(toolchainPath), "libgcc.a")), QuoteFileName(Path.Combine(androidLibPath, "libc.so")), QuoteFileName(Path.Combine(androidLibPath, "libm.so")) }; ldFlags = string.Join(";", libs); } foreach (var assembly in ResolvedAssemblies) { string outputFile = Path.Combine(outdir, string.Format("libaot-{0}.so", Path.GetFileName(assembly.ItemSpec))); string seqpointsFile = Path.Combine(outdir, string.Format("{0}.msym", Path.GetFileName(assembly.ItemSpec))); string aotOptions = string.Format( "{0}--aot={8}{1}outfile={2},asmwriter,mtriple={3},tool-prefix={4},ld-flags={5},llvm-path={6},temp-path={7}", EnableLLVM ? "--llvm " : string.Empty, AotMode != AotMode.Normal ? string.Format("{0},", AotMode.ToString().ToLowerInvariant()) : string.Empty, QuoteFileName(outputFile), mtriple, QuoteFileName(toolPrefix), ldFlags, QuoteFileName(SdkBinDirectory), QuoteFileName(outdir), sequencePointsMode == SequencePointsMode.Offline ? string.Format("gen-seq-points-file={0},", seqpointsFile) : string.Empty ); // Due to a Monodroid MSBuild bug we can end up with paths to assemblies that are not in the intermediate // assembly directory (typically obj/assemblies). This can lead to problems with the Mono loader not being // able to find their dependency laters, since framework assemblies are stored in different directories. // This can happen when linking is disabled (AndroidLinkMode=None). Workaround this problem by resolving // the paths to the right assemblies manually. var resolvedPath = Path.GetFullPath(assembly.ItemSpec); var intermediateAssemblyPath = Path.Combine(IntermediateAssemblyDir, Path.GetFileName(assembly.ItemSpec)); if (LinkMode.ToLowerInvariant() == "none") { if (!resolvedPath.Contains(IntermediateAssemblyDir) && File.Exists(intermediateAssemblyPath)) { resolvedPath = intermediateAssemblyPath; } } var assembliesPath = Path.GetFullPath(Path.GetDirectoryName(resolvedPath)); var assemblyPath = QuoteFileName(Path.GetFullPath(resolvedPath)); if (!RunAotCompiler(assembliesPath, aotCompiler, aotOptions, assemblyPath)) { Log.LogCodedError("XA3001", "Could not AOT the assembly: {0}", assembly.ItemSpec); return(false); } nativeLibs.Add(outputFile); } } NativeLibrariesReferences = nativeLibs.ToArray(); Log.LogDebugTaskItems("Aot Outputs:"); Log.LogDebugTaskItems(" NativeLibrariesReferences: ", NativeLibrariesReferences); return(true); }