From 4233b2b6d65b4cf2f66e1b0aceb0e6db3f8ff4dc Mon Sep 17 00:00:00 2001 From: Joseph Moreno <44370115+josephmoresena@users.noreply.github.com> Date: Sun, 19 Jan 2025 01:15:22 -0500 Subject: [PATCH] Fix Windows Build * Create BionicNativeAot.target * Remove sed dependency. * Use batch files only on Windows. --- .gitignore | 3 ++ AndroidHelloJniLib.csproj | 63 +++++----------------------------- BionicNativeAot.targets | 69 ++++++++++++++++++++++++++++++++++++++ README.md | 18 +++++----- android_fake_clang.cmd | 9 ----- android_fake_clang.command | 6 ---- android_fake_clang.sh | 6 ---- 7 files changed, 90 insertions(+), 84 deletions(-) create mode 100644 BionicNativeAot.targets delete mode 100644 android_fake_clang.cmd delete mode 100755 android_fake_clang.command delete mode 100644 android_fake_clang.sh diff --git a/.gitignore b/.gitignore index fc0e395..61a3cf8 100644 --- a/.gitignore +++ b/.gitignore @@ -336,3 +336,6 @@ ASALocalRun/ *.dll *.dwo .DS_Store +/android_clang.cmd +/android_llvm-objcopy.cmd +android_clang.sh diff --git a/AndroidHelloJniLib.csproj b/AndroidHelloJniLib.csproj index 3ea15f8..60a88bb 100644 --- a/AndroidHelloJniLib.csproj +++ b/AndroidHelloJniLib.csproj @@ -15,26 +15,10 @@ false - - - $(RuntimeIdentifier.ToLower().StartsWith('android')) - $(RuntimeIdentifier.ToLower().StartsWith('linux-bionic')) - $([MSBuild]::IsOSPlatform('Windows')) - $([MSBuild]::IsOSPlatform('Linux')) - $([MSBuild]::IsOSPlatform('OSX')) - - - - - - - - - - - - + + + true false @@ -45,44 +29,13 @@ true - - - - ./android_fake_clang.cmd - ./android_fake_clang.sh - ./android_fake_clang.command - windows-x86_64 - linux-x86_64 - darwin-x86_64 - $(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/$(NdkHost)/bin/llvm-objcopy - $(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/$(NdkHost)/sysroot - - 's/global: _init; _fini;/global: /g;' - - - - - - + + + - - - - + + - - - - - - - - @(ExportsLines) - - - - - diff --git a/BionicNativeAot.targets b/BionicNativeAot.targets new file mode 100644 index 0000000..d5aebc3 --- /dev/null +++ b/BionicNativeAot.targets @@ -0,0 +1,69 @@ + + + + + $(RuntimeIdentifier.ToLower().StartsWith('android')) + $(RuntimeIdentifier.ToLower().StartsWith('linux-bionic')) + $([MSBuild]::IsOSPlatform('Windows')) + $([MSBuild]::IsOSPlatform('Linux')) + $([MSBuild]::IsOSPlatform('OSX')) + true + + + + + $(ANDROID_NDK_ROOT)\source.properties + windows-x86_64 + linux-x86_64 + darwin-x86_64 + $(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/$(NdkHost)/sysroot + + $([System.IO.File]::ReadAllText('$(NdkPropertiesFile)')) + $([System.Text.RegularExpressions.Regex]::Match('$(BaseRevision)', 'Pkg.BaseRevision\s*=\s*(\S+)').Groups[1].Value.Split('.')[0]) + 0 + + + + + $(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/$(NdkHost)/bin/clang + $(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/$(NdkHost)/bin/llvm-objcopy + + android_clang.cmd + android_llvm-objcopy.cmd + + + + + + + + + + + + + + $(ANDROID_NDK_ROOT)\toolchains\llvm\prebuilt\$(NdkHost)\bin\ + $(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/$(NdkHost)/bin/ + + + + + + + + + + + + + + + + + + true + + + + diff --git a/README.md b/README.md index 1133ddd..5727c96 100644 --- a/README.md +++ b/README.md @@ -33,16 +33,18 @@ With new .NET 8.0 SDK we are now able to compile NativeAOT android binaries usin ### How to build it -This process was tested for android-arm64 (`linux-bionic-arm64`) but may work for android-x64 (`linux-bionic-x64`) too.
+This process was tested for android-arm64 (`linux-bionic-arm64`) but may work for android-x64 (`linux-bionic-x64`) +too.
The following commands assume: -* **ANDROID_NDK_ROOT** environment variable: Full path to NDK. Used to preconfigure **CppCompilerAndLinker**, - **ObjCopyName** and **SysRoot**. +* .NET project imports [BionicNativeAot.targets](BionicNativeAot.targets) file. +* **ANDROID_NDK_ROOT** environment variable: Full path to NDK. Used to preconfigure **CppCompilerAndLinker**, + **ObjCopyName** and **SysRoot**. * Android NDK version is **r26b**. * Target architecture is **arm64**. * Host architecture is windows, linux or macOS x64. - dotnet publish -r linux-bionic-arm64 -p:DisableUnsupportedError=true -p:PublishAotUsingRuntimePack=true -p:AssemblyName=libhello-jni -p:RemoveSections=true + dotnet publish -r linux-bionic-arm64 -p:DisableUnsupportedError=true -p:PublishAotUsingRuntimePack=true -p:AssemblyName=libhello-jni #### Environment Parameters @@ -50,12 +52,12 @@ The following commands assume: #### MSBuild Parameters -* **CppCompilerAndLinker**: Linker. The android_fake_clang is just a script that invokes the real NDK Clang executable. +* **CppCompilerAndLinker**: Linker. On Windows, NDK executables must be encapsulated in **cmd** files in order to be used. + On Linux and macOS, the executables are used directly. * **SysRoot**: Sysroot path from NDK. Needed for NDK compilation. -* **RemoveSections**: Hack to remove **__init** and **__fini** symbols from .exports file. +* **RemoveSections**: Removes **__init** and **__fini** symbols from .exports file. When it detects that the NDK version + is greater than r26 it is automatically set to true. * **AssemblyName**: In order to produce a .so file with given name. -* **UseLibCSections**: In order to use **__libc_init** and **__libc_fini** as exported **__init** and **__fini** - symbols. ## Considerations diff --git a/android_fake_clang.cmd b/android_fake_clang.cmd deleted file mode 100644 index f03292c..0000000 --- a/android_fake_clang.cmd +++ /dev/null @@ -1,9 +0,0 @@ -@ECHO OFF -SETLOCAL ENABLEDELAYEDEXPANSION - -SET "rawArgs=%*" -IF NOT DEFINED RealCppCompilerAndLinker ( - SET "RealCppCompilerAndLinker=%ANDROID_NDK_ROOT%\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe" -) - -"%RealCppCompilerAndLinker%" !rawArgs! \ No newline at end of file diff --git a/android_fake_clang.command b/android_fake_clang.command deleted file mode 100755 index cb43661..0000000 --- a/android_fake_clang.command +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -rawArgs="$@" -if [[ -z "${RealCppCompilerAndLinker}" ]]; then - RealCppCompilerAndLinker=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -fi -$RealCppCompilerAndLinker $rawArgs \ No newline at end of file diff --git a/android_fake_clang.sh b/android_fake_clang.sh deleted file mode 100644 index 07346ff..0000000 --- a/android_fake_clang.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -rawArgs="$@" -if [[ -z "${RealCppCompilerAndLinker}" ]]; then - RealCppCompilerAndLinker=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -fi -$RealCppCompilerAndLinker $rawArgs \ No newline at end of file