diff options
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/fstdata.cc | 2 | ||||
-rw-r--r-- | kernel/yosys.cc | 31 | ||||
-rw-r--r-- | kernel/yosys.h | 5 |
3 files changed, 34 insertions, 4 deletions
diff --git a/kernel/fstdata.cc b/kernel/fstdata.cc index fea8ee3c3..b2e574b02 100644 --- a/kernel/fstdata.cc +++ b/kernel/fstdata.cc @@ -33,7 +33,7 @@ FstData::FstData(std::string filename) : ctx(nullptr) std::string filename_trim = file_base_name(filename); if (filename_trim.size() > 4 && filename_trim.compare(filename_trim.size()-4, std::string::npos, ".vcd") == 0) { filename_trim.erase(filename_trim.size()-4); - tmp_file = stringf("/tmp/converted_%s.fst", filename_trim.c_str()); + tmp_file = stringf("%s/converted_%s.fst", get_base_tmpdir().c_str(), filename_trim.c_str()); std::string cmd = stringf("vcd2fst %s %s", filename.c_str(), tmp_file.c_str()); log("Exec: %s\n", cmd.c_str()); if (run_command(cmd) != 0) diff --git a/kernel/yosys.cc b/kernel/yosys.cc index 4e1a3ca7e..7f395761b 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -376,6 +376,35 @@ int run_command(const std::string &command, std::function<void(const std::string } #endif +// POSIX defines `TMPDIR` as the pathname of the directory +// where programs can create temporary files. On most systems, +// it points to '/tmp'. However, on some systems it can be a different +// location +// Source: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03 +std::string get_base_tmpdir() +{ + // We cache the directory name in a static variable here + // for faster calls later + static std::string tmpdir; + + if (!tmpdir.empty()) { + return tmpdir; + } + + char * var = std::getenv("TMPDIR"); + if (NULL == var) { + // if the variable is not set, then use '/tmp' + tmpdir.assign("/tmp"); + } else { + tmpdir.assign(var); + // We return the directory name without the trailing '/' + if (tmpdir.back() == '/') { + tmpdir.pop_back(); + } + } + return tmpdir; +} + std::string make_temp_file(std::string template_str) { #if defined(__wasm) @@ -384,7 +413,7 @@ std::string make_temp_file(std::string template_str) static size_t index = 0; template_str.replace(pos, 6, stringf("%06zu", index++)); #elif defined(_WIN32) - if (template_str.rfind("/tmp/", 0) == 0) { + if (template_str.rfind(get_base_tmpdir() + "/", 0) == 0) { # ifdef __MINGW32__ char longpath[MAX_PATH + 1]; char shortpath[MAX_PATH + 1]; diff --git a/kernel/yosys.h b/kernel/yosys.h index 93e7ff23e..448f896d4 100644 --- a/kernel/yosys.h +++ b/kernel/yosys.h @@ -278,8 +278,9 @@ bool patmatch(const char *pattern, const char *string); #if !defined(YOSYS_DISABLE_SPAWN) int run_command(const std::string &command, std::function<void(const std::string&)> process_line = std::function<void(const std::string&)>()); #endif -std::string make_temp_file(std::string template_str = "/tmp/yosys_XXXXXX"); -std::string make_temp_dir(std::string template_str = "/tmp/yosys_XXXXXX"); +std::string get_base_tmpdir(); +std::string make_temp_file(std::string template_str = get_base_tmpdir() + "/yosys_XXXXXX"); +std::string make_temp_dir(std::string template_str = get_base_tmpdir() + "/yosys_XXXXXX"); bool check_file_exists(std::string filename, bool is_exec = false); bool is_absolute_path(std::string filename); void remove_directory(std::string dirname); |