BareGit

Fix Emscripten bootstrap when MacroDown is a dependency

Preserve the native parent toolchain and expose the installed emcmake for
separate browser builds. Document dependency usage with ExternalProject.
Author: MetroWind <chris.corsair@gmail.com>
Date: Sun Oct 11 12:16:46 2026 -0700
Commit: 59c060fdc702ea967ee7ea3c2c692044c3a05578

Changes

diff --git a/CMakeLists.txt b/CMakeLists.txt
index a41133c..6abb997 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,7 +1,7 @@
 cmake_minimum_required(VERSION 3.24)
 
 option(MACRODOWN_BOOTSTRAP_EMSDK
-    "Download a temporary Emscripten SDK and build for WebAssembly" OFF)
+    "Install a temporary SDK; standalone builds select WebAssembly" OFF)
 if(MACRODOWN_BOOTSTRAP_EMSDK)
     include(cmake/bootstrap_emsdk.cmake)
 endif()
diff --git a/README.md b/README.md
index 7d607a9..ec5e171 100644
--- a/README.md
+++ b/README.md
@@ -172,12 +172,37 @@ for the first download. It installs and activates Emscripten 6.0.12 under
 and shell activation are unnecessary. Subsequent configurations reuse the
 SDK. It does not modify your shell startup files or install system packages.
 The SDK is temporary and may need to be downloaded again after `/tmp` is
-cleaned. Use a separate build directory from your native build.
+cleaned. For standalone builds, use a separate directory from your native
+build.
 
 Set `MACRODOWN_EMSDK_DIR` to change the installation directory and
 `MACRODOWN_EMSDK_VERSION` to select a release. When changing SDK versions,
 use a new SDK directory and build directory. The bootstrap cannot be
-combined with a different `CMAKE_TOOLCHAIN_FILE`.
+combined with a different `CMAKE_TOOLCHAIN_FILE` in standalone builds.
+
+When MacroDown is included through `FetchContent` or `add_subdirectory`,
+the option installs the SDK but keeps the parent project's native compiler
+and toolchain. It sets the `EMCMAKE_EXECUTABLE` cache variable to the SDK's
+`emcmake`, so the parent can configure a separate WebAssembly build:
+
+```cmake
+include(ExternalProject)
+ExternalProject_Add(macrodown_browser
+    SOURCE_DIR "${macrodown_SOURCE_DIR}"
+    BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/macrodown_wasm"
+    CONFIGURE_COMMAND "${EMCMAKE_EXECUTABLE}" "${CMAKE_COMMAND}"
+        -S <SOURCE_DIR> -B <BINARY_DIR>
+        -DCMAKE_BUILD_TYPE=Release
+        -DMACRODOWN_BUILD_WASM=ON
+        -DMACRODOWN_BUILD_CLI=OFF
+        -DMACRODOWN_BUILD_TESTS=OFF
+    INSTALL_COMMAND ""
+)
+```
+
+The browser artifacts are generated under `macrodown_wasm/web` in the
+parent's build directory. Bootstrap alone does not add a browser build to
+a native parent; the separate build above does.
 
 Alternatively, install and activate [Emscripten](https://emscripten.org/docs/getting_started/downloads.html),
 then configure a separate build directory:
diff --git a/cmake/bootstrap_emsdk.cmake b/cmake/bootstrap_emsdk.cmake
index 048380d..823bc38 100644
--- a/cmake/bootstrap_emsdk.cmake
+++ b/cmake/bootstrap_emsdk.cmake
@@ -1,17 +1,24 @@
-# Download and activate a local SDK before project() detects the compiler.
+# Install a local SDK; select its toolchain only for standalone builds.
 set(MACRODOWN_EMSDK_VERSION "6.0.12" CACHE STRING
     "Emscripten SDK release to install")
 set(MACRODOWN_EMSDK_DIR "/tmp/macrodown_emsdk" CACHE PATH
     "Directory for the temporary Emscripten SDK")
 
-if(DEFINED CMAKE_CXX_COMPILER AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
+set(macrodown_standalone OFF)
+if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
+    set(macrodown_standalone ON)
+endif()
+
+if(macrodown_standalone AND DEFINED CMAKE_CXX_COMPILER
+    AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
     message(FATAL_ERROR
         "SDK bootstrap requires a fresh build directory, not a native build")
 endif()
 
 set(emsdk_toolchain
     "${MACRODOWN_EMSDK_DIR}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake")
-if(CMAKE_TOOLCHAIN_FILE AND NOT CMAKE_TOOLCHAIN_FILE STREQUAL emsdk_toolchain)
+if(macrodown_standalone AND CMAKE_TOOLCHAIN_FILE
+    AND NOT CMAKE_TOOLCHAIN_FILE STREQUAL emsdk_toolchain)
     message(FATAL_ERROR
         "SDK bootstrap cannot be combined with a different toolchain file")
 endif()
@@ -46,6 +53,11 @@ execute_process(
         activate "${MACRODOWN_EMSDK_VERSION}"
     COMMAND_ERROR_IS_FATAL ANY
 )
-set(CMAKE_TOOLCHAIN_FILE
-    "${MACRODOWN_EMSDK_DIR}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake"
-    CACHE FILEPATH "Emscripten toolchain" FORCE)
+# A native parent can use this in a separate ExternalProject Wasm build.
+set(EMCMAKE_EXECUTABLE
+    "${MACRODOWN_EMSDK_DIR}/upstream/emscripten/emcmake"
+    CACHE FILEPATH "emcmake from the bootstrapped Emscripten SDK" FORCE)
+if(macrodown_standalone)
+    set(CMAKE_TOOLCHAIN_FILE "${emsdk_toolchain}"
+        CACHE FILEPATH "Emscripten toolchain" FORCE)
+endif()