Browse Source

Emscripten: Fix try_run to run the `.js` file and not the adjacent `.wasm`

Since commit 96d9b94a98 (Emscripten: Add platform modules, 2025-05-16,
v4.2.0-rc1~607^2~3) we've considered the `.wasm` to be the `try_compile`
output because we need `COPY_FILE` to get it for extracting `INFO:`
strings during our inspection checks.  This breaks `try_run` because
`node`, used via `CMAKE_CROSSCOMPILING_EMULATOR`, expects the `.js`.
Revert to considering the `.js` to be the primary output file, but
switch to the `.wasm` in `COPY_FILE`'s implementation.

Fixes: #27421
Brad King 3 months ago
parent
commit
a308ea38f3

+ 14 - 10
Source/cmCoreTryCompile.cxx

@@ -1232,8 +1232,20 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
 
     if ((res == 0) && arguments.CopyFileTo) {
       std::string const& copyFile = *arguments.CopyFileTo;
+      std::string outputFile = this->OutputFile;
+
+      // Emscripten `.js` executables have an adjacent `.wasm` file with the
+      // actual compiled binary.  Our COPY_FILE clients need the latter.
+      if (cmHasLiteralSuffix(outputFile, ".js")) {
+        std::string wasmOutput =
+          cmStrCat(outputFile.substr(0, outputFile.length() - 3), ".wasm");
+        if (cmSystemTools::FileExists(wasmOutput)) {
+          outputFile = std::move(wasmOutput);
+        }
+      }
+
       cmsys::SystemTools::CopyStatus status =
-        cmSystemTools::CopyFileAlways(this->OutputFile, copyFile);
+        cmSystemTools::CopyFileAlways(outputFile, copyFile);
       if (!status) {
         std::string err = status.GetString();
         switch (status.Path) {
@@ -1249,7 +1261,7 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
         /* clang-format off */
         err = cmStrCat(
           "Cannot copy output executable\n"
-          "  '", this->OutputFile, "'\n"
+          "  '", outputFile, "'\n"
           "to destination specified by COPY_FILE:\n"
           "  '", copyFile, "'\n"
           "because:\n"
@@ -1388,14 +1400,6 @@ void cmCoreTryCompile::FindOutputFile(std::string const& targetName)
     return;
   }
 
-  if (cmHasLiteralSuffix(outputFileLocation, ".js")) {
-    std::string wasmOutputLocation = cmStrCat(
-      outputFileLocation.substr(0, outputFileLocation.length() - 3), ".wasm");
-    if (cmSystemTools::FileExists(wasmOutputLocation)) {
-      outputFileLocation = wasmOutputLocation;
-    }
-  }
-
   this->OutputFile = cmSystemTools::CollapseFullPath(outputFileLocation);
 }
 

+ 2 - 0
Tests/RunCMake/Emscripten/C-try_run-stdout.txt

@@ -0,0 +1,2 @@
+-- COMPILE_RESULT='TRUE'
+-- RUN_RESULT='12'

+ 8 - 0
Tests/RunCMake/Emscripten/C-try_run.cmake

@@ -0,0 +1,8 @@
+enable_language(C)
+try_run(RUN_RESULT
+  COMPILE_RESULT
+  SOURCE_FROM_CONTENT main.c "int main(void) { return 12; }\n"
+  NO_CACHE
+)
+message(STATUS "COMPILE_RESULT='${COMPILE_RESULT}'")
+message(STATUS "RUN_RESULT='${RUN_RESULT}'")

+ 8 - 0
Tests/RunCMake/Emscripten/RunCMakeTest.cmake

@@ -87,4 +87,12 @@ foreach(_emscripten_toolchain IN LISTS _emscripten_toolchains)
     -DCMAKE_SYSTEM_NAME=Emscripten
     -DCMAKE_C_COMPILER=${c_comp}
   )
+
+  if(CMake_TEST_Emscripten_NODE)
+    run_cmake_with_options(C-try_run
+      -DCMAKE_SYSTEM_NAME=Emscripten
+      -DCMAKE_CROSSCOMPILING_EMULATOR=${CMake_TEST_Emscripten_NODE}
+      -DCMAKE_C_COMPILER=${c_comp}
+    )
+  endif()
 endforeach()