Changes
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 65fd17f..a41133c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,11 @@
cmake_minimum_required(VERSION 3.24)
+option(MACRODOWN_BOOTSTRAP_EMSDK
+ "Download a temporary Emscripten SDK and build for WebAssembly" OFF)
+if(MACRODOWN_BOOTSTRAP_EMSDK)
+ include(cmake/bootstrap_emsdk.cmake)
+endif()
+
project(MacroDown LANGUAGES CXX VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 23)
diff --git a/README.md b/README.md
index 2271e7a..7d607a9 100644
--- a/README.md
+++ b/README.md
@@ -157,7 +157,29 @@ cd build
## WebAssembly and live preview
-Install and activate [Emscripten](https://emscripten.org/docs/getting_started/downloads.html),
+You can let CMake download and install Emscripten into `/tmp`:
+
+```sh
+cmake -S . -B build_wasm -DMACRODOWN_BOOTSTRAP_EMSDK=ON \
+ -DCMAKE_BUILD_TYPE=Release
+cmake --build build_wasm -j24
+python3 -m http.server 8000 --directory build_wasm/web
+```
+
+This opt-in bootstrap requires Git, Python 3.8 or newer, and network access
+for the first download. It installs and activates Emscripten 6.0.12 under
+`/tmp/macrodown_emsdk`, then selects its toolchain automatically; `emcmake`
+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.
+
+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`.
+
+Alternatively, install and activate [Emscripten](https://emscripten.org/docs/getting_started/downloads.html),
then configure a separate build directory:
```sh
diff --git a/cmake/bootstrap_emsdk.cmake b/cmake/bootstrap_emsdk.cmake
new file mode 100644
index 0000000..048380d
--- /dev/null
+++ b/cmake/bootstrap_emsdk.cmake
@@ -0,0 +1,51 @@
+# Download and activate a local SDK before project() detects the compiler.
+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")
+ 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)
+ message(FATAL_ERROR
+ "SDK bootstrap cannot be combined with a different toolchain file")
+endif()
+
+find_package(Git REQUIRED)
+find_package(Python3 3.8 REQUIRED COMPONENTS Interpreter)
+
+# Serialize configurations that share this SDK directory.
+get_filename_component(emsdk_parent "${MACRODOWN_EMSDK_DIR}" DIRECTORY)
+file(MAKE_DIRECTORY "${emsdk_parent}")
+file(LOCK "${MACRODOWN_EMSDK_DIR}.lock" GUARD FILE TIMEOUT 600)
+
+if(NOT EXISTS "${MACRODOWN_EMSDK_DIR}/emsdk.py")
+ message(STATUS "Downloading emsdk ${MACRODOWN_EMSDK_VERSION}")
+ execute_process(
+ COMMAND "${GIT_EXECUTABLE}" clone --depth 1
+ --branch "${MACRODOWN_EMSDK_VERSION}"
+ https://github.com/emscripten-core/emsdk.git
+ "${MACRODOWN_EMSDK_DIR}"
+ COMMAND_ERROR_IS_FATAL ANY
+ )
+endif()
+
+message(STATUS "Installing Emscripten ${MACRODOWN_EMSDK_VERSION}")
+execute_process(
+ COMMAND "${Python3_EXECUTABLE}" "${MACRODOWN_EMSDK_DIR}/emsdk.py"
+ install "${MACRODOWN_EMSDK_VERSION}"
+ COMMAND_ERROR_IS_FATAL ANY
+)
+execute_process(
+ COMMAND "${Python3_EXECUTABLE}" "${MACRODOWN_EMSDK_DIR}/emsdk.py"
+ 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)