web-wasi-emulated-threads: initial addition

Todos:

- Add to CI if possible (builds LLVM/Clang)
- Add to README
This commit is contained in:
Matt McCormick 2025-01-22 16:26:14 -05:00
parent 1d459d9fbc
commit c0efd5bf10
13 changed files with 462 additions and 5 deletions

View File

@ -52,7 +52,7 @@ GEN_IMAGES := android-arm android-arm64 \
manylinux_2_28-x64 \
manylinux2014-x64 manylinux2014-x86 \
manylinux2014-aarch64 linux-arm64-lts \
web-wasm web-wasi web-wasi-threads linux-mips linux-mips-uclibc linux-mips-lts windows-arm64 windows-armv7 \
web-wasm web-wasi web-wasi-emulated-threads web-wasi-threads linux-mips linux-mips-uclibc linux-mips-lts windows-arm64 windows-armv7 \
windows-static-x86 windows-static-x64 windows-static-x64-posix \
windows-shared-x86 windows-shared-x64 windows-shared-x64-posix \
linux-armv7 linux-armv7a linux-armv7l-musl linux-armv7-lts linux-armv7a-lts linux-x86_64-full \
@ -63,13 +63,13 @@ GEN_IMAGES := android-arm android-arm64 \
# Generate both amd64 and arm64 images
MULTIARCH_IMAGES := linux-arm64 \
web-wasi
web-wasi web-wasi-emulated-threads
GEN_IMAGE_DOCKERFILES = $(addsuffix /Dockerfile,$(GEN_IMAGES))
# These images are expected to have explicit rules for *both* build and testing
NON_STANDARD_IMAGES := manylinux_2_28-x64 manylinux2014-x64 manylinux2014-x86 \
manylinux2014-aarch64 web-wasm web-wasi-threads
manylinux2014-aarch64 web-wasm web-wasi-emulated-threads web-wasi-threads
# Docker composite files
DOCKER_COMPOSITE_SOURCES = common.docker common.debian common.manylinux2014 common.manylinux_2_28 common.buildroot \

View File

@ -43,10 +43,10 @@ RUN git clone --recurse-submodules https://github.com/WebAssembly/wabt.git && \
cd ../ && \
rm -rf wabt*
RUN mkdir /wasi-runtimes
RUN mkdir -p /wasi-runtimes
ENV WASMTIME_HOME=/wasi-runtimes/wasmtime
RUN mkdir ${WASMTIME_HOME} && curl https://wasmtime.dev/install.sh -sSf | bash
RUN mkdir -p ${WASMTIME_HOME} && curl https://wasmtime.dev/install.sh -sSf | bash
COPY imagefiles/wasmtime-pwd.sh ${WASMTIME_HOME}/bin/
COPY imagefiles/wasmtime-pwd-threads.sh ${WASMTIME_HOME}/bin/
ENV PATH="$WASMTIME_HOME/bin:$PATH"

View File

@ -0,0 +1,37 @@
@headers@
#undef KEY
#if defined(__i386)
# define KEY '_','_','i','3','8','6'
#elif defined(__x86_64)
# define KEY '_','_','x','8','6','_','6','4'
#elif defined(__ppc__)
# define KEY '_','_','p','p','c','_','_'
#elif defined(__ppc64__)
# define KEY '_','_','p','p','c','6','4','_','_'
#endif
#define SIZE (sizeof(@type@))
char info_size[] = {'I', 'N', 'F', 'O', ':', 's','i','z','e','[',
('0' + ((SIZE / 10000)%10)),
('0' + ((SIZE / 1000)%10)),
('0' + ((SIZE / 100)%10)),
('0' + ((SIZE / 10)%10)),
('0' + (SIZE % 10)),
']',
#ifdef KEY
' ','k','e','y','[', KEY, ']',
#endif
'\0'};
#ifdef __CLASSIC_C__
int main(argc, argv) int argc; char *argv[];
#else
int main(int argc, char *argv[])
#endif
{
int require = 0;
require += info_size[argc];
(void)argv;
return SIZE;
}

View File

@ -0,0 +1,231 @@
#.rst:
# CheckTypeSize
# -------------
#
# Check sizeof a type
#
# ::
#
# CHECK_TYPE_SIZE(TYPE VARIABLE [BUILTIN_TYPES_ONLY]
# [LANGUAGE <language>])
#
# Check if the type exists and determine its size. On return,
# "HAVE_${VARIABLE}" holds the existence of the type, and "${VARIABLE}"
# holds one of the following:
#
# ::
#
# <size> = type has non-zero size <size>
# "0" = type has arch-dependent size (see below)
# "" = type does not exist
#
# Both ``HAVE_${VARIABLE}`` and ``${VARIABLE}`` will be created as internal
# cache variables.
#
# Furthermore, the variable "${VARIABLE}_CODE" holds C preprocessor code
# to define the macro "${VARIABLE}" to the size of the type, or leave
# the macro undefined if the type does not exist.
#
# The variable "${VARIABLE}" may be "0" when CMAKE_OSX_ARCHITECTURES has
# multiple architectures for building OS X universal binaries. This
# indicates that the type size varies across architectures. In this
# case "${VARIABLE}_CODE" contains C preprocessor tests mapping from
# each architecture macro to the corresponding type size. The list of
# architecture macros is stored in "${VARIABLE}_KEYS", and the value for
# each key is stored in "${VARIABLE}-${KEY}".
#
# If the BUILTIN_TYPES_ONLY option is not given, the macro checks for
# headers <sys/types.h>, <stdint.h>, and <stddef.h>, and saves results
# in HAVE_SYS_TYPES_H, HAVE_STDINT_H, and HAVE_STDDEF_H. The type size
# check automatically includes the available headers, thus supporting
# checks of types defined in the headers.
#
# If LANGUAGE is set, the specified compiler will be used to perform the
# check. Acceptable values are C and CXX
#
# Despite the name of the macro you may use it to check the size of more
# complex expressions, too. To check e.g. for the size of a struct
# member you can do something like this:
#
# ::
#
# check_type_size("((struct something*)0)->member" SIZEOF_MEMBER)
#
#
#
# The following variables may be set before calling this macro to modify
# the way the check is run:
#
# ::
#
# CMAKE_REQUIRED_FLAGS = string of compile command line flags
# CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
# CMAKE_REQUIRED_INCLUDES = list of include directories
# CMAKE_REQUIRED_LIBRARIES = list of libraries to link
# CMAKE_REQUIRED_QUIET = execute quietly without messages
# CMAKE_EXTRA_INCLUDE_FILES = list of extra headers to include
#=============================================================================
# Copyright 2002-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
include(CheckIncludeFile)
include(CheckIncludeFileCXX)
cmake_policy(PUSH)
cmake_policy(VERSION 3.0)
get_filename_component(__check_type_size_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
#-----------------------------------------------------------------------------
# Helper function. DO NOT CALL DIRECTLY.
function(__check_type_size_impl type var map builtin language)
if(NOT CMAKE_REQUIRED_QUIET)
message(STATUS "Check size of ${type}")
endif()
# Include header files.
set(headers)
if(builtin)
if(HAVE_SYS_TYPES_H)
set(headers "${headers}#include <sys/types.h>\n")
endif()
if(HAVE_STDINT_H)
set(headers "${headers}#include <stdint.h>\n")
endif()
if(HAVE_STDDEF_H)
set(headers "${headers}#include <stddef.h>\n")
endif()
endif()
foreach(h ${CMAKE_EXTRA_INCLUDE_FILES})
set(headers "${headers}#include \"${h}\"\n")
endforeach()
# Perform the check.
if("${language}" STREQUAL "C")
set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.c)
elseif("${language}" STREQUAL "CXX")
set(src ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.cpp)
else()
message(FATAL_ERROR "Unknown language:\n ${language}\nSupported languages: C, CXX.\n")
endif()
set(bin ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${var}.bin)
configure_file(${__check_type_size_dir}/CheckTypeSize.c.in ${src} @ONLY)
try_run(${var}_run_result HAVE_${var} ${CMAKE_BINARY_DIR} ${src}
COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}
CMAKE_FLAGS
"-DCOMPILE_DEFINITIONS:STRING=${CMAKE_REQUIRED_FLAGS}"
"-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}"
RUN_OUTPUT_VARIABLE ${var}_run_output
COMPILE_OUTPUT_VARIABLE output
)
if(${HAVE_${var}} AND NOT "${${var}_run_result}" STREQUAL "FAILED_TO_RUN")
set(${var} ${${var}_run_result})
if(NOT CMAKE_REQUIRED_QUIET)
message(STATUS "Check size of ${type} - done")
endif()
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
"Determining size of ${type} passed with the following output:\n${output}\n\n")
set(${var} "${${var}}" CACHE INTERNAL "CHECK_TYPE_SIZE: sizeof(${type})")
else()
# The check failed to compile.
if(NOT CMAKE_REQUIRED_QUIET)
message(STATUS "Check size of ${type} - failed")
endif()
file(READ ${src} content)
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Determining size of ${type} failed with the following output:\n${output}\n${src}:\n${content}\n\n")
set(${var} "" CACHE INTERNAL "CHECK_TYPE_SIZE: ${type} unknown")
file(REMOVE ${map})
endif()
endfunction()
#-----------------------------------------------------------------------------
macro(CHECK_TYPE_SIZE TYPE VARIABLE)
# parse arguments
unset(doing)
foreach(arg ${ARGN})
if("x${arg}" STREQUAL "xBUILTIN_TYPES_ONLY")
set(_CHECK_TYPE_SIZE_${arg} 1)
unset(doing)
elseif("x${arg}" STREQUAL "xLANGUAGE") # change to MATCHES for more keys
set(doing "${arg}")
set(_CHECK_TYPE_SIZE_${doing} "")
elseif("x${doing}" STREQUAL "xLANGUAGE")
set(_CHECK_TYPE_SIZE_${doing} "${arg}")
unset(doing)
else()
message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
endif()
endforeach()
if("x${doing}" MATCHES "^x(LANGUAGE)$")
message(FATAL_ERROR "Missing argument:\n ${doing} arguments requires a value\n")
endif()
if(DEFINED _CHECK_TYPE_SIZE_LANGUAGE)
if(NOT "x${_CHECK_TYPE_SIZE_LANGUAGE}" MATCHES "^x(C|CXX)$")
message(FATAL_ERROR "Unknown language:\n ${_CHECK_TYPE_SIZE_LANGUAGE}.\nSupported languages: C, CXX.\n")
endif()
set(_language ${_CHECK_TYPE_SIZE_LANGUAGE})
else()
set(_language C)
endif()
# Optionally check for standard headers.
if(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
set(_builtin 0)
else()
set(_builtin 1)
if("${_language}" STREQUAL "C")
check_include_file(sys/types.h HAVE_SYS_TYPES_H)
check_include_file(stdint.h HAVE_STDINT_H)
check_include_file(stddef.h HAVE_STDDEF_H)
elseif("${_language}" STREQUAL "CXX")
check_include_file_cxx(sys/types.h HAVE_SYS_TYPES_H)
check_include_file_cxx(stdint.h HAVE_STDINT_H)
check_include_file_cxx(stddef.h HAVE_STDDEF_H)
endif()
endif()
unset(_CHECK_TYPE_SIZE_BUILTIN_TYPES_ONLY)
unset(_CHECK_TYPE_SIZE_LANGUAGE)
# Compute or load the size or size map.
set(${VARIABLE}_KEYS)
set(_map_file ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/CheckTypeSize/${VARIABLE}.cmake)
if(NOT DEFINED HAVE_${VARIABLE})
__check_type_size_impl(${TYPE} ${VARIABLE} ${_map_file} ${_builtin} ${_language})
endif()
include(${_map_file} OPTIONAL)
set(_map_file)
set(_builtin)
# Create preprocessor code.
if(${VARIABLE}_KEYS)
set(${VARIABLE}_CODE)
set(_if if)
foreach(key ${${VARIABLE}_KEYS})
set(${VARIABLE}_CODE "${${VARIABLE}_CODE}#${_if} defined(${key})\n# define ${VARIABLE} ${${VARIABLE}-${key}}\n")
set(_if elif)
endforeach()
set(${VARIABLE}_CODE "${${VARIABLE}_CODE}#else\n# error ${VARIABLE} unknown\n#endif")
set(_if)
elseif(${VARIABLE})
set(${VARIABLE}_CODE "#define ${VARIABLE} ${${VARIABLE}}")
else()
set(${VARIABLE}_CODE "/* #undef ${VARIABLE} */")
endif()
endmacro()
#-----------------------------------------------------------------------------
cmake_policy(POP)

View File

@ -0,0 +1,42 @@
ARG ORG=dockcross
ARG HOST_ARCH=amd64
FROM ${ORG}/base:latest-${HOST_ARCH}
LABEL maintainer="Matt McCormick matt@mmmccormick.com"
ENV LLVM_VERSION=19
ENV WASI_VERSION=25
ENV WASI_VERSION_FULL=${WASI_VERSION}.0
ENV WASMTIME_HOME=/wasi-runtimes/wasmtime
RUN mkdir -p ${WASMTIME_HOME} && curl https://wasmtime.dev/install.sh -sSf | bash
ENV PATH="$WASMTIME_HOME/bin:$PATH"
COPY download-build-install-wasi-sdk.sh /usr/local/bin/
RUN /usr/local/bin/download-build-install-wasi-sdk.sh
ENV WASI_SYSROOT=/opt/wasi-sdk/share/wasi-sysroot
ENV WASI_SDK_PATH=/opt/wasi-sdk
COPY clang-wasi-sysroot.sh clang++-wasi-sysroot.sh /usr/local/bin/
ENV CROSS_TRIPLE=wasm32-wasi
ENV CROSS_ROOT=${WASI_SDK_PATH}
ENV AR=${WASI_SDK_PATH}/bin/llvm-ar \
CC=clang-wasi-sysroot.sh \
CXX=clang++-wasi-sysroot.sh \
LD=${WASI_SDK_PATH}/bin/wasm-ld \
RANLIB=${WASI_SDK_PATH}/bin/llvm-ranlib
#include "common.webassembly"
COPY WASI.cmake /usr/src/
RUN mv /usr/src/WASI.cmake /usr/share/cmake-*/Modules/Platform/
COPY *.cmake /usr/src/
RUN mv /usr/src/*.cmake /usr/share/cmake-*/Modules/
COPY CheckTypeSize.c.in /usr/src/
RUN mv /usr/src/CheckTypeSize.c.in /usr/share/cmake-*/Modules/
COPY Toolchain.cmake ${CROSS_ROOT}/
ENV CMAKE_TOOLCHAIN_FILE=${CROSS_ROOT}/Toolchain.cmake
#include "common.label-and-env"

View File

@ -0,0 +1,25 @@
# Locate OpenAL
# This module defines
# OPENAL_LIBRARY
# OPENAL_FOUND, if false, do not try to link to OpenAL
# OPENAL_INCLUDE_DIR, where to find the headers
# The implementation is based on the standard FindOpenAL.cmake provided with CMake,
# but customized for targeting Emscripten only.
if (NOT OPENAL_FOUND)
SET(OPENAL_FOUND TRUE)
# For Emscripten-compiled apps in the test suite (test_alut), this is expected...
SET(OPENAL_INCLUDE_DIR "${EMSCRIPTEN_ROOT_PATH}/system/include")
# ... but the stock FindOpenAL.cmake would have returned this.
#SET(OPENAL_INCLUDE_DIR "${EMSCRIPTEN_ROOT_PATH}/system/include/AL")
# Returning "-lopenal" is now considered mandatory
SET(OPENAL_LIBRARY "-lopenal")
SET(OPENAL_LIB "-lopenal")
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${EMSCRIPTEN_ROOT_PATH}/system/include" "${EMSCRIPTEN_ROOT_PATH}/system/include/AL")
MARK_AS_ADVANCED(OPENAL_LIBRARY OPENAL_INCLUDE_DIR)
endif()

View File

@ -0,0 +1,34 @@
# Locate OpenGL
# This module defines:
# OPENGL_FOUND - system has OpenGL
# OPENGL_XMESA_FOUND - system has XMESA
# OPENGL_GLU_FOUND - system has GLU
# OPENGL_INCLUDE_DIR - the GL include directory
# OPENGL_LIBRARIES - Link these to use OpenGL and GLU
# OPENGL_gl_LIBRARY - Path to OpenGL Library
# OPENGL_glu_LIBRARY - Path to GLU Library
# The implementation is based on the standard FindOpenGL.cmake provided with CMake,
# but customized for targeting Emscripten only.
# These libraries are provided with Emscripten
SET(OPENGL_FOUND TRUE)
SET(OPENGL_GLU_FOUND TRUE)
# Doesn't look like this one is part of Emscripten
SET(OPENGL_XMESA_FOUND FALSE)
# This is the path where <GL/gl.h> is found
SET(OPENGL_INCLUDE_DIR "${EMSCRIPTEN_ROOT_PATH}/system/include")
# No library to link against for OpenGL, since Emscripten picks it up automatically from library_webgl.js,
# but need to report something, or CMake thinks we failed in the search.
SET(OPENGL_LIBRARIES "nul")
SET(OPENGL_gl_LIBRARY "nul")
SET(OPENGL_glu_LIBRARY "nul")
mark_as_advanced(
OPENGL_INCLUDE_DIR
OPENGL_glu_LIBRARY
OPENGL_gl_LIBRARY
)

View File

@ -0,0 +1,30 @@
#.rst:
# TestBigEndian
# -------------
#
# The TestBigEndian.cmake module that ships with CMake, which
# checks if the system is big endian or little endian, assumes
# that a binary is produced that will have bytes that correspond to the
# endianness on the target system. Since emscripten produces Javascript, we
# override the default behavior and always return little endian.
#
# ::
#
# TEST_BIG_ENDIAN(VARIABLE)
# VARIABLE - variable to store the result to
#=============================================================================
# Copyright 2002-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
function(TEST_BIG_ENDIAN VARIABLE)
set(${VARIABLE} 0 CACHE INTERNAL "Result of TEST_BIG_ENDIAN" FORCE)
endfunction()

View File

@ -0,0 +1,14 @@
set(WASI_SDK_PREFIX $ENV{WASI_SDK_PATH})
include($ENV{WASI_SDK_PATH}/share/cmake/wasi-sdk.cmake)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_WASI_EMULATED_PTHREAD")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_WASI_EMULATED_PTHREAD")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lwasi-emulated-pthread")
set(CMAKE_FIND_ROOT_PATH $ENV{CROSS_ROOT})
set(CMAKE_SYSROOT $ENV{WASI_SYSROOT})
set(CMAKE_C_COMPILER /usr/local/bin/clang-wasi-sysroot.sh)
set(CMAKE_CXX_COMPILER /usr/local/bin/clang++-wasi-sysroot.sh)
set(CMAKE_CROSSCOMPILING_EMULATOR /wasi-runtimes/wasmtime/bin/wasmtime-pwd.sh)

View File

@ -0,0 +1,2 @@
set(WASI 1)
set(UNIX 1)

View File

@ -0,0 +1,3 @@
#!/usr/bin/env sh
exec ${WASI_SDK_PATH}/bin/clang++ -D_WASI_EMULATED_PTHREAD --target=wasm32-wasi --sysroot=${WASI_SYSROOT} "$@"

View File

@ -0,0 +1,3 @@
#!/usr/bin/env sh
exec ${WASI_SDK_PATH}/bin/clang -D_WASI_EMULATED_PTHREAD --target=wasm32-wasi --sysroot=${WASI_SYSROOT} "$@"

View File

@ -0,0 +1,36 @@
#!/usr/bin/env bash
set -eox pipefail
mkdir /tmp/dl
cd /tmp/dl
wasi_sdk_dir=/opt/wasi-sdk
mkdir -p $wasi_sdk_dir
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
export PATH=$HOME/.cargo/bin:$PATH
git clone --recurse-submodules -b enable-libcxx-threads https://github.com/thewtex/wasi-sdk
cd wasi-sdk
git fetch origin --tags
git remote add upstream https://github.com/WebAssembly/wasi-sdk
git fetch upstream wasi-sdk-${WASI_VERSION}
./ci/build.sh
cd build/dist
tar xzf wasi-toolchain-*.tar.gz --strip-components=1 -C /opt/wasi-sdk
mkdir -p /opt/wasi-sdk/share/wasi-sysroot
tar xzf wasi-sysroot-*.tar.gz --strip-components=1 -C /opt/wasi-sdk/share/wasi-sysroot
for wasi_toolchain in wasi wasip1 wasip2; do
libclang_rt_out_dir=/opt/wasi-sdk/lib/clang/${LLVM_VERSION}/${wasi_toolchain}
mkdir -p $libclang_rt_out_dir
tar xzf ./libclang_rt.builtins-*.tar.gz --strip-components=1 -C $libclang_rt_out_dir
done
mkdir -p /opt/wasi-sdk/lib/clang/${LLVM_VERSION}/lib/wasm32-unknown-wasi
cp ${libclang_rt_out_dir}/libclang_rt.builtins-wasm32.a /opt/wasi-sdk/lib/clang/${LLVM_VERSION}/lib/wasm32-unknown-wasi/libclang_rt.builtins.a
cd /tmp/
rm -rf /tmp/dl
rm -rf $HOME/.cargo