[C++] Add support for .POSIX:

Add a global flag that is set to "true" when .POSIX: is encountered. The
flag will switch the bash flags from "-c" to "-ec", i.e. enable correct
error handling of shell command lines.

Fixes https://github.com/google/kati/issues/48

Change-Id: Ieea386742b05b52d209b74e640e14212f0e2da88
diff --git a/dep.cc b/dep.cc
index da209c7..0a0fc8e 100644
--- a/dep.cc
+++ b/dep.cc
@@ -25,6 +25,7 @@
 
 #include "eval.h"
 #include "fileutil.h"
+#include "flags.h"
 #include "log.h"
 #include "rule.h"
 #include "stats.h"
@@ -261,6 +262,10 @@
       for (Symbol t : targets)
         phony_.insert(t);
     }
+    if (GetRuleInputs(Intern(".POSIX"), &targets, &loc)) {
+      // .POSIX: enables bash -e command line option globally
+      g_flags.posix_shell = true;
+    }
     if (GetRuleInputs(Intern(".KATI_RESTAT"), &targets, &loc)) {
       for (Symbol t : targets)
         restat_.insert(t);
@@ -287,7 +292,6 @@
       ".EXPORT_ALL_VARIABLES",
       ".NOTPARALLEL",
       ".ONESHELL",
-      ".POSIX",
       NULL
     };
     for (const char** p = kUnsupportedBuiltinTargets; *p; p++) {
diff --git a/flags.h b/flags.h
index 910acbf..35c53c4 100644
--- a/flags.h
+++ b/flags.h
@@ -34,6 +34,7 @@
   bool is_dry_run;
   bool is_silent_mode;
   bool is_syntax_check_only;
+  bool posix_shell;
   bool regen;
   bool regen_ignoring_kati_binary;
   bool use_find_emulator;
diff --git a/ninja.cc b/ninja.cc
index d2dc702..6fe6876 100644
--- a/ninja.cc
+++ b/ninja.cc
@@ -185,6 +185,7 @@
         default_target_(NULL) {
     ev_->set_avoid_io(true);
     shell_ = EscapeNinja(ev->EvalVar(kShellSym));
+    shell_flags_ = g_flags.posix_shell ? "ec" : "c";
     const string use_goma_str = ev->EvalVar(Intern("USE_GOMA"));
     use_goma_ = !(use_goma_str.empty() || use_goma_str == "false");
     if (g_flags.goma_dir)
@@ -501,7 +502,7 @@
         *o << " command = " << shell_ << " $out.rsp\n";
       } else {
         EscapeShell(&cmd_buf);
-        *o << " command = " << shell_ << " -c \"" << cmd_buf << "\"\n";
+        *o << " command = " << shell_ << " -" << shell_flags_ << " \"" << cmd_buf << "\"\n";
       }
       if (node->is_restat) {
         *o << " restat = 1\n";
@@ -769,6 +770,7 @@
   bool use_goma_;
   string gomacc_;
   string shell_;
+  string shell_flags_;
   map<string, string> used_envs_;
   string kati_binary_;
   const double start_time_;