From 3d894b3a470462ff2ad8822f8edba10ec286eb37 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 25 Jan 2022 12:50:51 +0100
Subject: [PATCH] =?UTF-8?q?Macroprocessor:=20under=20Windows,=20trim=20tra?=
 =?UTF-8?q?iling=20slashes=20and=20backslashes=20in=20the=20value=20of=20?=
 =?UTF-8?q?=E2=80=9C@#includepath=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is a  workaround for a GCC/MinGW bug present in version 10.2
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88881), that affects
gcc-mingw-w64 in Debian “Bullseye” 11. It is fixed in GCC 10.3, and thus should
be fixed in Debian “Bookworm” 12.

Ref. Madysson/estimation-codes#11
---
 src/macro/Directives.cc | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/macro/Directives.cc b/src/macro/Directives.cc
index 9e8ccd99..9d9f94c6 100644
--- a/src/macro/Directives.cc
+++ b/src/macro/Directives.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019-2021 Dynare Team
+ * Copyright © 2019-2022 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -98,7 +98,20 @@ IncludePath::interpret(ostream &output, Environment &env, vector<filesystem::pat
       StringPtr msp = dynamic_pointer_cast<String>(expr->eval(env));
       if (!msp)
         throw StackTrace("File name does not evaluate to a string");
+#ifdef _WIN32
+      /* Trim trailing slashes and backslashes in the path. This is a
+         workaround for a GCC/MinGW bug present in version 10.2
+         https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88881, that affects
+         gcc-mingw-w64 in Debian “Bullseye” 11. It is fixed in GCC 10.3, and
+         thus should be fixed in Debian “Bookworm” 12.
+         See Madysson/estimation-codes#11. */
+      string ipstr = static_cast<string>(*msp);
+      while (ipstr.size() > 1 && (ipstr.back() == '/' || ipstr.back() == '\\'))
+        ipstr.pop_back();
+      path ip{ipstr};
+#else
       path ip = static_cast<string>(*msp);
+#endif
       if (!is_directory(ip))
         throw StackTrace(ip.string() + " does not evaluate to a valid directory");
       if (!exists(ip))
-- 
GitLab