Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4243eb0
Update ApkBuilder to support static linking
kotlarmilos Apr 8, 2025
43310f2
Add static libs to the framework directory
kotlarmilos Apr 9, 2025
4463fae
Add static libs to the runtime pack
kotlarmilos Apr 14, 2025
f08e9fe
Fix formatting
kotlarmilos Apr 14, 2025
ffa5351
Add object libraries for Android static linking
kotlarmilos Apr 24, 2025
8507f65
Install libcoreclrpal.a as it is linked using -Wl,--whole-archive
kotlarmilos Apr 24, 2025
76b4d6a
Fix x64 static linking
kotlarmilos Apr 24, 2025
2ef4399
Add gc_vxsort objects
kotlarmilos Apr 25, 2025
1ff51f6
Add static test project
kotlarmilos Apr 25, 2025
0584ea8
Fix MainLibraryFileName in Android.Device_Emulator.JIT.Static.Test pr…
kotlarmilos Apr 25, 2025
d6a2893
Update src/coreclr/gc/vxsort/CMakeLists.txt
kotlarmilos Apr 26, 2025
2b3a5ee
Update src/coreclr/gc/unix/CMakeLists.txt
kotlarmilos Apr 26, 2025
db2e108
Update CMakeLists.txt to conditionally link gc_pal_objects for Android
kotlarmilos Apr 26, 2025
39dea31
Test static linking on linux
kotlarmilos Apr 26, 2025
de2506e
Fix typo
kotlarmilos Apr 26, 2025
1a12a79
Revert gc_pal changes to test the build
kotlarmilos Apr 27, 2025
7f282b6
Revert changes
kotlarmilos Apr 28, 2025
2df2281
Merge branch 'dotnet:main' into feature/coreclr-android-static-libs
kotlarmilos Apr 28, 2025
21b454f
Add gcinfo library
kotlarmilos Apr 28, 2025
d17fe7a
Conditionally add gcinfo
kotlarmilos Apr 28, 2025
d53aefc
FIx formatting
kotlarmilos Apr 29, 2025
cd867ae
Switch from static to object
kotlarmilos Apr 30, 2025
a9ac92c
Merge branch 'main' into feature/coreclr-android-static-libs
kotlarmilos Apr 30, 2025
e9d5b20
Fix coreclr build
kotlarmilos Apr 30, 2025
7b4efbf
Remove unnecessary static library installation for Android
kotlarmilos Apr 30, 2025
ea53d06
Update static libraries to be based on the object libraries
kotlarmilos May 1, 2025
4dc57cf
Merge branch 'main' into feature/coreclr-android-static-libs
kotlarmilos May 2, 2025
5e6cc7c
Add coreclrpal_objects
kotlarmilos May 2, 2025
ed47a53
Merge branch 'main' into feature/coreclr-android-static-libs
kotlarmilos May 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,23 @@ if (CLR_CMAKE_TARGET_OSX)
find_library(FOUNDATION Foundation REQUIRED)
endif()

target_sources(coreclr PUBLIC $<TARGET_OBJECTS:cee_wks_core>)
target_link_libraries(coreclr PUBLIC ${CORECLR_LIBRARIES} ${CLRJIT_STATIC} cee_wks ${FOUNDATION})
target_sources(coreclr_static PUBLIC $<TARGET_OBJECTS:cee_wks_core>)
target_link_libraries(coreclr_static PUBLIC ${CORECLR_LIBRARIES} clrjit_static cee_wks_mergeable ${FOUNDATION})
target_link_libraries(coreclr PUBLIC ${CORECLR_LIBRARIES} ${CLRJIT_STATIC} cee_wks_core cee_wks ${FOUNDATION})
target_link_libraries(coreclr_static PUBLIC ${CORECLR_LIBRARIES} cee_wks_core clrjit_static cee_wks_mergeable ${FOUNDATION})
target_compile_definitions(coreclr_static PUBLIC CORECLR_EMBEDDED)

if (CLR_CMAKE_HOST_ANDROID)
target_link_libraries(coreclr PUBLIC log)
target_link_libraries(coreclr_static PUBLIC log)
target_link_libraries(coreclr_static
PUBLIC
coreclrminipal_objects
coreclrpal_objects
eventprovider_objects
gcinfo
libunwind
log
minipal_objects
nativeresourcestring_objects
)
endif()

if(CLR_CMAKE_TARGET_WIN32)
Expand Down Expand Up @@ -220,9 +228,9 @@ endif(CLR_CMAKE_TARGET_WIN32)

# add the install targets
install_clr(TARGETS coreclr DESTINATIONS . sharedFramework COMPONENT runtime)
if(CLR_CMAKE_HOST_MACCATALYST OR CLR_CMAKE_HOST_IOS OR CLR_CMAKE_HOST_TVOS)
if(CLR_CMAKE_HOST_MACCATALYST OR CLR_CMAKE_HOST_IOS OR CLR_CMAKE_HOST_TVOS OR CLR_CMAKE_HOST_ANDROID)
install_clr(TARGETS coreclr_static DESTINATIONS . sharedFramework COMPONENT runtime)
endif()
endif(CLR_CMAKE_HOST_MACCATALYST OR CLR_CMAKE_HOST_IOS OR CLR_CMAKE_HOST_TVOS OR CLR_CMAKE_HOST_ANDROID)

# Enable profile guided optimization
add_pgo(coreclr)
2 changes: 1 addition & 1 deletion src/coreclr/gc/unix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ set(GC_PAL_SOURCES
events.cpp
cgroup.cpp)

add_library(gc_pal STATIC ${GC_PAL_SOURCES} ${VERSION_FILE_PATH})
add_library(gc_pal OBJECT ${GC_PAL_SOURCES} ${VERSION_FILE_PATH})
2 changes: 1 addition & 1 deletion src/coreclr/gc/vxsort/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ set (VXSORT_SOURCES
do_vxsort.h
)

add_library(gc_vxsort STATIC ${VXSORT_SOURCES})
add_library(gc_vxsort OBJECT ${VXSORT_SOURCES})
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be nice to use the same style, either this one in src/coreclr/gc/unix/CMakeLists.txt or the other way around.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean to conditionally add gc_vxsort objects if android host?

Copy link
Member

Choose a reason for hiding this comment

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

That or change static to object in gc/unix as well.

Copy link
Member Author

@kotlarmilos kotlarmilos Apr 25, 2025

Choose a reason for hiding this comment

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

I added gc_vxsort_objects for the android host. I could also switch it from static to an object library, but I’m not sure if archive is actually being used in other scenarios.

Copy link
Member

Choose a reason for hiding this comment

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

Do we need both, static and objects, for Android? If not, we can use an if-else block.

Copy link
Member

Choose a reason for hiding this comment

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

BTW: vxsort is a ton of code for relatively small performance gain. We may want to disable it on mobile. It is implemented on x64 only for now, but arm64 implementation is in the works #110692 .

Copy link
Member

Choose a reason for hiding this comment

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

Personally, I'd prefer to switch from static to object library here instead of introducing the conditional object for Android. At least for gc_pal and gc_vxsort, since they are pretty isolated (used for coreclr and standalone gc) and I think the conditional object build makes it more complicated that just switching - as opposed to something like coreclrminipal, which would fit better as a separate change per #114629 (review).

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree. I haven’t applied these changes to the other libraries in this PR, since I’m not sure whether they’re required as archives on other platforms.

7 changes: 6 additions & 1 deletion src/coreclr/minipal/Unix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ set(SOURCES
dn-u16.cpp
)

add_library(coreclrminipal_objects
OBJECT
${SOURCES}
)

add_library(coreclrminipal
STATIC
${SOURCES}
$<TARGET_OBJECTS:coreclrminipal_objects>
)

target_link_libraries(coreclrminipal PRIVATE minipal)
7 changes: 6 additions & 1 deletion src/coreclr/nativeresources/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
project(nativeresourcestring)

add_library_clr(nativeresourcestring_objects
OBJECT
resourcestring.cpp
)

add_library_clr(nativeresourcestring
STATIC
resourcestring.cpp
$<TARGET_OBJECTS:nativeresourcestring_objects>
)

install_clr (TARGETS nativeresourcestring DESTINATIONS lib)
8 changes: 6 additions & 2 deletions src/coreclr/pal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,15 @@ if(NOT CLR_CMAKE_USE_SYSTEM_LIBUNWIND AND NOT CLR_CMAKE_TARGET_ARCH_WASM)
set(LIBUNWIND_OBJECTS $<TARGET_OBJECTS:libunwind>)
endif(NOT CLR_CMAKE_USE_SYSTEM_LIBUNWIND AND NOT CLR_CMAKE_TARGET_ARCH_WASM)

add_library(coreclrpal
STATIC
add_library(coreclrpal_objects
OBJECT
${SOURCES}
${ARCH_SOURCES}
${PLATFORM_SOURCES}
)

add_library(coreclrpal STATIC
$<TARGET_OBJECTS:coreclrpal_objects>
${LIBUNWIND_OBJECTS}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ add_custom_command(OUTPUT ${DUMMY_PROVIDER_SOURCES}
COMMAND ${GENERATE_COMMAND}
DEPENDS ${EVENT_MANIFEST} ${GENERATE_SCRIPT})

add_library(eventprovider_objects
OBJECT
${DUMMY_PROVIDER_SOURCES}
)

add_library(eventprovider
STATIC
${DUMMY_PROVIDER_SOURCES}
$<TARGET_OBJECTS:eventprovider_objects>
)

set_target_properties(eventprovider PROPERTIES LINKER_LANGUAGE CXX)
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,8 @@
<ItemGroup Condition="'$(ArchiveTests)' == 'true' and '$(TargetOS)' == 'android' and '$(RuntimeFlavor)' == 'CoreCLR'">
<ProjectReference Include="$(RepoRoot)\src\tests\FunctionalTests\Android\Device_Emulator\JIT\Android.Device_Emulator.JIT.Test.csproj"
BuildInParallel="false" />
<ProjectReference Include="$(RepoRoot)\src\tests\FunctionalTests\Android\Device_Emulator\JIT\Android.Device_Emulator.JIT.Static.Test.csproj"
BuildInParallel="false" />
</ItemGroup>

<Target Name="GenerateMergedCoverageReport"
Expand Down
4 changes: 3 additions & 1 deletion src/mono/msbuild/android/build/AndroidBuild.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<_IsLibraryMode Condition="'$(UseMonoRuntime)' == 'true' and '$(UseNativeAOTRuntime)' != 'true' and '$(NativeLib)' != ''">true</_IsLibraryMode>
<_ReadRuntimeComponentsManifestTargetName Condition="'$(UseMonoRuntime)' == 'true' and '$(UseNativeAOTRuntime)' != 'true'">_MonoReadAvailableComponentsManifest</_ReadRuntimeComponentsManifestTargetName>

<StaticLinkedRuntime Condition="'$(RuntimeFlavor)' == 'coreclr' and '$(StaticLinkedRuntime)' == ''">false</StaticLinkedRuntime>

<AndroidBuildAfterThisTarget Condition="'$(AndroidBuildAfterThisTarget)' == ''">Publish</AndroidBuildAfterThisTarget>
<AndroidBuildDependsOn Condition="'$(UseMonoRuntime)' == 'true'">
$(_ReadRuntimeComponentsManifestTargetName);
Expand Down Expand Up @@ -41,4 +43,4 @@
<Import Condition="'$(UseNativeAOTRuntime)' != 'true' and '$(UseMonoRuntime)' == 'true'" Project="$(_CommonTargetsDir)CommonMobileBuild.props" />
<Import Condition="'$(UseNativeAOTRuntime)' != 'true' and '$(UseMonoRuntime)' == 'true'" Project="$(_CommonTargetsDir)RuntimeComponentManifest.targets" />
<Import Condition="'$(UseNativeAOTRuntime)' != 'true' and ('$(UseMonoRuntime)' == 'true' and '$(RunAOTCompilation)' == 'true')" Project="$(_CommonTargetsDir)MonoAOTCompiler.props" />
</Project>
</Project>
3 changes: 2 additions & 1 deletion src/mono/msbuild/android/build/AndroidBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@
ProjectName="$(AppName)"
RuntimeComponents="@(RuntimeComponents)"
RuntimeIdentifier="$(RuntimeIdentifier)"
StripDebugSymbols="$(StripDebugSymbols)">
StripDebugSymbols="$(StripDebugSymbols)"
StaticLinkedRuntime="$(StaticLinkedRuntime)">
<Output TaskParameter="ApkBundlePath" PropertyName="ApkBundlePath" />
<Output TaskParameter="ApkPackageId" PropertyName="ApkPackageId" />
</AndroidAppBuilderTask>
Expand Down
4 changes: 3 additions & 1 deletion src/mono/sample/Android/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ DEPLOY_AND_RUN?=true
RUNTIME_FLAVOR?=mono
R2R?=false
R2R_COMPOSITE?=false
STATIC_LINKING?=false

#If DIAGNOSTIC_PORTS is enabled, @(RuntimeComponents) must also include 'diagnostics_tracing'.
#If @(RuntimeComponents) includes 'diagnostics_tracing', DIAGNOSTIC_PORTS is optional.
Expand Down Expand Up @@ -57,7 +58,8 @@ run: appbuilder
/p:RuntimeFlavor=$(RUNTIME_FLAVOR) \
/p:PublishReadyToRun=$(R2R) \
/p:PublishReadyToRunComposite=$(R2R_COMPOSITE) \
/p:StaticLinkedRuntime=$(STATIC_LINKING) \
/bl
clean:
rm -rf $(REPO_DIR)/artifacts/bin/AndroidSampleApp
rm -rf $(REPO_DIR)/artifacts/obj/mono/AndroidSampleApp
rm -rf $(REPO_DIR)/artifacts/obj/mono/AndroidSampleApp
40 changes: 32 additions & 8 deletions src/tasks/AndroidAppBuilder/ApkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ public ApkBuilder(TaskLoggingHelper logger)
throw new ArgumentException($"Using DiagnosticPorts targeting Mono requires diagnostics_tracing runtime component, which was not included in 'RuntimeComponents' item group. @RuntimeComponents: '{string.Join(", ", RuntimeComponents)}'");
}

if (IsCoreCLR && StaticLinkedRuntime)
{
throw new ArgumentException("Static linking is not supported for CoreCLR runtime");
}

AndroidSdkHelper androidSdkHelper = new AndroidSdkHelper(AndroidSdk, BuildApiLevel, BuildToolsVersion);

if (string.IsNullOrEmpty(MinApiLevel))
Expand Down Expand Up @@ -263,6 +258,10 @@ public ApkBuilder(TaskLoggingHelper logger)
{
runtimeLib = Path.Combine(AppDir, "libmonosgen-2.0.so");
}
else if (StaticLinkedRuntime && IsCoreCLR)
{
runtimeLib = Path.Combine(AppDir, "libcoreclr_static.a");
}
else if (IsCoreCLR)
{
runtimeLib = Path.Combine(AppDir, "libcoreclr.so");
Expand All @@ -277,7 +276,7 @@ public ApkBuilder(TaskLoggingHelper logger)
nativeLibraries += $"{runtimeLib}{Environment.NewLine}";
}

if (StaticLinkedRuntime)
if (StaticLinkedRuntime && IsMono)
{
string[] staticComponentStubLibs = Directory.GetFiles(AppDir, "libmono-component-*-stub-static.a");

Expand Down Expand Up @@ -313,12 +312,35 @@ public ApkBuilder(TaskLoggingHelper logger)
}
}

if (StaticLinkedRuntime && IsCoreCLR)
{
string[] staticLibs = Directory.GetFiles(AppDir, "*.a")
.Where(lib => !Path.GetFileName(lib).Equals("libcoreclr_static.a", StringComparison.OrdinalIgnoreCase))
.ToArray();

foreach (string lib in staticLibs)
{
nativeLibraries += $" {lib}{Environment.NewLine}";
}

nativeLibraries += $" libc++abi.a{Environment.NewLine}";
nativeLibraries += $" libc++_static.a{Environment.NewLine}";
}

StringBuilder extraLinkerArgs = new StringBuilder();
foreach (ITaskItem item in ExtraLinkerArguments)
{
extraLinkerArgs.AppendLine($" \"{item.ItemSpec}\"");
}

if (StaticLinkedRuntime && IsCoreCLR)
{
// Ensure global symbol references in the shared library are resolved to definitions in
// the same shared library. For the static linked runtime specifically, we need this for
// global functions in assembly for the linker to treat relative offsets to them as constant
extraLinkerArgs.AppendLine($" \"-Wl,-Bsymbolic\"");
}

nativeLibraries += assemblerFilesToLink.ToString();

string aotSources = assemblerFiles.ToString();
Expand Down Expand Up @@ -401,7 +423,8 @@ public ApkBuilder(TaskLoggingHelper logger)
envVariables += $"\t\tsetEnv(\"{name}\", \"{value}\");\n";
}

string jniLibraryName = (IsLibraryMode) ? ProjectName! : "System.Security.Cryptography.Native.Android";
string jniLibraryName = (IsLibraryMode) ? ProjectName! :
(StaticLinkedRuntime && IsCoreCLR) ? "monodroid" : "System.Security.Cryptography.Native.Android";
string monoRunner = Utils.GetEmbeddedResource("MonoRunner.java")
.Replace("%EntryPointLibName%", Path.GetFileName(mainLibraryFileName))
.Replace("%JNI_LIBRARY_NAME%", jniLibraryName)
Expand Down Expand Up @@ -458,7 +481,8 @@ public ApkBuilder(TaskLoggingHelper logger)
excludedLibs.Add("libmscordaccore.so");
}
}
dynamicLibs.AddRange(Directory.GetFiles(AppDir, "*.so").Where(file => !excludedLibs.Contains(Path.GetFileName(file))));
if (!StaticLinkedRuntime)
dynamicLibs.AddRange(Directory.GetFiles(AppDir, "*.so").Where(file => !excludedLibs.Contains(Path.GetFileName(file))));
}

// add all *.so files to lib/%abi%/
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/AndroidAppBuilder/Templates/monodroid-coreclr.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ free_resources ()
}
if (g_executable_path)
{
free (g_executable_path);
free ((void*)g_executable_path);
g_executable_path = NULL;
}
if (g_coreclr_handle)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<RunAOTCompilation>false</RunAOTCompilation>
<TestRuntime>true</TestRuntime>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<MainLibraryFileName>Android.Device_Emulator.JIT.Static.Test.dll</MainLibraryFileName>
<ExpectedExitCode>42</ExpectedExitCode>
<StaticLinkedRuntime>true</StaticLinkedRuntime>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.cs" />
</ItemGroup>
</Project>
Loading