aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/yosys.cc
diff options
context:
space:
mode:
authorMiodrag Milanović <mmicko@gmail.com>2022-05-27 16:51:16 +0200
committerGitHub <noreply@github.com>2022-05-27 16:51:16 +0200
commit197c9e04e8778f99f82b1b6bddc9eba3fbf85104 (patch)
treede4c512aa6585c989dcd8a551f969bab0483dfee /kernel/yosys.cc
parentbf78041e8977effbce3a0928bfa22632222e8bbd (diff)
parent08275a1569af7d925ff28ff0288d9779057d27a9 (diff)
downloadyosys-197c9e04e8778f99f82b1b6bddc9eba3fbf85104.tar.gz
yosys-197c9e04e8778f99f82b1b6bddc9eba3fbf85104.tar.bz2
yosys-197c9e04e8778f99f82b1b6bddc9eba3fbf85104.zip
Merge pull request #3333 from mohamed/feature/tmpdir
Observe $TMPDIR variable when creating tmp files
Diffstat (limited to 'kernel/yosys.cc')
-rw-r--r--kernel/yosys.cc64
1 files changed, 40 insertions, 24 deletions
diff --git a/kernel/yosys.cc b/kernel/yosys.cc
index 4e1a3ca7e..64d2b4def 100644
--- a/kernel/yosys.cc
+++ b/kernel/yosys.cc
@@ -376,35 +376,54 @@ int run_command(const std::string &command, std::function<void(const std::string
}
#endif
-std::string make_temp_file(std::string template_str)
+std::string get_base_tmpdir()
{
-#if defined(__wasm)
- size_t pos = template_str.rfind("XXXXXX");
- log_assert(pos != std::string::npos);
- static size_t index = 0;
- template_str.replace(pos, 6, stringf("%06zu", index++));
-#elif defined(_WIN32)
- if (template_str.rfind("/tmp/", 0) == 0) {
+ static std::string tmpdir;
+
+ if (!tmpdir.empty()) {
+ return tmpdir;
+ }
+
+#if defined(_WIN32)
# ifdef __MINGW32__
- char longpath[MAX_PATH + 1];
- char shortpath[MAX_PATH + 1];
+ char longpath[MAX_PATH + 1];
+ char shortpath[MAX_PATH + 1];
# else
- WCHAR longpath[MAX_PATH + 1];
- TCHAR shortpath[MAX_PATH + 1];
+ WCHAR longpath[MAX_PATH + 1];
+ TCHAR shortpath[MAX_PATH + 1];
# endif
- if (!GetTempPath(MAX_PATH+1, longpath))
- log_error("GetTempPath() failed.\n");
- if (!GetShortPathName(longpath, shortpath, MAX_PATH + 1))
- log_error("GetShortPathName() failed.\n");
- std::string path;
- for (int i = 0; shortpath[i]; i++)
- path += char(shortpath[i]);
- template_str = stringf("%s\\%s", path.c_str(), template_str.c_str() + 5);
+ if (!GetTempPath(MAX_PATH+1, longpath))
+ log_error("GetTempPath() failed.\n");
+ if (!GetShortPathName(longpath, shortpath, MAX_PATH + 1))
+ log_error("GetShortPathName() failed.\n");
+ for (int i = 0; shortpath[i]; i++)
+ tmpdir += char(shortpath[i]);
+#else
+ char * var = std::getenv("TMPDIR");
+ if (var && strlen(var)!=0) {
+ tmpdir.assign(var);
+ // We return the directory name without the trailing '/'
+ while (!tmpdir.empty() && (tmpdir.back() == '/')) {
+ tmpdir.pop_back();
+ }
+ } else {
+ tmpdir.assign("/tmp");
}
+#endif
+ return tmpdir;
+}
+std::string make_temp_file(std::string template_str)
+{
size_t pos = template_str.rfind("XXXXXX");
log_assert(pos != std::string::npos);
-
+#if defined(__wasm)
+ static size_t index = 0;
+ template_str.replace(pos, 6, stringf("%06zu", index++));
+#elif defined(_WIN32)
+#ifndef YOSYS_WIN32_UNIX_DIR
+ std::replace(template_str.begin(), template_str.end(), '/', '\\');
+#endif
while (1) {
for (int i = 0; i < 6; i++) {
static std::string y = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
@@ -416,9 +435,6 @@ std::string make_temp_file(std::string template_str)
break;
}
#else
- size_t pos = template_str.rfind("XXXXXX");
- log_assert(pos != std::string::npos);
-
int suffixlen = GetSize(template_str) - pos - 6;
char *p = strdup(template_str.c_str());