Corrected addition of include path when including bin_fmt files across projects

PiperOrigin-RevId: 811937347
Change-Id: I64af5e8b60b125d2523440325869aead17a496ab
diff --git a/mpact/sim/decoder/bin_format_visitor.cc b/mpact/sim/decoder/bin_format_visitor.cc
index 58ff6ff..af53535 100644
--- a/mpact/sim/decoder/bin_format_visitor.cc
+++ b/mpact/sim/decoder/bin_format_visitor.cc
@@ -156,11 +156,11 @@
   // Add the directory of the input file to the include roots if not already
   // present.
   if (!file_names.empty()) {
-    std::string dir = std::filesystem::path(file_names[0]).stem().string();
+    std::string dir =
+        std::filesystem::path(file_names[0]).parent_path().string();
     auto it = std::find(include_dir_vec_.begin(), include_dir_vec_.end(), dir);
     if (it == include_dir_vec_.end()) {
-      include_dir_vec_.push_back(
-          std::filesystem::path(file_names[0]).stem().string());
+      include_dir_vec_.push_back(dir);
     }
   }
 
@@ -197,7 +197,8 @@
   for (int i = 1; i < file_names.size(); ++i) {
     // Add the directory of the input file to the include roots if not already
     // present.
-    std::string dir = std::filesystem::path(file_names[i]).stem().string();
+    std::string dir =
+        std::filesystem::path(file_names[i]).parent_path().string();
     auto it = std::find(include_dir_vec_.begin(), include_dir_vec_.end(), dir);
     if (it == include_dir_vec_.end()) {
       include_dir_vec_.push_back(dir);
@@ -672,6 +673,13 @@
     include_file.close();
     return;
   }
+  // Add the directory of the include file to the include roots if not already
+  // present.
+  std::string dir = std::filesystem::path(include_name).parent_path().string();
+  auto it = std::find(include_dir_vec_.begin(), include_dir_vec_.end(), dir);
+  if (it == include_dir_vec_.end()) {
+    include_dir_vec_.push_back(dir);
+  }
   std::string previous_file_name = error_listener()->file_name();
   int previous_file_index_ = current_file_index_;
   error_listener()->set_file_name(file_name);
diff --git a/mpact/sim/decoder/instruction_set_visitor.cc b/mpact/sim/decoder/instruction_set_visitor.cc
index a73f4b1..29cd685 100644
--- a/mpact/sim/decoder/instruction_set_visitor.cc
+++ b/mpact/sim/decoder/instruction_set_visitor.cc
@@ -111,11 +111,11 @@
 
   // Add the directory of the input file to the include roots.
   if (!file_names.empty()) {
-    std::string dir = std::filesystem::path(file_names[0]).stem().string();
+    std::string dir =
+        std::filesystem::path(file_names[0]).parent_path().string();
     auto it = std::find(include_dir_vec_.begin(), include_dir_vec_.end(), dir);
     if (it == include_dir_vec_.end()) {
-      include_dir_vec_.push_back(
-          std::filesystem::path(file_names[0]).stem().string());
+      include_dir_vec_.push_back(dir);
     }
   }
 
@@ -150,7 +150,8 @@
   for (int i = 1; i < file_names.size(); ++i) {
     // Add the directory of the input file to the include roots if not already
     // present.
-    std::string dir = std::filesystem::path(file_names[i]).stem().string();
+    std::string dir =
+        std::filesystem::path(file_names[i]).parent_path().string();
     auto it = std::find(include_dir_vec_.begin(), include_dir_vec_.end(), dir);
     if (it == include_dir_vec_.end()) {
       include_dir_vec_.push_back(dir);
@@ -551,10 +552,11 @@
   std::fstream include_file;
   // Open include file.
   include_file.open(file_name, std::fstream::in);
+  std::string include_name;
   if (!include_file.is_open()) {
     // Try each of the include file directories.
     for (auto const& dir : dirs) {
-      std::string include_name = dir + "/" + file_name;
+      include_name = dir + "/" + file_name;
       include_file.open(include_name, std::fstream::in);
       if (include_file.is_open()) break;
     }
@@ -565,6 +567,13 @@
       return;
     }
   }
+  // Add the directory of the include file to the include roots if not already
+  // present.
+  std::string dir = std::filesystem::path(include_name).parent_path().string();
+  auto it = std::find(include_dir_vec_.begin(), include_dir_vec_.end(), dir);
+  if (it == include_dir_vec_.end()) {
+    include_dir_vec_.push_back(dir);
+  }
   std::string previous_file_name = error_listener()->file_name();
   int previous_file_index_ = current_file_index_;
   error_listener()->set_file_name(file_name);