Skip to content

Commit

Permalink
refactor getting dirs and filenames from paths to files
Browse files Browse the repository at this point in the history
  • Loading branch information
widlarizer committed Sep 11, 2024
1 parent 7129b0e commit 13ba44f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
11 changes: 1 addition & 10 deletions backends/cxxrtl/cxxrtl_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -637,15 +637,6 @@ std::string escape_cxx_string(const std::string &input)
return output;
}

std::string basename(const std::string &filepath)
{
size_t sep_pos = filepath.find_last_of(OS_PATH_SEP);
if (sep_pos != std::string::npos)
return filepath.substr(sep_pos + 1);
else
return filepath;
}

template<class T>
std::string get_hdl_name(T *object)
{
Expand Down Expand Up @@ -2836,7 +2827,7 @@ struct CxxrtlWorker {
}

if (split_intf)
f << "#include \"" << basename(intf_filename) << "\"\n";
f << "#include \"" << name_from_file_path(intf_filename) << "\"\n";
else
f << "#include <cxxrtl/cxxrtl.h>\n";
f << "\n";
Expand Down
2 changes: 1 addition & 1 deletion frontends/ast/simplify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4382,7 +4382,7 @@ AstNode *AstNode::readmem(bool is_readmemh, std::string mem_filename, AstNode *m
std::ifstream f;
f.open(mem_filename.c_str());
if (f.fail()) {
std::string path = filename.substr(0, filename.find_last_of(OS_PATH_SEP)+1);
std::string path = parent_from_file_path(filename);
f.open(path + mem_filename.c_str());
yosys_input_files.insert(path + mem_filename);
} else {
Expand Down
2 changes: 1 addition & 1 deletion frontends/verilog/preproc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ frontend_verilog_preproc(std::istream &f,
// if the include file was not found, it is not given with an absolute path, and the
// currently read file is given with a path, then try again relative to its directory
ff.clear();
fixed_fn = filename.substr(0, filename.find_last_of(OS_PATH_SEP)+1) + fn;
fixed_fn = parent_from_file_path(filename) + fn;
ff.open(fixed_fn);
}
if (ff.fail() && fn.size() > 0 && fn_relative) {
Expand Down
19 changes: 19 additions & 0 deletions kernel/yosys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,25 @@ std::string escape_filename_spaces(const std::string& filename)
return out;
}

#ifdef _WIN32
const char* const OS_PATH_SEP = "/\\";
#else
const char* const OS_PATH_SEP = "/";
#endif

std::string name_from_file_path(std::string path) {
size_t sep_pos = path.find_last_of(OS_PATH_SEP);
if (sep_pos != std::string::npos)
return path.substr(sep_pos + 1);
else
return path;
}

// Includes OS_PATH_SEP at the end
std::string parent_from_file_path(std::string path) {
return path.substr(0, path.find_last_of(OS_PATH_SEP)+1);
}

bool already_setup = false;

void yosys_setup()
Expand Down
7 changes: 2 additions & 5 deletions kernel/yosys_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,8 @@ bool is_absolute_path(std::string filename);
void remove_directory(std::string dirname);
bool create_directory(const std::string& dirname);
std::string escape_filename_spaces(const std::string& filename);
#ifdef _WIN32
const char* const OS_PATH_SEP = "/\\";
#else
const char* const OS_PATH_SEP = "/";
#endif
std::string name_from_file_path(std::string path);
std::string parent_from_file_path(std::string path);

template<typename T> int GetSize(const T &obj) { return obj.size(); }
inline int GetSize(RTLIL::Wire *wire);
Expand Down

0 comments on commit 13ba44f

Please sign in to comment.