dotnet / runtimelab

This repo is for experimentation and exploring new ideas that may or may not make it into the main dotnet/runtime repo.
MIT License
1.42k stars 199 forks source link

[NativeAOT] [Question] Build for Android Shared/Static Library #1897

Open An0nyMooUS opened 2 years ago

An0nyMooUS commented 2 years ago

I followyed this tutorial:

https://github.com/dotnet/runtimelab/tree/feature/NativeAOT/samples/NativeLibrary

and change -r win-x64 to -r android-arm64. On Windows produces:

"error : Cross-OS native compilation is not supported"

that's ok..., i tryed to use WSL2 + Ubuntu to do this. and produces:


Microsoft (R) Build Engine version 17.2.0+41abc5629 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored /mnt/c/wtf/nativelibrary/NativeLibrary.csproj (in 2.44 sec).
/usr/share/dotnet/sdk/6.0.300/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(1114,5): warning NETSDK1179: One of '--self-contained' or '--no-self-contained' options are required when '--runtime' is used. [/mnt/c/wtf/nativelibrary/NativeLibrary.csproj]
  NativeLibrary -> /mnt/c/wtf/nativelibrary/bin/release/net6.0/android-arm64/NativeLibrary.dll
  Generating compatible native code. To optimize for size or speed, visit https://aka.ms/OptimizeCoreRT
  /usr/bin/ld: unrecognised emulation mode: aarch64linux
  Supported emulations: elf_x86_64 elf32_x86_64 elf_i386 elf_iamcu elf_l1om elf_k1om i386pep i386pe
clang : error : linker command failed with exit code 1 (use -v to see invocation) [/mnt/c/wtf/nativelibrary/NativeLibrary.csproj]
/root/.nuget/packages/microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/build/Microsoft.NETCore.Native.targets(374,5): error MSB3073: The command "clang "obj/release/net6.0/android-arm64/native/NativeLibrary.o" -o "bin/release/net6.0/android-arm64/native/NativeLibrary.so" -Wl,--version-script=obj/release/net6.0/android-arm64/native/NativeLibrary.exports /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/sdk/libbootstrapperdll.a /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/sdk/libRuntime.WorkstationGC.a /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/framework/libSystem.Native.a /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/framework/libSystem.Globalization.Native.a /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/framework/libSystem.IO.Compression.Native.a /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/framework/libSystem.Net.Security.Native.a /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/framework/libSystem.Security.Cryptography.Native.OpenSsl.a --target=aarch64-linux-gnu -g -Wl,-rpath,'$ORIGIN' -Wl,--as-needed -pthread -lstdc++ -ldl -lm -lz -lgssapi_krb5 -lrt -shared -Wl,-z,relro -Wl,-z,now -Wl,--require-defined,CoreRT_StaticInitialization -Wl,--discard-all -Wl,--gc-sections" exited with code 1. [/mnt/c/wtf/nativelibrary/NativeLibrary.csproj]

/usr/bin/ld: unrecognised emulation mode: aarch64linux.

Its possible to build for ARM/ARM64? i successfully build for android-x86/x64. but i can't compile for ARM/ARM 64.

An0nyMooUS commented 2 years ago

I export android NDK clang.

dotnet publish -r android-arm64 -c Release -p:CppCompilerAndLinker=aarch64-linux-android31-clang /p:NativeLib=Shared /p:SelfContained=true

Returns:

clang-12 : error : no input files [/mnt/c/wtf/nativelibrary/NativeLibrary.csproj]
/root/.nuget/packages/microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/build/Microsoft.NETCore.Native.targets(374,5): error MSB3073: The command "aarch64-linux-android31-clang "obj/Release/net6.0/android-arm64/native/NativeLibrary.o" -o "bin/Release/net6.0/android-arm64/native/NativeLibrary.so" -Wl,--version-script=obj/Release/net6.0/android-arm64/native/NativeLibrary.exports /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/sdk/libbootstrapperdll.a /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/sdk/libRuntime.WorkstationGC.a /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/framework/libSystem.Native.a /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/framework/libSystem.Globalization.Native.a /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/framework/libSystem.IO.Compression.Native.a /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/framework/libSystem.Net.Security.Native.a /root/.nuget/packages/runtime.linux-arm64.microsoft.dotnet.ilcompiler/7.0.0-alpha.1.22074.1/framework/libSystem.Security.Cryptography.Native.OpenSsl.a --target=aarch64-linux-gnu -g -Wl,-rpath,'$ORIGIN' -Wl,--as-needed -pthread -lstdc++ -ldl -lm -lz -lgssapi_krb5 -lrt -shared -Wl,-z,relro -Wl,-z,now -Wl,--require-defined,CoreRT_StaticInitialization -Wl,--discard-all -Wl,--gc-sections" exited with code 1. [/mnt/c/wtf/nativelibrary/NativeLibrary.csproj]

Only if p:NativeLib is Shared. I successfully build Static version. (but i need Shared (.so))