Use the bootstrap compiler only to build LLVM

When a bootstrap LLVM was installed, every CMake build that emsdk ran
was configured to use it as the compiler, i.e. also Binaryen, Ninja and
ccache. The only exclusion was the build directory of that bootstrap
version itself, so e.g. LLVM 17.0.6 would get built with LLVM 16.0.6 if
both were installed.

Choose the compiler per tool instead:
 - the bootstrap LLVM versions are built with the system compiler,
 - other versions of LLVM are built with the bootstrap compiler,
 - the other tools (Binaryen, Ninja, ccache) are built with the LLVM
   that was built with the bootstrap compiler, if it has been built.

Nothing changes when no bootstrap compiler is installed: everything is
then built with the system compiler, like before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/emsdk.py b/emsdk.py
index 46aeea4..92b83bf 100644
--- a/emsdk.py
+++ b/emsdk.py
@@ -1024,7 +1024,47 @@
 BOOTSTRAP_LLVM_VERSIONS = ['17.0.6', '16.0.6', '15.0.7']
 
 
-def cmake_configure(generator, build_root, src_root, build_type, extra_cmake_args):
+def find_bootstrap_compiler():
+    """Returns the (C compiler, C++ compiler) pair of the bootstrap compiler to
+    build LLVM with, or None if no bootstrap compiler has been installed.
+    """
+    for ver in BOOTSTRAP_LLVM_VERSIONS:
+        bootstrap_build_root = sdk_path(f'llvm/{ver}/build_llvmorg-{ver}_64')
+        bs_clang = os.path.join(bootstrap_build_root, 'bin', 'clang')
+        bs_clangxx = os.path.join(bootstrap_build_root, 'bin', 'clang++')
+        if os.path.isfile(bs_clang):
+            if MACOS and not glob.glob(os.path.join(bootstrap_build_root, 'lib', 'clang', '*', 'lib', 'darwin', 'libclang_rt.osx.a')):
+                # A bootstrap compiler that was built before the compiler-rt
+                # builtins were built along with it (see build_llvm).
+                info(f'Not using bootstrap compiler from {bs_clang}, since it does not have the compiler-rt builtins library. Reinstall llvm-git-{ver}-64bit to build it.')
+                continue
+            return (bs_clang, bs_clangxx)
+    return None
+
+
+def find_built_llvm_compiler():
+    """Returns the (C compiler, C++ compiler) pair of the LLVM that emsdk has
+    built from source with the bootstrap compiler, or None if there is no such
+    LLVM.
+
+    If a bootstrap compiler is installed, then the system compiler is too old,
+    so the other tools than LLVM are built with the LLVM that emsdk built. The
+    bootstrap compiler itself is only used to build LLVM.
+    """
+    if not find_bootstrap_compiler():
+        return None
+    for tool in tools:
+        if tool.id != 'llvm-git' or tool.version in BOOTSTRAP_LLVM_VERSIONS or tool.bitness != 64:
+            continue
+        bin_dir = os.path.join(tool.installation_path(), llvm_build_bin_dir(tool))
+        clang = os.path.join(bin_dir, exe_suffix('clang'))
+        clangxx = os.path.join(bin_dir, exe_suffix('clang++'))
+        if os.path.isfile(clang) and os.path.isfile(clangxx):
+            return (clang, clangxx)
+    return None
+
+
+def cmake_configure(generator, build_root, src_root, build_type, extra_cmake_args, compiler=None):
     debug_print(f'cmake_configure(generator={generator}, build_root={build_root}, '
                 f'src_root={src_root}, build_type={build_type}, extra_cmake_args={extra_cmake_args})')
     # Configure
@@ -1039,22 +1079,9 @@
     cmdline += ['-DCMAKE_OSX_DEPLOYMENT_TARGET=11.0']
     cmdline += [*extra_cmake_args, src_root]
 
-    # If we have a bootstrap compiler package installed, then use that compiler
-    # in the cmake build.
-    for ver in BOOTSTRAP_LLVM_VERSIONS:
-        bootstrap_build_root = os.path.abspath(f'llvm/{ver}/build_llvmorg-{ver}_64')
-        bootstrap_compiler_path = os.path.join(bootstrap_build_root, 'bin')
-        bs_clang = os.path.join(bootstrap_compiler_path, 'clang')
-        bs_clangxx = os.path.join(bootstrap_compiler_path, 'clang++')
-        if os.path.isfile(bs_clang) and f'llvm/{ver}' not in build_root:
-            if MACOS and not glob.glob(os.path.join(bootstrap_build_root, 'lib', 'clang', '*', 'lib', 'darwin', 'libclang_rt.osx.a')):
-                # A bootstrap compiler that was built before the compiler-rt
-                # builtins were built along with it (see build_llvm).
-                info(f'Not using bootstrap compiler from {bs_clang}, since it does not have the compiler-rt builtins library. Reinstall llvm-git-{ver}-64bit to build it.')
-                continue
-            info(f'Using bootstrap compiler from {bs_clang} in CMake configure')
-            cmdline += [f'-DCMAKE_C_COMPILER={bs_clang}', f'-DCMAKE_CXX_COMPILER={bs_clangxx}']
-            break
+    if compiler:
+        info(f'Using compiler {compiler[0]} in CMake configure')
+        cmdline += [f'-DCMAKE_C_COMPILER={compiler[0]}', f'-DCMAKE_CXX_COMPILER={compiler[1]}']
 
     print(f'Running CMake: {cmdline}')
 
@@ -1132,8 +1159,11 @@
     return (cmake_generator, args)
 
 
-def cmake_configure_and_build(cmake_generator, build_root, cmakelists_dir, build_type, args):
-    success = cmake_configure(cmake_generator, build_root, cmakelists_dir, build_type, args)
+def cmake_configure_and_build(cmake_generator, build_root, cmakelists_dir, build_type, args, compiler=None):
+    """Configures and builds the given CMake project. compiler is an optional
+    (C compiler, C++ compiler) pair to use instead of the system compiler.
+    """
+    success = cmake_configure(cmake_generator, build_root, cmakelists_dir, build_type, args, compiler)
     if success:
         success = make_build(build_root, build_type)
 
@@ -1142,7 +1172,7 @@
         shutil.rmtree(build_root)
 
         # And re-try configure + build again.
-        success = cmake_configure(cmake_generator, build_root, cmakelists_dir, build_type, args)
+        success = cmake_configure(cmake_generator, build_root, cmakelists_dir, build_type, args, compiler)
         if success:
             success = make_build(build_root, build_type)
 
@@ -1223,7 +1253,11 @@
 
     cmakelists_dir = os.path.join(llvm_src_root, 'llvm')
 
-    success = cmake_configure_and_build(cmake_generator, build_root, cmakelists_dir, build_type, args)
+    # Build LLVM with the bootstrap compiler, if one is installed. The bootstrap
+    # compilers themselves are built with the system compiler.
+    compiler = None if tool.version in BOOTSTRAP_LLVM_VERSIONS else find_bootstrap_compiler()
+
+    success = cmake_configure_and_build(cmake_generator, build_root, cmakelists_dir, build_type, args, compiler)
 
     return success
 
@@ -1246,7 +1280,7 @@
 
     cmakelists_dir = os.path.join(src_root)
 
-    success = cmake_configure_and_build(cmake_generator, build_root, cmakelists_dir, build_type, args)
+    success = cmake_configure_and_build(cmake_generator, build_root, cmakelists_dir, build_type, args, find_built_llvm_compiler())
 
     if success:
         bin_dir = os.path.join(root, 'bin')
@@ -1282,7 +1316,7 @@
 
     cmakelists_dir = os.path.join(src_root)
 
-    success = cmake_configure_and_build(cmake_generator, build_root, cmakelists_dir, build_type, args)
+    success = cmake_configure_and_build(cmake_generator, build_root, cmakelists_dir, build_type, args, find_built_llvm_compiler())
 
     if success:
         bin_dir = os.path.join(root, 'bin')
@@ -1628,7 +1662,7 @@
         if BUILD_FOR_TESTING:
             args += ['-DRUN_STATIC_ANALYZER=1']
 
-    success = cmake_configure_and_build(cmake_generator, build_root, src_root, build_type, args)
+    success = cmake_configure_and_build(cmake_generator, build_root, src_root, build_type, args, find_built_llvm_compiler())
 
     if success:
         # Deploy scripts needed from source repository to build directory