Esempio n. 1
0
        public static int Main(string[] args)
        {
            var start = DateTime.Now;

            WasHit = false;

            var dbg = Environment.GetEnvironmentVariable("CCLASH_DEBUG");

            if (!string.IsNullOrEmpty(dbg))
            {
                Settings.DebugFile    = dbg;
                Settings.DebugEnabled = true;
            }

            if (Settings.DebugEnabled)
            {
                Logging.Emit("command line args:");
                foreach (var a in args)
                {
                    Logging.Emit("arg: {0}", a);
                }
            }

            if (args.Contains("--cclash-server"))
            {
                foreach (var opt in args)
                {
                    switch (opt)
                    {
                    case "--attempt-pdb":
                        Environment.SetEnvironmentVariable("CCLASH_ATTEMPT_PDB_CACHE", "yes");
                        break;

                    case "--pdb-to-z7":
                        Environment.SetEnvironmentVariable("CCLASH_Z7_OBJ", "yes");
                        break;

                    case "--sqlite":
                        Environment.SetEnvironmentVariable("CCLASH_CACHE_TYPE", "sqlite");
                        break;

                    case "--debug":
                        if (Settings.DebugFile == null)
                        {
                            Settings.DebugFile    = "Console";
                            Settings.DebugEnabled = true;
                        }
                        break;

                    default:
                        if (opt.StartsWith("--cachedir="))
                        {
                            var dir = opt.Substring(1 + opt.IndexOf('='));
                            dir = Path.GetFullPath(dir);
                            Settings.CacheDirectory = dir;
                        }
                        break;
                    }
                }


                if (Settings.DebugEnabled)
                {
                    if (Settings.DebugFile != null && Settings.DebugFile != "Console")
                    {
                        Settings.DebugFile += ".serv";
                    }
                }

                Logging.Emit("starting in server mode");
                Logging.Emit("cache dir is {0}", Settings.CacheDirectory);
                Logging.Emit("cache type is {0}", Settings.CacheType);

                if (Settings.DebugFile != "Console")
                {
                    Logging.Emit("closing server console");
                    Console.Out.Close();
                    Console.Error.Close();
                    Console.In.Close();
                }


                Server = new CClashServer();
                if (Server.Preflight(Settings.CacheDirectory))
                {
                    Logging.Emit("server created");
                    Server.Listen(Settings.CacheDirectory);
                    return(0);
                }
                else
                {
                    Logging.Emit("another server is running.. quitting");
                    return(1);
                }
            }

            if (args.Contains("--cclash"))
            {
                Logging.Emit("maint mode");
                Console.Error.WriteLine("cclash {0} (c) Ian Norton, April 2016",
                                        typeof(Program).Assembly.GetName().Version.ToString());

                var compiler = Compiler.Find();
                if (Settings.ServiceMode)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        try
                        {
                            var cc = new CClashServerClient(Settings.CacheDirectory);
                            if (args.Contains("--stop"))
                            {
                                Console.Error.WriteLine("stopping server..");
                                cc.Transact(new CClashRequest()
                                {
                                    cmd = Command.Quit
                                });
                            }
                            else
                            {
                                #region server commands
                                if (args.Contains("--clear"))
                                {
                                    cc.Transact(new CClashRequest()
                                    {
                                        cmd = Command.ClearCache
                                    });
                                }
                                else if (args.Contains("--disable"))
                                {
                                    cc.Transact(new CClashRequest()
                                    {
                                        cmd = Command.DisableCache
                                    });
                                }
                                else if (args.Contains("--enable"))
                                {
                                    cc.Transact(new CClashRequest()
                                    {
                                        cmd = Command.EnableCache
                                    });
                                }
                                else if (args.Contains("--start"))
                                {
                                    Console.Out.WriteLine("starting server");
                                    CClashServerClient.StartBackgroundServer();
                                }
                                else
                                {
                                    var stats = cc.Transact(new CClashRequest()
                                    {
                                        cmd = Command.GetStats
                                    });
                                    Console.Out.WriteLine(stats.stdout);
                                }
                                return(0);

                                #endregion
                            }
                        }
                        catch (CClashErrorException ex)
                        {
                            Logging.Error(ex.Message);
                            return(-1);
                        }
                        catch (CClashWarningException)
                        {
                            System.Threading.Thread.Sleep(2000);
                        }
                        catch (CClashServerNotReadyException)
                        {
                            Logging.Emit("server not ready, try again");
                            return(-1);
                        }
                        catch (IOException ex)
                        {
                            Logging.Error(ex.ToString());
                            return(-1);
                        }
                    }
                }
                else
                {
                    ICompiler comp;
                    using (ICompilerCache cc =
                               CompilerCacheFactory.Get(Settings.DirectMode, Settings.CacheDirectory, compiler, Environment.CurrentDirectory, Compiler.GetEnvironmentDictionary(), out comp))
                    {
                        Console.Out.WriteLine(StatOutputs.GetStatsString(compiler, cc));
                    }
                }
                return(0);
            }

            var rv = RunBuild(args, start, AppendStdout, AppendStderr);
            if (rv != 0)
            {
                if (!Settings.NoAutoRebuild)
                {
                    for (int i = 1; i < 4; i++)
                    {
                        MainStdErr.Clear();
                        MainStdOut.Clear();
                        rv = RunBuild(args, start, AppendStdout, AppendStderr);
                        if (rv == 0)
                        {
                            break;
                        }
                        System.Threading.Thread.Sleep(100);
                    }
                }
            }
            Console.Error.Write(MainStdErr.ToString());
            Console.Out.Write(MainStdOut.ToString());

            if (spawnServer)
            {
                Logging.Emit("server needs to be started");
            }
            return(rv);
        }
Esempio n. 2
0
        public CClashResponse ProcessRequest(CClashRequest req)
        {
            var rv = new CClashResponse()
            {
                supported = false
            };

            Logging.Emit("{0}", DateTime.Now.ToString("s"));
            Logging.Emit("server req: cmd = {0}, workdir = {1}",
                         req.cmd, req.workdir);

            switch (req.cmd)
            {
            case Command.GetStats:
                rv.exitcode = 0;
                cache.SetupStats();     // commits stats to disk
                rv.stdout = StatOutputs.GetStatsString(req.compiler, cache);
                break;

            case Command.DisableCache:
                DisableCaching = true;
                rv.supported   = true;
                break;

            case Command.ClearCache:
                DisableCaching = true;
                cache.SetupStats();
                cache.Lock(CacheLockType.ReadWrite);
                cache.OutputCache.ClearLocked();
                cache.IncludeCache.ClearLocked();
                cache.Unlock(CacheLockType.ReadWrite);
                rv.supported = true;
                break;

            case Command.EnableCache:
                DisableCaching = false;
                rv.supported   = true;
                break;

            case Command.Run:
                var stdout = new StringBuilder();
                var stderr = new StringBuilder();
                var comp   = cache.SetCompilerEx(req.pid, req.compiler, req.workdir, new Dictionary <string, string>(req.envs));
                cache.SetCaptureCallback(comp, (so) => { stdout.Append(so); }, (se) => { stderr.Append(se); });
                if (DisableCaching)
                {
                    rv.exitcode = comp.InvokeCompiler(req.argv, null, null, false, new List <string>());
                }
                else
                {
                    rv.exitcode = cache.CompileOrCache(comp, req.argv);
                }
                rv.supported = true;
                rv.stderr    = stderr.ToString();
                rv.stdout    = stdout.ToString();

                break;

            case Command.Quit:
                cache.SetupStats();
                Stop();
                break;
            }

            Logging.Emit("server resp: {0}", rv.exitcode);

            return(rv);
        }