Skip to content

Commit

Permalink
Add relative path resolution support for script I/O operations
Browse files Browse the repository at this point in the history
  • Loading branch information
RCoeurjoly committed Aug 30, 2024
1 parent 0fc5812 commit b7af214
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
26 changes: 24 additions & 2 deletions kernel/yosys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@
#include <errno.h>

#include "libs/json11/json11.hpp"

#if !defined(__wasm)
#include <filesystem>
#endif
YOSYS_NAMESPACE_BEGIN

int autoidx = 1;
Expand Down Expand Up @@ -712,6 +714,20 @@ void rewrite_filename(std::string &filename)
if (filename.compare(0, 2, "~/") == 0)
filename = filename.replace(0, 1, getenv("HOME"));
#endif
#if !defined(__wasm)
if (yosys_get_design()->scratchpad_get_bool("script.relative_path_resolution_enabled")) {
std::filesystem::path file_path(filename);
if (file_path.is_relative()) {
const std::string script_dir = yosys_get_design()->scratchpad_get_string("script.dir", "");
if (!script_dir.empty()) {
std::filesystem::path script_dir_path(script_dir);
// Use std::filesystem::path operator/= for portable path concatenation
file_path = script_dir_path / file_path;
filename = file_path.string();
}
}
}
#endif
}

#ifdef YOSYS_ENABLE_TCL
Expand Down Expand Up @@ -1193,7 +1209,13 @@ bool run_frontend(std::string filename, std::string command, RTLIL::Design *desi

FILE *backup_script_file = Frontend::current_script_file;
Frontend::current_script_file = f;

#if !defined(__wasm)
const std::filesystem::path input_file_path(filename);
const std::filesystem::path script_dir = input_file_path.parent_path();
if (!script_dir.string().empty()) {
yosys_get_design()->scratchpad_set_string("script.dir", script_dir.string());
}
#endif
try {
std::string command;
while (fgetline(f, command)) {
Expand Down
1 change: 1 addition & 0 deletions tests/various/script_relative_path.ys
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
script script_relative_path_files/relative_path_read_write_operations.ys
1 change: 1 addition & 0 deletions tests/various/script_relative_path_files/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*.il
3 changes: 3 additions & 0 deletions tests/various/script_relative_path_files/design.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module top(input a, b, output o);
assign o = a & b;
endmodule
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
scratchpad -set script.relative_path_resolution_enabled 1
read_verilog design.v
design -reset

read_verilog ./design.v
design -reset

read_verilog ../script_relative_path_files/design.v
write_rtlil design.il
design -reset
read_rtlil design.il
design -reset

scratchpad -set script.relative_path_resolution_enabled 0
logger -expect error "Can't open input file `design.v' for reading: No such file or directory" 1
read_verilog design.v

0 comments on commit b7af214

Please sign in to comment.