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

[release/8.0-staging] Change assembler to clang in android MonoAOT #111666

Merged
Merged
4 changes: 3 additions & 1 deletion src/mono/mono.proj
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,9 @@

<!-- Linux specific options -->
<ItemGroup Condition="'$(AotHostOS)' == 'linux'">
<_LibClang Include="$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/$(MonoToolchainPrebuiltOS)/lib64/libclang.so.*"/>
<_LibClang Include="$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/$(MonoToolchainPrebuiltOS)/lib/libclang.so" Condition=" Exists('$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/$(MonoToolchainPrebuiltOS)/lib/libclang.so') "/>
<_LibClang Include="$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/$(MonoToolchainPrebuiltOS)/lib64/libclang.so.*" Condition=" '@(_LibClang)' == '' "/>
<_LibClang Include="/usr/local/lib/libclang.so" Condition="'@(_LibClang)' == ''" />
</ItemGroup>
<PropertyGroup Condition="'$(TargetsLinux)' == 'true' and '$(Platform)' == 'arm64'">
<MonoUseCrossTool>true</MonoUseCrossTool>
Expand Down
36 changes: 31 additions & 5 deletions src/mono/mono/mini/aot-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,11 @@ typedef struct MonoAotOptions {
gboolean allow_errors;
char *tool_prefix;
char *as_prefix;
char *as_name;
char *as_options;
char *ld_flags;
char *ld_name;
char *ld_options;
char *mtriple;
char *llvm_path;
char *temp_path;
Expand Down Expand Up @@ -8866,10 +8869,16 @@ mono_aot_parse_options (const char *aot_options, MonoAotOptions *opts)
opts->tool_prefix = g_strdup (arg + strlen ("tool-prefix="));
} else if (str_begins_with (arg, "as-prefix=")) {
opts->as_prefix = g_strdup (arg + strlen ("as-prefix="));
} else if (str_begins_with (arg, "as-name=")) {
opts->as_name = g_strdup (arg + strlen ("as-name="));
} else if (str_begins_with (arg, "as-options=")) {
opts->as_options = g_strdup (arg + strlen ("as-options="));
} else if (str_begins_with (arg, "ld-flags=")) {
opts->ld_flags = g_strdup (arg + strlen ("ld-flags="));
} else if (str_begins_with (arg, "ld-name=")) {
opts->ld_name = g_strdup (arg + strlen ("ld-name="));
} else if (str_begins_with (arg, "ld-options=")) {
opts->ld_options = g_strdup (arg + strlen ("ld-options="));
} else if (str_begins_with (arg, "soft-debug")) {
opts->soft_debug = TRUE;
// Intentionally undocumented x2-- deprecated
Expand Down Expand Up @@ -13275,7 +13284,16 @@ compile_asm (MonoAotCompile *acfg)
g_string_append (acfg->as_args, "-c -x assembler ");
#endif

command = g_strdup_printf ("\"%s%s\" %s %s -o %s %s", as_prefix, AS_NAME, AS_OPTIONS,
const char *as_binary_name = acfg->aot_opts.as_name;
if (as_binary_name == NULL) {
as_binary_name = AS_NAME;
}
const char *as_options = acfg->aot_opts.as_options;
if (as_options == NULL) {
as_options = AS_OPTIONS;
}

command = g_strdup_printf ("\"%s%s\" %s %s -o %s %s", as_prefix, as_binary_name, as_options,
acfg->as_args ? acfg->as_args->str : "",
wrap_path (objfile), wrap_path (acfg->tmpfname));
aot_printf (acfg, "Executing the native assembler: %s\n", command);
Expand All @@ -13286,7 +13304,7 @@ compile_asm (MonoAotCompile *acfg)
}

if (acfg->llvm && !acfg->llvm_owriter) {
command = g_strdup_printf ("\"%s%s\" %s %s -o %s %s", as_prefix, AS_NAME, AS_OPTIONS,
command = g_strdup_printf ("\"%s%s\" %s %s -o %s %s", as_prefix, as_binary_name, as_options,
acfg->as_args ? acfg->as_args->str : "",
wrap_path (acfg->llvm_ofile), wrap_path (acfg->llvm_sfile));
aot_printf (acfg, "Executing the native assembler: %s\n", command);
Expand Down Expand Up @@ -13335,16 +13353,21 @@ compile_asm (MonoAotCompile *acfg)

str = g_string_new ("");
const char *ld_binary_name = acfg->aot_opts.ld_name;

const char *ld_options = acfg->aot_opts.ld_options;
if (ld_options == NULL) {
ld_options = LD_OPTIONS;
}
#if defined(LD_NAME)
if (ld_binary_name == NULL) {
ld_binary_name = LD_NAME;
}
if (acfg->aot_opts.tool_prefix)
g_string_append_printf (str, "\"%s%s\" %s", tool_prefix, ld_binary_name, LD_OPTIONS);
g_string_append_printf (str, "\"%s%s\" %s", tool_prefix, ld_binary_name, ld_options);
else if (acfg->aot_opts.llvm_only)
g_string_append_printf (str, "%s", acfg->aot_opts.clangxx);
else
g_string_append_printf (str, "\"%s%s\" %s", tool_prefix, ld_binary_name, LD_OPTIONS);
g_string_append_printf (str, "\"%s%s\" %s", tool_prefix, ld_binary_name, ld_options);
#else
if (ld_binary_name == NULL) {
ld_binary_name = "ld";
Expand All @@ -13353,7 +13376,7 @@ compile_asm (MonoAotCompile *acfg)
// Default (linux)
if (acfg->aot_opts.tool_prefix)
/* Cross compiling */
g_string_append_printf (str, "\"%s%s\" %s", tool_prefix, ld_binary_name, LD_OPTIONS);
g_string_append_printf (str, "\"%s%s\" %s", tool_prefix, ld_binary_name, ld_options);
else if (acfg->aot_opts.llvm_only)
g_string_append_printf (str, "%s", acfg->aot_opts.clangxx);
else
Expand Down Expand Up @@ -14284,8 +14307,11 @@ aot_opts_free (MonoAotOptions *aot_opts)
g_free (aot_opts->dedup_include);
g_free (aot_opts->tool_prefix);
g_free (aot_opts->as_prefix);
g_free (aot_opts->as_name);
g_free (aot_opts->as_options);
g_free (aot_opts->ld_flags);
g_free (aot_opts->ld_name);
g_free (aot_opts->ld_options);
g_free (aot_opts->mtriple);
g_free (aot_opts->llvm_path);
g_free (aot_opts->temp_path);
Expand Down
39 changes: 19 additions & 20 deletions src/mono/msbuild/android/build/AndroidBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,21 @@
<_AotOutputType>ObjectFile</_AotOutputType>
</PropertyGroup>

<ItemGroup>
<MonoAOTCompilerDefaultAotArguments Condition="'$(TargetArchitecture)' == 'arm'" Include="mtriple=armv7-linux-gnueabi" />
<MonoAOTCompilerDefaultAotArguments Condition="'$(TargetArchitecture)' == 'arm64'" Include="mtriple=aarch64-linux-android" />
<MonoAOTCompilerDefaultAotArguments Condition="'$(TargetArchitecture)' == 'x86'" Include="mtriple=i686-linux-android" />
<MonoAOTCompilerDefaultAotArguments Condition="'$(TargetArchitecture)' == 'x64'" Include="mtriple=x86_64-linux-android" />
<PropertyGroup>
<_Triple Condition="'$(TargetArchitecture)' == 'arm'">armv7-linux-gnueabi</_Triple>
<_Triple Condition="'$(TargetArchitecture)' == 'arm64'">aarch64-linux-android</_Triple>
<_Triple Condition="'$(TargetArchitecture)' == 'x86'">i686-linux-android</_Triple>
<_Triple Condition="'$(TargetArchitecture)' == 'x64'">x86_64-linux-android</_Triple>
</PropertyGroup>

<PropertyGroup>
<_AsOptions>-target $(_Triple) -c -x assembler</_AsOptions>
<_LdName>clang</_LdName>
<_LdOptions>-fuse-ld=lld</_LdOptions>
<_AsName>clang</_AsName>
</PropertyGroup>

<ItemGroup>
<MonoAOTCompilerDefaultAotArguments Include="static" />
<MonoAOTCompilerDefaultAotArguments Include="dwarfdebug" />
<MonoAOTCompilerDefaultAotArguments Condition="'$(_IsLibraryMode)' == 'true'" Include="direct-icalls" />
Expand All @@ -154,19 +163,6 @@
<AndroidLibraryMinApiLevel Condition="'$(AndroidLibraryMinApiLevel)' == ''">21</AndroidLibraryMinApiLevel>
</PropertyGroup>

<NdkToolFinderTask
Condition="'$(AOTWithLibraryFiles)' == 'true' or '$(_IsLibraryMode)' == 'true'"
Architecture="$(TargetArchitecture)"
HostOS="$(_HostOS)"
MinApiLevel="$(AndroidLibraryMinApiLevel)">
<Output TaskParameter="AsPrefixPath" PropertyName="_AsPrefixPath" />
<Output TaskParameter="ToolPrefixPath" PropertyName="_ToolPrefixPath" />
<Output TaskParameter="Triple" PropertyName="_Triple" />
<Output TaskParameter="LdName" PropertyName="_LdName" />
<Output TaskParameter="LdPath" PropertyName="_LdPath" />
<Output TaskParameter="ClangPath" PropertyName="_ClangPath" />
</NdkToolFinderTask>

<PropertyGroup Condition="'$(AOTWithLibraryFiles)' == 'true' or '$(_IsLibraryMode)' == 'true'">
<_AsPrefixPath>$([MSBuild]::EnsureTrailingSlash('$(_AsPrefixPath)'))</_AsPrefixPath>
<_ToolPrefixPath>$([MSBuild]::EnsureTrailingSlash('$(_ToolPrefixPath)'))</_ToolPrefixPath>
Expand Down Expand Up @@ -234,20 +230,23 @@

<MonoAOTCompiler
AotModulesTablePath="$(_AotModuleTablePath)"
AsPrefix="$(_AsPrefixPath)"
AsName="$(_AsName)"
AsOptions="$(_AsOptions)"
Assemblies="@(_AotInputAssemblies)"
CompilerBinaryPath="$(_CompilerBinaryPath)"
DirectPInvokes="@(DirectPInvokes)"
DirectPInvokeLists="@(DirectPInvokeLists)"
EnableUnmanagedCallersOnlyMethodsExport="$(_EnableUnmanagedCallersOnlyMethodsExport)"
IntermediateOutputPath="$(_MobileIntermediateOutputPath)"
LdName="$(_LdName)"
LdOptions="$(_LdOptions)"
LibraryFormat="$(_AotLibraryFormat)"
LLVMPath="$(_MonoLLVMPath)"
MibcProfilePath="@(ProfiledAOTProfilePaths)"
Mode="$(_AOTMode)"
OutputDir="$(_MobileIntermediateOutputPath)"
OutputType="$(_AotOutputType)"
ToolPrefix="$(_ToolPrefixPath)"
Triple="$(_Triple)"
UseAotDataFile="$(_UseAotDataFile)"
UseLLVM="$(MonoEnableLLVM)">
<Output TaskParameter="CompiledAssemblies" ItemName="_AssembliesToBundleInternal" />
Expand Down
34 changes: 19 additions & 15 deletions src/mono/sample/Android/AndroidSampleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,36 +70,40 @@
<AndroidLibraryMinApiLevel Condition="'$(AndroidLibraryMinApiLevel)' == ''">21</AndroidLibraryMinApiLevel>
</PropertyGroup>

<NdkToolFinderTask
Condition="'$(AOTWithLibraryFiles)' == 'true'"
Architecture="$(TargetArchitecture)"
HostOS="$(_HostOS)"
MinApiLevel="$(AndroidLibraryMinApiLevel)">
<Output TaskParameter="AsPrefixPath" PropertyName="_AsPrefixPath" />
<Output TaskParameter="ToolPrefixPath" PropertyName="_ToolPrefixPath" />
<Output TaskParameter="Triple" PropertyName="_Triple" />
<Output TaskParameter="LdName" PropertyName="_LdName" />
<Output TaskParameter="LdPath" PropertyName="_LdPath" />
<Output TaskParameter="ClangPath" PropertyName="_ClangPath" />
</NdkToolFinderTask>

<PropertyGroup Condition="'$(AOTWithLibraryFiles)' == 'true'">
<_AsPrefixPath>$([MSBuild]::EnsureTrailingSlash('$(_AsPrefixPath)'))</_AsPrefixPath>
<_ToolPrefixPath>$([MSBuild]::EnsureTrailingSlash('$(_ToolPrefixPath)'))</_ToolPrefixPath>
</PropertyGroup>

<PropertyGroup>
<_Triple Condition="'$(TargetArchitecture)' == 'arm'">armv7-linux-gnueabi</_Triple>
<_Triple Condition="'$(TargetArchitecture)' == 'arm64'">aarch64-linux-android</_Triple>
<_Triple Condition="'$(TargetArchitecture)' == 'x86'">i686-linux-android</_Triple>
<_Triple Condition="'$(TargetArchitecture)' == 'x64'">x86_64-linux-android</_Triple>
</PropertyGroup>

<PropertyGroup>
<_AsOptions>-target $(_Triple) -c -x assembler</_AsOptions>
<_LdName>clang</_LdName>
<_LdOptions>-fuse-ld=lld</_LdOptions>
<_AsName>clang</_AsName>
</PropertyGroup>

<MonoAOTCompiler Condition="'$(ForceAOT)' == 'true'"
AotModulesTablePath="$(_AotModulesTablePath)"
AsPrefix="$(_AsPrefixPath)"
AsName="$(_AsName)"
AsOptions="$(_AsOptions)"
Assemblies="@(AotInputAssemblies)"
CompilerBinaryPath="@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier','$(TargetOS)-$(TargetArchitecture.ToLowerInvariant())'))"
IntermediateOutputPath="$(IntermediateOutputPath)"
LdName="$(_LdName)"
LdOptions="$(_LdOptions)"
LibraryFormat="$(_AotLibraryFormat)"
LLVMPath="$(MonoAotCrossDir)"
Mode="$(_AotMode)"
OutputDir="$(_MobileIntermediateOutputPath)"
OutputType="$(_AotOutputType)"
ToolPrefix="$(_ToolPrefixPath)"
Triple="$(_Triple)"
UseAotDataFile="false"
UseLLVM="$(UseLLVM)">
<Output TaskParameter="CompiledAssemblies" ItemName="BundleAssemblies" />
Expand Down
23 changes: 16 additions & 7 deletions src/native/libs/System.Native/pal_interfaceaddresses.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static inline uint8_t mask2prefix(uint8_t* mask, int length)
static int (*getifaddrs)(struct ifaddrs**) = NULL;
static void (*freeifaddrs)(struct ifaddrs*) = NULL;

static void try_loading_getifaddrs()
static void try_loading_getifaddrs(void)
{
if (android_get_device_api_level() >= 24)
{
Expand All @@ -139,7 +139,7 @@ static void try_loading_getifaddrs()
}
}

static bool ensure_getifaddrs_is_loaded()
static bool ensure_getifaddrs_is_loaded(void)
{
static pthread_once_t getifaddrs_is_loaded = PTHREAD_ONCE_INIT;
pthread_once(&getifaddrs_is_loaded, try_loading_getifaddrs);
Expand Down Expand Up @@ -169,11 +169,12 @@ int32_t SystemNative_EnumerateInterfaceAddresses(void* context,

for (struct ifaddrs* current = headAddr; current != NULL; current = current->ifa_next)
{
if (current->ifa_addr == NULL)
char *ifa_name = current->ifa_name;
if (current->ifa_addr == NULL || ifa_name == NULL)
{
continue;
}
uint32_t interfaceIndex = if_nametoindex(current->ifa_name);
uint32_t interfaceIndex = if_nametoindex(ifa_name);
// ifa_name may be an aliased interface name.
// Use if_indextoname to map back to the true device name.
char actualName[IF_NAMESIZE];
Expand Down Expand Up @@ -376,9 +377,17 @@ int32_t SystemNative_GetNetworkInterfaces(int32_t * interfaceCount, NetworkInter

while (ifaddrsEntry != NULL)
{
char *ifa_name = ifaddrsEntry->ifa_name;

if (ifa_name == NULL)
{
ifaddrsEntry = ifaddrsEntry->ifa_next;
continue;
}

//current = NULL;
nii = NULL;
uint ifindex = if_nametoindex(ifaddrsEntry->ifa_name);
uint ifindex = if_nametoindex(ifa_name);
for (index = 0; index < (int)ifcount; index ++)
{
if (((NetworkInterfaceInfo*)memoryBlock)[index].InterfaceIndex == ifindex)
Expand All @@ -393,8 +402,8 @@ int32_t SystemNative_GetNetworkInterfaces(int32_t * interfaceCount, NetworkInter
// We git new interface.
nii = &((NetworkInterfaceInfo*)memoryBlock)[ifcount++];

memcpy(nii->Name, ifaddrsEntry->ifa_name, sizeof(nii->Name));
nii->InterfaceIndex = if_nametoindex(ifaddrsEntry->ifa_name);
memcpy(nii->Name, ifa_name, sizeof(nii->Name));
nii->InterfaceIndex = ifindex;
nii->Speed = -1;
nii->HardwareType = ((ifaddrsEntry->ifa_flags & IFF_LOOPBACK) == IFF_LOOPBACK) ? NetworkInterfaceType_Loopback : NetworkInterfaceType_Unknown;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef struct CipherInfo
} CipherInfo;

#define DEFINE_CIPHER(cipherId, width, javaName, flags) \
CipherInfo* AndroidCryptoNative_ ## cipherId() \
CipherInfo* AndroidCryptoNative_ ## cipherId(void) \
{ \
static CipherInfo info = { flags, width, javaName }; \
return &info; \
Expand Down
32 changes: 31 additions & 1 deletion src/tasks/AotCompilerTask/MonoAOTCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
Expand Down Expand Up @@ -231,6 +231,16 @@ public class MonoAOTCompiler : Microsoft.Build.Utilities.Task
/// </summary>
public string? ToolPrefix { get; set; }

/// <summary>
/// Name of the assembler tool ran by the AOT compiler.
/// </summary>
public string? AsName { get; set; }

/// <summary>
/// Passes as-options to the AOT compiler
/// </summary>
public string? AsOptions { get; set; }

/// <summary>
/// Prepends a prefix to the name of the assembler (as) tool ran by the AOT compiler.
/// </summary>
Expand Down Expand Up @@ -278,6 +288,11 @@ public class MonoAOTCompiler : Microsoft.Build.Utilities.Task
/// </summary>
public string? LdFlags { get; set; }

/// <summary>
/// Passes ld-options to the AOT compiler
/// </summary>
public string? LdOptions { get; set; }

/// <summary>
/// Specify WorkingDirectory for the AOT compiler
/// </summary>
Expand Down Expand Up @@ -728,6 +743,16 @@ private PrecompileArguments GetPrecompileArgumentsFor(ITaskItem assemblyItem, st
aotArgs.Add($"tool-prefix={ToolPrefix}");
}

if (!string.IsNullOrEmpty(AsName))
{
aotArgs.Add($"as-name={AsName}");
}

if (!string.IsNullOrEmpty(AsOptions))
{
aotArgs.Add($"as-options={AsOptions}");
}

if (!string.IsNullOrEmpty(AsPrefix))
{
aotArgs.Add($"as-prefix={AsPrefix}");
Expand Down Expand Up @@ -943,6 +968,11 @@ private PrecompileArguments GetPrecompileArgumentsFor(ITaskItem assemblyItem, st
aotArgs.Add($"ld-flags={LdFlags}");
}

if (!string.IsNullOrEmpty(LdOptions))
{
aotArgs.Add($"ld-options={LdOptions}");
}

// we need to quote the entire --aot arguments here to make sure it is parsed
// on Windows as one argument. Otherwise it will be split up into multiple
// values, which wont work.
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/MobileBuildTasks/Android/Ndk/NdkTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ public string ClangPath

private void ValidateRequiredProps(string hostOS)
{
if (Ndk.NdkVersion.Main.Major != 23)
if (Ndk.NdkVersion.Main.Major != 27)
{
throw new Exception($"NDK 23 is required. An unsupported NDK version was found ({Ndk.NdkVersion.Main.Major}).");
throw new Exception($"NDK 27 is required. An unsupported NDK version was found ({Ndk.NdkVersion.Main.Major}).");
}

try
Expand Down
Loading