Esempio n. 1
0
        /// <summary>
        /// Sets up the parser options to work with the given Visual Studio toolchain.
        /// </summary>
        /// <param name="vsVersion">The version of Visual Studio to look for.</param>
        public void SetupMSVC(VisualStudioVersion vsVersion)
        {
            MicrosoftMode = true;
            Abi           = CppAbi.Microsoft;

            var clVersion = MSVCToolchain.GetCLVersion(vsVersion);

            ToolSetToUse = clVersion.Major * 10000000 + clVersion.Minor * 100000;

            if (!ForceClangToolchainLookup)
            {
                NoStandardIncludes = true;
                NoBuiltinIncludes  = true;

                vsVersion = MSVCToolchain.FindVSVersion(vsVersion);
                foreach (var include in MSVCToolchain.GetSystemIncludes(vsVersion))
                {
                    AddSystemIncludeDirs(include);
                }
            }

            // do not remove the CppSharp prefix becase the Mono C# compiler breaks
            if (!LanguageVersion.HasValue)
            {
                LanguageVersion = CppSharp.Parser.LanguageVersion.CPP14_GNU;
            }

            AddArguments("-fms-extensions");
            AddArguments("-fms-compatibility");
            AddArguments("-fdelayed-template-parsing");
        }
Esempio n. 2
0
        /// Sets up the parser options to work with the given Visual Studio toolchain.
        public void SetupMSVC(VisualStudioVersion vsVersion = VisualStudioVersion.Latest)
        {
            MicrosoftMode      = true;
            NoBuiltinIncludes  = true;
            NoStandardIncludes = true;
            Abi          = CppAbi.Microsoft;
            ToolSetToUse = MSVCToolchain.GetCLVersion(vsVersion) * 10000000;

            addArguments("-fms-extensions");
            addArguments("-fms-compatibility");
            addArguments("-fdelayed-template-parsing");

            var includes = MSVCToolchain.GetSystemIncludes(vsVersion);

            foreach (var include in includes)
            {
                addSystemIncludeDirs(include);
            }
        }
        void CompileMSVC(IEnumerable <string> files)
        {
            List <ToolchainVersion> vsSdks;

            MSVCToolchain.GetVisualStudioSdks(out vsSdks);

            if (vsSdks.Count == 0)
            {
                throw new Exception("Visual Studio SDK was not found on your system.");
            }

            ToolchainVersion vsSdk;

            if (Options.Compilation.VsVersion == VisualStudioVersion.Latest)
            {
                vsSdk = vsSdks.LastOrDefault();
            }
            else
            {
                var exactVersion = vsSdks.Where(vs => (int)vs.Version == (int)Options.Compilation.VsVersion)
                                   .Cast <ToolchainVersion?>().SingleOrDefault();
                if (!exactVersion.HasValue)
                {
                    throw new Exception($"Visual Studio SDK version {Options.Compilation.VsVersion} was not found on your system.");
                }

                vsSdk = exactVersion.Value;
            }

            var clBin = String.Empty;

            if ((int)vsSdk.Version == (int)VisualStudioVersion.VS2017)
            {
                var clFiles = System.IO.Directory.EnumerateFiles(Path.Combine(vsSdk.Directory, @"..\..\VC\Tools\MSVC"), "cl.exe", SearchOption.AllDirectories);
                clBin = clFiles.Where(s => s.Contains(@"x86\cl.exe")).First();
            }
            else
            {
                clBin = Path.GetFullPath(Path.Combine(vsSdk.Directory, "..", "..", "VC", "bin", "cl.exe"));
            }

            Diagnostics.Debug($"VS path {vsSdk.Directory}");

            var monoPath = ManagedToolchain.FindMonoPath();
            var output   = Path.Combine(Options.OutputDir, Options.LibraryName ??
                                        Path.GetFileNameWithoutExtension(Project.Assemblies[0]));

            var args = new List <string> {
                "/nologo",
                $"-D{DLLExportDefine}",
                $"-I\"{monoPath}\\include\\mono-2.0\"",
                string.Join(" ", files.Select(file => "\"" + Path.GetFullPath(file) + "\"")),
                $"\"{GetSgenLibPath(monoPath)}\"",
                Options.Compilation.CompileSharedLibrary ? "/LD" : string.Empty,
                $"/Fe{output}"
            };

            var invocation = string.Join(" ", args);

            var vsVersion = (VisualStudioVersion)(int)vsSdk.Version;
            var includes  = MSVCToolchain.GetSystemIncludes(vsVersion);

            var winSdks = new List <ToolchainVersion>();

            MSVCToolchain.GetWindowsKitsSdks(out winSdks);

            var libParentPath = Directory.GetParent(Directory.EnumerateDirectories(Path.Combine(winSdks.Last().Directory, "lib"), "um", SearchOption.AllDirectories).First());
            var libPaths      = libParentPath.EnumerateDirectories();

            Dictionary <string, string> envVars = null;

            if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("INCLUDE")))
            {
                envVars            = new Dictionary <string, string>();
                envVars["INCLUDE"] = string.Join(";", includes);

                var clLib = Path.GetFullPath(
                    Path.Combine(vsSdk.Directory, "..", "..", "VC", "lib"));
                envVars["LIB"] = clLib + ";" + string.Join(";", libPaths.Select(path => Path.Combine(path.FullName, "x86")));
            }

            Invoke(clBin, invocation, envVars);
        }
Esempio n. 4
0
        bool CompileMSVC(IEnumerable <string> files)
        {
            var vsSdks = MSVCToolchain.GetVisualStudioSdks();

            // Skip TestAgent VS instances as they do not provide native toolchains.
            vsSdks = vsSdks.Where(sdk => !sdk.Directory.Contains("TestAgent")).ToList();

            if (vsSdks.Count == 0)
            {
                throw new Exception("Visual Studio SDK was not found on your system.");
            }

            ToolchainVersion vsSdk;

            if (Options.Compilation.VsVersion == VisualStudioVersion.Latest)
            {
                vsSdk = vsSdks.LastOrDefault();
            }
            else
            {
                var exactVersion = vsSdks.Where(vs => (int)vs.Version == (int)Options.Compilation.VsVersion)
                                   .Cast <ToolchainVersion?>().SingleOrDefault();
                if (!exactVersion.HasValue)
                {
                    throw new Exception($"Visual Studio SDK version {Options.Compilation.VsVersion} was not found on your system.");
                }

                vsSdk = exactVersion.Value;
            }

            var clBin = string.Empty;
            var clLib = string.Empty;

            const string clArch = "x86";

            var isVS2017OrGreater = (int)vsSdk.Version >= (int)VisualStudioVersion.VS2017;

            if (isVS2017OrGreater)
            {
                var clBaseDir = Directory.EnumerateDirectories(Path.Combine(vsSdk.Directory, @"..\..\VC\Tools\MSVC")).Last();
                clBin = Path.Combine(clBaseDir, $"bin\\Hostx86\\{clArch}\\cl.exe");
                clLib = Path.Combine(clBaseDir, $"lib\\{clArch}");
            }
            else
            {
                clBin = Path.GetFullPath(Path.Combine(vsSdk.Directory, "..", "..", "VC", "bin", "cl.exe"));
                clLib = Path.GetFullPath(Path.Combine(vsSdk.Directory, "..", "..", "VC", "lib"));
            }

            Diagnostics.Debug($"VS path {vsSdk.Directory}");

            var outputPath = Path.Combine(Options.OutputDir, Options.LibraryName ??
                                          Path.GetFileNameWithoutExtension(Project.Assemblies[0]));

            var args = new List <string> {
                "/nologo",
                $"-D{DLLExportDefine}",
                $"-I\"{MonoSdkPath}\\include\\mono-2.0\"",
                string.Join(" ", files.Select(file => "\"" + Path.GetFullPath(file) + "\"")),
                $"\"{GetSgenLibPath(MonoSdkPath)}\"",
                Options.Compilation.CompileSharedLibrary ? "/LD" : string.Empty,
                $"/Fe{outputPath}"
            };

            var invocation = string.Join(" ", args);

            var vsVersion = (VisualStudioVersion)(int)vsSdk.Version;
            var includes  = MSVCToolchain.GetSystemIncludes(vsVersion);

            var winSdks = MSVCToolchain.GetWindowsKitsSdks();

            var libParentPath = Directory.GetParent(Directory.EnumerateDirectories(
                                                        Path.Combine(winSdks.Last().Directory, "lib"), "um", SearchOption.AllDirectories).First());
            var libPaths = libParentPath.EnumerateDirectories();

            Dictionary <string, string> envVars = null;

            if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("INCLUDE")))
            {
                envVars            = new Dictionary <string, string>();
                envVars["INCLUDE"] = string.Join(";", includes);
                envVars["LIB"]     = Path.GetFullPath(clLib) + ";" +
                                     string.Join(";", libPaths.Select(path => Path.Combine(path.FullName, clArch)));
            }

            var output = Invoke(clBin, invocation, envVars);

            return(output.ExitCode == 0);
        }
Esempio n. 5
0
        void CompileCode()
        {
            var files = GetOutputFiles("c");

            switch (Options.Language)
            {
            case GeneratorKind.ObjectiveC:
                files = files.Concat(GetOutputFiles("mm"));
                break;

            case GeneratorKind.CPlusPlus:
                files = files.Concat(GetOutputFiles("cpp"));
                break;
            }

            const string exportDefine = "MONO_M2N_DLL_EXPORT";

            if (Platform.IsWindows)
            {
                List <ToolchainVersion> vsSdks;
                MSVCToolchain.GetVisualStudioSdks(out vsSdks);

                if (vsSdks.Count == 0)
                {
                    throw new Exception("Visual Studio SDK was not found on your system.");
                }

                var vsSdk = vsSdks.FirstOrDefault();
                var clBin = Path.GetFullPath(
                    Path.Combine(vsSdk.Directory, "..", "..", "VC", "bin", "cl.exe"));

                var monoPath = ManagedToolchain.FindMonoPath();
                var output   = Options.LibraryName ??
                               Path.GetFileNameWithoutExtension(Options.Project.Assemblies[0]);
                output = Path.Combine(Options.OutputDir, output);
                var invocation = string.Format(
                    "/nologo /D{0} -I\"{1}\\include\\mono-2.0\" {2} \"{1}\\lib\\monosgen-2.0.lib\" {3} {4}",
                    exportDefine, monoPath, string.Join(" ", files.ToList()),
                    Options.CompileSharedLibrary ? "/LD" : string.Empty,
                    output);

                var vsVersion = (VisualStudioVersion)(int)vsSdk.Version;
                var includes  = MSVCToolchain.GetSystemIncludes(vsVersion);

                Dictionary <string, string> envVars = null;
                if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("INCLUDE")))
                {
                    envVars            = new Dictionary <string, string>();
                    envVars["INCLUDE"] = string.Join(";", includes);

                    var clLib = Path.GetFullPath(
                        Path.Combine(vsSdk.Directory, "..", "..", "VC", "lib"));
                    envVars["LIB"] = clLib;
                }

                InvokeCompiler(clBin, invocation, envVars);

                return;
            }
            else if (Platform.IsMacOS)
            {
                var xcodePath = XcodeToolchain.GetXcodeToolchainPath();
                var clangBin  = Path.Combine(xcodePath, "usr/bin/clang");
                var monoPath  = ManagedToolchain.FindMonoPath();

                var invocation = string.Format(
                    "-D{0} -framework CoreFoundation -I\"{1}/include/mono-2.0\" " +
                    "-L\"{1}/lib/\" -lmonosgen-2.0 {2}",
                    exportDefine, monoPath, string.Join(" ", files.ToList()));

                InvokeCompiler(clangBin, invocation);

                return;
            }

            throw new NotImplementedException();
        }
Esempio n. 6
0
        void CompileMSVC(IEnumerable <string> files)
        {
            List <ToolchainVersion> vsSdks;

            MSVCToolchain.GetVisualStudioSdks(out vsSdks);

            if (vsSdks.Count == 0)
            {
                throw new Exception("Visual Studio SDK was not found on your system.");
            }

            ToolchainVersion vsSdk;

            if (Options.VsVersion == VisualStudioVersion.Latest)
            {
                vsSdk = vsSdks.LastOrDefault();
            }
            else
            {
                var exactVersion = vsSdks.Where(vs => (int)vs.Version == (int)Options.VsVersion)
                                   .Cast <ToolchainVersion?>().SingleOrDefault();
                if (!exactVersion.HasValue)
                {
                    throw new Exception($"Visual Studio SDK version {Options.VsVersion} was not found on your system.");
                }

                vsSdk = exactVersion.Value;
            }

            var clBin = Path.GetFullPath(
                Path.Combine(vsSdk.Directory, "..", "..", "VC", "bin", "cl.exe"));

            var monoPath = ManagedToolchain.FindMonoPath();
            var output   = Path.Combine(Options.OutputDir, Options.LibraryName ??
                                        Path.GetFileNameWithoutExtension(Project.Assemblies[0]));

            var args = new List <string> {
                "/nologo",
                $"-D{DLLExportDefine}",
                $"-I\"{monoPath}\\include\\mono-2.0\"",
                string.Join(" ", files.Select(file => Path.GetFullPath(file))),
                $"\"{monoPath}\\lib\\monosgen-2.0.lib\"",
                Options.CompileSharedLibrary ? "/LD" : string.Empty,
                $"/Fe{output}"
            };

            var invocation = string.Join(" ", args);

            var vsVersion = (VisualStudioVersion)(int)vsSdk.Version;
            var includes  = MSVCToolchain.GetSystemIncludes(vsVersion);

            Dictionary <string, string> envVars = null;

            if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("INCLUDE")))
            {
                envVars            = new Dictionary <string, string>();
                envVars["INCLUDE"] = string.Join(";", includes);

                var clLib = Path.GetFullPath(
                    Path.Combine(vsSdk.Directory, "..", "..", "VC", "lib"));
                envVars["LIB"] = clLib;
            }

            Invoke(clBin, invocation, envVars);
        }