CClash.Compiler.ProcessArguments C# (CSharp) Method

ProcessArguments() public method

public ProcessArguments ( string args ) : bool
args string
return bool
        public bool ProcessArguments(string[] args)
        {
            try
            {
                CommandLine = args;
                for (int i = 0; i < args.Length; i++)
                {
                    var opt = getOption(args[i]);
                    var full = getFullOption(args[i]);

                    #region switch process each argument type
                    switch (opt)
                    {
                        case "/c":
                            HasDashC = true;
                            break;
                        case "/o":
                            return NotSupported("/o");
                        case "/D":
                            if (opt == full)
                            {
                                // define value is next argument...
                                i++;
                            }
                            break;
                        case "/I":
                            if (opt == full)
                            {
                                // include path is next argument..
                                // microsoft really dont know how to do command line!
                                i++;
                                if (i > args.Length)
                                {
                                    return NotSupported("-I has no path!");
                                }
                                full = "/I" + args[i];
                                goto default;
                            }
                            break;

                        case "/Z7":
                            GeneratePdb = false;
                            PdbFile = null;
                            break;

                        case "/Yu":
                            PrecompiledHeaders = true;
                            return NotSupported("pre-compiler headers {0}", opt);

                        case "/FI":
                            return NotSupported(opt);

                        case "/Zi":
                            GeneratePdb = true;
                            break;

                        case "/Fd":
                            PdbFile = Path.Combine(WorkingDirectory, full.Substring(3));
                            // openssl gives us a posix path here..
                            PdbFile = PdbFile.Replace('/', '\\');
                            if (!PdbFile.ToLower().EndsWith(".pdb") && !PdbFile.EndsWith("\\"))
                            {
                                PdbFile = PdbFile + ".pdb";
                            }
                            break;

                        case "/Fo":
                            ObjectTarget = Path.Combine(WorkingDirectory, full.Substring(3));
                            ObjectTarget = ObjectTarget.Replace('/', '\\');
                            if (!Path.GetFileName(ObjectTarget).Contains(".") && !ObjectTarget.EndsWith("\\"))
                                ObjectTarget += ".obj";
                            ObjectTargetIsFile = ObjectTarget.EndsWith(".obj");
                            break;

                        case "/Tp":
                        case "/Tc":
                            var srcfile = full.Substring(3);
                            if (!Path.IsPathRooted(srcfile))
                                srcfile = Path.Combine(WorkingDirectory, srcfile);

                            if (FileUtils.Exists(srcfile))
                            {
                                srcs.Add(srcfile);
                            }
                            else
                            {
                                return NotSupported("cant find file for {0}", full);
                            }
                            break;

                        case "/E":
                            return NotSupported(opt);

                        case "/EP":
                            return NotSupported(opt);

                        default:
                            #region positional or other flag options

                            if (full == "/link")
                            {
                                Linking = true;
                                return NotSupported("/link");
                            }

                            if (opt.StartsWith("@"))
                            {
                                #region response file
                                ResponseFile = full.Substring(1);

                                if (ResponseFile.EndsWith(InternalResponseFileSuffix))
                                {
                                    Logging.Emit("cclash misshelper internal response file");
                                    return false;
                                }

                                if (!Path.IsPathRooted(ResponseFile))
                                    ResponseFile = Path.Combine(WorkingDirectory, ResponseFile);
                                string rsptxt = File.ReadAllText(ResponseFile);
                                if (rsptxt.Length < 8191)
                                // windows max command line, this is why they invented response files
                                {
                                    Logging.Emit("response data [{0}]", rsptxt);
                                    if (args.Length == 1)
                                    {
                                        // this only works if it is the one and only arg!
                                        args = FixupArgs( CommandLineToArgs(rsptxt).Skip(1) ).ToArray();
                                        i = -1;
                                        // replace the command line with the response file content 
                                        // and restart parsing. This does go wrong if the response text is huge
                                        continue;
                                    }
                                }
                                else
                                {
                                    Logging.Emit("response file too large");
                                }

                                return NotSupported("response file error");
                                #endregion
                            }

                            if (!full.StartsWith("/"))
                            {
                                // NOTE, if we ever cache -link calls this will also match input objects and libs
                                var file = full;
                                if (!Path.IsPathRooted(file))
                                    file = Path.Combine(WorkingDirectory, file);

                                if (FileUtils.Exists(file))
                                {
                                    srcs.Add(file);
                                    continue;
                                }
                            }
                            if (full.StartsWith("/I"))
                            {
                                var d = full.Substring(2);
                                if (d == ".")
                                    d = WorkingDirectory;
                                if (d == "..")
                                    d = Path.GetDirectoryName(WorkingDirectory);

                                if (!Path.IsPathRooted(d))
                                {
                                    d = Path.Combine(WorkingDirectory, d);
                                }

                                if (Directory.Exists(d))
                                {
                                    cliincs.Add(d);
                                    continue;
                                }
                            }
                            #endregion

                            break;
                    }
                    #endregion

                }

                if (SingleSource)
                {
                    if (ObjectTarget == null)
                    {
                        var f = Path.GetFileNameWithoutExtension(SingleSourceFile) + ".obj";
                        if (Path.IsPathRooted(f))
                        {
                            ObjectTarget = f;
                        }
                        else
                        {
                            ObjectTarget = Path.Combine(WorkingDirectory, f);
                        }
                    }
                    else
                    {
                        if (!ObjectTargetIsFile)
                        {
                            ObjectTarget = Path.Combine(ObjectTarget, Path.GetFileNameWithoutExtension(SingleSourceFile)) + ".obj";
                        }
                    }

                    if (GeneratePdb)
                    {
                        if (Settings.ConvertObjPdbToZ7)
                        {
                            bool doconvert = false;
                            if (PdbFile == null)
                            {
                                doconvert = true;
                            }
                            else
                            {
                                if (PdbFile.EndsWith("\\"))
                                {
                                    doconvert = true;
                                }
                                else
                                {
                                    if (!FileUtils.Exists(PdbFile))
                                    {
                                        if (WorkingDirectory.Contains("openssl"))
                                        {
                                            doconvert = true;
                                        }
                                    }
                                }
                            }

                            if (doconvert)
                            {
                                Logging.Emit("converting pdb request to Z7 embedded debug {0}:{1}", WorkingDirectory, Path.GetFileName(ObjectTarget));
                                // append /Z7 to the arg list
                                var newargs = new List<string>();
                                foreach (var a in args)
                                {
                                    if (!(a.StartsWith("/Zi") || a.StartsWith("/Fd")))
                                    {
                                        newargs.Add(a);
                                    }
                                }
                                newargs.Add("/Z7");
                                AttemptPdb = false;
                                PdbFile = null;
                                GeneratePdb = false;
                                PdbExistsAlready = false;
                                args = newargs.ToArray();
                            }
                        }
                    }

                    if (GeneratePdb)
                    {
                        return NotSupported("PDB file requested");
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
                return NotSupported("option parser exception '{0}'", e);
            }
            CompileArgs = args.ToArray();
            return IsSupported;
        }

Usage Example

Beispiel #1
0
        public void ParseSupportedArgs(params string[] argv)
        {
            var c = new Compiler();
            c.SetWorkingDirectory(InitialDir);
            c.SetEnvironment(Compiler.GetEnvironmentDictionary());
            var sbo = new StringBuilder();
            var sbe = new StringBuilder();
            Assert.IsTrue(c.ProcessArguments(argv));
            Assert.IsFalse(c.Linking);
            Assert.IsTrue(c.SingleSource);
            Assert.IsNotNullOrEmpty(c.ObjectTarget);
            Assert.IsFalse(c.PrecompiledHeaders);
            Assert.AreNotEqual(c.SingleSourceFile, c.ObjectTarget);

            EnsureDeleted(c.ObjectTarget);
            EnsureDeleted(c.PdbFile);
            
            c.CompilerExe = CompilerPath;
            c.SetWorkingDirectory(InitialDir);
            c.SetEnvironment(Compiler.GetEnvironmentDictionary());
            var ec = c.InvokeCompiler(
                c.CommandLine,
                Console.Error.WriteLine, Console.Error.WriteLine, false, null);

            Assert.AreEqual(0, ec);

            Assert.IsTrue(File.Exists(c.ObjectTarget));
        }
All Usage Examples Of CClash.Compiler::ProcessArguments