Skip to content

Allow building when targeting WASI#1509

Closed
anuraaga wants to merge 2 commits intoabseil:masterfrom
anuraaga:wasi-sdk-build
Closed

Allow building when targeting WASI#1509
anuraaga wants to merge 2 commits intoabseil:masterfrom
anuraaga:wasi-sdk-build

Conversation

@anuraaga
Copy link
Contributor

@anuraaga anuraaga commented Aug 9, 2023

WASI is similar to emscripten, providing a syscall layer to WebAssembly focused on server side applications. There are some config knobs that are in place to allow building the repo for emscripten which also need to support wasi (as built with LLVM). Aside from that, there are still some features it supports less than escripten, for example related to signals, causing the build to be even more limited for it.

// NaCL __native_client__
// AsmJS __asmjs__
// WebAssembly __wasm__
// WebAssembly (Emscripten) __EMSCRIPTEN__
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebAssembly is an architecture, such as x86, not an operating system, which is a syscall layer so in its case, either Emscripten or WASI currently. I have changed this to clarify, as Emscripten is a WebAssembly "OS" which supports mmap while WASI does not.

I can add wasi to this list if it makes sense, but I suspect that would be a bigger decision on whether the project officially supports the OS now or not. But it would be great if we can at least add smaller fixes like this PR on a best-effort basis to allow compilation to pass to encourage the use of abseil within the server-side WebAssembly community

#include "absl/debugging/stacktrace.h"

#ifndef _WIN32
#if !defined(_WIN32) && !defined(__wasi__)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that Emscripten does support this codepath while wasi doesn't so I added a wasi-specific option here

@anuraaga
Copy link
Contributor Author

anuraaga commented Aug 9, 2023

To build, I did

docker run -it --rm -v `pwd`:/workspace --workdir /workspace ghcr.io/webassembly/wasi-sdk:wasi-sdk-20 bash
# Install nano and modify /usr/share/cmake/wasi-sdk.cmake to add threads to triplet and cflags, following https://github.com/WebAssembly/wasi-sdk/blob/main/wasi-sdk-pthread.cmake because the docker image does not currently include it
mkdir build
cmake -DCMAKE_CXX_FLAGS="-D_WASI_EMULATED_SIGNAL" -DCMAKE_CXX_STANDARD=14 ..
cmake --build . --target all

-D_WASI_EMULATED_SIGNAL is needed because even with the changes to some of the signal-related configs in this PR, the header itself is included either way and this allows the include to work with a stubbed file

@anuraaga
Copy link
Contributor Author

anuraaga commented Aug 9, 2023

/cc @sbc100 @abrown in case any questions about wasi-sdk come up I'm not familiar with

Also, just in case it helps to have a use case in mind, I compile RE2 to WASI for loading in Go apps.

https://github.com/wasilibs/go-re2

But RE2 has migrated to using absl and so newer versions of RE2 can't be compiled for that use anymore. It would be great if that can continue to work if it's possible. /cc @junyer FYI

@anuraaga
Copy link
Contributor Author

anuraaga commented Aug 9, 2023

In ec9fa08 I went ahead and added a wasi guard for the signal header file in thread_identity to at least allow that file to compile without the stubbed header file enabled via -D_WASI_EMULATED_SIGNAL. In other files, it is not as simple as just guarding, but I think this file, through its usage in logging, has a large impact on downstream so could be worth it, but I can revert if it's adding too many branches

@derekmauro
Copy link
Member

I'm testing this now. WASI won't receive official support, but this PR is simple enough that if it works, we can take it.

netkex pushed a commit to netkex/abseil-cpp that referenced this pull request Apr 3, 2024
Imported from GitHub PR abseil#1509

WASI is similar to emscripten, providing a syscall layer to WebAssembly focused on server side applications. There are some config knobs that are in place to allow building the repo for emscripten which also need to support wasi (as built with LLVM). Aside from that, there are still some features it supports less than escripten, for example related to signals, causing the build to be even more limited for it.
Merge ec9fa08 into 861e53c

Merging this change closes abseil#1509

COPYBARA_INTEGRATE_REVIEW=abseil#1509 from anuraaga:wasi-sdk-build ec9fa08
PiperOrigin-RevId: 557166498
Change-Id: Ic51149d8b092fd888c9a5c383275257fc8ff4232
liujinye-sys pushed a commit to open-vela/external_abseil-cpp that referenced this pull request Jul 23, 2025
Imported from GitHub PR abseil/abseil-cpp#1509

WASI is similar to emscripten, providing a syscall layer to WebAssembly focused on server side applications. There are some config knobs that are in place to allow building the repo for emscripten which also need to support wasi (as built with LLVM). Aside from that, there are still some features it supports less than escripten, for example related to signals, causing the build to be even more limited for it.
Merge ec9fa081609687035005dfdafd312754f31c3fbb into 861e53c

Merging this change closes #1509

COPYBARA_INTEGRATE_REVIEW=abseil/abseil-cpp#1509 from anuraaga:wasi-sdk-build ec9fa081609687035005dfdafd312754f31c3fbb
PiperOrigin-RevId: 557166498
Change-Id: Ic51149d8b092fd888c9a5c383275257fc8ff4232
copybara-service bot pushed a commit that referenced this pull request Aug 18, 2025
Imported from GitHub PR #1922

#1509 completely removed the mmap support when targeting WASI. wasi-libc supports an optional emulated mmap implementation if [`_WASI_EMULATED_MMAN`](https://github.com/WebAssembly/wasi-libc/blob/main/libc-top-half/musl/include/sys/mman.h) is set. Emulated mmap calls in wasi-libc come with a few restrictions, but users are aware of them as the feature is opt-in.

This PR enables mmap support for WASI if `_WASI_EMULATED_MMAN` is defined.
Merge e2519a6 into c5e0072

Merging this change closes #1922

COPYBARA_INTEGRATE_REVIEW=#1922 from zebreus:master e2519a6
PiperOrigin-RevId: 796418883
Change-Id: I5b7db767fa79269e191d7d7abcffd679d71d3ecc
liujinye-sys pushed a commit to open-vela/external_abseil-cpp that referenced this pull request Dec 16, 2025
Imported from GitHub PR abseil/abseil-cpp#1509

WASI is similar to emscripten, providing a syscall layer to WebAssembly focused on server side applications. There are some config knobs that are in place to allow building the repo for emscripten which also need to support wasi (as built with LLVM). Aside from that, there are still some features it supports less than escripten, for example related to signals, causing the build to be even more limited for it.
Merge ec9fa081609687035005dfdafd312754f31c3fbb into 861e53c

Merging this change closes #1509

COPYBARA_INTEGRATE_REVIEW=abseil/abseil-cpp#1509 from anuraaga:wasi-sdk-build ec9fa081609687035005dfdafd312754f31c3fbb
PiperOrigin-RevId: 557166498
Change-Id: Ic51149d8b092fd888c9a5c383275257fc8ff4232
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants