From 1646bcd474cfbdd9768a7a95e5c12b973ea01295 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Wed, 19 Mar 2025 18:19:07 +0100 Subject: [PATCH] WIP --- Makefile | 4 ++-- kernel/yosys.cc | 32 +++++++++++++++++++++++++++----- kernel/yosys.h | 2 ++ misc/yosys-config.in | 9 +++++++-- passes/cmds/plugin.cc | 30 ++++++++++++++++++------------ 5 files changed, 56 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 76c7cffbb..6777ee12b 100644 --- a/Makefile +++ b/Makefile @@ -631,7 +631,7 @@ endif endif kernel/log.o: CXXFLAGS += -DYOSYS_SRC='"$(YOSYS_SRC)"' -kernel/yosys.o: CXXFLAGS += -DYOSYS_DATDIR='"$(DATDIR)"' -DYOSYS_PROGRAM_PREFIX='"$(PROGRAM_PREFIX)"' +kernel/yosys.o: CXXFLAGS += -DYOSYS_DATDIR='"$(DATDIR)"' -DYOSYS_LIBDIR='"$(LIBDIR)"' -DYOSYS_PROGRAM_PREFIX='"$(PROGRAM_PREFIX)"' ifeq ($(ENABLE_ABC),1) ifneq ($(ABCEXTERNAL),) kernel/yosys.o: CXXFLAGS += -DABCEXTERNAL='"$(ABCEXTERNAL)"' @@ -771,7 +771,7 @@ endif $(PROGRAM_PREFIX)yosys-config: misc/yosys-config.in $(YOSYS_SRC)/Makefile $(P) $(SED) -e 's#@CXXFLAGS@#$(subst -Ilibs/dlfcn-win32,,$(subst -I. -I"$(YOSYS_SRC)",-I"$(DATDIR)/include",$(strip $(CXXFLAGS_NOVERIFIC))))#;' \ -e 's#@CXX@#$(strip $(CXX))#;' -e 's#@LINKFLAGS@#$(strip $(LINKFLAGS) $(PLUGIN_LINKFLAGS))#;' -e 's#@LIBS@#$(strip $(LIBS_NOVERIFIC) $(PLUGIN_LIBS))#;' \ - -e 's#@BINDIR@#$(strip $(BINDIR))#;' -e 's#@DATDIR@#$(strip $(DATDIR))#;' < $< > $(PROGRAM_PREFIX)yosys-config + -e 's#@BINDIR@#$(strip $(BINDIR))#;' -e 's#@DATDIR@#$(strip $(DATDIR))#;' -e 's#@LIBDIR@#$(strip $(LIBDIR))#;' < $< > $(PROGRAM_PREFIX)yosys-config $(Q) chmod +x $(PROGRAM_PREFIX)yosys-config .PHONY: check-git-abc diff --git a/kernel/yosys.cc b/kernel/yosys.cc index d39acf9d9..a216b662d 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -96,9 +96,10 @@ std::vector memhasher_store; uint32_t Hasher::fudge = 0; std::string yosys_share_dirname; +std::string yosys_lib_dirname; std::string yosys_abc_executable; -void init_share_dirname(); +void init_dirnames(); void init_abc_executable_name(); void memhasher_on() @@ -565,7 +566,7 @@ void yosys_setup() } #endif - init_share_dirname(); + init_dirnames(); init_abc_executable_name(); #define X(_id) RTLIL::ID::_id = "\\" # _id; @@ -895,12 +896,13 @@ std::string proc_self_dirname(void) #endif #if defined(EMSCRIPTEN) || defined(__wasm) -void init_share_dirname() +void init_dirnames() { yosys_share_dirname = "/share/"; + yosys_lib_dirname = "/lib/"; } #else -void init_share_dirname() +void init_dirname(const char* d) // TODO const char* d { # ifdef WITH_PYTHON PyObject *sys_obj = PyImport_ImportModule("sys"); @@ -926,6 +928,7 @@ void init_share_dirname() } # else std::string proc_share_path = proc_self_path + "share/"; + std::string proc_lib_path = proc_self_path + "lib/"; if (check_file_exists(proc_share_path, true)) { yosys_share_dirname = proc_share_path; return; @@ -944,6 +947,20 @@ void init_share_dirname() # endif # endif } +void init_lib_dirname() { +# ifdef YOSYS_LIBDIR + proc_lib_path = YOSYS_LIBDIR "/"; + log("proc_lib_path %s\n", proc_lib_path.c_str()); + if (check_file_exists(proc_lib_path, true)) { + yosys_lib_dirname = proc_lib_path; + return; + } +# endif +} +void init_dirnames() { + init_share_dirname(); + init_lib_dirname(); +} #endif void init_abc_executable_name() @@ -977,10 +994,15 @@ void init_abc_executable_name() std::string proc_share_dirname() { if (yosys_share_dirname.empty()) - log_error("init_share_dirname: unable to determine share/ directory!\n"); + log_error("init_dirnames: unable to determine share/ directory!\n"); return yosys_share_dirname; } +std::string proc_lib_dirname() +{ + return yosys_lib_dirname; +} + std::string proc_program_prefix() { std::string program_prefix; diff --git a/kernel/yosys.h b/kernel/yosys.h index d0359aea8..c1964db91 100644 --- a/kernel/yosys.h +++ b/kernel/yosys.h @@ -66,6 +66,7 @@ extern RTLIL::Design *yosys_design; RTLIL::Design *yosys_get_design(); std::string proc_self_dirname(); std::string proc_share_dirname(); +std::string proc_lib_dirname(); std::string proc_program_prefix(); const char *create_prompt(RTLIL::Design *design, int recursion_counter); std::vector glob_filename(const std::string &filename_pattern); @@ -95,6 +96,7 @@ extern std::map loaded_plugin_aliases; void load_plugin(std::string filename, std::vector aliases); extern std::string yosys_share_dirname; +extern std::string yosys_lib_dirname; extern std::string yosys_abc_executable; YOSYS_NAMESPACE_END diff --git a/misc/yosys-config.in b/misc/yosys-config.in index 2d9c35e4d..051073e19 100755 --- a/misc/yosys-config.in +++ b/misc/yosys-config.in @@ -15,6 +15,7 @@ help() { echo " --ldlibs (alias of --libs)" echo " --bindir @BINDIR@" echo " --datdir @DATDIR@" + echo " --libdir @LIBDIR@" echo "" echo "All other args are passed through as they are." echo "" @@ -31,8 +32,8 @@ help() { echo "" echo " $0 --prefix @ bindir: @bindir" echo "" - echo "The args --bindir and --datdir can be directly followed by a slash and" - echo "additional text. Example:" + echo "The args --bindir, --datdir and --libdir can be directly followed by a slash" + echo "and additional text. Example:" echo "" echo " $0 --datdir/simlib.v" echo "" @@ -77,10 +78,14 @@ for opt; do tokens=( "${tokens[@]}" '@BINDIR@' ) ;; "$prefix"datdir) tokens=( "${tokens[@]}" '@DATDIR@' ) ;; + "$prefix"libdir) + tokens=( "${tokens[@]}" '@LIBDIR@' ) ;; "$prefix"bindir/*) tokens=( "${tokens[@]}" '@BINDIR@'"${opt#${prefix}bindir}" ) ;; "$prefix"datdir/*) tokens=( "${tokens[@]}" '@DATDIR@'"${opt#${prefix}datdir}" ) ;; + "$prefix"libdir/*) + tokens=( "${tokens[@]}" '@LIBDIR@'"${opt#${prefix}libdir}" ) ;; --help|-\?|-h) if [ ${#tokens[@]} -eq 0 ]; then help diff --git a/passes/cmds/plugin.cc b/passes/cmds/plugin.cc index 4ad7c165b..5fe28ccba 100644 --- a/passes/cmds/plugin.cc +++ b/passes/cmds/plugin.cc @@ -85,21 +85,27 @@ void load_plugin(std::string filename, std::vector aliases) void *hdl = dlopen(filename.c_str(), RTLD_LAZY|RTLD_LOCAL); - // We were unable to open the file, try to do so from the plugin directory - if (hdl == NULL && orig_filename.find('/') == std::string::npos) { - hdl = dlopen([orig_filename]() { - std::string new_path = proc_share_dirname() + "plugins/" + orig_filename; - - // Check if we need to append .so - if (new_path.find(".so") == std::string::npos) + // We were unable to open the file, try to do so from the plugin directories + std::vector errors; + log("orig_filename %s\n", orig_filename.c_str()); + for (auto dir : {proc_lib_dirname() + "plugins/", proc_share_dirname() + "plugins/"}) + if (hdl == NULL && orig_filename.find('/') == std::string::npos) { + hdl = dlopen([dir, orig_filename]() { + std::string new_path = dir + orig_filename; + + // Check if we need to append .so + if (new_path.find(".so") == std::string::npos) new_path.append(".so"); - - return new_path; - }().c_str(), RTLD_LAZY|RTLD_LOCAL); - } + + log("new_path %s\n", new_path.c_str()); + return new_path; + }().c_str(), RTLD_LAZY|RTLD_LOCAL); + errors.push_back(dlerror()); + } if (hdl == NULL) - log_cmd_error("Can't load module `%s': %s\n", filename.c_str(), dlerror()); + for (auto error : errors) + log_cmd_error("Can't load module `%s': %s\n", filename.c_str(), error.c_str()); loaded_plugins[orig_filename] = hdl; Pass::init_register();