Skip to content

Commit 84aa331

Browse files
authored
feat: add binaryen opt (#181)
1 parent 777cd0e commit 84aa331

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

docs/guide/build-config.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
Build and publish related options are configured in `.csproj` file via MSBuild properties.
44

5-
| Property | Default | Description |
6-
|-----------------------------|------------|--------------------------------------------------------------|
7-
| BootsharpName | bootsharp | Name of the generated JavaScript module. |
8-
| BootsharpEmbedBinaries | true | Whether to embed binaries to the JavaScript module file. |
9-
| BootsharpAggressiveTrimming | false | Whether to disable some .NET features to reduce binary size. |
10-
| BootsharpLLVM | false | Enable experimental [NativeAOT-LLVM](/guide/llvm) backend. |
11-
| BootsharpBundleCommand | npx rollup | The command to bundle generated JavaScrip solution. |
12-
| BootsharpPublishDirectory | /bin | Directory to publish generated JavaScript module. |
13-
| BootsharpTypesDirectory | /types | Directory to publish type declarations. |
14-
| BootsharpBinariesDirectory | /bin | Directory to publish binaries when `EmbedBinaries` disabled. |
15-
| BootsharpPackageDirectory | / | Directory to publish `package.json` file. |
5+
| Property | Default | Description |
6+
|-----------------------------|------------|----------------------------------------------------------------------------------------------------------------------------------|
7+
| BootsharpName | bootsharp | Name of the generated JavaScript module. |
8+
| BootsharpEmbedBinaries | true | Whether to embed binaries to the JavaScript module file. |
9+
| BootsharpAggressiveTrimming | false | Whether to disable some .NET features to reduce binary size. |
10+
| BootsharpOptimize | none | Whether to optimize the WASM for `speed` or `size`. Requires [Binaryen](https://github.com/WebAssembly/binaryen) in system path. |
11+
| BootsharpLLVM | false | Enable experimental [NativeAOT-LLVM](/guide/llvm) backend. |
12+
| BootsharpBundleCommand | npx rollup | The command to bundle generated JavaScrip solution. |
13+
| BootsharpPublishDirectory | /bin | Directory to publish generated JavaScript module. |
14+
| BootsharpTypesDirectory | /types | Directory to publish type declarations. |
15+
| BootsharpBinariesDirectory | /bin | Directory to publish binaries when `EmbedBinaries` disabled. |
16+
| BootsharpPackageDirectory | / | Directory to publish `package.json` file. |
1617

17-
Below is an example configuration, which will make Bootsharp name compiled module "backend" (instead of the default "bootsharp"), publish the module under solution directory root (instead of "/bin"), disable binaries embedding and instead publish them under "public/bin" directory one level above the solution root and enable aggressive assembly trimming to reduce build size:
18+
Below is an example configuration, which will make Bootsharp name compiled module "backend" (instead of the default "bootsharp"), publish the module under solution directory root (instead of "/bin"), disable binaries embedding and instead publish them under "public/bin" directory one level above the solution root and enable aggressive assembly trimming and WASM optimization to reduce the build size:
1819

1920
```xml
2021
<Project Sdk="Microsoft.NET.Sdk">
@@ -27,6 +28,7 @@ Below is an example configuration, which will make Bootsharp name compiled modul
2728
<BootsharpEmbedBinaries>false</BootsharpEmbedBinaries>
2829
<BootsharpBinariesDirectory>$(SolutionDir)../public/bin</BootsharpBinariesDirectory>
2930
<BootsharpAggressiveTrimming>true</BootsharpAggressiveTrimming>
31+
<BootsharpOptimize>size</BootsharpOptimize>
3032
</PropertyGroup>
3133

3234
<ItemGroup>

docs/guide/llvm.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ Use following `.csproj` as a reference for enabling NativeAOT-LLVM with Bootshar
5858

5959
## Binaryen
6060

61-
Optionally, after publishing with NativeAOT-LLVM, you can further optimize the produced WASM using Binaryen:
61+
Optionally, you can further optimize the produced WASM using Binaryen:
6262

6363
1. Install the tool https://github.com/WebAssembly/binaryen
64-
2. Run `wasm-opt bin/bootsharp/bin/dotnet.native.wasm -O3 -o bin/bootsharp/bin/dotnet.native.wasm --all-features --strip-dwarf --strip-debug --vacuum`
65-
3. To optimize for size instead of speed, replace `-O3` with `-Oz`
64+
2. Make sure `wasm-opt` is in the system path
65+
3. Add `<BootsharpOptimize>speed</BootsharpOptimize>` to the project config to optimize for speed; replace `speed` with `size` to instead optimize for size

src/cs/Bootsharp/Build/Bootsharp.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
<BootsharpEmbedBinaries>true</BootsharpEmbedBinaries>
1919
<!-- Whether to disable some .NET features to reduce binary size (true or false); false by default. -->
2020
<BootsharpAggressiveTrimming>false</BootsharpAggressiveTrimming>
21+
<!-- Whether to optimize the WASM for speed or size (requires Binaryen's wasm-opt in system path); disabled by default. -->
22+
<BootsharpOptimize>none</BootsharpOptimize>
2123
<!-- Whether to app is built with the new experimental NativeAOT-LLVM backend; false by default. -->
2224
<BootsharpLLVM>false</BootsharpLLVM>
2325
<!-- The command to run when compiling/bundling generated JavaScrip solution. -->

src/cs/Bootsharp/Build/Bootsharp.targets

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
<BootsharpThreading Condition="'$(WasmEnableThreads)' == 'true'">true</BootsharpThreading>
110110
<BootsharpThreading Condition="'$(WasmEnableThreads)' != 'true'">false</BootsharpThreading>
111111
<BootsharpEmbedBinaries Condition="$(BootsharpThreading)">false</BootsharpEmbedBinaries>
112+
<BootsharpOptimizeFlag Condition="'$(BootsharpOptimize)' == 'speed'">-O3</BootsharpOptimizeFlag>
113+
<BootsharpOptimizeFlag Condition="'$(BootsharpOptimize)' == 'size'">-Oz</BootsharpOptimizeFlag>
112114
</PropertyGroup>
113115

114116
<!-- Copy bootsharp source files to the build directory. -->
@@ -117,6 +119,12 @@
117119
</ItemGroup>
118120
<Copy SourceFiles="@(BootsharpSourceFiles)" DestinationFolder="$(BootsharpBuildDirectory)"/>
119121

122+
<!-- Optimize the dotnet WASM with Binaryen. -->
123+
<Exec Condition="'$(BootsharpOptimize)' != 'none'"
124+
Command="wasm-opt dotnet.native.wasm $(BootsharpOptimizeFlag) -o dotnet.native.wasm --all-features --strip-dwarf --strip-debug --vacuum"
125+
WorkingDirectory="$(BootsharpBuildDirectory)"
126+
StdOutEncoding="utf-8" StdErrEncoding="utf-8"/>
127+
120128
<!-- Generate bindings, type declarations and (optionally) embed binaries. -->
121129
<BootsharpPack BuildDirectory="$(BootsharpBuildDirectory)"
122130
InspectedDirectory="$(OutputPath)"

src/cs/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4-
<Version>0.6.2</Version>
4+
<Version>0.6.3</Version>
55
<Authors>Elringus</Authors>
66
<PackageTags>javascript typescript ts js wasm node deno bun interop codegen</PackageTags>
77
<PackageProjectUrl>https://bootsharp.com</PackageProjectUrl>

0 commit comments

Comments
 (0)