13 namespace gate_library_manager
17 std::map<std::filesystem::path, std::unique_ptr<GateLibrary>> m_gate_libraries;
21 auto gate_types = lib->get_gate_types();
23 for (
const auto& [gt_name, gt] : gate_types)
27 lib->mark_vcc_gate_type(gt);
31 lib->mark_gnd_gate_type(gt);
35 if (lib->get_gnd_gate_types().empty())
37 std::string
name =
"HAL_GND";
38 if (gate_types.find(
name) != gate_types.end())
40 return ERR(
"could not prepare gate library '" + lib->get_name() +
"': no GND gate type found within gate library, but gate type 'HAL_GND' already exists");
46 return ERR_APPEND(res.get_error(),
"could not prepare gate library '" + lib->get_name() +
"': failed to create output pin 'O' for gate type 'HAL_GND'");
49 lib->mark_gnd_gate_type(gt);
50 log_info(
"gate_library_manager",
"gate library did not contain a GND gate, auto-generated type '{}'.",
name);
53 if (lib->get_vcc_gate_types().empty())
55 std::string
name =
"HAL_VDD";
56 if (gate_types.find(
name) != gate_types.end())
58 return ERR(
"could not prepare gate library '" + lib->get_name() +
"': no VDD gate type found within gate library, but gate type 'HAL_VDD' already exists");
64 return ERR_APPEND(res.get_error(),
"could not prepare gate library '" + lib->get_name() +
"': failed to create output pin 'O' for gate type 'HAL_VDD'");
67 lib->mark_vcc_gate_type(gt);
68 log_info(
"gate_library_manager",
"gate library did not contain a VDD gate, auto-generated type '{}'.",
name);
77 if (!std::filesystem::exists(file_path))
79 log_error(
"gate_library_manager",
"gate library file '{}' does not exist.", file_path.string());
83 if (!file_path.is_absolute())
85 file_path = std::filesystem::absolute(file_path);
90 if (
auto it = m_gate_libraries.find(file_path); it != m_gate_libraries.end())
92 log_info(
"gate_library_parser",
"the gate library file '{}' is already loaded.", file_path.string());
93 return it->second.get();
98 if (gate_lib ==
nullptr)
103 if (
auto res = prepare_library(gate_lib); res.is_error())
105 log_error(
"gate_library_parser",
"error encountered while loading gate library:\n{}", res.get_error().get());
110 m_gate_libraries[file_path.string()] = std::move(gate_lib);
118 for (
const auto& lib_dir : lib_dirs)
120 if (!std::filesystem::exists(lib_dir))
125 log_info(
"gate_library_manager",
"loading all gate library files from {}.", lib_dir.string());
129 load(lib_path.path(), reload);
136 std::vector<std::filesystem::path> retval;
139 if (!std::filesystem::exists(lib_dir))
142 retval.push_back(lib_path.path());
149 if (std::filesystem::exists(file_path))
153 log_info(
"gate_library_manager",
"gate library file '{}' already exists and will be overwritten.", file_path.string());
157 log_error(
"gate_library_manager",
"gate library file '{}' already exists, aborting.", file_path.string());
162 if (!file_path.is_absolute())
164 file_path = std::filesystem::absolute(file_path);
170 void remove(std::filesystem::path file_path)
172 m_gate_libraries.erase(file_path);
177 std::filesystem::path absolute_path;
179 if (std::filesystem::exists(file_path))
182 absolute_path = std::filesystem::absolute(file_path);
187 auto stripped_name = std::filesystem::path(file_path).filename();
188 log_info(
"gate_library_manager",
"file '{}' does not exist, searching for '{}' in the default gate library directories...", file_path, stripped_name.string());
191 if (lib_path.empty())
193 log_info(
"gate_library_manager",
"could not find gate library file '{}'.", stripped_name.string());
196 absolute_path = std::filesystem::absolute(lib_path);
200 if (
auto it = m_gate_libraries.find(absolute_path.string()); it != m_gate_libraries.end())
202 return it->second.get();
206 return load(absolute_path);
211 for (
const auto& it : m_gate_libraries)
213 if (it.second->get_name() == lib_name)
215 return it.second.get();
223 std::vector<GateLibrary*> res;
224 res.reserve(m_gate_libraries.size());
225 for (
const auto& it : m_gate_libraries)
227 res.push_back(it.second.get());
static BooleanFunction Const(const BooleanFunction::Value &value)
Result< GatePin * > create_pin(const u32 id, const std::string &name, PinDirection direction, PinType type=PinType::none, bool create_group=true)
void add_boolean_function(const std::string &name, const BooleanFunction &function)
#define log_error(channel,...)
#define log_info(channel,...)
#define ERR_APPEND(prev_error, message)
GateLibrary * get_gate_library_by_name(const std::string &lib_name)
std::vector< GateLibrary * > get_gate_libraries()
void remove(std::filesystem::path file_path)
std::vector< std::filesystem::path > get_all_path()
void load_all(bool reload)
GateLibrary * get_gate_library(const std::string &file_path)
GateLibrary * load(std::filesystem::path file_path, bool reload)
bool save(std::filesystem::path file_path, GateLibrary *gate_lib, bool overwrite)
std::unique_ptr< GateLibrary > parse(std::filesystem::path file_path)
bool write(const GateLibrary *gate_lib, const std::filesystem::path &file_path)
std::vector< std::filesystem::path > get_gate_library_directories()
std::filesystem::path get_file(std::string file_name, std::vector< std::filesystem::path > path_hints)