Skip to content

Instantly share code, notes, and snippets.

@gaomeng1900
Last active November 17, 2022 16:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaomeng1900/c0d0b06162e1d37a5323413457e1ea81 to your computer and use it in GitHub Desktop.
Save gaomeng1900/c0d0b06162e1d37a5323413457e1ea81 to your computer and use it in GitHub Desktop.
使用LLVM编译wasm
# 编译使用LLVM的实验性WASM编译器
# 编译支持
# [Cloudflare Workers WebAssembly Demo](http://www.joyk.com/dig/detail/1538467210844002)
# [lldでwasmをリンクするまで](https://qiita.com/chikoski/items/41853dfb2afdec52e7d1)
# [Using LLVM from Rust to generate WebAssembly binaries](https://medium.com/@jayphelps/using-llvm-from-rust-to-generate-webassembly-93e8c193fdb4)
# [Notes on working with C and WebAssembly](https://aransentin.github.io/cwasm/)
# llvm编译文档
# https://clang.llvm.org/get_started.html
# https://lld.llvm.org/WebAssembly.html
# https://lld.llvm.org/getting_started.html
# 工具栈
# https://zhuanlan.zhihu.com/p/24632251
# emscripten 发展
# [Shrinking WebAssembly and JavaScript code sizes in Emscripten](https://hacks.mozilla.org/2018/01/shrinking-webassembly-and-javascript-code-sizes-in-emscripten/)
# [WebAssembly Standalone](https://github.com/kripken/emscripten/wiki/WebAssembly-Standalone)
# 标准库
# [browsix](https://github.com/plasma-umass/browsix)
# [[WebAssembly] Call to action. Syscalls needed!](https://www.reddit.com/r/haskell/comments/7fu2vr/webassembly_call_to_action_syscalls_needed/)
# **一个似乎可行的方案** [If you build it](https://webghc.github.io/2017/07/25/buildinglibcedited.html)
# **另一个似乎可行的方案** https://github.com/jfbastien/musl
# 现状:
# llvm有实验性支持,可以交叉编译wasm
# 支持不完整,主要问题是连接器和标准库
# 需要从源码编译LLVM并开启实验性特性
# 标准库目前可以使用第三方方案
# 建个项目目录
mkdir llvm-wasm
cd llvm-wasm
# src dir
# llvm
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
# clang
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
cd ../..
# extra 必选
cd llvm/tools/clang/tools
svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
cd ../../../..
# RT 必选
cd llvm/projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
cd ../..
# libcxx ?必选?
cd llvm/projects
# 似乎会搞挂官网然后网络失败,反正这部分运行不起来......
svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
cd ../..
# lld 必选,编译wasm-ld
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/lld/trunk lld
cd ../..
# build dir
mkdir build
cd build
# cmake
cmake -G "Unix Makefiles" -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly ../llvm
# make 线程数写逻辑核数(如:4核8线程 -j8)
make -j8
# 使用
# export PATH=当前目录/llvm/build/bin:$PATH
clang -c -O3 --target=wasm32 test.cpp
wasm-ld test.o -o test.wasm --no-entry --strip-all --allow-undefined --export-all
# 注意使用O3优化之后暴露的函数名会加上前缀后缀来表示输入输出的数据类型
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment