2016-04-21 15:57:02 +03:00
// Copyright 2013--2014 Xamarin Inc. All rights reserved.
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.Globalization ;
using System.Linq ;
using System.IO ;
using System.Runtime.InteropServices ;
using System.Text ;
using System.Threading ;
using System.Threading.Tasks ;
using Mono.Cecil ;
using Mono.Tuner ;
using Mono.Linker ;
using Xamarin.Linker ;
using Xamarin.Utils ;
2016-05-11 14:10:35 +03:00
using XamCore.Registrar ;
2016-04-21 15:57:02 +03:00
#if MONOTOUCH
using MonoTouch ;
using MonoTouch.Tuner ;
using PlatformResolver = MonoTouch . Tuner . MonoTouchResolver ;
using PlatformLinkContext = MonoTouch . Tuner . MonoTouchLinkContext ;
#else
using MonoMac.Tuner ;
using PlatformResolver = Xamarin . Bundler . MonoMacResolver ;
using PlatformLinkContext = MonoMac . Tuner . MonoMacLinkContext ;
#endif
namespace Xamarin.Bundler {
public partial class Target {
public Application App ;
2017-01-25 12:47:51 +03:00
public AssemblyCollection Assemblies = new AssemblyCollection ( ) ; // The root assembly is not in this list.
2016-04-21 15:57:02 +03:00
public PlatformLinkContext LinkContext ;
2016-05-11 14:10:35 +03:00
public LinkerOptions LinkerOptions ;
2016-04-21 15:57:02 +03:00
public PlatformResolver Resolver = new PlatformResolver ( ) ;
public HashSet < string > Frameworks = new HashSet < string > ( ) ;
public HashSet < string > WeakFrameworks = new HashSet < string > ( ) ;
2016-12-23 20:50:35 +03:00
internal StaticRegistrar StaticRegistrar { get ; set ; }
2016-05-11 14:10:35 +03:00
#if MONOMAC
public bool Is32Build { get { return ! Driver . Is64Bit ; } }
public bool Is64Build { get { return Driver . Is64Bit ; } }
#endif
2016-04-21 15:57:02 +03:00
public Target ( Application app )
{
this . App = app ;
}
public void ExtractNativeLinkInfo ( List < Exception > exceptions )
{
foreach ( var a in Assemblies ) {
try {
a . ExtractNativeLinkInfo ( ) ;
2017-01-31 16:55:54 +03:00
#if MTOUCH
2017-02-03 09:40:44 +03:00
if ( a . HasLinkWithAttributes & & App . EnableBitCode & & ! App . OnlyStaticLibraries ) {
2017-01-31 16:55:54 +03:00
ErrorHelper . Warning ( 110 , "Incremental builds have been disabled because this version of Xamarin.iOS does not support incremental builds in projects that include third-party binding libraries and that compiles to bitcode." ) ;
2017-02-03 09:40:44 +03:00
App . ClearAssemblyBuildTargets ( ) ; // the default is to compile to static libraries, so just revert to the default.
2017-01-31 16:55:54 +03:00
}
#endif
2016-04-21 15:57:02 +03:00
} catch ( Exception e ) {
exceptions . Add ( e ) ;
}
}
2017-02-24 21:34:47 +03:00
#if MTOUCH
2017-02-28 12:11:40 +03:00
if ( ! App . OnlyStaticLibraries & & Assemblies . Count ( ( v ) = > v . HasLinkWithAttributes ) > 1 ) {
2017-02-24 21:34:47 +03:00
ErrorHelper . Warning ( 127 , "Incremental builds have been disabled because this version of Xamarin.iOS does not support incremental builds in projects that include more than one third-party binding libraries." ) ;
2017-02-28 12:11:40 +03:00
App . ClearAssemblyBuildTargets ( ) ; // the default is to compile to static libraries, so just revert to the default.
2017-02-24 21:34:47 +03:00
}
#endif
2016-04-21 15:57:02 +03:00
}
[DllImport (Constants.libSystemLibrary, SetLastError = true, EntryPoint = "strerror")]
static extern IntPtr _strerror ( int errno ) ;
internal static string strerror ( int errno )
{
return Marshal . PtrToStringAuto ( _strerror ( errno ) ) ;
}
[DllImport (Constants.libSystemLibrary, SetLastError = true)]
static extern string realpath ( string path , IntPtr zero ) ;
public static string GetRealPath ( string path )
{
var rv = realpath ( path , IntPtr . Zero ) ;
if ( rv ! = null )
return rv ;
var errno = Marshal . GetLastWin32Error ( ) ;
ErrorHelper . Warning ( 54 , "Unable to canonicalize the path '{0}': {1} ({2})." , path , strerror ( errno ) , errno ) ;
return path ;
}
2017-04-05 22:38:40 +03:00
public void ComputeLinkerFlags ( )
{
foreach ( var a in Assemblies )
a . ComputeLinkerFlags ( ) ;
}
public void GatherFrameworks ( )
{
2017-04-12 11:38:31 +03:00
Assembly asm = null ;
2017-04-05 22:38:40 +03:00
AssemblyDefinition productAssembly = null ;
foreach ( var assembly in Assemblies ) {
if ( assembly . AssemblyDefinition . Name . Name = = Driver . GetProductAssembly ( App ) ) {
2017-04-12 11:38:31 +03:00
asm = assembly ;
2017-04-05 22:38:40 +03:00
break ;
}
}
2017-04-12 11:38:31 +03:00
productAssembly = asm . AssemblyDefinition ;
2017-04-05 22:38:40 +03:00
// *** make sure any change in the above lists (or new list) are also reflected in
// *** Makefile so simlauncher-sgen does not miss any framework
HashSet < string > processed = new HashSet < string > ( ) ;
#if ! MONOMAC
Version v80 = new Version ( 8 , 0 ) ;
#endif
foreach ( ModuleDefinition md in productAssembly . Modules ) {
foreach ( TypeDefinition td in md . Types ) {
// process only once each namespace (as we keep adding logic below)
string nspace = td . Namespace ;
if ( processed . Contains ( nspace ) )
continue ;
processed . Add ( nspace ) ;
Framework framework ;
if ( Driver . GetFrameworks ( App ) . TryGetValue ( nspace , out framework ) ) {
// framework specific processing
#if ! MONOMAC
2017-05-24 22:04:45 +03:00
switch ( framework . Name ) {
2017-04-05 22:38:40 +03:00
case "CoreAudioKit" :
// CoreAudioKit seems to be functional in the iOS 9 simulator.
if ( App . IsSimulatorBuild & & App . SdkVersion . Major < 9 )
continue ;
break ;
case "Metal" :
case "MetalKit" :
case "MetalPerformanceShaders" :
// some frameworks do not exists on simulators and will result in linker errors if we include them
if ( App . IsSimulatorBuild )
continue ;
break ;
case "PushKit" :
// in Xcode 6 beta 7 this became an (ld) error - it was a warning earlier :(
// ld: embedded dylibs/frameworks are only supported on iOS 8.0 and later (@rpath/PushKit.framework/PushKit) for architecture armv7
// this was fixed in Xcode 6.2 (6.1 was still buggy) see #29786
if ( ( App . DeploymentTarget < v80 ) & & ( Driver . XcodeVersion < new Version ( 6 , 2 ) ) ) {
ErrorHelper . Warning ( 49 , "{0}.framework is supported only if deployment target is 8.0 or later. {0} features might not work correctly." , framework . Name ) ;
continue ;
}
break ;
}
2017-05-24 22:04:45 +03:00
#endif
2017-04-05 22:38:40 +03:00
if ( App . SdkVersion > = framework . Version ) {
2017-04-12 11:38:31 +03:00
var add_to = App . DeploymentTarget > = framework . Version ? asm . Frameworks : asm . WeakFrameworks ;
2017-04-05 22:38:40 +03:00
add_to . Add ( framework . Name ) ;
continue ;
}
}
}
}
// Make sure there are no duplicates between frameworks and weak frameworks.
// Keep the weak ones.
2017-04-12 11:38:31 +03:00
asm . Frameworks . ExceptWith ( asm . WeakFrameworks ) ;
2017-04-05 22:38:40 +03:00
}
2016-04-21 15:57:02 +03:00
}
}