Add tint::TmpFile
TmpFile constructs a temporary file that can be written to, and is automatically deleted on destruction.
Will be used to create a temporary source file for verifying generated HLSL and MSL against their shader compilers.
Change-Id: Ieaa6f257b93f4f2193dafe6297603816f6964928
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/41941
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index baf2889..4ecb9e0 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -872,6 +872,8 @@
"src/type/u32_type_test.cc",
"src/type/vector_type_test.cc",
"src/type_determiner_test.cc",
+ "src/utils/tmpfile_test.cc",
+ "src/utils/tmpfile.h",
"src/utils/unique_vector_test.cc",
"src/validator/validator_builtins_test.cc",
"src/validator/validator_control_block_test.cc",
@@ -882,6 +884,14 @@
"src/writer/float_to_string_test.cc",
]
+ if (is_linux || is_mac) {
+ sources += [ "src/utils/tmpfile_posix.cc" ]
+ } else if (is_win) {
+ sources += [ "src/utils/tmpfile_windows.cc" ]
+ } else {
+ sources += [ "src/utils/tmpfile_other.cc" ]
+ }
+
configs += [
":tint_common_config",
":tint_unittests_config",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index cc6732a..6d3ecd4 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -497,6 +497,8 @@
type/type_manager_test.cc
type/u32_type_test.cc
type/vector_type_test.cc
+ utils/tmpfile_test.cc
+ utils/tmpfile.h
utils/unique_vector_test.cc
validator/validator_builtins_test.cc
validator/validator_control_block_test.cc
@@ -508,6 +510,14 @@
writer/float_to_string_test.cc
)
+ if(UNIX OR APPLE)
+ list(APPEND TINT_TEST_SRCS utils/tmpfile_posix.cc)
+ elseif(WIN32)
+ list(APPEND TINT_TEST_SRCS utils/tmpfile_windows.cc)
+ else()
+ list(APPEND TINT_TEST_SRCS utils/tmpfile_other.cc)
+ endif()
+
if(${TINT_BUILD_SPV_READER})
list(APPEND TINT_TEST_SRCS
reader/spirv/enum_converter_test.cc
diff --git a/src/utils/tmpfile.h b/src/utils/tmpfile.h
new file mode 100644
index 0000000..fbb66ff
--- /dev/null
+++ b/src/utils/tmpfile.h
@@ -0,0 +1,74 @@
+// Copyright 2021 The Tint Authors.
+//
+// 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.
+
+#ifndef SRC_UTILS_TMPFILE_H_
+#define SRC_UTILS_TMPFILE_H_
+
+#include <sstream>
+#include <string>
+
+namespace tint {
+namespace utils {
+
+/// TmpFile constructs a temporary file that can be written to, and is
+/// automatically deleted on destruction.
+class TmpFile {
+ public:
+ /// Constructor.
+ /// Creates a new temporary file which can be written to.
+ /// The temporary file will be automatically deleted on destruction.
+ TmpFile();
+
+ /// Destructor.
+ /// Deletes the temporary file.
+ ~TmpFile();
+
+ /// @return true if the temporary file was successfully created.
+ operator bool() { return !path_.empty(); }
+
+ /// @return the path to the temporary file
+ std::string Path() const { return path_; }
+
+ /// Opens the temporary file and appends |size| bytes from |data| to the end
+ /// of the temporary file. The temporary file is closed again before
+ /// returning, allowing other processes to open the file on operating systems
+ /// that require exclusive ownership of opened files.
+ /// @param data the data to write to the end of the file
+ /// @param size the number of bytes to write from data
+ /// @returns true on success, otherwise false
+ bool Append(const void* data, size_t size) const;
+
+ /// Appends the argument to the end of the file.
+ /// @param data the data to write to the end of the file
+ /// @return a reference to this TmpFile
+ template <typename T>
+ inline TmpFile& operator<<(T&& data) {
+ std::stringstream ss;
+ ss << data;
+ std::string str = ss.str();
+ Append(str.data(), str.size());
+ return *this;
+ }
+
+ private:
+ TmpFile(const TmpFile&) = delete;
+ TmpFile& operator=(const TmpFile&) = delete;
+
+ std::string path_;
+};
+
+} // namespace utils
+} // namespace tint
+
+#endif // SRC_UTILS_TMPFILE_H_
diff --git a/src/utils/tmpfile_other.cc b/src/utils/tmpfile_other.cc
new file mode 100644
index 0000000..8a90238
--- /dev/null
+++ b/src/utils/tmpfile_other.cc
@@ -0,0 +1,29 @@
+// Copyright 2021 The Tint Authors.
+//
+// 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.
+
+#include "src/utils/tmpfile.h"
+
+namespace tint {
+namespace utils {
+
+TmpFile::TmpFile() = default;
+
+TmpFile::~TmpFile() = default;
+
+bool TmpFile::Append(const void* data, size_t size) const {
+ return false;
+}
+
+} // namespace utils
+} // namespace tint
diff --git a/src/utils/tmpfile_posix.cc b/src/utils/tmpfile_posix.cc
new file mode 100644
index 0000000..7486896
--- /dev/null
+++ b/src/utils/tmpfile_posix.cc
@@ -0,0 +1,56 @@
+// Copyright 2021 The Tint Authors.
+//
+// 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.
+
+#include "src/utils/tmpfile.h"
+
+#include <stdio.h>
+#include <unistd.h>
+#include <cstdio>
+
+namespace tint {
+namespace utils {
+
+namespace {
+
+std::string TmpFilePath() {
+ char name[] = "tint_XXXXXX";
+ int file = mkstemp(name);
+ if (file != -1) {
+ close(file);
+ return name;
+ }
+ return "";
+}
+
+} // namespace
+
+TmpFile::TmpFile() : path_(TmpFilePath()) {}
+
+TmpFile::~TmpFile() {
+ if (!path_.empty()) {
+ remove(path_.c_str());
+ }
+}
+
+bool TmpFile::Append(const void* data, size_t size) const {
+ if (auto* file = fopen(path_.c_str(), "ab")) {
+ fwrite(data, size, 1, file);
+ fclose(file);
+ return true;
+ }
+ return false;
+}
+
+} // namespace utils
+} // namespace tint
diff --git a/src/utils/tmpfile_test.cc b/src/utils/tmpfile_test.cc
new file mode 100644
index 0000000..44b2f72
--- /dev/null
+++ b/src/utils/tmpfile_test.cc
@@ -0,0 +1,71 @@
+// Copyright 2021 The Tint Authors.
+//
+// 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.
+
+#include "src/utils/tmpfile.h"
+
+#include <fstream>
+
+#include "gtest/gtest.h"
+
+namespace tint {
+namespace utils {
+namespace {
+
+TEST(TmpFileTest, WriteReadAppendDelete) {
+ std::string path;
+ {
+ TmpFile tmp;
+ if (!tmp) {
+ GTEST_SKIP() << "Unable to create a temporary file";
+ }
+
+ path = tmp.Path();
+
+ // Write a string to the temporary file
+ tmp << "hello world\n";
+
+ // Check the content of the file
+ {
+ std::ifstream file(path);
+ ASSERT_TRUE(file);
+ std::string line;
+ EXPECT_TRUE(std::getline(file, line));
+ EXPECT_EQ(line, "hello world");
+ EXPECT_FALSE(std::getline(file, line));
+ }
+
+ // Write some more content to the file
+ tmp << 42;
+
+ // Check the content of the file again
+ {
+ std::ifstream file(path);
+ ASSERT_TRUE(file);
+ std::string line;
+ EXPECT_TRUE(std::getline(file, line));
+ EXPECT_EQ(line, "hello world");
+ EXPECT_TRUE(std::getline(file, line));
+ EXPECT_EQ(line, "42");
+ EXPECT_FALSE(std::getline(file, line));
+ }
+ }
+
+ // Check the file has been deleted when it fell out of scope
+ std::ifstream file(path);
+ ASSERT_FALSE(file);
+}
+
+} // namespace
+} // namespace utils
+} // namespace tint
diff --git a/src/utils/tmpfile_windows.cc b/src/utils/tmpfile_windows.cc
new file mode 100644
index 0000000..23fb4b3
--- /dev/null
+++ b/src/utils/tmpfile_windows.cc
@@ -0,0 +1,54 @@
+// Copyright 2021 The Tint Authors.
+//
+// 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.
+
+#include "src/utils/tmpfile.h"
+
+#include <stdio.h>
+#include <cstdio>
+
+namespace tint {
+namespace utils {
+
+namespace {
+
+std::string TmpFilePath() {
+ char name[L_tmpnam];
+ if (tmpnam_s(name, L_tmpnam - 1) == 0) {
+ return name;
+ }
+ return "";
+}
+
+} // namespace
+
+TmpFile::TmpFile() : path_(TmpFilePath()) {}
+
+TmpFile::~TmpFile() {
+ if (!path_.empty()) {
+ remove(path_.c_str());
+ }
+}
+
+bool TmpFile::Append(const void* data, size_t size) const {
+ FILE* file = nullptr;
+ if (fopen_s(&file, path_.c_str(), "ab") != 0) {
+ return false;
+ }
+ fwrite(data, size, 1, file);
+ fclose(file);
+ return true;
+}
+
+} // namespace utils
+} // namespace tint