예제 #1
0
        protected override void BuildCefApi(CodeNamespace ns, CefApiClass @class)
        {
            var decl = new CodeClass(GetClassName(@class.Name));

            decl.Attributes = CodeAttributes.Public | CodeAttributes.Static | CodeAttributes.Partial;
            decl.Comments.AddVSDocComment(@class.Comment, "summary");
            decl.Members.Add(new CodeField("string", "ApiHash")
            {
                Value = "\"" + @class.ApiHash + "\"", Attributes = CodeAttributes.Public | CodeAttributes.Static | CodeAttributes.ReadOnly
            });

            foreach (CppFunction fn in @class.Functions.OrderBy(f => f.Name))
            {
                if (fn.Name == "cef_get_xdisplay")
                {
                    continue;
                }
                if (fn.Name == "cef_get_current_platform_thread_handle")
                {
                    continue;
                }
                DefineFunction(decl, fn);
            }

            ns.Types.Add(decl);
        }
예제 #2
0
파일: Program.cs 프로젝트: wuzlai/CefNet
        private static void GenerateFromCHeaders(string basePath, string outDirPath, bool onlyStdCall)
        {
            var options = new CppParserOptions();
            var files   = new List <string>(Directory.GetFiles(Path.Combine(basePath, "include", "capi"), "*.h"));

            files.Add(Path.Combine(basePath, "include", "cef_version.h"));
            files.Add(Path.Combine(basePath, "include", "cef_api_hash.h"));
            options.IncludeFolders.Add(ApplyHotPatch(basePath));
            options.IncludeFolders.Add(basePath);

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                options.IncludeFolders.Add("/usr/include/clang/8/include");
                options.TargetAbi    = "gnu";
                options.TargetCpu    = CppTargetCpu.X86_64;
                options.TargetSystem = "linux";
                options.TargetVendor = "pc";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                options.Defines.Add("_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH");
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                //options.TargetCpu = CppTargetCpu.X86_64;
                options.TargetSystem = "darwin";
                options.TargetVendor = "apple";
                options.SystemIncludeFolders.Add("/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include");
                options.SystemIncludeFolders.Add("/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1");
                options.SystemIncludeFolders.Add("/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include");
                options.SystemIncludeFolders.Add("/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/include");
                options.AdditionalArguments.Add("-stdlib=libc++");
            }

            var nativeBuild = new NativeCefApiBuilder(onlyStdCall)
            {
                Namespace     = "CefNet.CApi",
                BaseDirectory = basePath
            };

            var enumBuild = new NativeCefApiBuilder(onlyStdCall)
            {
                Namespace     = "CefNet",
                BaseDirectory = basePath
            };

            CppCompilation compilation = CppParser.ParseFiles(files, options);

            if (compilation.HasErrors)
            {
                foreach (var msg in compilation.Diagnostics.Messages)
                {
                    Console.WriteLine(msg);
                }

                Environment.Exit(-1);
                return;
            }
            var aliasResolver = new AliasResolver(compilation);

            nativeBuild.ResolveCefTypeDef += aliasResolver.HandleResolveEvent;
            enumBuild.ResolveCefTypeDef   += aliasResolver.HandleResolveEvent;
            Func <string, string> resolveType = aliasResolver.ResolveNonFail;

            foreach (CppClass @class in compilation.Classes)
            {
                string source = @class.Span.Start.File;
                if (!source.Contains("capi") && !source.Contains("internal"))
                {
                    continue;
                }

                if (IgnoreClasses.Contains(@class.Name))
                {
                    continue;
                }

                string fileName = aliasResolver.ResolveNonFail(@class.Name);
                using (var csfile = new StreamWriter(Path.Combine(outDirPath, "Native", "Types", fileName + ".cs"), false, Encoding.UTF8))
                    using (var ilfile = new StreamWriter(Path.Combine(outDirPath, "Native", "MSIL", fileName + ".il"), false, Encoding.UTF8))
                    {
                        nativeBuild.Format(@class, csfile, ilfile);
                        csfile.Flush();
                        ilfile.Flush();
                    }
            }

            foreach (CppTypedef typedef in compilation.Typedefs)
            {
                string name = typedef.Name;
                if (name.StartsWith("cef_string_") ||
                    name == "cef_color_t"
                    //|| name == "cef_platform_thread_id_t"
                    || name == "cef_platform_thread_handle_t")
                {
                    var sb = new StringBuilder();
                    try
                    {
                        var w = new StringWriter(sb);
                        nativeBuild.Format(typedef, w);
                        w.Flush();
                        w.Dispose();
                    }
                    catch (InvalidOperationException)
                    {
                        continue;
                    }
                    string fileName = aliasResolver.ResolveNonFail(typedef.Name);
                    File.WriteAllText(Path.Combine(outDirPath, "Native", "Typedefs", fileName + ".cs"), sb.ToString(), Encoding.UTF8);
                }
            }

            foreach (CppEnum @enum in compilation.Enums)
            {
                string fileName = aliasResolver.ResolveNonFail(@enum.Name);
                using (var csfile = new StreamWriter(Path.Combine(outDirPath, "Managed", "Enums", fileName + ".cs"), false, Encoding.UTF8))
                {
                    enumBuild.Format(@enum, csfile);
                    csfile.Flush();
                }
            }


            var api = new CefApiClass("CefNativeApi")
            {
                Functions = compilation.Functions,
                ApiHash   = GetApiHash(basePath)
            };

            using (var csfile = new StreamWriter(Path.Combine(outDirPath, "Native", "CefNativeApi.cs"), false, Encoding.UTF8))
            {
                nativeBuild.Format(api, csfile);
                csfile.Flush();
            }
        }
예제 #3
0
 protected abstract void BuildCefApi(CodeNamespace ns, CefApiClass @class);