Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ else()
message(FATAL_ERROR "Unknown WASI version: ${WASI}")
endif()

set(wasip2-version 0.2.0)
set(wasip3-version 0.3.0-rc-2026-01-06)

include(bindings)
include(builtins)
if (NOT (WASI STREQUAL "p1"))
Expand All @@ -151,6 +154,7 @@ endif()
include(check-symbols)
include(clang-format)
include(wasm-tools)
include(wasi-wits)

# =============================================================================
# Generic top-level build flags/settings
Expand Down
66 changes: 2 additions & 64 deletions cmake/bindings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ if (NOT BINDINGS_TARGET)
return()
endif()

include(ExternalProject)

# If `wit-bindgen` is on the system and has the right version, favor that,
# otherwise download a known good version.
find_program(WIT_BINDGEN_EXECUTABLE NAMES wit-bindgen)
Expand Down Expand Up @@ -37,54 +35,9 @@ else()
set(wit_bindgen ${WIT_BINDGEN_EXECUTABLE})
endif()

# If `wkg` is on the system and has the right version, favor that,
# otherwise download a known good version.
find_program(WKG_EXECUTABLE NAMES wkg)
if(WKG_EXECUTABLE)
message(STATUS "Found wkg: ${WKG_EXECUTABLE}")

execute_process(
COMMAND ${WKG_EXECUTABLE} --version
OUTPUT_VARIABLE WKG_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)

if (NOT (WKG_VERSION MATCHES "0\\.13\\.0"))
message(WARNING "wkg version 0.13.0 is required, found: ${WKG_VERSION}")
set(WKG_EXECUTABLE "")
endif()
endif()

if (NOT WKG_EXECUTABLE)
include(ba-download)
ba_download(
wkg
"https://github.com/bytecodealliance/wasm-pkg-tools"
"0.13.0"
)
ExternalProject_Get_Property(wkg DOWNLOADED_FILE)
set(wkg_bin ${DOWNLOADED_FILE})
else()
add_custom_target(wkg)
set(wkg_bin ${WKG_EXECUTABLE})
endif()

set(bottom_half "${CMAKE_SOURCE_DIR}/libc-bottom-half")

set(wasip2_wit_dir ${CMAKE_CURRENT_BINARY_DIR}/wasi/p2/wit)
set(wasip2-version 0.2.0)
file(MAKE_DIRECTORY ${wasip2_wit_dir})
configure_file(${CMAKE_SOURCE_DIR}/wasi/wasi-libc-wasip2.wit.in ${wasip2_wit_dir}/wasi-libc-wasip2.wit)
set(wasip2_stamp ${CMAKE_CURRENT_BINARY_DIR}/wasi/p2/.wit-fetch-stamp)
add_custom_command(
OUTPUT ${wasip2_stamp}
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/wasi/p2
COMMAND ${wkg_bin} wit fetch
COMMAND ${CMAKE_COMMAND} -E touch ${wasip2_stamp}
WORKING_DIRECTORY ${wasip2_wit_dir}/..
DEPENDS wkg ${wasip2_wit_dir}/wasi-libc-wasip2.wit
COMMENT "Fetching WASI P2 dependencies"
)
add_custom_target(wasip2-wits DEPENDS ${wasip2_stamp})
add_custom_target(
bindings-p2
COMMAND
Expand Down Expand Up @@ -120,28 +73,13 @@ add_custom_target(
--rename wasi:cli/terminal-stdin@${wasip2-version}=terminal_stdin
--rename wasi:cli/terminal-stdout@${wasip2-version}=terminal_stdout
--rename wasi:cli/terminal-stderr@${wasip2-version}=terminal_stderr
${wasip2_wit_dir}
${CMAKE_SOURCE_DIR}/wasi/p2/wit
COMMAND cmake -E copy wasip2.h ${bottom_half}/headers/public/wasi/__generated_wasip2.h
COMMAND cmake -E copy wasip2_component_type.o ${bottom_half}/sources
COMMAND cmake -E copy wasip2.c ${bottom_half}/sources
DEPENDS wit-bindgen wkg wasip2-wits
)

set(wasip3_wit_dir ${CMAKE_CURRENT_BINARY_DIR}/wasi/p3/wit)
set(wasip3-version 0.3.0-rc-2026-01-06)
file(MAKE_DIRECTORY ${wasip3_wit_dir})
configure_file(${CMAKE_SOURCE_DIR}/wasi/wasi-libc-wasip3.wit.in ${wasip3_wit_dir}/wasi-libc-wasip3.wit)
set(wasip3_stamp ${CMAKE_CURRENT_BINARY_DIR}/wasi/p3/.wit-fetch-stamp)
add_custom_command(
OUTPUT ${wasip3_stamp}
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/wasi/p3
COMMAND ${wkg_bin} wit fetch
COMMAND ${CMAKE_COMMAND} -E touch ${wasip3_stamp}
WORKING_DIRECTORY ${wasip3_wit_dir}/..
DEPENDS wkg ${wasip3_wit_dir}/wasi-libc-wasip3.wit
COMMENT "Fetching WASI P3 dependencies"
)
add_custom_target(wasip3-wits DEPENDS ${wasip3_stamp})
add_custom_target(
bindings-p3
COMMAND
Expand Down Expand Up @@ -169,7 +107,7 @@ add_custom_target(
--rename wasi:cli/terminal-stdin@${wasip3-version}=terminal_stdin
--rename wasi:cli/terminal-stdout@${wasip3-version}=terminal_stdout
--rename wasi:cli/terminal-stderr@${wasip3-version}=terminal_stderr
${wasip3_wit_dir}
${CMAKE_SOURCE_DIR}/wasi/p3/wit
COMMAND cmake -E copy wasip3.h ${bottom_half}/headers/public/wasi/__generated_wasip3.h
COMMAND cmake -E copy wasip3_component_type.o ${bottom_half}/sources
COMMAND cmake -E copy wasip3.c ${bottom_half}/sources
Expand Down
49 changes: 49 additions & 0 deletions cmake/wasi-wits.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Helper rules to download and vendor WITs used for WASI in this repository for
# bindings generation and future use.

# If `wkg` is on the system and has the right version, favor that,
# otherwise download a known good version.
find_program(WKG_EXECUTABLE NAMES wkg)
if(WKG_EXECUTABLE)
message(STATUS "Found wkg: ${WKG_EXECUTABLE}")

execute_process(
COMMAND ${WKG_EXECUTABLE} --version
OUTPUT_VARIABLE WKG_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)

if (NOT (WKG_VERSION MATCHES "0\\.13\\.0"))
message(WARNING "wkg version 0.13.0 is required, found: ${WKG_VERSION}")
set(WKG_EXECUTABLE "")
endif()
endif()

if (NOT WKG_EXECUTABLE)
include(ba-download)
ba_download(
wkg
"https://github.com/bytecodealliance/wasm-pkg-tools"
"0.13.0"
)
ExternalProject_Get_Property(wkg DOWNLOADED_FILE)
set(wkg_bin ${DOWNLOADED_FILE})
else()
add_custom_target(wkg)
set(wkg_bin ${WKG_EXECUTABLE})
endif()

foreach(version p2 p3)
set(wit_dir ${CMAKE_SOURCE_DIR}/wasi/${version}/wit)
set(stamp ${CMAKE_BINARY_DIR}/wasi${version}-wit.stamp)

configure_file(${wit_dir}/wasi-libc.wit.in ${wit_dir}/wasi-libc.wit)
add_custom_command(
OUTPUT ${stamp}
COMMAND ${wkg_bin} wit fetch
COMMAND ${CMAKE_COMMAND} -E touch ${stamp}
WORKING_DIRECTORY ${wit_dir}/..
DEPENDS wkg ${wit_dir}/wasi-libc.wit
COMMENT "Fetching WASI${version} dependencies"
)
add_custom_target(wasi${version}-wits DEPENDS ${stamp})
endforeach()
31 changes: 13 additions & 18 deletions libc-bottom-half/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,16 @@ if (WASI STREQUAL "p1")
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_OBJECTS:crt1-command> ${crt_sysroot}/crt1-command.o
DEPENDS crt1-command $<TARGET_OBJECTS:crt1-command>
)
elseif (WASI STREQUAL "p2")
# wasip2: crt1-command.o is modified from what CMake produces to
else()
if (WASI STREQUAL "p2")
set(world wasi:cli/command@${wasip2-version})
elseif (WASI STREQUAL "p3")
set(world wasi:cli/command@${wasip3-version})
else()
message(FATAL_ERROR "Unknown WASI version: ${WASI}")
endif()

# wasip2+: crt1-command.o is modified from what CMake produces to
# additionally have a custom section representing the type information needed
# for its contained export. This is the `wasi:cli/run` interface, for example.
#
Expand All @@ -238,25 +246,12 @@ elseif (WASI STREQUAL "p2")
OUTPUT ${crt_sysroot}/crt1-command.o
COMMAND
${wasm_tools} component embed
${wasip2_wit_dir}
${CMAKE_SOURCE_DIR}/wasi/${WASI}/wit
$<TARGET_OBJECTS:crt1-command>
--world wasi:cli/command@0.2.0
--world ${world}
-o ${crt_sysroot}/crt1-command.o
DEPENDS crt1-command wasip2-wits $<TARGET_OBJECTS:crt1-command> wasm-tools
DEPENDS crt1-command $<TARGET_OBJECTS:crt1-command> wasm-tools
)
elseif (WASI STREQUAL "p3")
add_custom_command(
OUTPUT ${crt_sysroot}/crt1-command.o
COMMAND
${wasm_tools} component embed
${wasip3_wit_dir}
$<TARGET_OBJECTS:crt1-command>
--world wasi:cli/command@0.3.0-rc-2026-01-06
-o ${crt_sysroot}/crt1-command.o
DEPENDS crt1-command wasip3-wits $<TARGET_OBJECTS:crt1-command> wasm-tools
)
else()
message(FATAL_ERROR "Unknown WASI version: ${WASI}")
endif()

# Provide a plain crt1.o for toolchain compatibility, identical to
Expand Down
159 changes: 159 additions & 0 deletions wasi/p2/wit/deps/wasi-cli-0.2.0/package.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package wasi:cli@0.2.0;

interface environment {
/// Get the POSIX-style environment variables.
///
/// Each environment variable is provided as a pair of string variable names
/// and string value.
///
/// Morally, these are a value import, but until value imports are available
/// in the component model, this import function should return the same
/// values each time it is called.
get-environment: func() -> list<tuple<string, string>>;

/// Get the POSIX-style arguments to the program.
get-arguments: func() -> list<string>;

/// Return a path that programs should use as their initial current working
/// directory, interpreting `.` as shorthand for this.
initial-cwd: func() -> option<string>;
}

interface exit {
/// Exit the current instance and any linked instances.
exit: func(status: result);
}

interface run {
/// Run the program.
run: func() -> result;
}

interface stdin {
use wasi:io/streams@0.2.0.{input-stream};

get-stdin: func() -> input-stream;
}

interface stdout {
use wasi:io/streams@0.2.0.{output-stream};

get-stdout: func() -> output-stream;
}

interface stderr {
use wasi:io/streams@0.2.0.{output-stream};

get-stderr: func() -> output-stream;
}

/// Terminal input.
///
/// In the future, this may include functions for disabling echoing,
/// disabling input buffering so that keyboard events are sent through
/// immediately, querying supported features, and so on.
interface terminal-input {
/// The input side of a terminal.
resource terminal-input;
}

/// Terminal output.
///
/// In the future, this may include functions for querying the terminal
/// size, being notified of terminal size changes, querying supported
/// features, and so on.
interface terminal-output {
/// The output side of a terminal.
resource terminal-output;
}

/// An interface providing an optional `terminal-input` for stdin as a
/// link-time authority.
interface terminal-stdin {
use terminal-input.{terminal-input};

/// If stdin is connected to a terminal, return a `terminal-input` handle
/// allowing further interaction with it.
get-terminal-stdin: func() -> option<terminal-input>;
}

/// An interface providing an optional `terminal-output` for stdout as a
/// link-time authority.
interface terminal-stdout {
use terminal-output.{terminal-output};

/// If stdout is connected to a terminal, return a `terminal-output` handle
/// allowing further interaction with it.
get-terminal-stdout: func() -> option<terminal-output>;
}

/// An interface providing an optional `terminal-output` for stderr as a
/// link-time authority.
interface terminal-stderr {
use terminal-output.{terminal-output};

/// If stderr is connected to a terminal, return a `terminal-output` handle
/// allowing further interaction with it.
get-terminal-stderr: func() -> option<terminal-output>;
}

world imports {
import environment;
import exit;
import wasi:io/error@0.2.0;
import wasi:io/poll@0.2.0;
import wasi:io/streams@0.2.0;
import stdin;
import stdout;
import stderr;
import terminal-input;
import terminal-output;
import terminal-stdin;
import terminal-stdout;
import terminal-stderr;
import wasi:clocks/monotonic-clock@0.2.0;
import wasi:clocks/wall-clock@0.2.0;
import wasi:filesystem/types@0.2.0;
import wasi:filesystem/preopens@0.2.0;
import wasi:sockets/network@0.2.0;
import wasi:sockets/instance-network@0.2.0;
import wasi:sockets/udp@0.2.0;
import wasi:sockets/udp-create-socket@0.2.0;
import wasi:sockets/tcp@0.2.0;
import wasi:sockets/tcp-create-socket@0.2.0;
import wasi:sockets/ip-name-lookup@0.2.0;
import wasi:random/random@0.2.0;
import wasi:random/insecure@0.2.0;
import wasi:random/insecure-seed@0.2.0;
}
world command {
import environment;
import exit;
import wasi:io/error@0.2.0;
import wasi:io/poll@0.2.0;
import wasi:io/streams@0.2.0;
import stdin;
import stdout;
import stderr;
import terminal-input;
import terminal-output;
import terminal-stdin;
import terminal-stdout;
import terminal-stderr;
import wasi:clocks/monotonic-clock@0.2.0;
import wasi:clocks/wall-clock@0.2.0;
import wasi:filesystem/types@0.2.0;
import wasi:filesystem/preopens@0.2.0;
import wasi:sockets/network@0.2.0;
import wasi:sockets/instance-network@0.2.0;
import wasi:sockets/udp@0.2.0;
import wasi:sockets/udp-create-socket@0.2.0;
import wasi:sockets/tcp@0.2.0;
import wasi:sockets/tcp-create-socket@0.2.0;
import wasi:sockets/ip-name-lookup@0.2.0;
import wasi:random/random@0.2.0;
import wasi:random/insecure@0.2.0;
import wasi:random/insecure-seed@0.2.0;

export run;
}
Loading