Re: [PATCH] Fix rust on *-w64-mingw32

2024-04-27 Thread LIU Hao via Gcc
Attached is an alternative patch to functionalize `load_macros_array`. It allows GCC to build on 
x86_64-w64-mingw32. Not tested though, as I know no Rust.


As before, please edit the patch at your disposal.

--
Best regards,
LIU Hao

diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc 
b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
index 5daa7eb8ded..9991e4b14f3 100644
--- a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
@@ -24,20 +24,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
-  (void) ret;
-}
-
 void
 dump_function_bir (const std::string &filename, BIR::Function &func,
   const std::string &name)
@@ -61,7 +47,7 @@ BorrowChecker::go (HIR::Crate &crate)
 
   if (enable_dump_bir)
 {
-  mkdir_wrapped ("bir_dump");
+  mkdir ("bir_dump", 0755);
   auto mappings = Analysis::Mappings::get ();
   bool ok
= mappings->get_crate_name (crate.get_mappings ().get_crate_num (),
diff --git a/gcc/rust/expand/rust-proc-macro.cc 
b/gcc/rust/expand/rust-proc-macro.cc
index 09680733e98..d658b4d10db 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 
+#else
 #include 
 #endif
 
@@ -102,12 +105,16 @@ static_assert (
 
 } // namespace
 
-template 
+template 
 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,
@@ -129,7 +136,18 @@ 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)
@@ -137,6 +155,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))
@@ -153,12 +172,12 @@ load_macros_array (std::string path)
   auto symbol_name = generate_proc_macro_decls_symbol (0 /* FIXME */);
 
   return *reinterpret_cast (
-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 504a409cc4d..634ad0bf0c8 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;


OpenPGP_signature.asc
Description: OpenPGP digital signature


gcc-14-20240427 is now available

2024-04-27 Thread GCC Administrator via Gcc
Snapshot gcc-14-20240427 is now available on
  https://gcc.gnu.org/pub/gcc/snapshots/14-20240427/
and on various mirrors, see https://gcc.gnu.org/mirrors.html for details.

This snapshot has been generated from the GCC 14 git branch
with the following options: git://gcc.gnu.org/git/gcc.git branch 
releases/gcc-14 revision 3e04b6f6ba568e6183e8aa8223d4156234503843

You'll find:

 gcc-14-20240427.tar.xz   Complete GCC

  SHA256=7eeb7d6a732184d170390715f436a316c3adab67d06897a8f4f00a86e07baeac
  SHA1=5f64d92579cdbcd681a17bb4a0e454e290a96ed3

Diffs from 14-20240421 are available in the diffs/ subdirectory.

When a particular snapshot is ready for public consumption the LATEST-14
link is updated and a message is sent to the gcc list.  Please do not use
a snapshot before it has been announced that way.