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

[android][ios] Add SDK and workload to support library mode #83487

Merged
merged 10 commits into from
Mar 18, 2023
20 changes: 10 additions & 10 deletions src/mono/msbuild/android/build/AndroidBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@
</ItemGroup>

<ItemGroup>
<AssembliesToBundleInternal Remove="@(AssembliesToBundleInternal)" />
<AssembliesToBundleInternal Include="@(AndroidAssembliesToBundle)">
<_AssembliesToBundleInternal Remove="@(_AssembliesToBundleInternal)" />
<_AssembliesToBundleInternal Include="@(AndroidAssembliesToBundle)">
<_InternalForceInterpret>%(AndroidAssembliesToBundle._InternalForceInterpret)</_InternalForceInterpret>
<_IsNative>%(AndroidAssembliesToBundle._IsNative)</_IsNative>
</AssembliesToBundleInternal>
</_AssembliesToBundleInternal>
</ItemGroup>
</Target>

Expand Down Expand Up @@ -127,14 +127,14 @@
</PropertyGroup>

<ItemGroup>
<_AotInputAssemblies Include="@(AssembliesToBundleInternal)"
Condition="'%(AssembliesToBundleInternal._InternalForceInterpret)' != 'true'">
<_AotInputAssemblies Include="@(_AssembliesToBundleInternal)"
Condition="'%(_AssembliesToBundleInternal._InternalForceInterpret)' != 'true'">
<AotArguments>$(AotArguments)</AotArguments>
<ProcessArguments>$(ProcessArguments)</ProcessArguments>
</_AotInputAssemblies>

<_AOT_InternalForceInterpretAssemblies Include="@(AssembliesToBundleInternal->WithMetadataValue('_InternalForceInterpret', 'true'))" />
<AssembliesToBundleInternal Remove="@(AssembliesToBundleInternal)" />
<_AOT_InternalForceInterpretAssemblies Include="@(_AssembliesToBundleInternal->WithMetadataValue('_InternalForceInterpret', 'true'))" />
<_AssembliesToBundleInternal Remove="@(_AssembliesToBundleInternal)" />
</ItemGroup>

<MakeDir Directories="$(_MobileIntermediateOutputPath)" />
Expand Down Expand Up @@ -195,11 +195,11 @@
OutputType="AsmOnly"
UseAotDataFile="$(_UseAotDataFile)"
UseLLVM="$(MonoEnableLLVM)">
<Output TaskParameter="CompiledAssemblies" ItemName="AssembliesToBundleInternal" />
<Output TaskParameter="CompiledAssemblies" ItemName="_AssembliesToBundleInternal" />
</MonoAOTCompiler>

<ItemGroup>
<AssembliesToBundleInternal Include="@(_AOT_InternalForceInterpretAssemblies)" />
<_AssembliesToBundleInternal Include="@(_AOT_InternalForceInterpretAssemblies)" />
</ItemGroup>
</Target>

Expand All @@ -215,7 +215,7 @@

<AndroidAppBuilderTask
AppDir="$(AndroidBuildDir)"
Assemblies="@(AssembliesToBundleInternal)"
Assemblies="@(_AssembliesToBundleInternal)"
DiagnosticPorts="$(DiagnosticPorts)"
EnvironmentVariables="@(AndroidEnv)"
ExtraLinkerArguments="@(ExtraAppLinkerArgs)"
Expand Down
35 changes: 24 additions & 11 deletions src/mono/msbuild/apple/build/AppleBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@
</ItemGroup>

<ItemGroup>
<AssembliesToBundleInternal Remove="@(AssembliesToBundleInternal)" />
<AssembliesToBundleInternal Include="@(AppleAssembliesToBundle)">
<_AssembliesToBundleInternal Remove="@(_AssembliesToBundleInternal)" />
<_AssembliesToBundleInternal Include="@(AppleAssembliesToBundle)">
<_InternalForceInterpret>%(AppleAssembliesToBundle._InternalForceInterpret)</_InternalForceInterpret>
<_IsNative>%(AppleAssembliesToBundle._IsNative)</_IsNative>
</AssembliesToBundleInternal>
</_AssembliesToBundleInternal>
</ItemGroup>
</Target>

Expand Down Expand Up @@ -154,14 +154,14 @@
<ItemGroup>
<_AotExcludeAssemblies Include="*System.Runtime.WindowsRuntime.dll" />

<_AotInputAssemblies Include="@(AssembliesToBundleInternal)"
Condition="'%(AssembliesToBundleInternal._InternalForceInterpret)' != 'true'">
<_AotInputAssemblies Include="@(_AssembliesToBundleInternal)"
Condition="'%(_AssembliesToBundleInternal._InternalForceInterpret)' != 'true'">
<AotArguments>$(AotArguments)</AotArguments>
<ProcessArguments>$(ProcessArguments)</ProcessArguments>
</_AotInputAssemblies>

<_AOT_InternalForceInterpretAssemblies Include="@(AssembliesToBundleInternal->WithMetadataValue('_InternalForceInterpret', 'true'))" />
<AssembliesToBundleInternal Remove="@(AssembliesToBundleInternal)" />
<_AOT_InternalForceInterpretAssemblies Include="@(_AssembliesToBundleInternal->WithMetadataValue('_InternalForceInterpret', 'true'))" />
<_AssembliesToBundleInternal Remove="@(_AssembliesToBundleInternal)" />
</ItemGroup>

<MakeDir Directories="$(_MobileIntermediateOutputPath)" />
Expand All @@ -185,12 +185,25 @@
</_AotInputAssemblies>
</ItemGroup>

<!--
In order for the runtime to work when static linking, we must supply
a list of direct pinvokes otherwise the runtime will crash
-->
<ItemGroup Condition="'$(_IsLibraryMode)' == 'true'">
<DirectPInvokes Include="libSystem.Native" />
<DirectPInvokes Include="libSystem.IO.Compression.Native" />
<DirectPInvokes Include="libSystem.Net.Security.Native" />
<DirectPInvokes Include="libSystem.Security.Cryptography.Native.Apple" />
</ItemGroup>

<MonoAOTCompiler Condition="'$(RunAOTCompilation)' == 'true'"
AotModulesTablePath="$(_AotModuleTablePath)"
AotModulesTableLanguage="ObjC"
Assemblies="@(_AotInputAssemblies)"
CompilerBinaryPath="@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier','$(TargetOS.ToLowerInvariant())-$(TargetArchitecture.ToLowerInvariant())'))"
DedupAssembly="$(_iOSLikeDedupAssembly)"
DirectPInvokes="@(DirectPInvokes)"
DirectPInvokeLists="@(DirectPInvokeLists)"
EnableUnmanagedCallersOnlyMethodsExport="$(_EnableUnmanagedCallersOnlyMethodsExport)"
IntermediateOutputPath="$(_MobileIntermediateOutputPath)"
LLVMPath="$(MonoAotCrossDir)"
Expand All @@ -199,13 +212,13 @@
OutputType="AsmOnly"
UseAotDataFile="$(_UseAotDataFile)"
UseLLVM="$(MonoEnableLLVM)">
<Output TaskParameter="CompiledAssemblies" ItemName="AssembliesToBundleInternal" />
<Output TaskParameter="CompiledAssemblies" ItemName="_AssembliesToBundleInternal" />
</MonoAOTCompiler>

<ILStrip Condition="'$(ShouldILStrip)' == 'true'" Assemblies="@(AssembliesToBundleInternal)" />
<ILStrip Condition="'$(ShouldILStrip)' == 'true'" Assemblies="@(_AssembliesToBundleInternal)" />

<ItemGroup>
<AssembliesToBundleInternal Include="@(_AOT_InternalForceInterpretAssemblies)" />
<_AssembliesToBundleInternal Include="@(_AOT_InternalForceInterpretAssemblies)" />
</ItemGroup>
</Target>

Expand All @@ -225,7 +238,7 @@
<AppleAppBuilderTask
AppDir="$(AppleBuildDir)"
Arch="$(TargetArchitecture)"
Assemblies="@(AssembliesToBundleInternal)"
Assemblies="@(_AssembliesToBundleInternal)"
BuildAppBundle="$(GenerateXcodeProject)"
DevTeamProvisioning="$(DevTeamProvisioning)"
DiagnosticPorts="$(DiagnosticPorts)"
Expand Down
2 changes: 1 addition & 1 deletion src/mono/msbuild/common/LibraryBuilder.targets
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</ItemGroup>

<LibraryBuilderTask
Assemblies="@(AssembliesToBundleInternal)"
Assemblies="@(_AssembliesToBundleInternal)"
AssembliesLocation="$(AssembliesLocation)"
ExtraLinkerArguments="@(_ExtraLinkerArgs)"
ExtraSources="@(_ExtraLibrarySources)"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. -->
<Project>
<PropertyGroup>
<TargetsAppleMobile Condition="'$(RuntimeIdentifier)' == 'ios-arm64' or '$(RuntimeIdentifier)' == 'iossimulator-arm64' or '$(RuntimeIdentifier)' == 'iossimulator-x64' or '$(RuntimeIdentifier)' == 'maccatalyst-arm64' or '$(RuntimeIdentifier)' == 'maccatalyst-x64' or '$(RuntimeIdentifier)' == 'tvos-arm64'">true</TargetsAppleMobile>
<TargetsAndroid Condition="'$(RuntimeIdentifier)' == 'android-arm64' or '$(RuntimeIdentifier)' == 'android-arm' or '$(RuntimeIdentifier)' == 'android-x64' or '$(RuntimeIdentifier)' == 'android-x86'">true</TargetsAndroid>

<TargetOS>$(RuntimeIdentifier.Substring(0, $(RuntimeIdentifier.IndexOf('-'))))</TargetOS>

<TargetsAppleMobile Condition="'$(TargetOS)' == 'ios' or '$(TargetOS)' == 'iossimulator' or '$(TargetOS)' == 'maccatalyst' or '$(TargetOS)' == 'tvos' or '$(TargetOS)' == 'tvossimulator'">true</TargetsAppleMobile>
<TargetsAndroid Condition="'$(TargetOS)' == 'android'">true</TargetsAndroid>
</PropertyGroup>
</Project>
5 changes: 3 additions & 2 deletions src/tasks/AndroidAppBuilder/ApkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,13 @@ public ApkBuilder(TaskLoggingHelper logger)
nativeLibraries += assemblerFilesToLink.ToString();

string aotSources = assemblerFiles.ToString();
string monodroidSource = (IsLibraryMode) ? "monodroid-librarymode.c" : "monodroid.c";

string cmakeLists = Utils.GetEmbeddedResource("CMakeLists-android.txt")
.Replace("%ProjectName%", ProjectName)
.Replace("%MonoInclude%", monoRuntimeHeaders)
.Replace("%NativeLibrariesToLink%", nativeLibraries)
.Replace("%MONODROID_SOURCE%", monodroidSource)
.Replace("%AotSources%", aotSources)
.Replace("%AotModulesSource%", string.IsNullOrEmpty(aotSources) ? "" : "modules.c")
.Replace("%APP_LINKER_ARGS%", extraLinkerArgs.ToString());
Expand Down Expand Up @@ -370,8 +372,7 @@ public ApkBuilder(TaskLoggingHelper logger)
cmakeLists = cmakeLists.Replace("%Defines%", defines.ToString());

File.WriteAllText(Path.Combine(OutputDir, "CMakeLists.txt"), cmakeLists);

File.WriteAllText(Path.Combine(OutputDir, "monodroid.c"), Utils.GetEmbeddedResource("monodroid.c"));
File.WriteAllText(Path.Combine(OutputDir, monodroidSource), Utils.GetEmbeddedResource(monodroidSource));

AndroidProject project = new AndroidProject("monodroid", runtimeIdentifier, AndroidNdk, logger);
project.GenerateCMake(OutputDir, MinApiLevel, StripDebugSymbols);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ endif()
add_library(
monodroid
SHARED
monodroid.c
%MONODROID_SOURCE%
%AotModulesSource%
)

Expand Down
63 changes: 63 additions & 0 deletions src/tasks/AndroidAppBuilder/Templates/monodroid-librarymode.c
steveisok marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <jni.h>
#include <android/log.h>
#include <assert.h>
#include <unistd.h>

#define LOG_INFO(fmt, ...) __android_log_print(ANDROID_LOG_DEBUG, "DOTNET", fmt, ##__VA_ARGS__)
#define LOG_ERROR(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, "DOTNET", fmt, ##__VA_ARGS__)
steveisok marked this conversation as resolved.
Show resolved Hide resolved

static void
strncpy_str (JNIEnv *env, char *buff, jstring str, int nbuff)
{
jboolean isCopy = 0;
const char *copy_buff = (*env)->GetStringUTFChars (env, str, &isCopy);
strncpy (buff, copy_buff, nbuff);
if (isCopy)
(*env)->ReleaseStringUTFChars (env, str, copy_buff);
}

void SayHello ();

int invoke_netlibrary_entrypoints (void)
{
SayHello ();

return 42;
}

void
Java_net_dot_MonoRunner_setEnv (JNIEnv* env, jobject thiz, jstring j_key, jstring j_value)
{
const char *key = (*env)->GetStringUTFChars(env, j_key, 0);
const char *val = (*env)->GetStringUTFChars(env, j_value, 0);
setenv (key, val, true);
(*env)->ReleaseStringUTFChars(env, j_key, key);
(*env)->ReleaseStringUTFChars(env, j_value, val);
}

int
Java_net_dot_MonoRunner_initRuntime (JNIEnv* env, jobject thiz, jstring j_files_dir, jstring j_cache_dir, jstring j_testresults_dir, jstring j_entryPointLibName, jobjectArray j_args, long current_local_time)
{
char file_dir[2048];
char cache_dir[2048];
char testresults_dir[2048];
strncpy_str (env, file_dir, j_files_dir, sizeof(file_dir));
strncpy_str (env, cache_dir, j_cache_dir, sizeof(cache_dir));
strncpy_str (env, testresults_dir, j_testresults_dir, sizeof(testresults_dir));

setenv ("HOME", file_dir, true);
setenv ("DOTNET_LIBRARY_ASSEMBLY_PATH", file_dir, true);
setenv ("TMPDIR", cache_dir, true);
setenv ("TEST_RESULTS_DIR", testresults_dir, true);

int res = invoke_netlibrary_entrypoints ();

return res;
steveisok marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 4 additions & 1 deletion src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ if(TARGETS_ANDROID)
libz.so
log
)
set(CMAKE_C_FLAGS, "${CMAKE_C_FLAGS} -fPIC")
add_definitions(-DHOST_ANDROID=1)
add_definitions(-g)
add_definitions(-fPIC)
add_definitions(-fvisibility=hidden)
set(USE_GCC_ATOMIC_OPS 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now this only applies to Android, didn't we want this applied to all?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm mistaken, we only apply these to android.

elseif(TARGETS_APPLE_MOBILE)
set(MOBILE_SYSTEM_LIBS
"-framework Foundation"
Expand Down