public int Build(MakeState s, string tfile, string wc_match) { #if DEBUG // System.Console.WriteLine("Considering building " + tfile); #endif List <string> depends = new List <string>(); List <string> inputs = new List <string>(); List <string> all_deps = new List <string>(); /*MakeState cur_s = state_at_def.Clone(); * cur_s.Merge(s); * s = cur_s;*/ MakeState cur_s = s.Clone(); cur_s.Merge(state_at_def); s = cur_s; if (depend_list != null) { /* Build a list of dependencies */ //List<string> dfiles = FlattenToString(depend_list, s); foreach (string cur_dfile in dfiles) { string dfile = cur_dfile; int wc_index = -1; for (int i = 0; i < dfile.Length; i++) { if (dfile[i] == '%') { if (i == 0 || dfile[i - 1] != '\\') { wc_index = i; break; } } } if (wc_index != -1) { if (wc_match == null) { throw new Exception("wildcard specified in depends list but not in target name"); } dfile = dfile.Substring(0, wc_index) + wc_match + dfile.Substring(wc_index + 1); } depends.Add(dfile); all_deps.Add(dfile); } } if (inputs_list != null) { //List<string> dfiles = FlattenToString(inputs_list, s); foreach (string cur_dfile in ifiles) { string dfile = cur_dfile; int wc_index = -1; for (int i = 0; i < dfile.Length; i++) { if (dfile[i] == '%') { if (i == 0 || dfile[i - 1] != '\\') { wc_index = i; break; } } } if (wc_index != -1) { if (wc_match == null) { throw new Exception("wildcard specified in inputs list but not in target name"); } dfile = dfile.Substring(0, wc_index) + wc_match + dfile.Substring(wc_index + 1); } inputs.Add(dfile); all_deps.Add(dfile); } } #if DEBUG //System.Console.Write("Dependencies: "); //foreach(var dep in all_deps) // System.Console.Write(dep + ", "); //System.Console.WriteLine(); #endif /* Now ensure all the dependencies are available */ DateTime most_recent_dependency = new DateTime(0); System.IO.FileInfo mrd_fi = null; // store the most recent dependency for debugging purposes foreach (string depend in all_deps) { BuildCommandStatement bc = new BuildCommandStatement(); int dep_ret = (int)bc.Run(s, new List <Expression.EvalResult> { new Expression.EvalResult(depend) }).AsInt; System.IO.FileInfo dep_fi = null; if (dep_ret == BuildCommandStatement.RUN_NO_RULE) { /* No rule to build the file - its still okay as long as the file already exists */ dep_fi = new System.IO.FileInfo(depend); if (!Statement.FileDirExists(depend)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(depend + " does not exist and no rule to build it"); Console.ResetColor(); return(-1); } } else if (dep_ret != 0) { return(dep_ret); } dep_fi = new System.IO.FileInfo(depend); if (!Statement.FileDirExists(depend)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error building " + depend); Console.ResetColor(); return(-1); } if ((dep_fi.Attributes & System.IO.FileAttributes.Directory) != System.IO.FileAttributes.Directory) { if (dep_fi.LastWriteTime.CompareTo(most_recent_dependency) > 0) { most_recent_dependency = dep_fi.LastWriteTime; mrd_fi = dep_fi; } } } /* See if we need to build this file */ bool to_build = false; System.IO.FileSystemInfo targ_fi = new System.IO.FileInfo(tfile); DateTime targ_lwt = DateTime.Now; if (s.GetDefine("REBUILD_ALL").AsInt != 0 && ((targ_fi.Attributes & System.IO.FileAttributes.Directory) != System.IO.FileAttributes.Directory)) { to_build = true; Console.ForegroundColor = ConsoleColor.Green; System.Console.WriteLine("Building " + tfile #if DEBUG + " because REBUILD_ALL is set" #endif ); Console.ResetColor(); } else if (!Statement.FileDirExists(tfile)) { to_build = true; Console.ForegroundColor = ConsoleColor.Green; System.Console.WriteLine("Building " + tfile #if DEBUG + " because it does not exist" #endif ); Console.ResetColor(); } else if (most_recent_dependency.CompareTo(targ_lwt = targ_fi.LastWriteTime) > 0) { to_build = true; Console.ForegroundColor = ConsoleColor.Green; System.Console.WriteLine("Building " + tfile #if DEBUG + " because of a newer dependency (" + mrd_fi.FullName + ")" #endif ); Console.ResetColor(); } else if (depend_list == null) { to_build = true; Console.ForegroundColor = ConsoleColor.Green; System.Console.WriteLine("Building " + tfile #if DEBUG + " because dependency list is null" #endif ); Console.ResetColor(); } if (to_build) { MakeState new_s = s.Clone(); if (inputs.Count > 0) { new_s.SetDefine("_RULE_INPUT", new Expression.EvalResult(inputs[0])); } StringBuilder inputs_str = new StringBuilder(); for (int i = 0; i < inputs.Count; i++) { if (i != 0) { inputs_str.Append(" "); } inputs_str.Append(inputs[i]); } StringBuilder deps_str = new StringBuilder(); for (int i = 0; i < depends.Count; i++) { if (i != 0) { deps_str.Append(" "); } deps_str.Append(depends[i]); } new_s.SetDefine("_RULE_INPUTS", new Expression.EvalResult(inputs_str.ToString())); new_s.SetDefine("_RULE_DEPENDS", new Expression.EvalResult(deps_str.ToString())); new_s.SetDefine("_RULE_OUTPUT", new Expression.EvalResult(tfile)); var ret = (int)rules.Execute(new_s).AsInt; /* See if the target failed to build despite success from the rules */ if (ret == 0) { targ_fi = new System.IO.FileInfo(tfile); if ((targ_fi.Attributes & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory) { targ_fi = new System.IO.DirectoryInfo(tfile); } if (targ_fi.Exists == false || targ_fi.LastWriteTime.CompareTo(targ_lwt - new TimeSpan(0, 0, 1)) < 0) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Rule failed to build " + tfile); Console.ResetColor(); return(-1); } } return(ret); } else { #if DEBUG // System.Console.WriteLine("Not building " + tfile); #endif } return(0); }
public override Expression.EvalResult Run(MakeState s, List <Expression.EvalResult> passed_args) { var cmd = passed_args[0].strval; int first_space = cmd.IndexOf(' '); string fname, args; if (first_space == -1) { fname = cmd; args = ""; } else { fname = cmd.Substring(0, first_space); args = cmd.Substring(first_space + 1); } System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = fname; p.StartInfo.Arguments = args; p.StartInfo.UseShellExecute = false; //p.StartInfo.RedirectStandardError = true; //p.StartInfo.RedirectStandardOutput = true; /* Find the name of the path environment variable */ /*string path = "PATH"; * foreach (string env_key in p.StartInfo.EnvironmentVariables.Keys) * { * if (env_key.ToLower() == "path") * path = env_key; * } * string cur_path = ""; * if (p.StartInfo.EnvironmentVariables.ContainsKey(path)) * cur_path = p.StartInfo.EnvironmentVariables[path]; * if (cur_path != "") * cur_path += ";"; * cur_path += "f:/cygwin64/bin"; * p.StartInfo.EnvironmentVariables[path] = cur_path;*/ Environment.SetEnvironmentVariable("PATH", s.GetDefine("PATH").strval); Console.WriteLine("shellcmd: " + p.StartInfo.FileName + " " + p.StartInfo.Arguments); try { if (p.Start() == false) { throw new Exception("unable to execute " + fname); } } catch (Exception e) { System.Console.WriteLine("error: " + e.ToString()); return(new Expression.EvalResult(-1)); } p.WaitForExit(); if (p.ExitCode != 0) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Shell command returned error " + p.ExitCode.ToString() + " (" + p.ExitCode.ToString("X8") + ")"); Console.ResetColor(); s.returns = new Expression.EvalResult(-1); return(new Expression.EvalResult(-1)); } else { return(new Expression.EvalResult(0)); } }