Observe $TMPDIR variable when creating tmp files
authorMohamed A. Bamakhrama <mohamed@alumni.tum.de>
Mon, 23 May 2022 20:21:45 +0000 (22:21 +0200)
committerMiodrag Milanovic <mmicko@gmail.com>
Fri, 27 May 2022 13:06:53 +0000 (15:06 +0200)
POSIX defines $TMPDIR as containing the pathname of the directory where
programs can create temporary files. On most systems, this variable points to
"/tmp". However, on some systems it can point to a different location.
Without respecting this variable, yosys fails to run on such systems.

Signed-off-by: Mohamed A. Bamakhrama <mohamed@alumni.tum.de>
kernel/fstdata.cc
kernel/yosys.cc
kernel/yosys.h
passes/sat/qbfsat.cc
passes/techmap/abc.cc
passes/techmap/abc9.cc

index fea8ee3c3cd3376665a7bc85a80d3694e907ff91..b2e574b02c92edac1f8c02acd6bfef7830ae7518 100644 (file)
@@ -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)
index 4e1a3ca7e2d883175e8d778629f297352c2d7d63..7f395761b09e2263ad09276a0a278da4125c3e30 100644 (file)
@@ -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];
index 93e7ff23e45cd576d37bc2f0252fd40facf9062e..448f896d4e688b59788620cb0690ad13622dd2a8 100644 (file)
@@ -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);
index 6db7d4b64fa0dcf0c9a6f2111dce5fb036f42f79..864d6f05dd494f330c1837ccf3152e82aa0eb3ad 100644 (file)
@@ -251,7 +251,7 @@ QbfSolutionType call_qbf_solver(RTLIL::Module *mod, const QbfSolveOptions &opt,
 
 QbfSolutionType qbf_solve(RTLIL::Module *mod, const QbfSolveOptions &opt) {
        QbfSolutionType ret, best_soln;
-       const std::string tempdir_name = make_temp_dir("/tmp/yosys-qbfsat-XXXXXX");
+       const std::string tempdir_name = make_temp_dir(get_base_tmpdir() + "/yosys-qbfsat-XXXXXX");
        RTLIL::Module *module = mod;
        RTLIL::Design *design = module->design;
        std::string module_name = module->name.str();
index ff98a6e36b7de2e4bfdf69f3aa3ddaa8258c82d6..61ee99ee70d9887eec17ab73b74b9893b1c2f75f 100644 (file)
@@ -780,7 +780,7 @@ void abc_module(RTLIL::Design *design, RTLIL::Module *current_module, std::strin
        if (dff_mode && clk_sig.empty())
                log_cmd_error("Clock domain %s not found.\n", clk_str.c_str());
 
-       std::string tempdir_name = "/tmp/" + proc_program_prefix()+ "yosys-abc-XXXXXX";
+       std::string tempdir_name = get_base_tmpdir() + "/" + proc_program_prefix()+ "yosys-abc-XXXXXX";
        if (!cleanup)
                tempdir_name[0] = tempdir_name[4] = '_';
        tempdir_name = make_temp_dir(tempdir_name);
index fe0802d70888f0bde417473b20d7342e8a049571..79c994b11c79477d0ddc66640efd3388fa3b18ba 100644 (file)
@@ -404,7 +404,7 @@ struct Abc9Pass : public ScriptPass
                                        if (!active_design->selected_whole_module(mod))
                                                log_error("Can't handle partially selected module %s!\n", log_id(mod));
 
-                                       std::string tempdir_name = "/tmp/" + proc_program_prefix() + "yosys-abc-XXXXXX";
+                                       std::string tempdir_name = get_base_tmpdir() + "/" + proc_program_prefix() + "yosys-abc-XXXXXX";
                                        if (!cleanup)
                                                tempdir_name[0] = tempdir_name[4] = '_';
                                        tempdir_name = make_temp_dir(tempdir_name);