예제 #1
0
        /// <summary>
        /// Creates the project files generator for the specified project format.
        /// </summary>
        /// <param name="format">The format.</param>
        /// <param name="type">The target project type.</param>
        /// <returns>The generator.</returns>
        public static ProjectGenerator Create(ProjectFormat format, TargetType type)
        {
            // Pick the newest installed Visual Studio version
            if (format == ProjectFormat.VisualStudio)
            {
                if (VisualStudioInstance.HasIDE(VisualStudioVersion.VisualStudio2022))
                {
                    format = ProjectFormat.VisualStudio2022;
                }
                else if (VisualStudioInstance.HasIDE(VisualStudioVersion.VisualStudio2019))
                {
                    format = ProjectFormat.VisualStudio2019;
                }
                else if (VisualStudioInstance.HasIDE(VisualStudioVersion.VisualStudio2017))
                {
                    format = ProjectFormat.VisualStudio2017;
                }
                else if (VisualStudioInstance.HasIDE(VisualStudioVersion.VisualStudio2015))
                {
                    format = ProjectFormat.VisualStudio2015;
                }
                else
                {
                    Log.Warning("Failed to find default Visual Studio installation");
                    format = ProjectFormat.VisualStudio2015;
                }
            }

            switch (format)
            {
            case ProjectFormat.VisualStudio2015:
            case ProjectFormat.VisualStudio2017:
            case ProjectFormat.VisualStudio2019:
            case ProjectFormat.VisualStudio2022:
            {
                VisualStudioVersion vsVersion;
                switch (format)
                {
                case ProjectFormat.VisualStudio2015:
                    vsVersion = VisualStudioVersion.VisualStudio2015;
                    break;

                case ProjectFormat.VisualStudio2017:
                    vsVersion = VisualStudioVersion.VisualStudio2017;
                    break;

                case ProjectFormat.VisualStudio2019:
                    vsVersion = VisualStudioVersion.VisualStudio2019;
                    break;

                case ProjectFormat.VisualStudio2022:
                    vsVersion = VisualStudioVersion.VisualStudio2022;
                    break;

                default: throw new ArgumentOutOfRangeException(nameof(format), format, null);
                }
                switch (type)
                {
                case TargetType.NativeCpp: return(new VCProjectGenerator(vsVersion));

                case TargetType.DotNet: return(new CSProjectGenerator(vsVersion));

                default: throw new ArgumentOutOfRangeException(nameof(type), type, null);
                }
            }

            case ProjectFormat.VisualStudioCode: return(new VisualStudioCodeProjectGenerator());

            case ProjectFormat.Custom:
                if (CustomProjectTypes.TryGetValue(Configuration.ProjectFormatCustom, out var factory))
                {
                    return(factory(type));
                }
                throw new Exception($"Unknown custom project format type '{Configuration.ProjectFormatCustom}'");

            default: throw new ArgumentOutOfRangeException(nameof(format), "Unknown project format.");
            }
        }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WindowsToolchainBase"/> class.
        /// </summary>
        /// <param name="platform">The platform.</param>
        /// <param name="architecture">The target architecture.</param>
        /// <param name="toolsetVer">The target platform toolset version.</param>
        /// <param name="sdkVer">The target platform SDK version.</param>
        protected WindowsToolchainBase(WindowsPlatformBase platform, TargetArchitecture architecture, WindowsPlatformToolset toolsetVer, WindowsPlatformSDK sdkVer)
            : base(platform, architecture)
        {
            var toolsets = WindowsPlatformBase.GetToolsets();
            var sdks     = WindowsPlatformBase.GetSDKs();

            // Pick the overriden toolset
            if (Configuration.Compiler != null)
            {
                if (Enum.TryParse(Configuration.Compiler, out WindowsPlatformToolset compiler))
                {
                    toolsetVer = compiler;
                }
            }

            // Pick the newest installed Visual Studio version if using the default toolset
            if (toolsetVer == WindowsPlatformToolset.Default)
            {
                if (VisualStudioInstance.HasIDE(VisualStudioVersion.VisualStudio2019))
                {
                    toolsetVer = WindowsPlatformToolset.v142;
                }
                else if (VisualStudioInstance.HasIDE(VisualStudioVersion.VisualStudio2017))
                {
                    toolsetVer = WindowsPlatformToolset.v141;
                }
                else
                {
                    toolsetVer = WindowsPlatformToolset.v140;
                }
            }
            // Pick the latest toolset
            else if (toolsetVer == WindowsPlatformToolset.Latest)
            {
                toolsetVer = toolsets.Keys.Max();
            }

            // Pick the latest SDK
            if (sdkVer == WindowsPlatformSDK.Latest)
            {
                sdkVer = sdks.Keys.Max();
            }

            // Get tools
            Toolset = toolsetVer;
            SDK     = sdkVer;
            if (!toolsets.ContainsKey(Toolset))
            {
                throw new Exception(string.Format("Missing toolset {0} for platform Windows", Toolset));
            }
            if (!sdks.ContainsKey(SDK))
            {
                throw new Exception(string.Format("Missing SDK {0} for platform Windows", SDK));
            }

            // Get the tools paths
            string vcToolPath;

            if (Architecture == TargetArchitecture.x64)
            {
                vcToolPath = WindowsPlatformBase.GetVCToolPath64(Toolset);
            }
            else
            {
                vcToolPath = WindowsPlatformBase.GetVCToolPath32(Toolset);
            }
            _vcToolPath   = vcToolPath;
            _compilerPath = Path.Combine(vcToolPath, "cl.exe");
            _linkerPath   = Path.Combine(vcToolPath, "link.exe");
            _libToolPath  = Path.Combine(vcToolPath, "lib.exe");
            _xdcmakePath  = Path.Combine(vcToolPath, "xdcmake.exe");

            // Add Visual C++ toolset include and library paths
            var vcToolChainDir = toolsets[Toolset];

            SystemIncludePaths.Add(Path.Combine(vcToolChainDir, "include"));
            switch (Toolset)
            {
            case WindowsPlatformToolset.v140:
            {
                switch (Architecture)
                {
                case TargetArchitecture.AnyCPU: break;

                case TargetArchitecture.ARM:
                    SystemLibraryPaths.Add(Path.Combine(vcToolChainDir, "lib", "arm"));
                    break;

                case TargetArchitecture.ARM64:
                    SystemLibraryPaths.Add(Path.Combine(vcToolChainDir, "lib", "arm64"));
                    break;

                case TargetArchitecture.x86:
                    SystemLibraryPaths.Add(Path.Combine(vcToolChainDir, "lib"));
                    break;

                case TargetArchitecture.x64:
                    SystemLibraryPaths.Add(Path.Combine(vcToolChainDir, "lib", "amd64"));
                    break;

                default: throw new InvalidArchitectureException(architecture);
                }

                // When using Visual Studio 2015 toolset and using pre-Windows 10 SDK, find a Windows 10 SDK and add the UCRT include paths
                if (SDK == WindowsPlatformSDK.v8_1)
                {
                    var sdk = sdks.FirstOrDefault(x => x.Key != WindowsPlatformSDK.v8_1);
                    if (sdk.Value == null)
                    {
                        throw new Exception("Combination of Windows Toolset v140 and Windows SDK 8.1 requires the Universal CRT to be installed.");
                    }

                    var    sdkVersionName = WindowsPlatformBase.GetSDKVersion(sdk.Key).ToString();
                    string includeRootDir = Path.Combine(sdk.Value, "include", sdkVersionName);
                    SystemIncludePaths.Add(Path.Combine(includeRootDir, "ucrt"));

                    string libraryRootDir = Path.Combine(sdk.Value, "lib", sdkVersionName);
                    switch (Architecture)
                    {
                    case TargetArchitecture.AnyCPU: break;

                    case TargetArchitecture.ARM:
                        SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "ucrt", "arm"));
                        break;

                    case TargetArchitecture.ARM64:
                        SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "ucrt", "arm64"));
                        break;

                    case TargetArchitecture.x86:
                        SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "ucrt", "x86"));
                        break;

                    case TargetArchitecture.x64:
                        SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "ucrt", "x64"));
                        break;

                    default: throw new InvalidArchitectureException(architecture);
                    }
                }
                break;
            }

            case WindowsPlatformToolset.v141:
            case WindowsPlatformToolset.v142:
            {
                switch (Architecture)
                {
                case TargetArchitecture.AnyCPU: break;

                case TargetArchitecture.ARM:
                    SystemLibraryPaths.Add(Path.Combine(vcToolChainDir, "lib", "arm"));
                    break;

                case TargetArchitecture.ARM64:
                    SystemLibraryPaths.Add(Path.Combine(vcToolChainDir, "lib", "arm64"));
                    break;

                case TargetArchitecture.x86:
                    SystemLibraryPaths.Add(Path.Combine(vcToolChainDir, "lib", "x86"));
                    break;

                case TargetArchitecture.x64:
                    SystemLibraryPaths.Add(Path.Combine(vcToolChainDir, "lib", "x64"));
                    break;

                default: throw new InvalidArchitectureException(architecture);
                }
                break;
            }

            default: throw new ArgumentOutOfRangeException();
            }

            // Add Windows SDK include and library paths
            var windowsSdkDir = sdks[SDK];

            switch (SDK)
            {
            case WindowsPlatformSDK.v8_1:
            {
                string includeRootDir = Path.Combine(windowsSdkDir, "include");
                SystemIncludePaths.Add(Path.Combine(includeRootDir, "shared"));
                SystemIncludePaths.Add(Path.Combine(includeRootDir, "um"));
                SystemIncludePaths.Add(Path.Combine(includeRootDir, "winrt"));
                SystemIncludePaths.Add(Path.Combine(includeRootDir, "ucrt"));

                string libraryRootDir = Path.Combine(windowsSdkDir, "lib", "winv6.3");
                switch (Architecture)
                {
                case TargetArchitecture.AnyCPU: break;

                case TargetArchitecture.ARM:
                {
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "um", "arm"));
                    break;
                }

                case TargetArchitecture.ARM64:
                {
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "um", "arm64"));
                    break;
                }

                case TargetArchitecture.x86:
                {
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "um", "x86"));
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "ucrt", "x86"));
                    var binRootDir = Path.Combine(windowsSdkDir, "bin", "x86");
                    _resourceCompilerPath = Path.Combine(binRootDir, "rc.exe");
                    _makepriPath          = Path.Combine(binRootDir, "makepri.exe");
                    break;
                }

                case TargetArchitecture.x64:
                {
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "um", "x64"));
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "ucrt", "x64"));
                    var binRootDir = Path.Combine(windowsSdkDir, "bin", "x64");
                    _resourceCompilerPath = Path.Combine(binRootDir, "rc.exe");
                    _makepriPath          = Path.Combine(binRootDir, "makepri.exe");
                    break;
                }

                default: throw new InvalidArchitectureException(architecture);
                }
                break;
            }

            case WindowsPlatformSDK.v10_0_10240_0:
            case WindowsPlatformSDK.v10_0_10586_0:
            case WindowsPlatformSDK.v10_0_14393_0:
            case WindowsPlatformSDK.v10_0_15063_0:
            case WindowsPlatformSDK.v10_0_16299_0:
            case WindowsPlatformSDK.v10_0_17134_0:
            case WindowsPlatformSDK.v10_0_17763_0:
            case WindowsPlatformSDK.v10_0_18362_0:
            case WindowsPlatformSDK.v10_0_19041_0:
            {
                var    sdkVersionName = WindowsPlatformBase.GetSDKVersion(SDK).ToString();
                string includeRootDir = Path.Combine(windowsSdkDir, "include", sdkVersionName);
                SystemIncludePaths.Add(Path.Combine(includeRootDir, "ucrt"));
                SystemIncludePaths.Add(Path.Combine(includeRootDir, "shared"));
                SystemIncludePaths.Add(Path.Combine(includeRootDir, "um"));
                SystemIncludePaths.Add(Path.Combine(includeRootDir, "winrt"));

                string libraryRootDir = Path.Combine(windowsSdkDir, "lib", sdkVersionName);
                switch (Architecture)
                {
                case TargetArchitecture.AnyCPU: break;

                case TargetArchitecture.ARM:
                {
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "ucrt", "arm"));
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "um", "arm"));
                    break;
                }

                case TargetArchitecture.ARM64:
                {
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "ucrt", "arm64"));
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "um", "arm64"));
                    break;
                }

                case TargetArchitecture.x86:
                {
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "ucrt", "x86"));
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "um", "x86"));
                    var binRootDir = Path.Combine(windowsSdkDir, "bin", sdkVersionName, "x86");
                    _resourceCompilerPath = Path.Combine(binRootDir, "rc.exe");
                    _makepriPath          = Path.Combine(binRootDir, "makepri.exe");
                    break;
                }

                case TargetArchitecture.x64:
                {
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "ucrt", "x64"));
                    SystemLibraryPaths.Add(Path.Combine(libraryRootDir, "um", "x64"));
                    var binRootDir = Path.Combine(windowsSdkDir, "bin", sdkVersionName, "x64");
                    _resourceCompilerPath = Path.Combine(binRootDir, "rc.exe");
                    _makepriPath          = Path.Combine(binRootDir, "makepri.exe");
                    break;
                }

                default: throw new InvalidArchitectureException(architecture);
                }
                break;
            }

            default: throw new ArgumentOutOfRangeException(nameof(SDK));
            }
        }