From 5580e1bd3c0e7ad177b7727c790c88e934cf57e7 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Wed, 6 Mar 2024 21:28:46 -0500
Subject: [PATCH] rts: Drop .wasm suffix from .prof file names

This replicates the behavior on Windows, where `Hi.exe` will produce
profiling output named `Hi.prof` instead of `Hi.exe.prof`.

While in the area I also fixed the extension-stripping logic, which
incorrectly rewrote `Hi.exefoo` to `Hi.foo`.

Closes #24515.
---
 rts/ProfHeap.c  | 14 +++++---------
 rts/Profiling.c | 17 ++++++-----------
 rts/RtsUtils.c  | 12 ++++++++++++
 rts/RtsUtils.h  |  3 +++
 4 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/rts/ProfHeap.c b/rts/ProfHeap.c
index 65044b5e801e..52bdc158e4fb 100644
--- a/rts/ProfHeap.c
+++ b/rts/ProfHeap.c
@@ -448,18 +448,14 @@ initHeapProfiling(void)
         stem = stgMallocBytes(strlen(RtsFlags.CcFlags.outputFileNameStem) + 1, "initHeapProfiling");
         strcpy(stem, RtsFlags.CcFlags.outputFileNameStem);
     } else {
-
         stem = stgMallocBytes(strlen(prog_name) + 1, "initHeapProfiling");
         strcpy(stem, prog_name);
+
+        // Drop the platform's executable suffix if there is one
 #if defined(mingw32_HOST_OS)
-            // on Windows, drop the .exe suffix if there is one
-            {
-                char *suff;
-                suff = strrchr(stem,'.');
-                if (suff != NULL && !strcmp(suff,".exe")) {
-                    *suff = '\0';
-                }
-            }
+        dropExtension(stem, ".exe");
+#elif defined(wasm32_HOST_ARCH)
+        dropExtension(stem, ".wasm");
 #endif
     }
 
diff --git a/rts/Profiling.c b/rts/Profiling.c
index 2ba985434b16..af629eb7421d 100644
--- a/rts/Profiling.c
+++ b/rts/Profiling.c
@@ -245,19 +245,14 @@ initProfilingLogFile(void)
     if (RtsFlags.CcFlags.outputFileNameStem) {
         stem = RtsFlags.CcFlags.outputFileNameStem;
     } else {
-        char *prog;
-
-        prog = arenaAlloc(prof_arena, strlen(prog_name) + 1);
+        char *prog = arenaAlloc(prof_arena, strlen(prog_name) + 1);
         strcpy(prog, prog_name);
+
+        // Drop the platform's executable suffix if there is one
 #if defined(mingw32_HOST_OS)
-        // on Windows, drop the .exe suffix if there is one
-        {
-            char *suff;
-            suff = strrchr(prog,'.');
-            if (suff != NULL && !strcmp(suff,".exe")) {
-                *suff = '\0';
-            }
-        }
+        dropExtension(prog, ".exe");
+#elif defined(wasm32_HOST_ARCH)
+        dropExtension(prog, ".wasm");
 #endif
         stem = prog;
     }
diff --git a/rts/RtsUtils.c b/rts/RtsUtils.c
index 4cac10ba156d..6a1faf7a3079 100644
--- a/rts/RtsUtils.c
+++ b/rts/RtsUtils.c
@@ -456,3 +456,15 @@ void checkFPUStack(void)
     }
 #endif
 }
+
+// Drop the given extension from a filepath.
+void dropExtension(char *path, const char *extension) {
+    int ext_len = strlen(extension);
+    int path_len = strlen(path);
+    if (ext_len < path_len) {
+        char *s = &path[path_len - ext_len];
+        if (strcmp(s, extension) == 0) {
+            *s = '\0';
+        }
+    }
+}
diff --git a/rts/RtsUtils.h b/rts/RtsUtils.h
index 2f4827c5263d..095f8d1bc7a7 100644
--- a/rts/RtsUtils.h
+++ b/rts/RtsUtils.h
@@ -62,4 +62,7 @@ void checkFPUStack(void);
 #define xstr(s) str(s)
 #define str(s) #s
 
+// Drop the given extension from a filepath.
+void dropExtension(char *path, const char *extension);
+
 #include "EndPrivate.h"
-- 
GitLab