Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 additions & 0 deletions eng/RuntimeIdentifier.props
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@
<TargetsWindows Condition="'$(TargetOS)' == 'windows'">true</TargetsWindows>
<TargetsUnix Condition="'$(TargetsOpenBSD)' == 'true' or '$(TargetsFreeBSD)' == 'true' or '$(Targetsillumos)' == 'true' or '$(TargetsSolaris)' == 'true' or '$(TargetsHaiku)' == 'true' or '$(TargetsLinux)' == 'true' or '$(TargetsNetBSD)' == 'true' or '$(TargetsOSX)' == 'true' or '$(TargetsMacCatalyst)' == 'true' or '$(TargetstvOS)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetsAndroid)' == 'true'">true</TargetsUnix>
</PropertyGroup>
<!-- Values of the public OperatingSystem.IsXxx() checks for the TargetOS. These must match the
TARGET_* conditions in System.Private.CoreLib's OperatingSystem.cs. -->
<ItemGroup Label="TargetOperatingSystemChecks">
<TargetOperatingSystemCheck Include="IsAndroid" Value="$([MSBuild]::ValueOrDefault('$(TargetsAndroid)', 'false'))" />
<TargetOperatingSystemCheck Include="IsBrowser" Value="$([MSBuild]::ValueOrDefault('$(TargetsBrowser)', 'false'))" />
<TargetOperatingSystemCheck Include="IsFreeBSD" Value="$([MSBuild]::ValueOrDefault('$(TargetsFreeBSD)', 'false'))" />
<TargetOperatingSystemCheck Include="IsIOS" Value="true" Condition="'$(TargetsiOS)' == 'true' or '$(TargetsMacCatalyst)' == 'true'" />
<TargetOperatingSystemCheck Include="IsIOS" Value="false" Condition="'$(TargetsiOS)' != 'true' and '$(TargetsMacCatalyst)' != 'true'" />
<TargetOperatingSystemCheck Include="IsLinux" Value="true" Condition="'$(TargetsLinux)' == 'true' and '$(TargetsAndroid)' != 'true'" />
<TargetOperatingSystemCheck Include="IsLinux" Value="false" Condition="'$(TargetsLinux)' != 'true' or '$(TargetsAndroid)' == 'true'" />
<TargetOperatingSystemCheck Include="IsMacCatalyst" Value="$([MSBuild]::ValueOrDefault('$(TargetsMacCatalyst)', 'false'))" />
<TargetOperatingSystemCheck Include="IsMacOS" Value="$([MSBuild]::ValueOrDefault('$(TargetsOSX)', 'false'))" />
<TargetOperatingSystemCheck Include="IsOpenBSD" Value="$([MSBuild]::ValueOrDefault('$(TargetsOpenBSD)', 'false'))" />
<TargetOperatingSystemCheck Include="IsTvOS" Value="$([MSBuild]::ValueOrDefault('$(TargetstvOS)', 'false'))" />
<TargetOperatingSystemCheck Include="IsWasi" Value="$([MSBuild]::ValueOrDefault('$(TargetsWasi)', 'false'))" />
<TargetOperatingSystemCheck Include="IsWatchOS" Value="false" />
<TargetOperatingSystemCheck Include="IsWindows" Value="$([MSBuild]::ValueOrDefault('$(TargetsWindows)', 'false'))" />
</ItemGroup>
<PropertyGroup>
<_ImportedRuntimeIdentifierProps>true</_ImportedRuntimeIdentifierProps>
</PropertyGroup>
Expand Down
64 changes: 64 additions & 0 deletions eng/illink.targets
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,70 @@
</PropertyGroup>
</Target>

<!-- OperatingSystem.IsXxx() are constants in the OS-specific System.Private.CoreLib, but the library build
resolves them from the System.Runtime reference assembly. Substitute their values for the OS being
built so that the library build trims code guarded by checks for other operating systems.
Not applied to packages: binaries shipped in them (e.g. a runtimes/unix asset) must remain valid on every
OS that their flavor covers. -->
<PropertyGroup>
<!-- The linux, ios and tvos flavors are also used by the TargetOS values that fall back to them:
android and openharmony (linux), maccatalyst and iossimulator (ios), tvossimulator (tvos). -->
<_OSSubstitutionsFlavorMatchesTargetOS Condition="'$(TargetOS)' != '' and
('$(TargetPlatformIdentifier)' == '$(TargetOS)' or
('$(TargetPlatformIdentifier)' == 'unix' and '$(TargetsUnix)' == 'true') or
('$(TargetPlatformIdentifier)' == 'linux' and '$(TargetsLinux)' == 'true') or
('$(TargetPlatformIdentifier)' == 'ios' and ('$(TargetsiOS)' == 'true' or '$(TargetsMacCatalyst)' == 'true')) or
('$(TargetPlatformIdentifier)' == 'tvos' and '$(TargetstvOS)' == 'true'))">true</_OSSubstitutionsFlavorMatchesTargetOS>
<!-- The OS whose values are substituted: the TargetOS for the flavors it uses, otherwise the OS of an OS-specific
flavor, so that a flavor produces the same binary regardless of the TargetOS the build was invoked for.
The unix flavor covers multiple operating systems, so it is only substituted when built for one of them. -->
<_OSSubstitutionsTargetOS Condition="'$(_OSSubstitutionsFlavorMatchesTargetOS)' == 'true'">$(TargetOS)</_OSSubstitutionsTargetOS>
<_OSSubstitutionsTargetOS Condition="'$(_OSSubstitutionsFlavorMatchesTargetOS)' != 'true' and '$(TargetPlatformIdentifier)' != 'unix'">$(TargetPlatformIdentifier.ToLowerInvariant())</_OSSubstitutionsTargetOS>
<_OSSubstitutionsApply Condition="'$(_OSSubstitutionsTargetOS)' != '' and
'$(IsPackable)' != 'true' and
'$(BuildAllConfigurations)' != 'true' and
'$(ILLinkSubstitutionsLibraryBuildXml)' == ''">true</_OSSubstitutionsApply>
</PropertyGroup>

<Target Name="_GetTargetOperatingSystemChecks"
Returns="@(TargetOperatingSystemCheck)" />

<Target Name="_GenerateOperatingSystemSubstitutions"
Condition="'$(_OSSubstitutionsApply)' == 'true' and '$(ILLinkTrimAssembly)' == 'true'"
BeforeTargets="ILLinkTrimAssembly">
<ItemGroup Condition="'$(_OSSubstitutionsTargetOS)' == '$(TargetOS)'">
<_OSSubstitutionsCheck Include="@(TargetOperatingSystemCheck)" />
</ItemGroup>

<!-- Evaluate the checks for the flavor's OS from the same definitions in RuntimeIdentifier.props. -->
<MSBuild Projects="$(MSBuildProjectFullPath)"
Targets="_GetTargetOperatingSystemChecks"
Properties="TargetOS=$(_OSSubstitutionsTargetOS);TargetFramework=$(TargetFramework)"
Condition="'$(_OSSubstitutionsTargetOS)' != '$(TargetOS)'">
<Output TaskParameter="TargetOutputs" ItemName="_OSSubstitutionsCheck" />
</MSBuild>

<PropertyGroup>
<ILLinkSubstitutionsLibraryBuildXml>$(IntermediateOutputPath)ILLink.Substitutions.OperatingSystem.LibraryBuild.xml</ILLinkSubstitutionsLibraryBuildXml>
<_OSSubstitutions><![CDATA[<linker>
<assembly fullname="System.Runtime">
<type fullname="System.OperatingSystem">
@(_OSSubstitutionsCheck->' <method signature="System.Boolean %(Identity)()" body="stub" value="%(Value)" />', '%0A')
</type>
</assembly>
</linker>]]></_OSSubstitutions>
</PropertyGroup>

<WriteLinesToFile File="$(ILLinkSubstitutionsLibraryBuildXml)"
Lines="$(_OSSubstitutions)"
Overwrite="true"
WriteOnlyWhenDifferent="true" />

<ItemGroup>
<FileWrites Include="$(ILLinkSubstitutionsLibraryBuildXml)" />
</ItemGroup>
</Target>

<!-- Examines the "input assembly" for IL that is unreachable from public API and trims that,
rewriting the assembly to an "output assembly" -->
<UsingTask TaskName="ILLink"
Expand Down
Loading