TargetFrameworkUtil.cs 7.7 KB
Newer Older
J
Jared Parsons 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
using static TestReferences;

namespace Roslyn.Test.Utilities
{
    public enum TargetFramework
    {
        /// <summary>
        /// Explicit pick a target framework that has no references
        /// </summary>
J
Jared Parsons 已提交
17
        Empty,
J
Jared Parsons 已提交
18 19

        NetStandard20,
J
Jared Parsons 已提交
20
        WinRT,
J
Jared Parsons 已提交
21 22 23 24 25

        /// <summary>
        /// Eventually this will be deleted and replaced with NetStandard20. Short term this creates the "standard"
        /// API set across destkop and coreclr 
        /// </summary>
J
Jared Parsons 已提交
26
        Standard,
J
Jared Parsons 已提交
27
        StandardAndCSharp,
J
Jared Parsons 已提交
28

29 30
        StandardAndVBRuntime,

J
Jared Parsons 已提交
31 32 33 34
        /// <summary>
        /// This is represents the set of tests which must be mscorlib40 on desktop but full net standard on coreclr.
        /// </summary>
        StandardCompat,
J
Jared Parsons 已提交
35 36 37 38 39
       
        /// <summary>
        /// Compat framework for the default set of references many vb compilations get.
        /// </summary>
        DefaultVb,
J
Jared Parsons 已提交
40 41 42 43

        // The flavors of mscorlib we support + extending them with LINQ and dynamic.
        Mscorlib40,
        Mscorlib40Extended, 
J
Jared Parsons 已提交
44
        Mscorlib40AndSystemCore,
45
        Mscorlib40AndVBRuntime,
J
Jared Parsons 已提交
46 47
        Mscorlib45,
        Mscorlib45Extended,
J
Jared Parsons 已提交
48
        Mscorlib45AndCSharp,
49
        Mscorlib45AndVBRuntime,
J
Jared Parsons 已提交
50 51
        Mscorlib46,
        Mscorlib46Extended,
J
Jared Parsons 已提交
52 53 54 55
    }

    public static class TargetFrameworkUtil
    {
J
Jared Parsons 已提交
56
        public static MetadataReference StandardCSharpReference => CoreClrShim.IsRunningOnCoreClr ? NetStandard20.MicrosoftCSharpRef : TestBase.CSharpDesktopRef;
J
Jared Parsons 已提交
57

58 59 60 61 62 63 64
        /*
         * ⚠ Dev note ⚠: properties in TestBase are backed by Lazy<T>. Avoid changes to the following properties
         * which would force the initialization of these properties in the static constructor, since the stack traces
         * for a TypeLoadException are missing important information for resolving problems if/when they occur.
         * https://github.com/dotnet/roslyn/issues/25961
         */

65 66 67
        public static ImmutableArray<MetadataReference> Mscorlib40References => ImmutableArray.Create(TestBase.MscorlibRef);
        public static ImmutableArray<MetadataReference> Mscorlib40ExtendedReferences => ImmutableArray.Create(TestBase.MscorlibRef, TestBase.SystemRef, TestBase.SystemCoreRef, TestBase.ValueTupleRef, TestBase.SystemRuntimeFacadeRef);
        public static ImmutableArray<MetadataReference> Mscorlib40andSystemCoreReferences => ImmutableArray.Create(TestBase.MscorlibRef, TestBase.SystemCoreRef);
J
Jared Parsons 已提交
68
        public static ImmutableArray<MetadataReference> Mscorlib40andVBRuntimeReferences => ImmutableArray.Create(TestBase.MscorlibRef, TestBase.SystemRef, TestBase.MsvbRef);
69 70 71
        public static ImmutableArray<MetadataReference> Mscorlib45References => ImmutableArray.Create(TestBase.MscorlibRef_v4_0_30316_17626);
        public static ImmutableArray<MetadataReference> Mscorlib45ExtendedReferences => ImmutableArray.Create(TestBase.MscorlibRef_v4_0_30316_17626, TestBase.SystemRef, TestBase.SystemCoreRef, TestBase.ValueTupleRef, TestBase.SystemRuntimeFacadeRef);
        public static ImmutableArray<MetadataReference> Mscorlib45AndCSharpReferences => ImmutableArray.Create(TestBase.MscorlibRef_v4_0_30316_17626, TestBase.SystemCoreRef, TestBase.CSharpRef);
J
Jared Parsons 已提交
72
        public static ImmutableArray<MetadataReference> Mscorlib45AndVBRuntimeReferences => ImmutableArray.Create(TestBase.MscorlibRef_v4_0_30316_17626, TestBase.SystemRef, TestBase.MsvbRef_v4_0_30319_17929);
73 74 75 76 77 78
        public static ImmutableArray<MetadataReference> Mscorlib46References => ImmutableArray.Create(TestBase.MscorlibRef_v46);
        public static ImmutableArray<MetadataReference> Mscorlib46ExtendedReferences => ImmutableArray.Create(TestBase.MscorlibRef_v46, TestBase.SystemRef_v46, TestBase.SystemCoreRef_v46, TestBase.ValueTupleRef, TestBase.SystemRuntimeFacadeRef);
        public static ImmutableArray<MetadataReference> NetStandard20References => ImmutableArray.Create<MetadataReference>(NetStandard20.NetStandard, NetStandard20.MscorlibRef, NetStandard20.SystemRuntimeRef, NetStandard20.SystemCoreRef, NetStandard20.SystemDynamicRuntimeRef);
        public static ImmutableArray<MetadataReference> WinRTReferences => ImmutableArray.Create(TestBase.WinRtRefs);
        public static ImmutableArray<MetadataReference> StandardReferences => CoreClrShim.IsRunningOnCoreClr ? NetStandard20References : Mscorlib46ExtendedReferences;
        public static ImmutableArray<MetadataReference> StandardAndCSharpReferences => StandardReferences.Add(StandardCSharpReference);
79
        public static ImmutableArray<MetadataReference> StandardAndVBRuntimeReferences => CoreClrShim.IsRunningOnCoreClr ? NetStandard20References.Add(NetStandard20.MicrosoftVisualBasicRef) : Mscorlib46ExtendedReferences.Add(TestBase.MsvbRef_v4_0_30319_17929);
80
        public static ImmutableArray<MetadataReference> StandardCompatReferences => CoreClrShim.IsRunningOnCoreClr ? NetStandard20References : Mscorlib40References;
J
Jared Parsons 已提交
81 82
        public static ImmutableArray<MetadataReference> DefaultVbReferencs => ImmutableArray.Create(TestBase.MscorlibRef, TestBase.SystemRef, TestBase.SystemCoreRef, TestBase.MsvbRef);
 
J
Jared Parsons 已提交
83 84 85 86
        public static ImmutableArray<MetadataReference> GetReferences(TargetFramework tf)
        {
            switch (tf)
            {
J
Jared Parsons 已提交
87
                case TargetFramework.Empty: return ImmutableArray<MetadataReference>.Empty;
J
Jared Parsons 已提交
88 89
                case TargetFramework.Mscorlib40: return Mscorlib40References;
                case TargetFramework.Mscorlib40Extended: return Mscorlib40ExtendedReferences;
J
Jared Parsons 已提交
90
                case TargetFramework.Mscorlib40AndSystemCore: return Mscorlib40andSystemCoreReferences;
91
                case TargetFramework.Mscorlib40AndVBRuntime: return Mscorlib40andVBRuntimeReferences;
J
Jared Parsons 已提交
92 93
                case TargetFramework.Mscorlib45: return Mscorlib45References;
                case TargetFramework.Mscorlib45Extended: return Mscorlib45ExtendedReferences;
J
Jared Parsons 已提交
94
                case TargetFramework.Mscorlib45AndCSharp: return Mscorlib45AndCSharpReferences;
95
                case TargetFramework.Mscorlib45AndVBRuntime: return Mscorlib45AndVBRuntimeReferences;
J
Jared Parsons 已提交
96 97
                case TargetFramework.Mscorlib46: return Mscorlib46References;
                case TargetFramework.Mscorlib46Extended: return Mscorlib46ExtendedReferences;
J
Jared Parsons 已提交
98
                case TargetFramework.NetStandard20: return NetStandard20References;
J
Jared Parsons 已提交
99
                case TargetFramework.WinRT: return WinRTReferences;
J
Jared Parsons 已提交
100
                case TargetFramework.Standard: return StandardReferences;
J
Jared Parsons 已提交
101
                case TargetFramework.StandardAndCSharp: return StandardAndCSharpReferences;
102
                case TargetFramework.StandardAndVBRuntime: return StandardAndVBRuntimeReferences;
J
Jared Parsons 已提交
103
                case TargetFramework.StandardCompat: return StandardCompatReferences;
J
Jared Parsons 已提交
104
                case TargetFramework.DefaultVb: return DefaultVbReferencs;
J
Jared Parsons 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
                default: throw new InvalidOperationException($"Unexpected target framework {tf}");
            }
        }

        public static ImmutableArray<MetadataReference> GetReferences(TargetFramework tf, IEnumerable<MetadataReference> additionalReferences)
        {
            var references = GetReferences(tf);
            if (additionalReferences == null)
            {
                return references;
            }

            foreach (var r in additionalReferences)
            {
                if (references.Contains(r))
                {
                    throw new Exception($"Duplicate reference detected {r.Display}");
                }
            }

            return references.AddRange(additionalReferences);
        }
    }
}