Difference between revisions of "WebAssembly/Compiler"
(removed the outdated section) |
|||
Line 1: | Line 1: | ||
− | + | = Instructions = | |
− | + | == Prerequisites == | |
The ''wasm-ld'' linker. This the linker from the LLVM project. The external assembler ''llvm-mc'' is optional, since commit bc76487b878e59133ce3c2cc19ebb13f9a9826ea, in which, the internal assembler and object writer was enabled. | The ''wasm-ld'' linker. This the linker from the LLVM project. The external assembler ''llvm-mc'' is optional, since commit bc76487b878e59133ce3c2cc19ebb13f9a9826ea, in which, the internal assembler and object writer was enabled. | ||
Line 9: | Line 9: | ||
LLVM 12.0.1 also works. | LLVM 12.0.1 also works. | ||
− | === Obtaining the compiler sources | + | === Ubuntu 20.04 === |
+ | |||
+ | The wasm-ld is part of package ''lld-12''. fpc searches for the tool ''wasm32-wasi-wasm-ld'' or ''wasm-ld'' depending on the -XP flag you use (see /etc/fpc.cfg): | ||
+ | apt install lld-12 | ||
+ | ln -sf /usr/lib/llvm-12/bin/wasm-ld ~/bin/wasm32-wasi-wasm-ld | ||
+ | |||
+ | Note: This assumes you have a $HOME/bin directory and it is in PATH. | ||
+ | |||
+ | If you need the external assembler, install the package ''llvm-12'' and set a symlink: | ||
+ | apt install llvm-12 | ||
+ | ln -sf /usr/lib/llvm-12/bin/llvm-mc ~/bin/wasm32-wasi-llvm-mc | ||
+ | |||
+ | |||
+ | == Obtaining the compiler sources == | ||
The WebAssembly target was merged into FPC SVN trunk in r48955 on 2021-03-14. Since then, Free Pascal has moved to GitLab, so now it's now included in the git 'main' branch. | The WebAssembly target was merged into FPC SVN trunk in r48955 on 2021-03-14. Since then, Free Pascal has moved to GitLab, so now it's now included in the git 'main' branch. | ||
Get the sources: | Get the sources: | ||
− | + | git clone https://gitlab.com/freepascal.org/fpc/source.git fpc | |
− | git clone https://gitlab.com/freepascal.org/fpc/source.git fpc | ||
enter the directory: | enter the directory: | ||
− | cd fpc | + | cd fpc |
− | + | == Supported targets == | |
− | + | === WASI - The WebAssembly System Interface === | |
− | [https://wasi.dev/ WASI] is a modular system interface for WebAssembly. It allows creating portable and secure programs that can run in a sandboxed command-line environment. See the WASI website for more information. | + | [https://wasi.dev/ WASI] is a modular system interface for WebAssembly. It allows creating portable and secure programs that can run in a sandboxed command-line environment or in the browser. See the WASI website for more information. |
To build the compiler for the WASI target: | To build the compiler for the WASI target: | ||
− | |||
make all OS_TARGET=wasi CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc | make all OS_TARGET=wasi CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc | ||
Line 55: | Line 66: | ||
and drag and dropping the .wasm binary directly into the browser window. | and drag and dropping the .wasm binary directly into the browser window. | ||
− | + | === Embedded target === | |
− | The [[TARGET_Embedded|Embedded target]] is primarily used for embedded systems, without an operating system. However, it also happens to be a perfect fit for creating WebAssembly modules that don't use any particular operating system-like API. | + | The [[TARGET_Embedded|Embedded target]] is primarily used for embedded systems (not a browser), without an operating system. However, it also happens to be a perfect fit for creating WebAssembly modules that don't use any particular operating system-like API. |
make all OS_TARGET=embedded CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc | make all OS_TARGET=embedded CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc | ||
Line 63: | Line 74: | ||
=== Using the compiler === | === Using the compiler === | ||
− | + | By default /etc/fpc.cfg contains a ''-XP$FPCTARGET-'' flag, so fpc searches for ''wasm32-wasi-wasm-ld''. If you want to search for ''wasm-ld'' instead adapt your fpc.cfg : | |
+ | |||
+ | If your fpc.cfg has | ||
<pre> | <pre> | ||
#IFDEF FPC_CROSSCOMPILING | #IFDEF FPC_CROSSCOMPILING | ||
Line 85: | Line 98: | ||
Otherwise, you have to create symlinks wasm32-wasi-llvm-mc to llvm-mc, and wasm32-wasi-wasm-ld to wasm-ld. | Otherwise, you have to create symlinks wasm32-wasi-llvm-mc to llvm-mc, and wasm32-wasi-wasm-ld to wasm-ld. | ||
− | + | =See Also= | |
*[[WebAssembly]] | *[[WebAssembly]] | ||
[[Category:WebAssembly]] | [[Category:WebAssembly]] |
Revision as of 13:54, 28 December 2021
Instructions
Prerequisites
The wasm-ld linker. This the linker from the LLVM project. The external assembler llvm-mc is optional, since commit bc76487b878e59133ce3c2cc19ebb13f9a9826ea, in which, the internal assembler and object writer was enabled.
LLVM 11.0 is known to work. Development was done, using the Fedora packages llvm-11.0.0-1.fc33.x86_64 and lld-11.0.0-1.fc33.x86_64. Initial tests on MacOS used llvm-11 installed with MacPorts (https://www.macports.org) and according symlinks in /opt/local/bin (wasm32-wasi-llvm-mc -> llvm-mc-mp-11 and wasm32-embedded-llvm-mc -> llvm-mc-mp-11).
LLVM 12.0.1 also works.
Ubuntu 20.04
The wasm-ld is part of package lld-12. fpc searches for the tool wasm32-wasi-wasm-ld or wasm-ld depending on the -XP flag you use (see /etc/fpc.cfg):
apt install lld-12 ln -sf /usr/lib/llvm-12/bin/wasm-ld ~/bin/wasm32-wasi-wasm-ld
Note: This assumes you have a $HOME/bin directory and it is in PATH.
If you need the external assembler, install the package llvm-12 and set a symlink:
apt install llvm-12 ln -sf /usr/lib/llvm-12/bin/llvm-mc ~/bin/wasm32-wasi-llvm-mc
Obtaining the compiler sources
The WebAssembly target was merged into FPC SVN trunk in r48955 on 2021-03-14. Since then, Free Pascal has moved to GitLab, so now it's now included in the git 'main' branch.
Get the sources:
git clone https://gitlab.com/freepascal.org/fpc/source.git fpc
enter the directory:
cd fpc
Supported targets
WASI - The WebAssembly System Interface
WASI is a modular system interface for WebAssembly. It allows creating portable and secure programs that can run in a sandboxed command-line environment or in the browser. See the WASI website for more information.
To build the compiler for the WASI target:
make all OS_TARGET=wasi CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc
The following units have been ported for the WASI target:
- system
- objpas
- iso7185
- ctypes
- strings
- wasiapi - interface for the WASI API
Additionally, these units compile, but are not yet fully functional and/or tested very well:
- dos
- sysutils
- classes
- math
- fgl
See WebAssembly/Roadmap for details.
Compiled WASI binaries work with wasmtime version 0.23.0 or in the browser by opening:
https://webassembly.sh/
and drag and dropping the .wasm binary directly into the browser window.
Embedded target
The Embedded target is primarily used for embedded systems (not a browser), without an operating system. However, it also happens to be a perfect fit for creating WebAssembly modules that don't use any particular operating system-like API.
make all OS_TARGET=embedded CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc
Using the compiler
By default /etc/fpc.cfg contains a -XP$FPCTARGET- flag, so fpc searches for wasm32-wasi-wasm-ld. If you want to search for wasm-ld instead adapt your fpc.cfg :
If your fpc.cfg has
#IFDEF FPC_CROSSCOMPILING #IFDEF NEEDCROSSBINUTILS -XP$FPCTARGET- #ENDIF #ENDIF
Enclose it in #IFNDEF CPUWASM32 :
#IFDEF FPC_CROSSCOMPILING #IFDEF NEEDCROSSBINUTILS #ifndef cpuwasm32 -XP$FPCTARGET- #endif #ENDIF #ENDIF
Otherwise, you have to create symlinks wasm32-wasi-llvm-mc to llvm-mc, and wasm32-wasi-wasm-ld to wasm-ld.