utils: Put temporary files in the tmp directory

Instead of the CWD.
Can improve performance of the test-runner, as emitting a lot of short-lived files in the source tree can waist a lot of cycles triggering IDE file monitoring logic.

Change-Id: I25de15af02ab816fff5d8a079fda901883793478
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/60342
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Auto-Submit: Ben Clayton <bclayton@google.com>
diff --git a/src/utils/io/tmpfile_posix.cc b/src/utils/io/tmpfile_posix.cc
index 39d2af9..d75764d 100644
--- a/src/utils/io/tmpfile_posix.cc
+++ b/src/utils/io/tmpfile_posix.cc
@@ -25,6 +25,11 @@
 namespace {
 
 std::string TmpFilePath(std::string ext) {
+  char const* dir = getenv("TMPDIR");
+  if (dir == nullptr) {
+    dir = "/tmp";
+  }
+
   // mkstemps requires an `int` for the file extension name but STL represents
   // size_t. Pre-C++20 there the behavior for unsigned-to-signed conversion
   // (when the source value exceeds the representable range) is implementation
@@ -32,7 +37,7 @@
   // enforce this here at runtime.
   TINT_ASSERT(Utils, ext.length() <=
                          static_cast<size_t>(std::numeric_limits<int>::max()));
-  std::string name = "tint_XXXXXX" + ext;
+  std::string name = std::string(dir) + "/tint_XXXXXX" + ext;
   int file = mkstemps(&name[0], static_cast<int>(ext.length()));
   if (file != -1) {
     close(file);