Skip to content

Commit

Permalink
Fix macOS host + linux-bionic-arm64 target cross-compile build (#9)
Browse files Browse the repository at this point in the history
Context: dotnet/runtime#101727

Previously, trying to use `dotnet publish` from a macOS host would
result in obscure build failures, e.g.

	% dotnet publish -c Release
	…
	clang : error : invalid linker name in argument '-fuse-ld=lld'

Apply the workaround from dotnet/runtime#101727 to workaround this.
The `$(ANDROID_NDK_HOME)` environment variable requuired:

	# we're all xamarin-android devs here, right? ;-)
	% export ANDROID_NDK_HOME=$HOME/android-toolchain/ndk
	% dotnet publish -c Release
	# Creates DotNet/bin/Release/net8.0/linux-bionic-arm64/publish/libdotnet.so

* Set `$ANDROID_NDK_HOME` from `devcontainer.json`

Co-authored-by: Jonathan Peppers <[email protected]>
  • Loading branch information
jonpryor and jonathanpeppers authored Apr 30, 2024
1 parent 0e9b75b commit a2fd74c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"postCreateCommand": "${containerWorkspaceFolder}/.devcontainer/postCreateCommand.sh",

"remoteEnv": {
"PATH": "${containerEnv:ANDROID_HOME}/ndk/23.2.8568313/toolchains/llvm/prebuilt/linux-x86_64/bin/:${containerEnv:PATH}"
"ANDROID_NDK_HOME": "${containerEnv:ANDROID_HOME}/ndk/23.2.8568313"
},
"customizations": {
"vscode": {
Expand Down
2 changes: 2 additions & 0 deletions DotNet/libdotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@
<Copy SourceFiles="@(_FilesToCopy)" DestinationFolder="$(_NativeFolder)" />
</Target>

<Import Project="libdotnet.targets" />

</Project>
35 changes: 35 additions & 0 deletions DotNet/libdotnet.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project>
<PropertyGroup>
<_NdkSysrootAbi Condition=" '$(RuntimeIdentifier)' == 'linux-bionic-arm64' ">aarch64-linux-android</_NdkSysrootAbi>
<_NdkClangPrefix Condition=" '$(RuntimeIdentifier)' == 'linux-bionic-arm64' ">aarch64-linux-android21-</_NdkClangPrefix>
<_NdkPrebuiltAbi Condition=" '$(NETCoreSdkRuntimeIdentifier)' == 'osx-x64' ">darwin-x86_64</_NdkPrebuiltAbi>
<_NdkPrebuiltAbi Condition=" '$(NETCoreSdkRuntimeIdentifier)' == 'linux-x64' ">linux-x86_64</_NdkPrebuiltAbi>
<_NdkSysrootDir>$(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/sysroot/usr/lib/$(_NdkSysrootAbi)</_NdkSysrootDir>
<_NdkBinDir>$(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/bin</_NdkBinDir>
</PropertyGroup>

<PropertyGroup Condition="$(RuntimeIdentifier.StartsWith('linux-bionic'))">
<LinkStandardCPlusPlusLibrary>true</LinkStandardCPlusPlusLibrary>
<CppCompilerAndLinker>$(_NdkBinDir)/$(_NdkClangPrefix)clang++</CppCompilerAndLinker>
<ObjCopyName>$(_NdkBinDir)/llvm-objcopy</ObjCopyName>
</PropertyGroup>

<ItemGroup Condition="$(RuntimeIdentifier.StartsWith('linux-bionic'))">
<LinkerArg Include="-Wl,--defsym,_init=__libc_init" />
<LinkerArg Include="-Wl,--defsym,_fini=__libc_fini" />
<LinkerArg Include="-L &quot;$(_NdkSysrootDir)&quot;" />
<NativeSystemLibrary Include="c" />
</ItemGroup>

<Target Name="_ValidateEnvironment"
BeforeTargets="Build">
<Error
Condition=" '$(ANDROID_NDK_HOME)' == '' Or !Exists($(ANDROID_NDK_HOME)) "
Text="Set the %24ANDROID_NDK_HOME environment variable to the path of the Android NDK."
/>
<Error
Condition=" !Exists($(_NdkSysrootDir))"
Text="NDK 'sysroot' dir `$(_NdkSysrootDir)` does not exist. You're on your own."
/>
</Target>
</Project>

0 comments on commit a2fd74c

Please sign in to comment.