# 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")
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(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()
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
)
# 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()