WebAssembly/Compiler: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: WebAssembly/Compiler
| Old | New | Line | |
|---|---|---|---|
| 1 | + | # Prerequisites | |
| 2 | + | ||
| 3 | + | The linker *wasm-ld* from the LLVM project. LLVM 11.0 and 12.0.1 are known to work. | |
| 4 | + | ||
| 5 | + | ## Fedora | |
| 6 | + | ||
| 7 | + | Development was done, using the Fedora packages llvm-11.0.0-1.fc33.x86_64 and lld-11.0.0-1.fc33.x86_64. | |
| 8 | + | ||
| 9 | + | ## Ubuntu | |
| 10 | + | ||
| 11 | + | Tested on Ubuntu 20.04. *wasm-ld* is part of package *lld-12*. Install it and set necessary symlinks: | |
| 12 | + | ||
| 13 | + | `% sudo apt install lld-12`\ | |
| 14 | + | `% ln -sf /usr/lib/llvm-12/bin/wasm-ld ~/bin/wasm32-wasi-wasm-ld`\ | |
| 15 | + | `% ln -sf /usr/lib/llvm-12/bin/wasm-ld ~/bin/wasm32-embedded-wasm-ld` | |
| 16 | + | ||
| 17 | + | Note: This assumes you have a \$HOME/bin directory and it is in PATH. | |
| 18 | + | ||
| 19 | + | ## macOS | |
| 20 | + | ||
| 21 | + | ### Catalina | |
| 22 | + | ||
| 23 | + | Tested on **macOS Catalina** with LLVM 11. For [MacPorts](https://www.macports.org) *wasm-ld* is part of package *llvm-11*; upon installation it exists as *wasm-ld-mp-11*. Install the package and set necessary symlinks: | |
| 24 | + | ||
| 25 | + | `% sudo port install llvm-11`\ | |
| 26 | + | `% ln -sf /opt/local/bin/wasm-ld-mp-11 ~/bin/wasm32-wasi-wasm-ld`\ | |
| 27 | + | `% ln -sf /opt/local/bin/wasm-ld-mp-11 ~/bin/wasm32-embedded-wasm-ld` | |
| 28 | + | ||
| 29 | + | Note: This assumes you have a \$HOME/bin directory and it is in PATH. | |
| 30 | + | ||
| 31 | + | ### Monterey | |
| 32 | + | ||
| 33 | + | Tested on **macOS Monterey 12.5.1**: wasm-ld is part of the official clang release for macOS: <https://releases.llvm.org/download.html#11.1.0> | |
| 34 | + | ||
| 35 | + | Unpack it. When running wasm-ld, macOS might warn, that ldd cannot be verified. Open the System Preferences / Security / General. There should be ldd, unlock, then click Open Anyway. | |
| 36 | + | ||
| 37 | + | ## Windows 10 | |
| 38 | + | ||
| 39 | + | Tested on Windows 10 Home 21H2. Download the [LLVM 12.0.1](https://github.com/llvm/llvm-project/releases/tag/llvmorg-12.0.1) 64-bit Windows installer. You are encouraged to verify the GPG signature on the installer. As administrator, run the installer. Let's say you installed into C:\llvm. Enable Windows 'developer mode' if you have not done so. Then set necessary symlinks: | |
| 40 | + | ||
| 41 | + | ` C:\> cd llvm\bin`\ | |
| 42 | + | ` C:\llvm\bin> mklink wasm32-wasi-wasm-ld.exe wasm-ld.exe`\ | |
| 43 | + | ` C:\llvm\bin> mklink wasm32-embedded-wasm-ld.exe wasm-ld.exe` | |
| 44 | + | ||
| 45 | + | Add C:\llvm\bin to your PATH. | |
| 46 | + | ||
| 47 | + | # Obtaining the compiler sources | |
| 48 | + | ||
| 49 | + | ## Linux and macOS | |
| 50 | + | ||
| 51 | + | The WebAssembly compiler target is part of the 'main' branch of the GitLab FPC source repo. Get the source: | |
| 52 | + | ||
| 53 | + | `% git clone `[`https://gitlab.com/freepascal.org/fpc/source.git`](https://gitlab.com/freepascal.org/fpc/source.git)` fpc` | |
| 54 | + | ||
| 55 | + | Enter the sources directory: | |
| 56 | + | ||
| 57 | + | `% cd fpc` | |
| 58 | + | ||
| 59 | + | ## Windows | |
| 60 | + | ||
| 61 | + | The WebAssembly compiler target is part of the 'main' branch of the GitLab FPC source repo. Get the source: | |
| 62 | + | ||
| 63 | + | `C:\Users\OnlyMe> git clone `[`https://gitlab.com/freepascal.org/fpc/source.git`](https://gitlab.com/freepascal.org/fpc/source.git)` fpc` | |
| 64 | + | ||
| 65 | + | Enter the sources directory: | |
| 66 | + | ||
| 67 | + | `C:\Users\OnlyMe> cd fpc` | |
| 68 | + | ||
| 69 | + | # Building and installing | |
| 70 | + | ||
| 71 | + | If you are new to building the compiler, see [[Installing the Free Pascal Compiler]] and [[Cross compiling]]. | |
| 72 | + | ||
| 73 | + | FPC supports two WebAssembly compilation targets: WASI and embedded. | |
| 74 | + | ||
| 75 | + | ## Compilation target: WASI | |
| 76 | + | ||
| 77 | + | ### Linux and macOS | |
| 78 | + | ||
| 79 | + | Build the compiler and FPC units for the WASI target: | |
| 80 | + | ||
| 81 | + | `% make clean all OS_TARGET=wasi CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc` | |
| 82 | + | ||
| 83 | + | Then, install, using INSTALL_PREFIX to specify a destination within your home directory: | |
| 84 | + | ||
| 85 | + | `% make crossinstall OS_TARGET=wasi CPU_TARGET=wasm32 INSTALL_PREFIX=$HOME/fpcwasm` | |
| 86 | + | ||
| 87 | + | Note: On macOS you might have to add *-XR/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk* to *OPT*. | |
| 88 | + | ||
| 89 | + | Make a link to the cross compiler executable *ppcrosswasm32*: | |
| 90 | + | ||
| 91 | + | `% ln -sf ~/fpcwasm/lib/fpc/3.3.1/ppcrosswasm32 ~/bin/ppcrosswasm32` | |
| 92 | + | ||
| 93 | + | Note: This assumes you have a \$HOME/bin directory and it is in PATH. | |
| 94 | + | ||
| 95 | + | ### Windows | |
| 96 | + | ||
| 97 | + | Make sure the Windows native compiler fpc.exe and the FPC-bundled make.exe are in your PATH. Especially ensure that the FPC make.exe comes first in your PATH; using another make.exe (such as the one installed by Delphi) that happens to be ahead in PATH may not work. | |
| 98 | + | ||
| 99 | + | Build the compiler and FPC units for the WASI target: | |
| 100 | + | ||
| 101 | + | `C:\Users\OnlyMe\fpc> make clean all OS_TARGET=wasi CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc` | |
| 102 | + | ||
| 103 | + | Then, install, using INSTALL_PREFIX to specify a destination folder: | |
| 104 | + | ||
| 105 | + | `C:\Users\OnlyMe\fpc> make crossinstall OS_TARGET=wasi CPU_TARGET=wasm32 INSTALL_PREFIX=c:\fpcwasm PP=fpc` | |
| 106 | + | ||
| 107 | + | Add C:\fpcwasm\bin\x86_64-win64 to your PATH to easily run the compiler *ppcrosswasm32.exe*. | |
| 108 | + | ||
| 109 | + | ## Compilation target: Embedded | |
| 110 | + | ||
| 111 | + | ### Linux and macOS | |
| 112 | + | ||
| 113 | + | Build the compiler and FPC units for the 'embedded' target: | |
| 114 | + | ||
| 115 | + | `% make clean all OS_TARGET=embedded CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc` | |
| 116 | + | ||
| 117 | + | Then, install, using INSTALL_PREFIX to specify a destination within your home directory: | |
| 118 | + | ||
| 119 | + | `% make crossinstall OS_TARGET=embedded CPU_TARGET=wasm32 INSTALL_PREFIX=$HOME/fpcwasm` | |
| 120 | + | ||
| 121 | + | If you have not built and installed the WASI cross compiler, then make a link to the cross compiler executable *ppcrosswasm32*; | |
| 122 | + | ||
| 123 | + | `% ln -sf ~/fpcwasm/lib/fpc/3.3.1/ppcrosswasm32 ~/bin/ppcrosswasm32` | |
| 124 | + | ||
| 125 | + | Note: This assumes you have a \$HOME/bin directory and it is in PATH. | |
| 126 | + | ||
| 127 | + | ### Windows | |
| 128 | + | ||
| 129 | + | Make sure the Windows native compiler fpc.exe and the FPC-bundled make.exe are in your PATH. Especially ensure that the FPC make.exe comes first in your PATH; using another make.exe (such as the one installed by Delphi) that happens to be ahead in PATH may not work. | |
| 130 | + | ||
| 131 | + | Build the compiler and FPC units for the 'embedded' target: | |
| 132 | + | ||
| 133 | + | `C:\Users\OnlyMe\fpc> make clean all OS_TARGET=embedded CPU_TARGET=wasm32 BINUTILSPREFIX= OPT="-O-" PP=fpc` | |
| 134 | + | ||
| 135 | + | Then, install, using INSTALL_PREFIX to specify a destination folder: | |
| 136 | + | ||
| 137 | + | `C:\Users\OnlyMe\fpc> make crossinstall OS_TARGET=embedded CPU_TARGET=wasm32 INSTALL_PREFIX=c:\fpcwasm PP=fpc` | |
| 138 | + | ||
| 139 | + | Add C:\fpcwasm\bin\x86_64-win64 to your PATH if you have not already done so. | |
| 140 | + | ||
| 141 | + | # Using the compiler | |
| 142 | + | ||
| 143 | + | For Linux and macOS, if you have installed the cross compiler and units into \$HOME/fpcwasm (as per the commands used above), then you need to configure \$HOME/.fpc.cfg so that the cross compiler can locate its units. | |
| 144 | + | ||
| 145 | + | For Ubuntu, add these lines: | |
| 146 | + | ||
| 147 | + | #ifdef cpuwasm32 | |
| 148 | + | -Fu/home/yourusername/fpcwasm/lib/fpc/$fpcversion/units/$fpctarget/* | |
| 149 | + | -Fu/home/yourusername/fpcwasm/lib/fpc/$fpcversion/units/$fpctarget/rtl | |
| 150 | + | #endif | |
| 151 | + | ||
| 152 | + | For macOS, add these lines: | |
| 153 | + | ||
| 154 | + | #ifdef cpuwasm32 | |
| 155 | + | -Fu/Users/yourusername/fpcwasm/lib/fpc/$fpcversion/units/$fpctarget/* | |
| 156 | + | -Fu/Users/yourusername/fpcwasm/lib/fpc/$fpcversion/units/$fpctarget/rtl | |
| 157 | + | #endif | |
| 158 | + | ||
| 159 | + | On Windows, if you have set C:\fpcwasm\bin\x86_64-win64 directory in your PATH, then *ppcrosswasm32.exe* will be able to find its units. | |
| 160 | + | ||
| 161 | + | # Using the compiler with Lazarus | |
| 162 | + | ||
| 163 | + | When you create a new Lazarus project for a wasm program you can use the *Simple Program* template and then adjust the *Project / Project Options / Compiler Options*: | |
| 164 | + | ||
| 165 | + | - Paths / Target file name: Lazarus 2.3.0 (Dec 28th 2021) appends the required .wasm file extension to the output file. To support older IDEs disable *Apply conventions* and set *\<programname\>.wasm*. | |
| 166 | + | - Config and Target / Target platform: | |
| 167 | + | - Target OS: Wasi | |
| 168 | + | - Target CPU: wasm32 | |
| 169 | + | - Debugging / Generate info for debugger: disable (Dec 28th 2021: otherwise you will get can't find unit linfodwrf) | |
| 170 | + | - Compiler Commands / Compiler / Command: If you are using different fpc versions / folders you can here set the path to ppcrosswasm32 (e.g. Linux: /usr/lib/fpc/3.3.1/ppcrosswasm32) | |
| 171 | + | ||
| 172 | + | # See Also | |
| 173 | + | ||
| 174 | + | - [[WebAssembly]] | |
| 175 | + | ||
| 176 | + | --- | |
| 177 | + | *From the Free Pascal wiki, page "WebAssembly/Compiler", as it stood on 2022-09-08 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 178 | + | Category: WebAssembly |
