Skip to content

Commit f16e8bd

Browse files
ivanpovazanradical
andauthored
[mono] Clean up internal usage of RuntimeComponents to always be treated as MSBuild Item (#91800)
* Always use runtime components as MSBuild items * Addressing PR feedback * Renaming MonoSharedBuild.props into CommonMobileBuild.props * Formatting: whitespace Co-authored-by: Ankit Jain <radical@gmail.com> * Formatting: whitespace Co-authored-by: Ankit Jain <radical@gmail.com> --------- Co-authored-by: Ankit Jain <radical@gmail.com>
1 parent fa0ba15 commit f16e8bd

29 files changed

+194
-181
lines changed

docs/design/mono/diagnostics-tracing.md

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,27 @@ Depending on platform, there are different recommended and supported ways to inc
3333

3434
### Android
3535

36-
Android is build using dynamic component support, meaning that components are included as shared objects and runtime will try to load them from the same location as `libmonosgen-2.0.so`. If runtime fails to load component, it will be disabled, if it successfully loads the component at runtime, it will be enabled and used. Enabling/disabling components is then a matter of including/excluding the needed shared library files in the APK (in same folder as `libmonosgen-2.0.so`). The same runtime build can be used to support any combination of components.
36+
Android is built using dynamic component support, meaning that components are included as shared objects and runtime will try to load them from the same location as `libmonosgen-2.0.so`. If runtime fails to load component, it will be disabled, if it successfully loads the component at runtime, it will be enabled and used. Enabling/disabling components is then a matter of including/excluding the needed shared library files in the APK (in same folder as `libmonosgen-2.0.so`). The same runtime build can be used to support any combination of components.
3737

38-
If `AndroidAppBuilderTask` is used, there is a msbuild property, `RuntimeComponents` that can be used to include specific components in the generated application. By default, its empty, meaning all components will be disabled, using a `*` will enabled all components and by specify individual components, only those will be enabled. Enabling tracing would look like this, `RuntimeComponents="diagnostics_tracing"`, more components can be enabled by separating them with `;`.
38+
Android runtime pack has the following runtime components included: `debugger`, `hot_reload`, `diagnostics_tracing`, `marshal-ilgen`.
3939

40-
Android runtime pack have the following component libraries included. For default scenarios, the dynamic versions should be used together with `libmonosgen-2.0.so`, but runtime pack also includes static versions of the components that can be used if runtime is built statically using `libmonosgen-2.0.a`. In case of static linking, using `libmono-component-*-stub-static.a` library will disable the component, using `libmono-component-*-static.a` will enable it.
40+
For default scenarios, the dynamic versions should be used together with `libmonosgen-2.0.so`, but runtime pack also includes static versions of the components that can be used if runtime is built statically using `libmonosgen-2.0.a`. In case of static linking, using `libmono-component-*-stub-static.a` library will disable the component, using `libmono-component-*-static.a` will enable it.
4141

4242
```
4343
libmono-component-diagnostics_tracing.so
4444
libmono-component-diagnostics_tracing-static.a
4545
libmono-component-diagnostics_tracing-stub-static.a
4646
```
4747

48+
In order to enable the `diagnostic tracing` runtime component in your build, please take a look at [Enabling runtime components](#enabling-runtime-components) section.
49+
4850
### iOS
4951

50-
iOS is build using static component support, meaning that components are included as static libraries that needs to be linked together with `libmonosgen-2.0.a` to produce final application. Static components come in two flavors, the component library, and a stub library. Linking the component library will enable the component in final application, while linking the stub library disables the component. Depending on linked component flavors it is possible to create a build that enables specific components while disabling others. All components needs to be linked in (using component or stub library) or there will be unresolved symbols in `libmonosgen-2.0.a`.
52+
iOS is built using static component support, meaning that components are included as static libraries that needs to be linked together with `libmonosgen-2.0.a` to produce final application. Static components come in two flavors, the component library, and a stub library. Linking the component library will enable the component in final application, while linking the stub library disables the component. Depending on linked component flavors it is possible to create a build that enables specific components while disabling others. All components needs to be linked in (using component or stub library) or there will be unresolved symbols in `libmonosgen-2.0.a`.
5153

52-
If `AppleAppBuilderTask` is used, there is a msbuild property, `RuntimeComponents` that can be used to include specific components in the build application. By default, its empty, meaning all components will be disabled, using a `*` will enabled all components and by specify individual components, only those will be enabled. Enabling tracing would look like this, `RuntimeComponents="diagnostics_tracing"`, more components can be enabled by separating them with `;`.
54+
iOS runtime pack has the following runtime components included: `debugger`, `hot_reload`, `diagnostics_tracing`, `marshal-ilgen`.
5355

54-
iOS runtime pack have the following component libraries included. Using `libmono-component-*-stub-static.a` library will disable the component, using `libmono-component-*-static.a` will enable it.
56+
Using `libmono-component-*-stub-static.a` library will disable the component, using `libmono-component-*-static.a` will enable it.
5557

5658
```
5759
libmono-component-diagnostics_tracing-static.a
@@ -60,6 +62,39 @@ libmono-component-diagnostics_tracing-stub-static.a
6062

6163
NOTE, running on iOS simulator offers some additional capabilities, so runtime pack for iOS includes shared as well as static library builds, like the Android use case described above.
6264

65+
In order to enable the `diagnostic tracing` runtime component in your build, please take a look at [Enabling runtime components](#enabling-runtime-components) section.
66+
67+
### Enabling runtime components
68+
69+
When using `AndroidAppBuilderTask` to target `Android`, or `AppleAppBuilderTask` to target `iOS` platforms, there is a MSBuild item: `RuntimeComponents` that can be used to include specific components in the generated application. By default, its empty, meaning all components will be disabled.
70+
To enable a single component (eg: `diagnostic tracing`), by adding the following to your project file:
71+
```xml
72+
<ItemGroup>
73+
<RuntimeComponents Include="diagnostics_tracing" />
74+
</ItemGroup>
75+
```
76+
will enable only that runtime component.
77+
78+
On the other hand, if it is desired to include all components, there are two options:
79+
1. Manually, include all supported components manually via:
80+
```xml
81+
<ItemGroup>
82+
<RuntimeComponents Include="debugger" />
83+
<RuntimeComponents Include="hot_reload" />
84+
<RuntimeComponents Include="diagnostics_tracing" />
85+
<RuntimeComponents Include="marshal-ilgen" />
86+
</ItemGroup>
87+
```
88+
2. Automatically, use provided MSBuild property that includes all the supported components for you, in the following way:
89+
- Import `AndroidBuild.props/targets` in your project file (the file can be found [here](../../../src/mono/msbuild/android/build/AndroidBuild.targets))
90+
- Set `UseAllRuntimeComponents` MSBuild property to `true` via:
91+
- By adding: `-p:UseAllRuntimeComponents=true` to your build command, or
92+
- By adding the following in your project file:
93+
```xml
94+
<PropertyGroup>
95+
<UseAllRuntimeComponents>true</UseAllRuntimeComponents>
96+
</PropertyGroup>
97+
```
6398
## Install diagnostic client tooling
6499

65100
```

src/libraries/System.Diagnostics.Tracing/tests/System.Diagnostics.Tracing.Tests.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<TestRuntime>true</TestRuntime>
66
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
77
</PropertyGroup>
8-
<PropertyGroup>
9-
<RuntimeComponents Condition="'$(TargetsAppleMobile)' == 'true' or '$(TargetOS)' == 'android'">diagnostics_tracing;marshal-ilgen</RuntimeComponents>
10-
</PropertyGroup>
8+
<ItemGroup Condition="'$(TargetsAppleMobile)' == 'true' or '$(TargetOS)' == 'android'">
9+
<RuntimeComponents Include="diagnostics_tracing" />
10+
<RuntimeComponents Include="marshal-ilgen" />
11+
</ItemGroup>
1112
<!-- Windows only files -->
1213
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
1314
<Compile Include="BasicEventSourceTest\Harness\EtwListener.cs" />

src/libraries/sendtohelix-mobile.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<_XHarnessAppleCustomCommand Condition="'$(NeedsiOSSDK)' == 'true'">
5959
source build-apple-app.sh
6060
</_XHarnessAppleCustomCommand>
61+
<_RuntimeComponentManifestDir>$([MSBuild]::NormalizeDirectory('$(MonoArtifactsPath)', 'build'))</_RuntimeComponentManifestDir>
6162
</PropertyGroup>
6263

6364
<ItemGroup Condition="'$(NeedsiOSSDK)' == 'true'">
@@ -66,6 +67,8 @@
6667
<HelixCorrelationPayload Include="$(MonoAOTCompilerDir)" Condition="'$(RuntimeFlavor)' == 'mono'"
6768
Destination="build/MonoAOTCompiler" />
6869
<HelixCorrelationPayload Include="$(MicrosoftNetCoreAppRuntimePackDir)" Destination="build/microsoft.netcore.app.runtime.$(TargetOS)-$(TargetArchitecture.ToLower())" />
70+
<HelixCorrelationPayload Include="$(_RuntimeComponentManifestDir)" Condition="'$(RuntimeFlavor)' == 'mono'"
71+
Destination="build/microsoft.netcore.app.runtime.$(TargetOS)-$(TargetArchitecture.ToLower())/runtimes/$(TargetOS)-$(TargetArchitecture.ToLower())/build" />
6972
<HelixCorrelationPayload Include="$(iOSLikeBuildTargetsDir)" Destination="build/apple" />
7073
<HelixCorrelationPayload Include="$(iOSLikeLibraryBuilderTargetsDir)" Destination="build/common" />
7174
<HelixCorrelationPayload Include="$(MonoAotCrossDir)" Condition="'$(RuntimeFlavor)' == 'mono'"

src/mono/msbuild/android/build/AndroidBuild.props

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
<_HostOS Condition="'$(_HostOS)' == ''">linux</_HostOS>
1010

1111
<_IsLibraryMode Condition="'$(UseNativeAOTRuntime)' != 'true' and '$(NativeLib)' != ''">true</_IsLibraryMode>
12+
<_ReadRuntimeComponentsManifestTargetName Condition="'$(UseNativeAOTRuntime)' != 'true'">_MonoReadAvailableComponentsManifest</_ReadRuntimeComponentsManifestTargetName>
1213

1314
<AndroidBuildAfterThisTarget Condition="'$(AndroidBuildAfterThisTarget)' == ''">Publish</AndroidBuildAfterThisTarget>
1415
<AndroidBuildDependsOn>
16+
$(_ReadRuntimeComponentsManifestTargetName);
1517
_InitializeCommonProperties;
1618
_BeforeAndroidBuild;
1719
_AndroidResolveReferences;
@@ -21,5 +23,11 @@
2123
_AndroidGenerateAppBundle;
2224
_AfterAndroidBuild
2325
</AndroidBuildDependsOn>
26+
27+
<!-- When building on Helix $(_CommonTargetsDir) will be properly set, otherwise we have to set it to a in-tree location -->
28+
<_CommonTargetsDir Condition="'$(_CommonTargetsDir)' == ''">$([MSBuild]::NormalizeDirectory($(MSBuildThisFileDirectory), '..', '..', 'common'))</_CommonTargetsDir>
2429
</PropertyGroup>
30+
31+
<Import Condition="'$(UseNativeAOTRuntime)' != 'true'" Project="$(_CommonTargetsDir)CommonMobileBuild.props" />
32+
<Import Condition="'$(UseNativeAOTRuntime)' != 'true'" Project="$(_CommonTargetsDir)RuntimeComponentManifest.targets" />
2533
</Project>

src/mono/msbuild/android/build/AndroidBuild.targets

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,40 +41,27 @@
4141
<_MonoHeaderPath>$([MSBuild]::NormalizeDirectory($(MicrosoftNetCoreAppRuntimePackRidNativeDir), 'include', 'mono-2.0'))</_MonoHeaderPath>
4242
</PropertyGroup>
4343

44-
<!-- Make sure marshal-ilgen is included in the components list. -->
45-
<ItemGroup Condition="'$(RuntimeComponents)' != '*'">
46-
<_RuntimeComponentList Include="$(RuntimeComponents)" />
47-
<_RuntimeComponentList Include="marshal-ilgen" KeepDuplicates="false"/>
44+
<ItemGroup Condition="'$(UseNativeAOTRuntime)' != 'true'">
45+
<RuntimeComponents Condition="'$(UseAllRuntimeComponents)' == 'true'" Include="@(_MonoRuntimeAvailableComponents)" />
46+
<!-- Make sure marshal-ilgen is included in the components list. -->
47+
<RuntimeComponents Condition="'$(UseAllRuntimeComponents)' != 'true'" Include="marshal-ilgen" KeepDuplicates="false" />
4848
</ItemGroup>
4949

50-
<PropertyGroup Condition="'$(RuntimeComponents)' != '*'">
51-
<RuntimeComponents>@(_RuntimeComponentList)</RuntimeComponents>
52-
</PropertyGroup>
53-
5450
<ItemGroup Condition="'$(_IsLibraryMode)' == 'true'">
5551
<_CommonLinkerArgs Include="-l:libz.so" />
5652
<_CommonLinkerArgs Include="-l:liblog.so" />
5753
<_CommonLinkerArgs Include="-l:libc.so" />
5854
<_CommonLinkerArgs Include="-l:libm.so" />
5955
<_CommonLinkerArgs Include="--build-id=sha1" />
6056

61-
<!-- add all non stub libs first -->
62-
<!-- if RuntimeComponents is empty, exclude -static.a and include -stub-static.a instead -->
63-
<!-- if RuntimeComponents is *, we're ok because all -static.a is included -->
64-
<!-- if RuntimeComponents is a list, add to items and only pull in -static.a -->
65-
66-
<_UsedComponents
67-
Condition="'$(RuntimeComponents)' != '' and '$(RuntimeComponents)' != '*'"
68-
Include="$(RuntimeComponents)" />
69-
70-
<_RuntimeLibraries
71-
Include="$(AndroidBuildDir)\*-stub-static.a" />
72-
<_RuntimeLibraries
73-
Include="$(AndroidBuildDir)\*.a"
74-
Exclude="$(AndroidBuildDir)\*-static.a" />
75-
76-
<_RuntimeLibraries Remove="$(AndroidBuildDir)\libmono-component-%(_UsedComponents.Identity)-stub-static.a" />
77-
<_RuntimeLibraries Include="$(AndroidBuildDir)\libmono-component-%(_UsedComponents.Identity)-static.a" />
57+
<!-- include all libraries except components -->
58+
<_RuntimeLibraries Include="$(AndroidBuildDir)\*.a" Exclude="$(AndroidBuildDir)\libmono-component-*.a" />
59+
<!-- include all component stub libraries -->
60+
<_RuntimeLibraries Include="$(AndroidBuildDir)\libmono-component-*-stub-static.a" />
61+
<!-- if RuntimeComponents is not empty, remove stubs for the required components and include the actual component library -->
62+
<_RuntimeLibraries Condition="'@(RuntimeComponents)' != ''" Remove="$(AndroidBuildDir)\libmono-component-%(RuntimeComponents.Identity)-stub-static.a" />
63+
<_RuntimeLibraries Condition="'@(RuntimeComponents)' != ''" Include="$(AndroidBuildDir)\libmono-component-%(RuntimeComponents.Identity)-static.a" />
64+
<!-- if RuntimeComponents is empty, do nothing as we already included all the component stubs above -->
7865
</ItemGroup>
7966
</Target>
8067

@@ -283,7 +270,7 @@
283270
NativeDependencies="@(_NativeDependencies)"
284271
OutputDir="$(AndroidBundleDir)"
285272
ProjectName="$(AssemblyName)"
286-
RuntimeComponents="$(RuntimeComponents)"
273+
RuntimeComponents="@(RuntimeComponents)"
287274
RuntimeIdentifier="$(RuntimeIdentifier)"
288275
StripDebugSymbols="False">
289276
<Output TaskParameter="ApkBundlePath" PropertyName="ApkBundlePath" />

src/mono/msbuild/apple/build/AppleBuild.LocalBuild.props

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
$(AppleBuildSupportDir) - directory which has all the tasks, targets, and runtimepack
2020
-->
2121
<Project>
22-
<Import Project="$(MSBuildThisFileDirectory)AppleBuild.props" />
2322

2423
<PropertyGroup>
2524
<!-- Keep these underscored targets in sync with the root Directory.Build.props -->
@@ -84,4 +83,7 @@
8483
<MonoTargetsTasksAssemblyPath>$([MSBuild]::NormalizePath('$(MonoTargetsTasksDir)', 'MonoTargetsTasks.dll'))</MonoTargetsTasksAssemblyPath>
8584
</PropertyGroup>
8685

86+
<!-- Due to dependencies on resolving properties like: $(MicrosoftNetCoreAppRuntimePackRidDir) we need to import the default props at the end -->
87+
<Import Project="$(MSBuildThisFileDirectory)AppleBuild.props" />
88+
8789
</Project>

src/mono/msbuild/apple/build/AppleBuild.props

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
<_AotCompileTargetName Condition="'$(UseNativeAOTRuntime)' == 'true'">_AppleNativeAotCompile</_AotCompileTargetName>
2525
<_AotCompileTargetName Condition="'$(UseNativeAOTRuntime)' != 'true'">_AppleAotCompile</_AotCompileTargetName>
2626
<IlcCompileDependsOn>ComputeIlcCompileInputs;SetupOSSpecificProps;PrepareForILLink</IlcCompileDependsOn>
27+
<_ReadRuntimeComponentsManifestTargetName Condition="'$(UseNativeAOTRuntime)' != 'true'">_MonoReadAvailableComponentsManifest</_ReadRuntimeComponentsManifestTargetName>
2728

2829
<AppleBuildAfterThisTarget Condition="'$(AppleBuildAfterThisTarget)' == ''">Publish</AppleBuildAfterThisTarget>
2930
<AppleBuildDependsOn>
31+
$(_ReadRuntimeComponentsManifestTargetName);
3032
_InitializeCommonProperties;
3133
_BeforeAppleBuild;
3234
_AppleResolveReferences;
@@ -37,5 +39,11 @@
3739
_AppleGenerateAppBundle;
3840
_AfterAppleBuild
3941
</AppleBuildDependsOn>
42+
43+
<!-- When building on Helix $(_CommonTargetsDir) will be properly set, otherwise we have to set it to a in-tree location -->
44+
<_CommonTargetsDir Condition="'$(_CommonTargetsDir)' == ''">$([MSBuild]::NormalizeDirectory($(MSBuildThisFileDirectory), '..', '..', 'common'))</_CommonTargetsDir>
4045
</PropertyGroup>
46+
47+
<Import Condition="'$(UseNativeAOTRuntime)' != 'true'" Project="$(_CommonTargetsDir)CommonMobileBuild.props" />
48+
<Import Condition="'$(UseNativeAOTRuntime)' != 'true'" Project="$(_CommonTargetsDir)RuntimeComponentManifest.targets" />
4149
</Project>

0 commit comments

Comments
 (0)