Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make use of nativeaot static linking feature #9503

Open
ivanjx opened this issue Nov 21, 2022 · 55 comments
Open

make use of nativeaot static linking feature #9503

ivanjx opened this issue Nov 21, 2022 · 55 comments

Comments

@ivanjx
Copy link

ivanjx commented Nov 21, 2022

Is your feature request related to a problem? Please describe.
it is now possible to static link native libraries inside the main binary so it should be possible to do that to the 2 native dlls from avalonia's native aot output, namely libHarfBuzzSharp.dll and libSkiaSharp.dll.

Describe the solution you'd like
include the static library files of those 2 dlls instead and use DirectPInvoke and NativeLibrary.

Additional context
https://github.com/dotnet/runtimelab/blob/feature/NativeAOT/docs/using-nativeaot/interop.md#linking

@maxkatz6
Copy link
Member

It should be optional, as it works only with native AOT.
I suppose ideal plan would be something like:

  • Include lib/a files into our nuget packages. Including Skia and Angle
  • Add new msbuild option to enable avalonia libs static linking
  • In the msbuild targets files include and DirectPInvoke items if avalonia static linking is enabled
  • Exclude dll/so files from the output if avalonia static linking is enabled

@ivanjx
Copy link
Author

ivanjx commented Nov 21, 2022

no need for another static linking configuration in the csproj. DirectPInvoke and NativeLibrary is smart enough to ignore themselves if it is not being published. native aot will only kick in dotnet publish command.

@ivanjx
Copy link
Author

ivanjx commented Nov 22, 2022

@maxkatz6 are there any static library files .lib for those 2 dlls? if there arent how to create one? thanks

@maxkatz6
Copy link
Member

you will need to look for them here, possibly compile Skia manually
https://github.com/mono/SkiaSharp

@danielmayost
Copy link
Contributor

danielmayost commented Feb 6, 2023

Is there a workaround to achieve this?

My software is distributed as singlefile, it is quite impractical to use AOT when 3 more dlls are added on the side.

@maxkatz6
Copy link
Member

maxkatz6 commented Feb 8, 2023

For the SingleFile deployment it's possible to include native dependencies: https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview?tabs=cli#include-native-libraries

I don't think it's applicable for the NativeAOT, that's more a question to the .NET runtime team.

@danielmayost
Copy link
Contributor

For the SingleFile deployment it's possible to include native dependencies: https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview?tabs=cli#include-native-libraries

yes, i know, meant for NativeAOT.

I don't think it's applicable for the NativeAOT, that's more a question to the .NET runtime team.

it's not works.

@ivanjx
Copy link
Author

ivanjx commented Feb 8, 2023

deploying native aot avalonia apps is possible with single exe as long as you have the static libs mentioned above. i have compiled those and works fine

@danielmayost
Copy link
Contributor

@ivanjx static lib, Where did you get them? So you successfully create a single AOT file?

@ivanjx
Copy link
Author

ivanjx commented Feb 8, 2023

@danielmayost yes. you need to compile skia and harfbuzz urself. my hands are full right now so cant explain the details but u can search on how to do those.

@ivanjx
Copy link
Author

ivanjx commented Feb 18, 2023

here is my compiled required libs for avalonia on windows x64 platform if anyone is interested.
https://mega.nz/file/z34V3bSZ#boItQx_nLBRGu-biDL3PbKSSG_Ti87JL7KNY7qz2Li8

@ivanjx
Copy link
Author

ivanjx commented Feb 18, 2023

you can build skia for other platforms if you look inside the cake files. here is the ones for windows that i use as reference: https://github.com/mono/SkiaSharp/blob/main/native/windows/build.cake

for harfbuzz you can use this and open in visual studio. there is some settings to change to enable static build but i forgot which: https://github.com/mono/SkiaSharp/tree/main/native/windows/libHarfBuzzSharp

reference about the build commands: https://github.com/mono/SkiaSharp/wiki/Building-on-Linux

@Cubody
Copy link

Cubody commented Mar 17, 2023

@ivanjx
I downloaded your x64 win native libs and wrote static linking in .csproj:

    <ItemGroup>
        <DirectPInvoke Include="libHarfBuzzSharp" />
        <NativeLibrary Include="native\libHarfBuzzSharp.lib" Condition="$(RuntimeIdentifier.StartsWith('win'))" />
        <DirectPInvoke Include="libSkiaSharp" />
        <NativeLibrary Include="native\libSkiaSharp.lib" Condition="$(RuntimeIdentifier.StartsWith('win'))" />
    </ItemGroup>

But I'm getting strange errors that I can't understand. Can you help me?

libHarfBuzzSharp.lib(hb-face.obj) : found .netmodule MSIL or a module compiled with the /GL parameter; the layout is restarted with the /LTCG parameter; add /LTCG to the command line of the layout to improve the performance of the linker
EXEC : fatal error C1900: IL mismatch between "P1", version "20220715" and "P2", version "20220421"
LINK : fatal error LNK1257: failed to generate code
C:\Users\green\.nuget\packages\microsoft.dotnet.ilcompiler\8.0.0-preview.1.23110.8\build\Microsoft.NETCore.Native.targets(354,5): error MSB3073: exiting the "" commandC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\
MSVC\14.33.31629\bin\Hostx64\x64\link.exe " @"obj\Release\net8.0\win-x64\native\link.rsp"" with code 1257.

@ivanjx
Copy link
Author

ivanjx commented Mar 17, 2023

@Cubody
i only tested this with net 7.0. maybe try to use that. or you can try to build the static libs with your current visual studio installation.

@Cubody
Copy link

Cubody commented Mar 17, 2023

@ivanjx
I also tried with .NET 7.0, but SDK is 8.0.
I will be very grateful if you can enlighten a little more in compiling native libraries, because I have never met with this before.
The feeling is that the error says that the nuget package(both Skia and second) is a different version instead of that's natives. I am using Avalonia 11 preview 5.

I'm also using Rider and can not download Visual Studio due to stupid region restriction :/

@ivanjx
Copy link
Author

ivanjx commented Mar 17, 2023

i believe avalonia version is not the problem here as i am using nightly.
try the 7.0 sdk

@ivanjx
Copy link
Author

ivanjx commented Mar 17, 2023

for compiling without visual studio i dont think it is possible as the compilers came from the visual studio installation. maybe you can install the compilers manually but idk how

@Cubody
Copy link

Cubody commented Mar 17, 2023

SDK 7.0 didn't help me =(
I will try to install VS, but how to compile natives?
Can you provide full instruction for these two libraries if it's not hard for you?
I think I can deal with .cake if I'll read docs but it's not clear about HarfBuzzSharp.

@ivanjx
Copy link
Author

ivanjx commented Mar 17, 2023

in general this is what you have to do at first:

  1. install LLVM
  2. clone https://github.com/mono/SkiaSharp
  3. git submodule update --init --recursive
  4. cd externals\skia
  5. python .\tools\git-sync-deps

with skiasharp, you can follow these commands with a few adjustments (it has been a while but it is very similar). for the args required it is all mentioned in the .cake file for windows.

with harfbuzzsharp it is actually a lot easier. just need to open the .sln file inside the folder, re-target to latest if prompted, change the configuration type to static library, then build with Release configuration.

image

in your case @Cubody, i think the problem is only with the harfbuzz static library. so hopefully you can just build that yourself without rebuilding the skiasharp.

@ivanjx
Copy link
Author

ivanjx commented Mar 17, 2023

so here is the full command that i use for compiling skiasharp

  1. ..\depot_tools\gn gen 'out/windows/x64' --args='target_os=\"win\" target_cpu=\"x64\" is_static_skiasharp=true skia_enable_fontmgr_win_gdi=false skia_use_dng_sdk=true skia_use_icu=false skia_use_piex=true skia_use_sfntly=false skia_use_system_expat=false skia_use_system_libjpeg_turbo=false skia_use_system_libpng=false skia_use_system_libwebp=false skia_use_system_zlib=false skia_enable_skottie=true skia_use_vulkan=true skia_enable_gpu=true clang_win=\"C:\Program Files\LLVM\" extra_cflags=[ \"-DSKIA_C_DLL\", \"/MT\", \"/EHsc\", \"/Z7\", \"-D_HAS_AUTO_PTR_ETC=1\" ]'
  2. ..\depot_tools\ninja 'SkiaSharp' -C 'out/windows/x64'
  3. open developer powershell for vs 2022. change directory to externals\skia\out\windows\x64
  4. lib.exe /OUT:libSkiaSharp.lib *.lib

the first 2 commands are run inside the externals\skia folder.
beware that the 2nd command will use a lot of your cpu!

note: if you encounter issues when running gn, go inside your python installation path, copy python.exe into python3.exe

@Cubody
Copy link

Cubody commented Mar 26, 2023

@ivanjx Thanks a lot! I'll try it.

@Cubody
Copy link

Cubody commented Apr 2, 2023

I just tried one more time with your libs and it works as expected! I deleted all .NET packages and installed just .NET 8 preview. Now it's fine.

@KieranDevvs
Copy link
Contributor

KieranDevvs commented May 19, 2023

Its not clear to me where the issue lies, which one(s) of the following is making this occour?

A) SkiaSharp & HarfBuzzSharp don't package static libraries in their NuGet packages so there's nothing to statically link.
B) The NativeAOT compilation process in .NET doesn't support automatically statically linking 3rd party libs.
C) Both A & B are false and something else is blocking it from building a true published single executable

While its nice that there's a work around of building your own static libraries and configuring your project to use it, I don't consider that to be a fix for the problem. The goal should be that it works out of the box.

@danielmayost
Copy link
Contributor

danielmayost commented May 19, 2023

If I understand correctly A is the problem.
It's really frustrating not being able to publish a single AOT file, try as I might, I couldn't build SkiaSharp by myself.

@ivanjx
Copy link
Author

ivanjx commented May 19, 2023

i think including the static lib in the nuget wont be ideal for skiasharp as it is very big even for a single platform. imagine adding other platforms as well.

@KieranDevvs
Copy link
Contributor

KieranDevvs commented May 19, 2023

i think including the static lib in the nuget wont be ideal for skiasharp as it is very big even for a single platform. imagine adding other platforms as well.

It doesn't have to be in the same NuGet package, they can split it out into:

  • SkiaSharp (All platforms with dynamic libs)
  • SkiaSharp.Static (All platforms without dynamic libraries)
  • SkiaSharp.x64Linux.Static (Specific platform)
    etc

And then end users can configure their projects to include the relevant packages for specific builds i.e

<ItemGroup>
    <PackageReference Include="SkiaSharp.x64Linux.Static" Condition="$(TargetPlatform) = '...'" />
</ItemGroup>

@danielmayost
Copy link
Contributor

danielmayost commented Jun 5, 2023

Maybe someone would be so kind as to share the static libraries they managed to create (av_libglesv2 and libSkiaSharp) for the benefit of those who haven't managed to do it yet.

@ivanjx
Copy link
Author

ivanjx commented Jun 5, 2023

@danielmayost do you have any issues compiling the native libraries by yourself? please try follow this #9503 (comment)

@MichalStrehovsky
Copy link

If you see found .netmodule MSIL or a module compiled with the /GL parameter; the layout is restarted with the /LTCG parameter that means that something in the LIB was compiled with the /GL parameter passed to the C++ compiler ("Whole program optimization"). Whole program C++ optimizations require that all the LIB files, link.exe, etc. have a matching version. You'd need to match the version of the C++ compiler used to compile the native parts of the .NET runtime. That might work sometimes, but fail other times. The fix is to not pass /GL when building your LIB.

@ivanjx
Copy link
Author

ivanjx commented Jun 20, 2023

@MichalStrehovsky if it works on compile time with wont crash on runtime correct? thank you for the info.

@danielmayost
Copy link
Contributor

danielmayost commented Jul 2, 2023

@ivanjx I finally succeeded to compile SkiaSharp (according to the commands you wrote), the final file I received is about 1GB and in the files you sent it is about 400MB, do you have any idea?

@ivanjx
Copy link
Author

ivanjx commented Jul 3, 2023

@danielmayost you dont have to worry about the lib size since you dont have to ship it with your application. in my case the output exe is only around 60 MB.

my latest output size of the lib file is also 1 GB. maybe there are some differences between the linker command i used the first time with what i wrote here. forgot what i used the first time.

edit:
i got ~400MB size because i use LLVM.

@ivanjx
Copy link
Author

ivanjx commented Oct 25, 2023

updated build steps for windows

requirements:

  • vs2022 with desktop development with c++
  • LLVM (optional)

libSkiaSharp:

  1. git clone https://github.com/mono/SkiaSharp.git
  2. git checkout release/2.88.6 (or pick according the skiasharp version in the nuget list)
  3. git submodule update --init --recursive
  4. cd external/skia
  5. python .\tools\git-sync-deps
  6. python ..\depot_tools\gn.py args .\out\windows\x64\
    this is what i put (according to this)
    target_os="win"
    target_cpu="x64"
    is_static_skiasharp=true
    skia_enable_fontmgr_win_gdi=false
    skia_use_dng_sdk=true
    skia_use_icu=false
    skia_use_piex=true
    skia_use_sfntly=false
    skia_use_system_expat=false
    skia_use_system_libjpeg_turbo=false
    skia_use_system_libpng=false
    skia_use_system_libwebp=false
    skia_use_system_zlib=false
    skia_use_vulkan=true
    skia_use_angle=true
    skia_enable_skottie=true
    skia_enable_gpu=true
    clang_win="C:\Program Files\LLVM"    <<=== REMOVE IF YOU DONT HAVE LLVM
    extra_cflags=[ "-DSKIA_C_DLL", "/MT", "/EHsc", "/Z7", "-D_HAS_AUTO_PTR_ETC=1" ]
    
  7. ..\depot_tools\ninja -C .\out\windows\x64\ SkiaSharp
  8. cd .\out\windows\x64\
  9. lib.exe /OUT:libSkiaSharp.lib *.lib

note:

  • run all those commands in the Developer Powershell for VS 2022.
  • if you get an error after gn.py args go to C:\Program Files\LLVM\lib\clang and rename the folder to be full version (eg: 16.0.0 instead of just 16).
  • later versions of SkiaSharp may need to change the 7th step command to: python ..\depot_tools\ninja.py -C .\out\windows\x64\ SkiaSharp

libHarfBuzzSharp

  • open native\windows\libHarfBuzzSharp\libHarfBuzzSharp.sln.
  • on retarget projects window, click ok.
  • change configuration Debug to Release.
  • open project properties (alt + enter).
  • change Configuration Type to Static library.
  • go to c/c++ -> All Options.
  • find Whole Program Optimization. change to No.
  • go to c/c++ -> Command Line. add /bigobj.
  • save & build.
  • the lib file should be in native\windows\libHarfBuzzSharp\bin\x64\Release.

@peaceshi
Copy link

I have created two github actions to build these static libs.

libSkiaSharp and libHarfBuzzSharp
https://github.com/2ndlab/SkiaSharp.Static

ANGLE static
https://github.com/2ndlab/ANGLE.Static

@Cubody
Copy link

Cubody commented Nov 20, 2023

@peaceshi

Have you made Avalonia single file native aot to work with static ANGLE? It doesn't work for me. I have a black background instead of transparent - the Avalonia UI uses software rendering.

I made
<NativeLibrary Include="Native\av_libglesv2.lib" />
in csproj

To undestand if it works or no you can add options to Build app:

BuildAvaloniaApp()
                .With(new Win32PlatformOptions
                    {RenderingMode = new[] {Win32RenderingMode.AngleEgl}})
                .StartWithClassicDesktopLifetime(args);

If it'll not find ANGLE lib it'll crash.

Also Avalonia uses it's own fork of ANGLE with some fixes.

@peaceshi
Copy link

@peaceshi

Have you made Avalonia single file native aot to work with static ANGLE? It doesn't work for me. I have a black background instead of transparent - the Avalonia UI uses software rendering.

I made <NativeLibrary Include="Native\av_libglesv2.lib" /> in csproj

To undestand if it works or no you can add options to Build app:

BuildAvaloniaApp()
                .With(new Win32PlatformOptions
                    {RenderingMode = new[] {Win32RenderingMode.AngleEgl}})
                .StartWithClassicDesktopLifetime(args);

If it'll not find ANGLE lib it'll crash.

Also Avalonia uses it's own fork of ANGLE with some fixes.

You can try to build this for yourself.
https://github.com/AvaloniaUI/angle/tree/avalonia-fork-master

@Cubody
Copy link

Cubody commented Nov 21, 2023

@peaceshi
Have you made Avalonia single file native aot to work with static ANGLE? It doesn't work for me. I have a black background instead of transparent - the Avalonia UI uses software rendering.
I made <NativeLibrary Include="Native\av_libglesv2.lib" /> in csproj
To undestand if it works or no you can add options to Build app:

BuildAvaloniaApp()
                .With(new Win32PlatformOptions
                    {RenderingMode = new[] {Win32RenderingMode.AngleEgl}})
                .StartWithClassicDesktopLifetime(args);

If it'll not find ANGLE lib it'll crash.
Also Avalonia uses it's own fork of ANGLE with some fixes.

You can try to build this for yourself. https://github.com/AvaloniaUI/angle/tree/avalonia-fork-master

Btw have you made Avalonia single file native aot to work with this? I already built but it doesn't help.
Thats why Katz says that it's main problem to build angle as static library with all needable dependencies.

@peaceshi
Copy link

@peaceshi
Have you made Avalonia single file native aot to work with static ANGLE? It doesn't work for me. I have a black background instead of transparent - the Avalonia UI uses software rendering.
I made <NativeLibrary Include="Native\av_libglesv2.lib" /> in csproj
To undestand if it works or no you can add options to Build app:

BuildAvaloniaApp()
                .With(new Win32PlatformOptions
                    {RenderingMode = new[] {Win32RenderingMode.AngleEgl}})
                .StartWithClassicDesktopLifetime(args);

If it'll not find ANGLE lib it'll crash.
Also Avalonia uses it's own fork of ANGLE with some fixes.

You can try to build this for yourself. https://github.com/AvaloniaUI/angle/tree/avalonia-fork-master

Btw have you made Avalonia single file native aot to work with this? I already built but it doesn't help. Thats why Katz says that it's main problem to build angle as static library with all needable dependencies.

Emm, what is the all needable dependencies? Can you explain it further?

@Cubody
Copy link

Cubody commented Nov 21, 2023

@peaceshi
Have you made Avalonia single file native aot to work with static ANGLE? It doesn't work for me. I have a black background instead of transparent - the Avalonia UI uses software rendering.
I made <NativeLibrary Include="Native\av_libglesv2.lib" /> in csproj
To undestand if it works or no you can add options to Build app:

BuildAvaloniaApp()
                .With(new Win32PlatformOptions
                    {RenderingMode = new[] {Win32RenderingMode.AngleEgl}})
                .StartWithClassicDesktopLifetime(args);

If it'll not find ANGLE lib it'll crash.
Also Avalonia uses it's own fork of ANGLE with some fixes.

You can try to build this for yourself. https://github.com/AvaloniaUI/angle/tree/avalonia-fork-master

Btw have you made Avalonia single file native aot to work with this? I already built but it doesn't help. Thats why Katz says that it's main problem to build angle as static library with all needable dependencies.

Emm, what is the all needable dependencies? Can you explain it further?

I mean if I'll build just angle it'll not work because .dll itself uses many functions from other libs that angle bulk builds.

@peaceshi
Copy link

@peaceshi
Have you made Avalonia single file native aot to work with static ANGLE? It doesn't work for me. I have a black background instead of transparent - the Avalonia UI uses software rendering.
I made <NativeLibrary Include="Native\av_libglesv2.lib" /> in csproj
To undestand if it works or no you can add options to Build app:

BuildAvaloniaApp()
                .With(new Win32PlatformOptions
                    {RenderingMode = new[] {Win32RenderingMode.AngleEgl}})
                .StartWithClassicDesktopLifetime(args);

If it'll not find ANGLE lib it'll crash.
Also Avalonia uses it's own fork of ANGLE with some fixes.

You can try to build this for yourself. https://github.com/AvaloniaUI/angle/tree/avalonia-fork-master

Btw have you made Avalonia single file native aot to work with this? I already built but it doesn't help. Thats why Katz says that it's main problem to build angle as static library with all needable dependencies.

Emm, what is the all needable dependencies? Can you explain it further?

I mean if I'll build just angle it'll not work because .dll itself uses many functions from other libs that angle bulk builds.

No. I think it is just a static lib bug of ANGLE.

@Cubody
Copy link

Cubody commented Nov 21, 2023

@peaceshi
Have you made Avalonia single file native aot to work with static ANGLE? It doesn't work for me. I have a black background instead of transparent - the Avalonia UI uses software rendering.
I made <NativeLibrary Include="Native\av_libglesv2.lib" /> in csproj
To undestand if it works or no you can add options to Build app:

BuildAvaloniaApp()
                .With(new Win32PlatformOptions
                    {RenderingMode = new[] {Win32RenderingMode.AngleEgl}})
                .StartWithClassicDesktopLifetime(args);

If it'll not find ANGLE lib it'll crash.
Also Avalonia uses it's own fork of ANGLE with some fixes.

You can try to build this for yourself. https://github.com/AvaloniaUI/angle/tree/avalonia-fork-master

Btw have you made Avalonia single file native aot to work with this? I already built but it doesn't help. Thats why Katz says that it's main problem to build angle as static library with all needable dependencies.

Emm, what is the all needable dependencies? Can you explain it further?

I mean if I'll build just angle it'll not work because .dll itself uses many functions from other libs that angle bulk builds.

No. I think it is just a static lib bug of ANGLE.

Here is old thread about static lib
https://groups.google.com/g/angleproject/c/xKmUgKZFpgY

@peaceshi
Copy link

@peaceshi
Have you made Avalonia single file native aot to work with static ANGLE? It doesn't work for me. I have a black background instead of transparent - the Avalonia UI uses software rendering.
I made <NativeLibrary Include="Native\av_libglesv2.lib" /> in csproj
To undestand if it works or no you can add options to Build app:

BuildAvaloniaApp()
                .With(new Win32PlatformOptions
                    {RenderingMode = new[] {Win32RenderingMode.AngleEgl}})
                .StartWithClassicDesktopLifetime(args);

If it'll not find ANGLE lib it'll crash.
Also Avalonia uses it's own fork of ANGLE with some fixes.

You can try to build this for yourself. https://github.com/AvaloniaUI/angle/tree/avalonia-fork-master

Btw have you made Avalonia single file native aot to work with this? I already built but it doesn't help. Thats why Katz says that it's main problem to build angle as static library with all needable dependencies.

Emm, what is the all needable dependencies? Can you explain it further?

I mean if I'll build just angle it'll not work because .dll itself uses many functions from other libs that angle bulk builds.

No. I think it is just a static lib bug of ANGLE.

Here is old thread about static lib https://groups.google.com/g/angleproject/c/xKmUgKZFpgY

I have linked all missing symbols. It is working now.
Download av_libglesv2.7z from https://github.com/2ndlab/ANGLE.Static/releases

@Cubody
Copy link

Cubody commented Nov 22, 2023

@peaceshi
Have you made Avalonia single file native aot to work with static ANGLE? It doesn't work for me. I have a black background instead of transparent - the Avalonia UI uses software rendering.
I made <NativeLibrary Include="Native\av_libglesv2.lib" /> in csproj
To undestand if it works or no you can add options to Build app:

BuildAvaloniaApp()
                .With(new Win32PlatformOptions
                    {RenderingMode = new[] {Win32RenderingMode.AngleEgl}})
                .StartWithClassicDesktopLifetime(args);

If it'll not find ANGLE lib it'll crash.
Also Avalonia uses it's own fork of ANGLE with some fixes.

You can try to build this for yourself. https://github.com/AvaloniaUI/angle/tree/avalonia-fork-master

Btw have you made Avalonia single file native aot to work with this? I already built but it doesn't help. Thats why Katz says that it's main problem to build angle as static library with all needable dependencies.

Emm, what is the all needable dependencies? Can you explain it further?

I mean if I'll build just angle it'll not work because .dll itself uses many functions from other libs that angle bulk builds.

No. I think it is just a static lib bug of ANGLE.

Here is old thread about static lib https://groups.google.com/g/angleproject/c/xKmUgKZFpgY

I have linked all missing symbols. It is working now. Download av_libglesv2.7z from https://github.com/2ndlab/ANGLE.Static/releases

Nice work, appreciate it! Can you add these instructions to pipeline? It'll be easier for Avalonia to make its own Nuget.

Also I want to ask, is here any way to make my application size smaller? (I'm using some compilation flags) Now it's 70mb.

 <PropertyGroup>
        <IlcOptimizationPreference>Size</IlcOptimizationPreference>
        <AutoreleasePoolSupport>false</AutoreleasePoolSupport>
        <EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization>
        <EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding>
        <EventSourceSupport>false</EventSourceSupport>
        <HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
        <MetadataUpdaterSupport>false</MetadataUpdaterSupport>
        <UseSystemResourceKeys>true</UseSystemResourceKeys>
        <IlcGenerateStackTraceData>false</IlcGenerateStackTraceData>
        <IlcTrimMetadata>true</IlcTrimMetadata>
        <!-- <IlcDisableReflection>true</IlcDisableReflection> -->
        <IlcFoldIdenticalMethodBodies>true</IlcFoldIdenticalMethodBodies>
    </PropertyGroup>

    <PropertyGroup>
        <!--These can help when debugging weird exceptions especially when reflection is involved. See https://github.com/dotnet/corert/blob/master/Documentation/using-corert/optimizing-corert.md -->
        <!--RootAllApplicationAssemblies: False -> TrimMode:link See https://github.com/dotnet/runtimelab/issues/597 and https://github.com/dotnet/runtimelab/blob/feature/NativeAOT/docs/using-nativeaot/optimizing.md -->
        <IlcGenerateCompleteTypeMetadata>false</IlcGenerateCompleteTypeMetadata>
        <IlcDisableUnhandledExceptionExperience>true</IlcDisableUnhandledExceptionExperience>
    </PropertyGroup>

@peaceshi
Copy link

@peaceshi
Have you made Avalonia single file native aot to work with static ANGLE? It doesn't work for me. I have a black background instead of transparent - the Avalonia UI uses software rendering.
I made <NativeLibrary Include="Native\av_libglesv2.lib" /> in csproj
To undestand if it works or no you can add options to Build app:

BuildAvaloniaApp()
                .With(new Win32PlatformOptions
                    {RenderingMode = new[] {Win32RenderingMode.AngleEgl}})
                .StartWithClassicDesktopLifetime(args);

If it'll not find ANGLE lib it'll crash.
Also Avalonia uses it's own fork of ANGLE with some fixes.

You can try to build this for yourself. https://github.com/AvaloniaUI/angle/tree/avalonia-fork-master

Btw have you made Avalonia single file native aot to work with this? I already built but it doesn't help. Thats why Katz says that it's main problem to build angle as static library with all needable dependencies.

Emm, what is the all needable dependencies? Can you explain it further?

I mean if I'll build just angle it'll not work because .dll itself uses many functions from other libs that angle bulk builds.

No. I think it is just a static lib bug of ANGLE.

Here is old thread about static lib https://groups.google.com/g/angleproject/c/xKmUgKZFpgY

I have linked all missing symbols. It is working now. Download av_libglesv2.7z from https://github.com/2ndlab/ANGLE.Static/releases

Nice work, appreciate it! Can you add these instructions to pipeline? It'll be easier for Avalonia to make its own Nuget.

Also I want to ask, is here any way to make my application size smaller? (I'm using some compilation flags) Now it's 70mb.

 <PropertyGroup>
        <IlcOptimizationPreference>Size</IlcOptimizationPreference>
        <AutoreleasePoolSupport>false</AutoreleasePoolSupport>
        <EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization>
        <EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding>
        <EventSourceSupport>false</EventSourceSupport>
        <HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
        <MetadataUpdaterSupport>false</MetadataUpdaterSupport>
        <UseSystemResourceKeys>true</UseSystemResourceKeys>
        <IlcGenerateStackTraceData>false</IlcGenerateStackTraceData>
        <IlcTrimMetadata>true</IlcTrimMetadata>
        <!-- <IlcDisableReflection>true</IlcDisableReflection> -->
        <IlcFoldIdenticalMethodBodies>true</IlcFoldIdenticalMethodBodies>
    </PropertyGroup>

    <PropertyGroup>
        <!--These can help when debugging weird exceptions especially when reflection is involved. See https://github.com/dotnet/corert/blob/master/Documentation/using-corert/optimizing-corert.md -->
        <!--RootAllApplicationAssemblies: False -> TrimMode:link See https://github.com/dotnet/runtimelab/issues/597 and https://github.com/dotnet/runtimelab/blob/feature/NativeAOT/docs/using-nativeaot/optimizing.md -->
        <IlcGenerateCompleteTypeMetadata>false</IlcGenerateCompleteTypeMetadata>
        <IlcDisableUnhandledExceptionExperience>true</IlcDisableUnhandledExceptionExperience>
    </PropertyGroup>

I will add these files to actions later.
About the file size, you can use upx or sfx to compress it. But for the build trimming, all we can do is wait for .NET team creating a better trimming or other optimizations.

@peaceshi
Copy link

https://github.com/2ndlab/ANGLE.Static/releases/tag/v1.1.0

Everything works well now.

@NCLnclNCL
Copy link

Good

@amakvana
Copy link

amakvana commented Feb 1, 2024

@ivanjx sorry to piggyback off this. I've download all 3 .libs: av_libglesv2.lib, libHarfBuzzSharp.lib and libSkiaSharp.lib. Imported them into my project and referenced them inside the .csproj. However, when Publishing, the 3 .dll files still get copied over.

Any idea what I am doing wrong? Cheers

@maxkatz6
Copy link
Member

maxkatz6 commented Feb 1, 2024

If libs were successfully statically linked into your executable, you can safely delete remaining dll files.

@ALIENQuake
Copy link

@peaceshi I'm assuming that all ibs must be compiled at the specific platform to have the same outcome. Can you add Linux/macOS versions of those libs?

@peaceshi
Copy link

peaceshi commented Mar 4, 2024

@peaceshi I'm assuming that all ibs must be compiled at the specific platform to have the same outcome. Can you add Linux/macOS versions of those libs?

From #8130 (reply in thread).
Never. You can not static link glibc.
I can not imagine how many files you need to rewrite if you build with musl. 😆

@ALIENQuake
Copy link

@Cubody

@peaceshi
Also Avalonia uses it's own fork of ANGLE with some fixes.

For Avalonia apps, how feasible is it to use peaceshi libs that come from the official repository instead of Avalonia ANGLE fork? If the official repo doesn't have Avalonia fixes I'm assuming it could be a problem.

@peaceshi
Copy link

peaceshi commented Mar 5, 2024

@Cubody

@peaceshi
Also Avalonia uses it's own fork of ANGLE with some fixes.

For Avalonia apps, how feasible is it to use peaceshi libs that come from the official repository instead of Avalonia ANGLE fork? If the official repo doesn't have Avalonia fixes I'm assuming it could be a problem.

you can patch it manually by yourself just be like vcpkg done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants