https://gcc.gnu.org/g:a739da61d30920d91193a1e198efd50ae5790bb1

commit r15-8139-ga739da61d30920d91193a1e198efd50ae5790bb1
Author: Arthur Cohen <arthur.co...@embecosm.com>
Date:   Mon Sep 16 14:06:13 2024 +0200

    gccrs: mingw: Fix build with patch from Liu Hao
    
    This commit adds Liu Hao's patch from
    
https://github.com/lhmouse/MINGW-packages/blob/5859d27b2b6101204a08ad9702cb2937f8797be9/mingw-w64-gcc/0100-rust-fix.patch
    
    gcc/rust/ChangeLog:
    
            * checks/errors/borrowck/rust-borrow-checker.cc (mkdir_wrapped): 
Remove.
            (BorrowChecker::go): Use `mkdir` instead.
            * expand/rust-proc-macro.cc (register_callback): Use Windows APIs to
            open dynamic proc macro library.
            (load_macros_array): Likewise.
            * parse/rust-parse.cc (defined): Replace separators in paths using
            std::replace.

Diff:
---
 .../checks/errors/borrowck/rust-borrow-checker.cc  | 23 +++-----------
 gcc/rust/expand/rust-proc-macro.cc                 | 36 +++++++++++++++++-----
 gcc/rust/parse/rust-parse.cc                       |  2 +-
 3 files changed, 34 insertions(+), 27 deletions(-)

diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc 
b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
index 6eec021abcab..29a9f4d27bbd 100644
--- a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
@@ -26,20 +26,6 @@
 namespace Rust {
 namespace HIR {
 
-void
-mkdir_wrapped (const std::string &dirname)
-{
-  int ret;
-#ifdef _WIN32
-  ret = _mkdir (dirname.c_str ());
-#elif unix
-  ret = mkdir (dirname.c_str (), 0775);
-#elif __APPLE__
-  ret = mkdir (dirname.c_str (), 0775);
-#endif
-  rust_assert (ret == 0 || errno == EEXIST);
-}
-
 void
 dump_function_bir (const std::string &filename, BIR::Function &func,
                   const std::string &name)
@@ -63,11 +49,11 @@ BorrowChecker::go (HIR::Crate &crate)
 
   if (enable_dump_bir)
     {
-      mkdir_wrapped ("bir_dump");
+      mkdir ("bir_dump", 0755);
       auto &mappings = Analysis::Mappings::get ();
       crate_name
        = *mappings.get_crate_name (crate.get_mappings ().get_crate_num ());
-      mkdir_wrapped ("nll_facts_gccrs");
+      mkdir ("nll_facts_gccrs", 0755);
     }
 
   FunctionCollector collector;
@@ -95,8 +81,9 @@ BorrowChecker::go (HIR::Crate &crate)
 
       if (enable_dump_bir)
        {
-         mkdir_wrapped ("nll_facts_gccrs/"
-                        + func->get_function_name ().as_string ());
+         auto dir
+           = "nll_facts_gccrs/" + func->get_function_name ().as_string ();
+         mkdir (dir.c_str (), 0755);
          auto dump_facts_to_file
            = [&] (const std::string &suffix,
                   void (Polonius::Facts::*fn) (std::ostream &) const) {
diff --git a/gcc/rust/expand/rust-proc-macro.cc 
b/gcc/rust/expand/rust-proc-macro.cc
index 7a02ff330580..22023e19a749 100644
--- a/gcc/rust/expand/rust-proc-macro.cc
+++ b/gcc/rust/expand/rust-proc-macro.cc
@@ -22,7 +22,10 @@
 #include "rust-token-converter.h"
 #include "rust-attributes.h"
 
-#ifndef _WIN32
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#else
 #include <dlfcn.h>
 #endif
 
@@ -100,12 +103,16 @@ static_assert (
 
 } // namespace
 
-template <typename Symbol, typename Callback>
+template <typename Handle, typename Symbol, typename Callback>
 bool
-register_callback (void *handle, Symbol, std::string symbol_name,
+register_callback (Handle handle, Symbol, std::string symbol_name,
                   Callback callback)
 {
+#ifdef _WIN32
+  FARPROC addr = GetProcAddress (handle, symbol_name.c_str ());
+#else
   void *addr = dlsym (handle, symbol_name.c_str ());
+#endif
   if (addr == nullptr)
     {
       rust_error_at (UNDEF_LOCATION,
@@ -127,7 +134,19 @@ register_callback (void *handle, Symbol, std::string 
symbol_name,
 const ProcMacro::ProcmacroArray *
 load_macros_array (std::string path)
 {
-#ifndef _WIN32
+#ifdef _WIN32
+  HMODULE handle = LoadLibraryA (path.c_str ());
+  // We're leaking the handle since we can't ever unload it
+  if (!handle)
+    {
+      char msg[300];
+      FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM
+                       | FORMAT_MESSAGE_IGNORE_INSERTS,
+                     nullptr, GetLastError (), 0, msg, sizeof msg, nullptr);
+      rust_debug ("Error whilst opening procedural macro: %s", msg);
+      return nullptr;
+    }
+#else
   void *handle = dlopen (path.c_str (), RTLD_LAZY | RTLD_LOCAL);
   // We're leaking the handle since we can't ever unload it
   if (!handle)
@@ -135,6 +154,7 @@ load_macros_array (std::string path)
       rust_debug ("Error whilst opening procedural macro: %s", dlerror ());
       return nullptr;
     }
+#endif
 
   if (!REGISTER_CALLBACK (handle, __gccrs_proc_macro_ts_from_str_,
                          tokenstream_from_string))
@@ -151,12 +171,12 @@ load_macros_array (std::string path)
   auto symbol_name = generate_proc_macro_decls_symbol (0 /* FIXME */);
 
   return *reinterpret_cast<const ProcMacro::ProcmacroArray **> (
-    dlsym (handle, symbol_name.c_str ()));
+#ifdef _WIN32
+    GetProcAddress (handle, symbol_name.c_str ())
 #else
-  rust_sorry_at (UNDEF_LOCATION,
-                "Procedural macros are not yet supported on windows host");
-  rust_unreachable ();
+    dlsym (handle, symbol_name.c_str ())
 #endif
+  );
 }
 
 #undef REGISTER_CALLBACK
diff --git a/gcc/rust/parse/rust-parse.cc b/gcc/rust/parse/rust-parse.cc
index 9aca78105221..43d15aa2bb9e 100644
--- a/gcc/rust/parse/rust-parse.cc
+++ b/gcc/rust/parse/rust-parse.cc
@@ -89,7 +89,7 @@ extract_module_path (const AST::AttrVec &inner_attrs,
   // Source: rustc compiler
   // 
(https://github.com/rust-lang/rust/blob/9863bf51a52b8e61bcad312f81b5193d53099f9f/compiler/rustc_expand/src/module.rs#L174)
 #if defined(HAVE_DOS_BASED_FILE_SYSTEM)
-  path.replace ('/', '\\');
+  std::replace (path.begin (), path.end (), '/', '\\');
 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
 
   return path;

Reply via email to