Add KATI_shell_no_rerun

We want to add the ability for kati to injest starlark
code to android, but we don't want starlark to rerun
on every invocation of kati. Instead, add a
$(KATI_shell_no_rerun) that will run the shell command
when kati regenerates the ninja file, but not when
checking the stamp file. Then, the starlark files can
be added as dependencies with $(KATI_extra_file_deps).
diff --git a/src/func.cc b/src/func.cc
index 7580fd5..316e423 100644
--- a/src/func.cc
+++ b/src/func.cc
@@ -609,8 +609,8 @@
     return;
   }
 
-  const std::string&& shell = ev->GetShell();
-  const std::string&& shellflag = ev->GetShellFlag();
+  std::string shell = ev->GetShell();
+  std::string shellflag = ev->GetShellFlag();
 
   std::string out;
   FindCommand* fc = NULL;
@@ -630,6 +630,32 @@
   ShellStatusVar::SetValue(returnCode);
 }
 
+void ShellFuncNoRerun(const std::vector<Value*>& args,
+                      Evaluator* ev,
+                      std::string* s) {
+  std::string cmd = args[0]->Eval(ev);
+  if (ev->avoid_io() && !HasNoIoInShellScript(cmd)) {
+    // In the regular ShellFunc, if it sees a $(shell) inside of a rule when in
+    // ninja mode, the shell command will just be written to the ninja file
+    // instead of run directly by kati. So it already has the benefits of not
+    // rerunning every time kati is invoked.
+    ERROR_LOC(ev->loc(),
+              "KATI_shell_no_rerun provides no benefit over regular $(shell) "
+              "inside of a rule.",
+              cmd.c_str());
+    return;
+  }
+
+  std::string shell = ev->GetShell();
+  std::string shellflag = ev->GetShellFlag();
+
+  std::string out;
+  FindCommand* fc = NULL;
+  int returnCode = ShellFuncImpl(shell, shellflag, cmd, ev->loc(), &out, &fc);
+  *s += out;
+  ShellStatusVar::SetValue(returnCode);
+}
+
 void CallFunc(const std::vector<Value*>& args, Evaluator* ev, std::string* s) {
   static const Symbol tmpvar_names[] = {
       Intern("0"), Intern("1"), Intern("2"), Intern("3"), Intern("4"),
@@ -1085,6 +1111,7 @@
     ENTRY("KATI_variable_location", &VariableLocationFunc, 1, 1, false, false),
 
     ENTRY("KATI_extra_file_deps", &ExtraFileDepsFunc, 0, 0, false, false),
+    ENTRY("KATI_shell_no_rerun", &ShellFuncNoRerun, 1, 1, false, false),
 };
 
 }  // namespace
diff --git a/testcase/ninja_shell_no_rerun.sh b/testcase/ninja_shell_no_rerun.sh
new file mode 100644
index 0000000..3b5fd7a
--- /dev/null
+++ b/testcase/ninja_shell_no_rerun.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+#
+# Copyright 2023 Google Inc. All rights reserved
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http:#www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+set -eu
+
+log=stderr_log
+mk="$@"
+
+echo foo > a.txt
+
+if echo "${mk}" | grep -q kati; then
+  cat <<EOF > Makefile
+RESULT := \$(KATI_shell_no_rerun cat a.txt)
+all:
+	echo \$(RESULT)
+EOF
+else
+  cat <<EOF > Makefile
+RESULT := \$(shell cat a.txt)
+all:
+	echo \$(RESULT)
+EOF
+fi
+
+${mk} 2> ${log}
+if [ -e ninja.sh ]; then
+  ./ninja.sh
+fi
+
+# Only change the file for kati so that make matches kati's broken output of printing foo 2 times.
+# ("broken" because the user forgot to add a.txt to $(KATI_extra_file_deps))
+if echo "${mk}" | grep -q kati; then
+echo bar > a.txt
+fi
+
+${mk} 2> ${log}
+if [ -e ninja.sh ]; then
+  if grep -q regenerating ${log}; then
+    echo 'Should not be regenerated'
+  fi
+  ./ninja.sh
+fi
diff --git a/testcase/ninja_shell_no_rerun_error_in_rule.sh b/testcase/ninja_shell_no_rerun_error_in_rule.sh
new file mode 100644
index 0000000..a59c360
--- /dev/null
+++ b/testcase/ninja_shell_no_rerun_error_in_rule.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+#
+# Copyright 2023 Google Inc. All rights reserved
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http:#www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+set -eu
+
+log=stderr_log
+mk="$@"
+
+echo foo > a.txt
+
+if echo "${mk}" | grep -q kati; then
+  cat <<EOF > Makefile
+all:
+	echo \$(KATI_shell_no_rerun echo foo)
+EOF
+else
+  cat <<EOF > Makefile
+all:
+	echo foo
+EOF
+fi
+
+${mk} 2> ${log} || true
+if grep -q "KATI_shell_no_rerun provides no benefit over regular \$(shell) inside of a rule" ${log}; then
+  echo foo
+fi