Copy clang crash file

Change-Id: If21c1ce4a48b6931c1c74afc95dfadcf6622525b
Bug: https://github.com/flutter/flutter/issues/140458
Reviewed-on: https://flutter-review.googlesource.com/c/recipes/+/57060
Reviewed-by: Ricardo Amador <ricardoamador@google.com>
Commit-Queue: Keyong Han <keyonghan@google.com>
Reviewed-by: Godofredo Contreras <godofredoc@google.com>
diff --git a/recipe_modules/build_util/api.py b/recipe_modules/build_util/api.py
index ad51817..9c1fb42 100644
--- a/recipe_modules/build_util/api.py
+++ b/recipe_modules/build_util/api.py
@@ -159,6 +159,10 @@
           self.m.file.copy(
               'Copy crash reproduce file %s' % f, f, flutter_logs_dir
           )
+    # Copy the llvm_<YYYY-MM-DD-HHMMSS>_<hostname>.crash file to log dir.
+    copy_script = self.resource('copy_crash.sh')
+    self.m.step('Set execute permission', ['chmod', '755', copy_script])
+    self.m.step('copy crash file', [copy_script, flutter_logs_dir])
 
   def build(self, config, checkout_path, targets, env, rbe_working_path=None):
     """Builds using ninja.
diff --git a/recipe_modules/build_util/examples/full.expected/basic_crash.json b/recipe_modules/build_util/examples/full.expected/basic_crash.json
index 8b40259..77b230b 100644
--- a/recipe_modules/build_util/examples/full.expected/basic_crash.json
+++ b/recipe_modules/build_util/examples/full.expected/basic_crash.json
@@ -481,6 +481,45 @@
     ]
   },
   {
+    "cmd": [
+      "chmod",
+      "755",
+      "RECIPE_MODULE[flutter::build_util]/resources/copy_crash.sh"
+    ],
+    "env": {
+      "GOMA_TMP_DIR": "[CLEANUP]/goma",
+      "GOMA_USE_LOCAL": "False"
+    },
+    "env_suffixes": {
+      "DEPOT_TOOLS_UPDATE": [
+        "0"
+      ],
+      "PATH": [
+        "RECIPE_REPO[depot_tools]"
+      ]
+    },
+    "name": "Set execute permission"
+  },
+  {
+    "cmd": [
+      "RECIPE_MODULE[flutter::build_util]/resources/copy_crash.sh",
+      "[START_DIR]"
+    ],
+    "env": {
+      "GOMA_TMP_DIR": "[CLEANUP]/goma",
+      "GOMA_USE_LOCAL": "False"
+    },
+    "env_suffixes": {
+      "DEPOT_TOOLS_UPDATE": [
+        "0"
+      ],
+      "PATH": [
+        "RECIPE_REPO[depot_tools]"
+      ]
+    },
+    "name": "copy crash file"
+  },
+  {
     "cmd": [],
     "name": "teardown goma (2)"
   },
diff --git a/recipe_modules/build_util/examples/full.expected/mac.json b/recipe_modules/build_util/examples/full.expected/mac.json
index 72066f0..4f37f61 100644
--- a/recipe_modules/build_util/examples/full.expected/mac.json
+++ b/recipe_modules/build_util/examples/full.expected/mac.json
@@ -974,6 +974,67 @@
     ]
   },
   {
+    "cmd": [
+      "chmod",
+      "755",
+      "RECIPE_MODULE[flutter::build_util]/resources/copy_crash.sh"
+    ],
+    "env": {
+      "RBE_cache_dir": "[CACHE]/rbe/deps",
+      "RBE_deps_cache_max_mb": "512",
+      "RBE_enable_deps_cache": "true",
+      "RBE_instance": "fake_rbe_instance",
+      "RBE_log_dir": "[CLEANUP]/rbe",
+      "RBE_log_format": "reducedtext",
+      "RBE_output_dir": "[CLEANUP]/rbe",
+      "RBE_proxy_log_dir": "[CLEANUP]/rbe",
+      "RBE_server_address": "unix://[CLEANUP]/rbe/reproxy.sock",
+      "RBE_service": "remotebuildexecution.googleapis.com:443",
+      "RBE_socket_path": "[CLEANUP]/rbe/reproxy.sock",
+      "RBE_use_application_default_credentials": "false",
+      "RBE_use_gce_credentials": "true"
+    },
+    "env_suffixes": {
+      "DEPOT_TOOLS_UPDATE": [
+        "0"
+      ],
+      "PATH": [
+        "RECIPE_REPO[depot_tools]"
+      ]
+    },
+    "name": "Set execute permission"
+  },
+  {
+    "cmd": [
+      "RECIPE_MODULE[flutter::build_util]/resources/copy_crash.sh",
+      "[START_DIR]"
+    ],
+    "env": {
+      "RBE_cache_dir": "[CACHE]/rbe/deps",
+      "RBE_deps_cache_max_mb": "512",
+      "RBE_enable_deps_cache": "true",
+      "RBE_instance": "fake_rbe_instance",
+      "RBE_log_dir": "[CLEANUP]/rbe",
+      "RBE_log_format": "reducedtext",
+      "RBE_output_dir": "[CLEANUP]/rbe",
+      "RBE_proxy_log_dir": "[CLEANUP]/rbe",
+      "RBE_server_address": "unix://[CLEANUP]/rbe/reproxy.sock",
+      "RBE_service": "remotebuildexecution.googleapis.com:443",
+      "RBE_socket_path": "[CLEANUP]/rbe/reproxy.sock",
+      "RBE_use_application_default_credentials": "false",
+      "RBE_use_gce_credentials": "true"
+    },
+    "env_suffixes": {
+      "DEPOT_TOOLS_UPDATE": [
+        "0"
+      ],
+      "PATH": [
+        "RECIPE_REPO[depot_tools]"
+      ]
+    },
+    "name": "copy crash file"
+  },
+  {
     "cmd": [],
     "name": "teardown remote execution"
   },
diff --git a/recipe_modules/build_util/examples/full.expected/win.json b/recipe_modules/build_util/examples/full.expected/win.json
index 7985ba0..a370c1d 100644
--- a/recipe_modules/build_util/examples/full.expected/win.json
+++ b/recipe_modules/build_util/examples/full.expected/win.json
@@ -642,6 +642,37 @@
     ]
   },
   {
+    "cmd": [
+      "chmod",
+      "755",
+      "RECIPE_MODULE[flutter::build_util]\\resources\\copy_crash.sh"
+    ],
+    "env_suffixes": {
+      "DEPOT_TOOLS_UPDATE": [
+        "0"
+      ],
+      "PATH": [
+        "RECIPE_REPO[depot_tools]"
+      ]
+    },
+    "name": "Set execute permission"
+  },
+  {
+    "cmd": [
+      "RECIPE_MODULE[flutter::build_util]\\resources\\copy_crash.sh",
+      "[START_DIR]"
+    ],
+    "env_suffixes": {
+      "DEPOT_TOOLS_UPDATE": [
+        "0"
+      ],
+      "PATH": [
+        "RECIPE_REPO[depot_tools]"
+      ]
+    },
+    "name": "copy crash file"
+  },
+  {
     "failure": {
       "failure": {},
       "humanReason": "Step('build release mytarget') (retcode: 1)"
diff --git a/recipe_modules/build_util/resources/copy_crash.sh b/recipe_modules/build_util/resources/copy_crash.sh
new file mode 100644
index 0000000..1e54969
--- /dev/null
+++ b/recipe_modules/build_util/resources/copy_crash.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+# Helper script to copy mobileprovisioning profile to
+# bot's default location.
+set -e
+
+destination=$1
+origin="$HOME/Library/Logs/DiagnosticReports/"
+if [ -f "$origin"/llvm_*.crash ]; then
+  cp "$origin"/llvm_*.crash "$destination"/.
+fi
\ No newline at end of file