Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add opt-in support for resolving relative paths in Yosys scripts based on script location #4571

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading