Skip to content

Commit

Permalink
Enable specifying install search location options for published `apph…
Browse files Browse the repository at this point in the history
…ost` (#42455)

- `AppHostDotNetSearch` allows selection of which search locations should be used
  - Errors on creating `apphost` if set to an invalid value
- `AppHostRelativeDotNet` allows specifying a relative path to the runtime
  - Setting implies `AppHostDotNetSearch=AppRelative` if `AppHostDotNetSearch` is not explicitly set
- Values are only embedded in `apphost` on `publish` (not `build`)

SDK side of https://github.com/dotnet/designs/blob/main/proposed/apphost-embed-install-location.md.
  • Loading branch information
elinor-fung authored Aug 7, 2024
1 parent e001cc1 commit 521a00d
Show file tree
Hide file tree
Showing 19 changed files with 319 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/Tasks/Common/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -956,5 +956,9 @@ You may need to build the project on another operating system or architecture, o
<value>NETSDK1216: Package Microsoft.Net.Sdk.Compilers.Toolset is not downloaded but it is needed because your MSBuild and SDK versions are mismatched. Ensure version {0} of the package is available in your NuGet source feeds and then run NuGet package restore from Visual Studio or MSBuild.</value>
<comment>{StrBegin="NETSDK1216: "}{Locked="Microsoft.Net.Sdk.Compilers.Toolset"} {0} is a NuGet package version and should not be translated.</comment>
</data>
<!-- The latest message added is MicrosoftNetSdkCompilersToolsetNotFound. Please update this value with each PR to catch parallel PRs both adding a new message -->
<data name="InvalidAppHostDotNetSearch" xml:space="preserve">
<value>NETSDK1217: Invalid value in AppHostDotNetSearch: '{0}'.</value>
<comment>{StrBegin="NETSDK1217: "}</comment>
</data>
<!-- The latest message added is InvalidAppHostDotNetSearch. Please update this value with each PR to catch parallel PRs both adding a new message -->
</root>
5 changes: 5 additions & 0 deletions src/Tasks/Common/Resources/xlf/Strings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Common/Resources/xlf/Strings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Common/Resources/xlf/Strings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Common/Resources/xlf/Strings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Common/Resources/xlf/Strings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Common/Resources/xlf/Strings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Common/Resources/xlf/Strings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Common/Resources/xlf/Strings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Common/Resources/xlf/Strings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Common/Resources/xlf/Strings.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Common/Resources/xlf/Strings.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Common/Resources/xlf/Strings.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Common/Resources/xlf/Strings.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion src/Tasks/Microsoft.NET.Build.Tasks/CreateAppHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public class CreateAppHost : TaskBase

public bool DisableCetCompat { get; set; } = false;

public ITaskItem[] DotNetSearchLocations { get; set; }

public string AppRelativeDotNet { get; set; } = null;

protected override void ExecuteCore()
{
try
Expand All @@ -61,13 +65,38 @@ protected override void ExecuteCore()
{
try
{
HostWriter.DotNetSearchOptions options = null;
if (DotNetSearchLocations?.Length > 0)
{
HostWriter.DotNetSearchOptions.SearchLocation searchLocation = default;
foreach (var locationItem in DotNetSearchLocations)
{
if (Enum.TryParse(locationItem.ItemSpec, out HostWriter.DotNetSearchOptions.SearchLocation location)
&& Enum.IsDefined(typeof(HostWriter.DotNetSearchOptions.SearchLocation), location))
{
searchLocation |= location;
}
else
{
throw new BuildErrorException(Strings.InvalidAppHostDotNetSearch, locationItem.ItemSpec);
}
}

options = new HostWriter.DotNetSearchOptions()
{
Location = searchLocation,
AppRelativeDotNet = AppRelativeDotNet
};
}

HostWriter.CreateAppHost(appHostSourceFilePath: AppHostSourcePath,
appHostDestinationFilePath: AppHostDestinationPath,
appBinaryFilePath: AppBinaryName,
windowsGraphicalUserInterface: isGUI,
assemblyToCopyResourcesFrom: resourcesAssembly,
enableMacOSCodeSign: EnableMacOSCodeSign,
disableCetCompat: DisableCetCompat);
disableCetCompat: DisableCetCompat,
dotNetSearchOptions: options);
return;
}
catch (Exception ex) when (ex is IOException ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,35 @@ Copyright (c) .NET Foundation. All rights reserved.
/>
</Target>

<!--
============================================================
_CreateAppHostForPublish
Create the apphost for the publish scenario if app is configuring .NET install search behaviour
Because there is no SDK support yet for output with a layout conforming to the configuration,
only do this on publish, such that the inner dev loop is unaffected.
============================================================
-->
<Target Name="_CreateAppHostForPublish"
Inputs="@(IntermediateAssembly);$(AppHostSourcePath)"
Outputs="$(AppHostForPublishIntermediatePath)"
DependsOnTargets="_GetAppHostPaths;_GetAppHostCreationConfiguration"
Condition="'$(_UpdateAppHostForPublish)' == 'true' and
Exists('@(IntermediateAssembly)') and
Exists('$(AppHostSourcePath)')">
<CreateAppHost AppHostSourcePath="$(AppHostSourcePath)"
AppHostDestinationPath="$(AppHostForPublishIntermediatePath)"
AppBinaryName="$(AssemblyName)$(TargetExt)"
IntermediateAssembly="@(IntermediateAssembly->'%(FullPath)')"
WindowsGraphicalUserInterface="$(_UseWindowsGraphicalUserInterface)"
Retries="$(CopyRetryCount)"
RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)"
EnableMacOSCodeSign="$(_EnableMacOSCodeSign)"
DisableCetCompat="$(_DisableCetCompat)"
DotNetSearchLocations="$(AppHostDotNetSearch)"
AppRelativeDotNet="$(AppHostRelativeDotNet)"
/>
</Target>

<!--
============================================================
_ComputeCopyToPublishDirectoryItems
Expand Down Expand Up @@ -762,6 +791,7 @@ Copyright (c) .NET Foundation. All rights reserved.
DependsOnTargets="AssignTargetPaths;
DefaultCopyToPublishDirectoryMetadata;
_CreateSingleFileHost;
_CreateAppHostForPublish;
_SplitProjectReferencesByFileExistence;
_GetProjectReferenceTargetFrameworkProperties">

Expand Down Expand Up @@ -861,6 +891,15 @@ Copyright (c) .NET Foundation. All rights reserved.
<_SourceItemsToCopyToPublishDirectoryAlways Include="$(SingleFileHostIntermediatePath)" CopyToOutputDirectory="Always" TargetPath="$(AssemblyName)$(_NativeExecutableExtension)" />
</ItemGroup>

<ItemGroup Condition="'$(_UpdateAppHostForPublish)' == 'true' and Exists('$(AppHostForPublishIntermediatePath)')">
<!-- Remove apphost from build from items to publish -->
<_SourceItemsToCopyToPublishDirectoryAlways Remove="$(AppHostIntermediatePath)" />
<_SourceItemsToCopyToPublishDirectory Remove="$(AppHostIntermediatePath)" />

<!-- Add the apphost created as part of publish -->
<_SourceItemsToCopyToPublishDirectoryAlways Include="$(AppHostForPublishIntermediatePath)" CopyToOutputDirectory="Always" TargetPath="$(AssemblyName)$(_NativeExecutableExtension)" />
</ItemGroup>

<ItemGroup>
<AllPublishItemsFullPathWithTargetPath Include="@(_SourceItemsToCopyToPublishDirectoryAlways->'%(FullPath)');@(_SourceItemsToCopyToPublishDirectory->'%(FullPath)')"/>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,11 @@ Copyright (c) .NET Foundation. All rights reserved.
'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and
$([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), 5.0))">true</_UseSingleFileHostForPublish>
<_DisableCetCompat Condition="'$(CetCompat)' == 'false'">true</_DisableCetCompat>

<!-- Default to AppHostDotNetSearch=AppRelative if AppHostRelativeDotNet is set -->
<AppHostDotNetSearch Condition="'$(AppHostRelativeDotNet)' != '' and '$(AppHostDotNetSearch)' == ''">AppRelative</AppHostDotNetSearch>
<_UpdateAppHostForPublish Condition="'$(_UseSingleFileHostForPublish)' != 'true' and
('$(AppHostRelativeDotNet)' != '' or '$(AppHostDotNetSearch)' != '')">true</_UpdateAppHostForPublish>
</PropertyGroup>
</Target>

Expand Down Expand Up @@ -851,6 +856,7 @@ Copyright (c) .NET Foundation. All rights reserved.
</PropertyGroup>
<PropertyGroup Condition="'$(UseAppHostFromAssetsFile)' == 'false' Or '@(_NativeRestoredAppHostNETCore)' != ''">
<AppHostIntermediatePath>$([System.IO.Path]::GetFullPath('$(IntermediateOutputPath)apphost$(_NativeExecutableExtension)'))</AppHostIntermediatePath>
<AppHostForPublishIntermediatePath>$([System.IO.Path]::GetFullPath('$(IntermediateOutputPath)apphost_publish$(_NativeExecutableExtension)'))</AppHostForPublishIntermediatePath>
<SingleFileHostIntermediatePath>$([System.IO.Path]::GetFullPath('$(IntermediateOutputPath)singlefilehost$(_NativeExecutableExtension)'))</SingleFileHostIntermediatePath>
</PropertyGroup>

Expand Down
Loading

0 comments on commit 521a00d

Please sign in to comment.