Example #1
0
        private VCEnvironment(CPPTargetPlatform InPlatform, WindowsCompiler InCompiler, bool bSupportWindowsXP)
        {
            Platform = InPlatform;
            Compiler = InCompiler;

            // Get the Visual Studio install directory
            WindowsPlatform.TryGetVSInstallDir(Compiler, out VSInstallDir);

            // Get the Visual C++ compiler install directory.
            if (!WindowsPlatform.TryGetVCInstallDir(Compiler, out VCInstallDir))
            {
                throw new BuildException(WindowsPlatform.GetCompilerName(Compiler) + " must be installed in order to build this target.");
            }

            // Figure out the default toolchain directory. VS15 separates this out into separate directories, with platforms as subdirectories under that.
            DirectoryReference VCToolChainDir = null;

            if (Compiler == WindowsCompiler.VisualStudio2017)
            {
                string Version = File.ReadAllText(FileReference.Combine(VCInstallDir, "Auxiliary", "Build", "Microsoft.VCToolsVersion.default.txt").FullName).Trim();
                VCToolChainDir = DirectoryReference.Combine(VCInstallDir, "Tools", "MSVC", Version);
            }

            WindowsSDKDir          = FindWindowsSDKInstallationFolder(Platform, Compiler, bSupportWindowsXP);
            WindowsSDKLibVersion   = FindWindowsSDKLibVersion(WindowsSDKDir, bSupportWindowsXP);
            WindowsSDKExtensionDir = bSupportWindowsXP ? "" : FindWindowsSDKExtensionInstallationFolder(Compiler);
            NetFxSDKExtensionDir   = bSupportWindowsXP ? "" : FindNetFxSDKExtensionInstallationFolder(Compiler);
            WindowsSDKExtensionHeaderLibVersion = bSupportWindowsXP ? new Version(0, 0, 0, 0) : FindWindowsSDKExtensionLatestVersion(WindowsSDKExtensionDir);
            UniversalCRTDir     = bSupportWindowsXP ? "" : FindUniversalCRTInstallationFolder(Compiler);
            UniversalCRTVersion = bSupportWindowsXP ? "0.0.0.0" : FindUniversalCRTVersion(UniversalCRTDir);

            VCToolPath32 = GetVCToolPath32(Compiler, VCInstallDir, VCToolChainDir);
            VCToolPath64 = GetVCToolPath64(Compiler, VCInstallDir, VCToolChainDir);

            // Compile using 64 bit tools for 64 bit targets, and 32 for 32.
            DirectoryReference CompilerDir = (Platform == CPPTargetPlatform.Win64) ? VCToolPath64 : VCToolPath32;

            // Regardless of the target, if we're linking on a 64 bit machine, we want to use the 64 bit linker (it's faster than the 32 bit linker and can handle large linking jobs)
            DirectoryReference LinkerDir = VCToolPath64;

            CompilerPath         = GetCompilerToolPath(InPlatform, CompilerDir);
            CLExeVersion         = FindCLExeVersion(CompilerPath.FullName);
            LinkerPath           = GetLinkerToolPath(InPlatform, LinkerDir);
            LibraryManagerPath   = GetLibraryLinkerToolPath(InPlatform, LinkerDir);
            ResourceCompilerPath = new FileReference(GetResourceCompilerToolPath(Platform, bSupportWindowsXP));

            // Make sure the base 32-bit VS tool path is in the PATH, regardless of which configuration we're using. The toolchain may need to reference support DLLs from this directory (eg. mspdb120.dll).
            string PathEnvironmentVariable = Environment.GetEnvironmentVariable("PATH") ?? "";

            if (!PathEnvironmentVariable.Split(';').Any(x => String.Compare(x, VCToolPath32.FullName, true) == 0))
            {
                PathEnvironmentVariable = VCToolPath32.FullName + ";" + PathEnvironmentVariable;
                Environment.SetEnvironmentVariable("PATH", PathEnvironmentVariable);
            }

            // Setup the INCLUDE environment variable
            List <string> IncludePaths = GetVisualCppIncludePaths(Compiler, VCInstallDir, VCToolChainDir, UniversalCRTDir, UniversalCRTVersion, NetFxSDKExtensionDir, WindowsSDKDir, WindowsSDKLibVersion, bSupportWindowsXP);

            if (InitialIncludePaths != null)
            {
                IncludePaths.Add(InitialIncludePaths);
            }
            Environment.SetEnvironmentVariable("INCLUDE", String.Join(";", IncludePaths));

            // Setup the LIB environment variable
            List <string> LibraryPaths = GetVisualCppLibraryPaths(Compiler, VCInstallDir, VCToolChainDir, UniversalCRTDir, UniversalCRTVersion, NetFxSDKExtensionDir, WindowsSDKDir, WindowsSDKLibVersion, Platform, bSupportWindowsXP);

            if (InitialLibraryPaths != null)
            {
                LibraryPaths.Add(InitialLibraryPaths);
            }
            Environment.SetEnvironmentVariable("LIB", String.Join(";", LibraryPaths));
        }
Example #2
0
        private void WriteEnvironmentSetup()
        {
            DirectoryReference VCInstallDir = null;
            string             VCToolPath64 = "";
            VCEnvironment      VCEnv        = null;

            try
            {
                VCEnv = VCEnvironment.Create(WindowsPlatform.GetDefaultCompiler(null), CppPlatform.Win64, null, null);
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to get Visual Studio environment.");
            }

            // Copy environment into a case-insensitive dictionary for easier key lookups
            Dictionary <string, string> envVars = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (DictionaryEntry entry in Environment.GetEnvironmentVariables())
            {
                envVars[(string)entry.Key] = (string)entry.Value;
            }

            if (envVars.ContainsKey("CommonProgramFiles"))
            {
                AddText("#import CommonProgramFiles\n");
            }

            if (envVars.ContainsKey("DXSDK_DIR"))
            {
                AddText("#import DXSDK_DIR\n");
            }

            if (envVars.ContainsKey("DurangoXDK"))
            {
                AddText("#import DurangoXDK\n");
            }

            if (VCEnv != null)
            {
                string platformVersionNumber = "VSVersionUnknown";
                platformVersionNumber = "140";


                if (!WindowsPlatform.TryGetVSInstallDir(WindowsPlatform.GetDefaultCompiler(null), out VCInstallDir))
                {
                    string exceptionString = "Error: Cannot locate Visual Studio Installation.";
                    Console.WriteLine(exceptionString);
                    throw new BuildException(exceptionString);
                }

                VCToolPath64 = Environment.GetEnvironmentVariable("VC_COMPILER_DIR");

                AddText(string.Format(".WindowsSDKBasePath = '{0}'\n", VCEnv.WindowsSdkDir));

                AddText("Compiler('UE4ResourceCompiler') \n{\n");
                AddText(string.Format("\t.Executable = '{0}'\n", VCEnv.ResourceCompilerPath));
                AddText("\t.CompilerFamily  = 'custom'\n");
                AddText("}\n\n");


                AddText("Compiler('UE4Compiler') \n{\n");

                AddText(string.Format("\t.Root = '{0}'\n", VCEnv.CompilerPath.Directory));
                AddText("\t.Executable = '$Root$/cl.exe'\n");
                AddText("\t.ExtraFiles =\n\t{\n");
                AddText("\t\t'$Root$/c1.dll'\n");
                AddText("\t\t'$Root$/c1xx.dll'\n");
                AddText("\t\t'$Root$/c2.dll'\n");

                if (File.Exists(FileReference.Combine(VCEnv.CompilerPath.Directory, "1033/clui.dll").ToString()))                 //Check English first...
                {
                    AddText("\t\t'$Root$/1033/clui.dll'\n");
                }
                else
                {
                    var numericDirectories = Directory.GetDirectories(VCToolPath64).Where(d => Path.GetFileName(d).All(char.IsDigit));
                    var cluiDirectories    = numericDirectories.Where(d => Directory.GetFiles(d, "clui.dll").Any());
                    if (cluiDirectories.Any())
                    {
                        AddText(string.Format("\t\t'$Root$/{0}/clui.dll'\n", Path.GetFileName(cluiDirectories.First())));
                    }
                }
                AddText("\t\t'$Root$/mspdbsrv.exe'\n");
                AddText("\t\t'$Root$/mspdbcore.dll'\n");

                AddText(string.Format("\t\t'$Root$/mspft{0}.dll'\n", platformVersionNumber));
                AddText(string.Format("\t\t'$Root$/msobj{0}.dll'\n", platformVersionNumber));
                AddText(string.Format("\t\t'$Root$/mspdb{0}.dll'\n", platformVersionNumber));

                AddText(string.Format("\t\t'{0}/VC/Redist/MSVC/14.21.27702/x64/Microsoft.VC142.CRT/msvcp{1}.dll'\n", VCInstallDir.ToString(), platformVersionNumber));
                AddText(string.Format("\t\t'{0}/VC/Redist/MSVC/14.21.27702/x64/Microsoft.VC142.CRT/vccorlib{1}.dll'\n", VCInstallDir.ToString(), platformVersionNumber));


                AddText("\t}\n");                 //End extra files

                AddText("}\n\n");                 //End compiler
            }

            AddText("Settings \n{\n");

            //Start Environment
            AddText("\t.Environment = \n\t{\n");
            if (VCEnv != null)
            {
                AddText(string.Format("\t\t\"PATH={0}\\Common7\\IDE\\;{1}\",\n", VCInstallDir.ToString(), VCToolPath64));
                if (VCEnv.IncludePaths.Count() > 0)
                {
                    AddText(string.Format("\t\t\"INCLUDE={0}\",\n", String.Join(";", VCEnv.IncludePaths.Select(x => x))));
                }

                if (VCEnv.LibraryPaths.Count() > 0)
                {
                    AddText(string.Format("\t\t\"LIB={0}\",\n", String.Join(";", VCEnv.LibraryPaths.Select(x => x))));
                }
            }
            if (envVars.ContainsKey("TMP"))
            {
                AddText(string.Format("\t\t\"TMP={0}\",\n", envVars["TMP"]));
            }
            if (envVars.ContainsKey("SystemRoot"))
            {
                AddText(string.Format("\t\t\"SystemRoot={0}\",\n", envVars["SystemRoot"]));
            }
            if (envVars.ContainsKey("INCLUDE"))
            {
                AddText(string.Format("\t\t\"INCLUDE={0}\",\n", envVars["INCLUDE"]));
            }
            if (envVars.ContainsKey("LIB"))
            {
                AddText(string.Format("\t\t\"LIB={0}\",\n", envVars["LIB"]));
            }

            AddText("\t}\n");             //End environment
            AddText("}\n\n");             //End Settings
        }
 /// <summary>
 /// Tries to get the directory for an installed Visual Studio version
 /// </summary>
 /// <param name="Compiler">The compiler version</param>
 /// <param name="InstallDir">Receives the install directory on success</param>
 /// <returns>True if successful</returns>
 public static bool TryGetVSInstallDir(WindowsCompiler Compiler, out DirectoryReference InstallDir)
 {
     return(WindowsPlatform.TryGetVSInstallDir(Compiler, out InstallDir));
 }
        private void WriteEnvironmentSetup()
        {
            DirectoryReference VCInstallDir = null;
            string             VCToolPath64 = "";
            VCEnvironment      VCEnv        = null;

            try
            {
                VCEnv = VCEnvironment.Create(WindowsPlatform.GetDefaultCompiler(null), UnrealTargetPlatform.Win64, WindowsArchitecture.x64, null, null, null);
            }
            catch (Exception)
            {
                Log.TraceError("Failed to get Visual Studio environment.");
            }

            // Copy environment into a case-insensitive dictionary for easier key lookups
            Dictionary <string, string> envVars = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (DictionaryEntry entry in Environment.GetEnvironmentVariables())
            {
                envVars[(string)entry.Key] = (string)entry.Value;
            }

            if (envVars.ContainsKey("CommonProgramFiles"))
            {
                AddText("#import CommonProgramFiles\n");
            }

            if (envVars.ContainsKey("DXSDK_DIR"))
            {
                AddText("#import DXSDK_DIR\n");
            }

            if (envVars.ContainsKey("DurangoXDK"))
            {
                AddText("#import DurangoXDK\n");
            }

            if (VCEnv != null)
            {
                string platformVersionNumber = "VSVersionUnknown";

                switch (VCEnv.Compiler)
                {
                case WindowsCompiler.VisualStudio2017:
                    // For now we are working with the 140 version, might need to change to 141 or 150 depending on the version of the Toolchain you chose
                    // to install
                    platformVersionNumber = "140";
                    break;

                case WindowsCompiler.VisualStudio2019:
                    platformVersionNumber = "140";
                    break;

                default:
                    string exceptionString = "Error: Unsupported Visual Studio Version.";
                    Log.TraceError(exceptionString);
                    throw new BuildException(exceptionString);
                }


                if (!WindowsPlatform.TryGetVSInstallDir(WindowsPlatform.GetDefaultCompiler(null), out VCInstallDir))
                {
                    string exceptionString = "Error: Cannot locate Visual Studio Installation.";
                    Log.TraceError(exceptionString);
                    throw new BuildException(exceptionString);
                }

                VCToolPath64 = VCEnv.CompilerPath.Directory.ToString() + "\\";

                AddText(string.Format(".WindowsSDKBasePath = '{0}'\n", VCEnv.WindowsSdkDir));

                AddText("Compiler('UE4ResourceCompiler') \n{\n");
                AddText(string.Format("\t.Executable = '{0}'\n", VCEnv.ResourceCompilerPath));
                AddText("\t.CompilerFamily  = 'custom'\n");
                AddText("}\n\n");


                AddText("Compiler('UE4Compiler') \n{\n");

                AddText(string.Format("\t.Root = '{0}'\n", VCEnv.CompilerPath.Directory));
                AddText("\t.Executable = '$Root$/cl.exe'\n");
                AddText("\t.ExtraFiles =\n\t{\n");
                AddText("\t\t'$Root$/c1.dll'\n");
                AddText("\t\t'$Root$/c1xx.dll'\n");
                AddText("\t\t'$Root$/c2.dll'\n");


                if (File.Exists(FileReference.Combine(VCEnv.CompilerPath.Directory, "1033/clui.dll").ToString())) //Check English first...
                {
                    AddText("\t\t'$Root$/1033/clui.dll'\n");
                }
                else
                {
                    var numericDirectories = Directory.GetDirectories(VCToolPath64).Where(d => Path.GetFileName(d).All(char.IsDigit));
                    var cluiDirectories    = numericDirectories.Where(d => Directory.GetFiles(d, "clui.dll").Any());
                    if (cluiDirectories.Any())
                    {
                        AddText(string.Format("\t\t'$Root$/{0}/clui.dll'\n", Path.GetFileName(cluiDirectories.First())));
                    }
                }
                AddText("\t\t'$Root$/mspdbsrv.exe'\n");
                AddText("\t\t'$Root$/mspdbcore.dll'\n");



                AddText(string.Format("\t\t'$Root$/mspft{0}.dll'\n", platformVersionNumber));
                AddText(string.Format("\t\t'$Root$/msobj{0}.dll'\n", platformVersionNumber));
                AddText(string.Format("\t\t'$Root$/mspdb{0}.dll'\n", platformVersionNumber));


                var redistDirs = Directory.GetDirectories(VCInstallDir.ToString() + "\\VC\\Redist\\MSVC\\", "*", SearchOption.TopDirectoryOnly);



                if (redistDirs.Length > 0)
                {
                    Regex  regex     = new Regex(@"\d{2}\.\d{2}\.\d{5}$");
                    string redistDir = redistDirs.First((s) =>
                    {
                        return(regex.IsMatch(s));
                    });
                    if (VCEnv.Compiler == WindowsCompiler.VisualStudio2019)
                    {
                        AddText(string.Format("\t\t'{0}/x64/Microsoft.VC142.CRT/msvcp{1}.dll'\n", redistDir, platformVersionNumber));
                        AddText(string.Format("\t\t'{0}/x64/Microsoft.VC142.CRT/vccorlib{1}.dll'\n", redistDir, platformVersionNumber));
                        AddText("\t\t'$Root$/tbbmalloc.dll'\n");
                    }
                    else if (VCEnv.Compiler == WindowsCompiler.VisualStudio2017)
                    {
                        //VS 2017 is really confusing in terms of version numbers and paths so these values might need to be modified depending on what version of the tool chain you
                        // chose to install.
                        AddText(string.Format("\t\t'{0}/x64/Microsoft.VC141.CRT/msvcp{1}.dll'\n", redistDir, platformVersionNumber));
                        AddText(string.Format("\t\t'{0}/x64/Microsoft.VC141.CRT/vccorlib{1}.dll'\n", redistDir, platformVersionNumber));
                    }
                }

                AddText("\t}\n"); //End extra files

                AddText("}\n\n"); //End compiler
            }

            if (envVars.ContainsKey("SCE_ORBIS_SDK_DIR"))
            {
                AddText(string.Format(".SCE_ORBIS_SDK_DIR = '{0}'\n", envVars["SCE_ORBIS_SDK_DIR"]));
                AddText(string.Format(".PS4BasePath = '{0}/host_tools/bin'\n\n", envVars["SCE_ORBIS_SDK_DIR"]));
                AddText("Compiler('UE4PS4Compiler') \n{\n");
                AddText("\t.Executable = '$PS4BasePath$/orbis-clang.exe'\n");
                AddText("\t.ExtraFiles = '$PS4BasePath$/orbis-snarl.exe'\n");
                AddText("}\n\n");
            }

            AddText("Settings \n{\n");

            // Optional cachePath user setting
            if (bEnableCaching && CachePath != "")
            {
                AddText(string.Format("\t.CachePath = '{0}'\n", CachePath));
            }

            //Start Environment
            AddText("\t.Environment = \n\t{\n");
            if (VCEnv != null)
            {
                AddText(string.Format("\t\t\"PATH={0}\\Common7\\IDE\\;{1};{2}\",\n", VCInstallDir.ToString(), VCToolPath64, VCEnv.ResourceCompilerPath.Directory));
                if (VCEnv.IncludePaths.Count() > 0)
                {
                    AddText(string.Format("\t\t\"INCLUDE={0}\",\n", String.Join(";", VCEnv.IncludePaths.Select(x => x))));
                }

                if (VCEnv.LibraryPaths.Count() > 0)
                {
                    AddText(string.Format("\t\t\"LIB={0}\",\n", String.Join(";", VCEnv.LibraryPaths.Select(x => x))));
                }
            }
            if (envVars.ContainsKey("TMP"))
            {
                AddText(string.Format("\t\t\"TMP={0}\",\n", envVars["TMP"]));
            }
            if (envVars.ContainsKey("SystemRoot"))
            {
                AddText(string.Format("\t\t\"SystemRoot={0}\",\n", envVars["SystemRoot"]));
            }
            if (envVars.ContainsKey("INCLUDE"))
            {
                AddText(string.Format("\t\t\"INCLUDE={0}\",\n", envVars["INCLUDE"]));
            }
            if (envVars.ContainsKey("LIB"))
            {
                AddText(string.Format("\t\t\"LIB={0}\",\n", envVars["LIB"]));
            }

            AddText("\t}\n"); //End environment


            AddText("}\n\n"); //End Settings
        }