Difference between revisions of "WebAssembly/Compiler"

From Lazarus wiki
Jump to navigationJump to search
(→‎Compiling the compiler: - compilation should be done via makefiles)
(removed the outdated section)
(25 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
== Instructions ==
 
== Instructions ==
=== Compiling the compiler ===
+
 
 +
=== 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.
 +
 
 +
=== 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:
 
Get the sources:
  
  svn checkout https://svn.freepascal.org/svn/fpc/branches/wasm/ fpc-wasm
+
  git clone https://gitlab.com/freepascal.org/fpc/source.git fpc-wasm
  
 
enter the directory:
 
enter the directory:
 
  cd fpc-wasm
 
  cd fpc-wasm
  
==== WASI target ====
+
=== 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.
 +
 
 +
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
  
==== Embedded target ====
+
The following units have been ported for the WASI target:
make all OS_TARGET=embedded CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc
 
  
=== Using the compiler ===
+
* system
 +
* objpas
 +
* iso7185
 +
* ctypes
 +
* strings
 +
* wasiapi - interface for the WASI API
  
You need a system.pas unit. It can be found here:
+
Additionally, these units compile, but are not yet fully functional and/or tested very well:
  
[https://github.com/skalogryz/wasm-demo wasm-demo]
+
* dos
 +
* sysutils
 +
* classes
 +
* math
 +
* fgl
  
You'll also need the wasm bin tools:
+
See [[WebAssembly/Roadmap]] for details.
  
[https://github.com/WebAssembly/wabt/releases wasm bin tools]
+
Compiled WASI binaries work with wasmtime version 0.23.0 or in the browser by opening:
  
Extract the binaries and put them somewhere in your PATH.
+
https://webassembly.sh/
  
You'll also need the wasmtool:
+
and drag and dropping the .wasm binary directly into the browser window.
  
[https://github.com/skalogryz/wasmbin wasmtool]
+
==== Embedded target ====
  
* Compile wasmtool.lpi
+
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.
  
Put wasmtool somewhere in your PATH
+
make all OS_TARGET=embedded CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc
  
And finally you need ''wasm-ld'' (don't confuse it with ''wasmld'').
+
=== Using the compiler ===
* Download the [http://releases.llvm.org/download.html llvm-9 release], and put the '''bin/ldd''' as '''wasmld''' in your PATH.
 
  
 
Hint: If your fpc.cfg has
 
Hint: If your fpc.cfg has
Line 47: Line 72:
 
</pre>
 
</pre>
  
Enclose it in '''#IFNDEF CPUWASM''' :
+
Enclose it in '''#IFNDEF CPUWASM32''' :
 
<pre>
 
<pre>
 
#IFDEF FPC_CROSSCOMPILING
 
#IFDEF FPC_CROSSCOMPILING
 
#IFDEF NEEDCROSSBINUTILS
 
#IFDEF NEEDCROSSBINUTILS
#ifndef cpuwasm
+
#ifndef cpuwasm32
 
   -XP$FPCTARGET-
 
   -XP$FPCTARGET-
 
#endif
 
#endif
Line 58: Line 83:
 
</pre>
 
</pre>
  
Otherwise, you have to create symlinks wasm-wasm-wasmld to wasmld, and wasm-wasm-wasmtool to wasmtool.
+
Otherwise, you have to create symlinks wasm32-wasi-llvm-mc to llvm-mc, and wasm32-wasi-wasm-ld to wasm-ld.
 
 
==wasm-demo==
 
Wasm-demo is a Pascal WebAssembly demo project, ported Google's C WebAssembly example (https://codelabs.developers.google.com/codelabs/web-assembly-intro)
 
 
 
You follow these steps in order to get the demo project working:
 
 
 
1. Get the project sources: https://github.com/skalogryz/wasm-demo
 
 
 
===Using in webassembly.studio===
 
2. Run the wasm compiler against lyff.pas, but request the assembler file to be kept
 
  pp -a lyff.pas
 
Webassembly.studio accepts the assembler file and would compile it on the server side.
 
 
 
3. open up  https://webassembly.studio and select "Create empty Wat Project". The project will consist of:
 
:main.html
 
:main.js
 
:main.wat
 
All files of the project can be edited through the web interface.
 
 
 
4. Copy and paste the contents of the following files:
 
: (compiled) lyff.wat to main.wat.
 
: wasm-demo\webassembly.studio\main.html to main.html
 
: wasm-demo\webassembly.studio\main.js to main.js
 
 
 
Note: every time you copy and paste, don't forget to press "SAVE" button (on the right side of the web interface)
 
 
 
5. once all files are update, hit "Build and Run"
 
  
 
==See Also==
 
==See Also==
 
*[[WebAssembly]]
 
*[[WebAssembly]]
 
[[Category:WebAssembly]]
 
[[Category:WebAssembly]]

Revision as of 05:27, 3 October 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.

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-wasm

enter the directory:

cd fpc-wasm

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. 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, 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

Hint: 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.

See Also