]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: import: Add cli extern crate resolution
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 14 Jun 2023 13:20:21 +0000 (15:20 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:55:58 +0000 (18:55 +0100)
This commit add the ability to specify the path to an extern crate
through the -frust-extern cli option. Path given as cli argument
shall resolve to the exact extern crate location.

gcc/rust/ChangeLog:

* metadata/rust-imports.h: Make the function to load a given
file public.
* rust-session-manager.cc (Session::load_extern_crate): Change
path resolution depending on extern crate declaration in cli
arguments.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/metadata/rust-imports.h
gcc/rust/rust-session-manager.cc

index 51cc4fc761383292112d135b9194ebc0a51806c1..c728adbbbd1976d8be86959da684a7c2b6b2be08 100644 (file)
@@ -112,6 +112,8 @@ public:
   static Stream *open_package (const std::string &filename, Location location,
                               const std::string &relative_import_path);
 
+  static Stream *try_package_in_directory (const std::string &, Location);
+
   // Constructor.
   Import (Stream *, Location);
 
@@ -153,8 +155,6 @@ public:
   void clear_stream () { this->stream_ = NULL; }
 
 private:
-  static Stream *try_package_in_directory (const std::string &, Location);
-
   static int try_suffixes (std::string *);
 
   static Stream *find_export_data (const std::string &filename, int fd,
index af57d577412488c8207413ba45fd2cf9f55badf6..cc3a2089c1ec7359ba0573bddf5368923ccf5e3b 100644 (file)
@@ -973,12 +973,26 @@ Session::load_extern_crate (const std::string &crate_name, location_t locus)
     }
 
   std::string relative_import_path = "";
-  Import::Stream *s
-    = Import::open_package (crate_name, locus, relative_import_path);
+  std::string import_name = crate_name;
+
+  // The path to the extern crate might have been specified by the user using
+  // -frust-extern
+  auto cli_extern_crate = extern_crates.find (crate_name);
+
+  Import::Stream *s;
+  if (cli_extern_crate != extern_crates.end ())
+    {
+      auto path = cli_extern_crate->second;
+      s = Import::try_package_in_directory (path, locus);
+    }
+  else
+    {
+      s = Import::open_package (import_name, locus, relative_import_path);
+    }
   if (s == NULL)
     {
       rust_error_at (locus, "failed to locate crate %<%s%>",
-                    crate_name.c_str ());
+                    import_name.c_str ());
       return UNKNOWN_NODEID;
     }