Example #1
0
        public static void ConstructCommandLine(CreateCompileCommand export, out string command, out string commandArguments)
        {
            export.Report(MessageImportance.Low, $"Building for Windows");

            WinSDK winSdk    = g_WinSdk.Value;
            string vsInstall = g_VsInstallPath.Value;
            string vcToolDir = GetVCToolsRootDir(vsInstall);

            export.Report(CreateCompileCommand.DevImportance, $"VS Install: {vsInstall}\nVC Tools: {vcToolDir}\nWinSDK Version: {winSdk.Version}");

            bool isDebug = IsDebug(export.Configuration);
            bool is64Bit = Is64BitTarget(export.Architecture);

            var archDir = is64Bit ? "x64" : "x86";

            // VC inc and lib paths
            var vcIncDir = Path.Combine(vcToolDir, "include");
            var libDir   = Path.Combine(vcToolDir, "lib", archDir);

            // For now we assume building always happens on a x64 machine.
            var binDir = Path.Combine(vcToolDir, "bin\\Hostx64", archDir);

            // Create arguments
            var compilerFlags = new StringBuilder();
            var linkerFlags   = new StringBuilder();

            SetConfigurationBasedFlags(isDebug, ref compilerFlags, ref linkerFlags);

            // Set compiler flags
            compilerFlags.Append($"/TC /MT /GS /Zi ");
            compilerFlags.Append($"/D DNNE_ASSEMBLY_NAME={export.AssemblyName} ");
            compilerFlags.Append($"/I \"{vcIncDir}\" /I \"{export.PlatformPath}\" /I \"{export.NetHostPath}\" ");

            // Add WinSDK inc paths
            foreach (var incPath in winSdk.IncPaths)
            {
                compilerFlags.Append($"/I \"{incPath}\" ");
            }

            compilerFlags.Append($"\"{export.Source}\" \"{Path.Combine(export.PlatformPath, "platform.c")}\" ");

            // Set linker flags
            linkerFlags.Append($"/DLL ");
            linkerFlags.Append($"/LIBPATH:\"{libDir}\" ");

            // Add WinSDK lib paths
            foreach (var libPath in winSdk.LibPaths)
            {
                linkerFlags.Append($"/LIBPATH:\"{Path.Combine(libPath, archDir)}\" ");
            }

            linkerFlags.Append($"\"{Path.Combine(export.NetHostPath, "libnethost.lib")}\" Advapi32.lib ");
            linkerFlags.Append($"/IGNORE:4099 "); // libnethost.lib doesn't ship PDBs so linker warnings occur.
            linkerFlags.Append($"/out:\"{Path.Combine(export.OutputPath, export.OutputName)}\" ");

            command          = Path.Combine(binDir, "cl.exe");
            commandArguments = $"{compilerFlags} /link {linkerFlags}";
        }
Example #2
0
        /// <inheritdoc />
        public override bool Execute()
        {
            PlatformType platform = ConvertPlatform();

            var       vsInstalls = g_VsInstalls.Value;
            VSInstall vsInstall  = null;

            if (string.IsNullOrEmpty(this.VSVersion))
            {
                vsInstall = vsInstalls.First();
            }
            else
            {
                if (!Version.TryParse(this.VSVersion, out Version vs))
                {
                    throw new Exception($"Invalid version format for Visual Studio: '{this.VSVersion}'");
                }

                // Enumerate VS versions.
                foreach (var versionMaybe in vsInstalls)
                {
                    this.Log.LogMessage(MessageImportance.Normal, $"Consider Visual Studio version: {versionMaybe.Version}");
                    if (versionMaybe.Version == vs)
                    {
                        vsInstall = versionMaybe;
                        break;
                    }
                }

                if (vsInstall == null)
                {
                    throw new Exception($"Visual Studio version not found: {this.VSVersion}");
                }
            }

            var winSdks = g_WinSdks.Value;

            if (!winSdks.Any())
            {
                this.Log.LogMessage(MessageImportance.High, "No Windows SDKs found.");
            }

            WinSDK winSdk = null;

            if (string.IsNullOrEmpty(this.WinSDKVersion))
            {
                winSdk = winSdks.FirstOrDefault() ?? new WinSDK();
            }
            else
            {
                if (!Version.TryParse(this.WinSDKVersion, out Version vs))
                {
                    throw new Exception($"Invalid version format for Windows SDK: '{this.WinSDKVersion}'");
                }

                // Enumerate WinSDK versions.
                foreach (var versionMaybe in winSdks)
                {
                    this.Log.LogMessage(MessageImportance.Normal, $"Consider Windows SDK version: {versionMaybe.Version}");
                    if (versionMaybe.Version == vs)
                    {
                        winSdk = versionMaybe;
                        break;
                    }
                }

                if (winSdk == null)
                {
                    throw new Exception($"Windows SDK version not found: {this.WinSDKVersion}");
                }
            }

            string vcToolDir = GetVCToolsRootDir(vsInstall.Install);

            // Compute bin directory for compiler
            var hostPlatform = Environment.Is64BitOperatingSystem ? PlatformType.x64 : PlatformType.x86;

            this.CompilerPath = Path.Combine(vcToolDir, "bin", $"Host{hostPlatform}", platform.ToString(), "cl.exe");

            // Collect the include paths
            var incPaths = new List <string>();

            var vcIncDir = Path.Combine(vcToolDir, "include");

            incPaths.Add(vcIncDir);

            // Add WinSDK inc paths
            incPaths.AddRange(winSdk.IncPaths);

            this.IncludePaths = incPaths.ToArray();

            // Collect the lib paths
            var libPaths = new List <string>();

            var vcLibDir = Path.Combine(vcToolDir, "lib", platform.ToString());

            libPaths.Add(vcLibDir);

            // Add WinSDK lib paths
            foreach (var libPath in winSdk.LibPaths)
            {
                libPaths.Add(Path.Combine(libPath, platform.ToString()));
            }

            this.LibPaths = libPaths.ToArray();

            this.WinSDKToolPath = Path.Combine(winSdk.BinPath, platform.ToString());

            return(true);
        }
Example #3
0
        public static void ConstructCommandLine(CreateCompileCommand export, out string command, out string commandArguments)
        {
            export.Report(MessageImportance.Low, $"Building for Windows");

            WinSDK winSdk    = g_WinSdk.Value;
            string vsInstall = g_VsInstallPath.Value;
            string vcToolDir = GetVCToolsRootDir(vsInstall);

            export.Report(CreateCompileCommand.DevImportance, $"VS Install: {vsInstall}\nVC Tools: {vcToolDir}\nWinSDK Version: {winSdk.Version}");

            bool isDebug = IsDebug(export.Configuration);
            bool is64Bit = Is64BitTarget(export.Architecture, export.RuntimeID);

            var archDir = is64Bit ? "x64" : "x86";

            // VC inc and lib paths
            var vcIncDir = Path.Combine(vcToolDir, "include");
            var libDir   = Path.Combine(vcToolDir, "lib", archDir);

            // For now we assume building always happens on a x64 machine.
            var binDir = Path.Combine(vcToolDir, "bin\\Hostx64", archDir);

            // Create arguments
            var compilerFlags = new StringBuilder();
            var linkerFlags   = new StringBuilder();

            SetConfigurationBasedFlags(isDebug, ref compilerFlags, ref linkerFlags);

            // Set compiler flags
            compilerFlags.Append($"/TC /MT /GS /Zi ");
            compilerFlags.Append($"/D DNNE_ASSEMBLY_NAME={export.AssemblyName} /D DNNE_COMPILE_AS_SOURCE ");
            compilerFlags.Append($"/I \"{vcIncDir}\" /I \"{export.PlatformPath}\" /I \"{export.NetHostPath}\" ");

            // Add WinSDK inc paths
            foreach (var incPath in winSdk.IncPaths)
            {
                compilerFlags.Append($"/I \"{incPath}\" ");
            }

            // Add user defined inc paths last - these will be searched last on MSVC.
            // https://docs.microsoft.com/cpp/build/reference/i-additional-include-directories#remarks
            foreach (var incPath in export.SafeAdditionalIncludeDirectories)
            {
                compilerFlags.Append($"/I \"{incPath.ItemSpec}\" ");
            }

            compilerFlags.Append($"\"{export.Source}\" \"{Path.Combine(export.PlatformPath, "platform.c")}\" ");

            // Set linker flags
            linkerFlags.Append($"/DLL ");
            linkerFlags.Append($"/LIBPATH:\"{libDir}\" ");

            // Add WinSDK lib paths
            foreach (var libPath in winSdk.LibPaths)
            {
                linkerFlags.Append($"/LIBPATH:\"{Path.Combine(libPath, archDir)}\" ");
            }

            linkerFlags.Append($"\"{Path.Combine(export.NetHostPath, "libnethost.lib")}\" Advapi32.lib ");
            linkerFlags.Append($"/IGNORE:4099 "); // libnethost.lib doesn't ship PDBs so linker warnings occur.

            // Define artifact names
            var outputPath = Path.Combine(export.OutputPath, export.OutputName);
            var impLibPath = Path.ChangeExtension(outputPath, ".lib");

            linkerFlags.Append($"/IMPLIB:\"{impLibPath}\" /OUT:\"{outputPath}\" ");

            command          = Path.Combine(binDir, "cl.exe");
            commandArguments = $"{compilerFlags} /link {linkerFlags}";
        }