dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.27k stars 4.73k forks source link

[NativeAOT] dotnet publish fails for multi-target projects #107807

Open Quake4 opened 1 month ago

Quake4 commented 1 month ago

Description

If the project has multiple targets, then the Aot build does not work.

Reproduction Steps

Project

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net8.0-windows;net46</TargetFrameworks>
  </PropertyGroup>

</Project>

Run command dotnet publish -r win-x64 -f net8.0-windows -c Release -p:PublishAot=true

Expected behavior

build aot

Actual behavior

Error:

  Определение проектов для восстановления...
C:\Program Files\dotnet\sdk\8.0.400\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.FrameworkReferenceResolution.targets(90,5): error NETSDK1207: AOT-компиляция не поддерживается целевой платформой. [d:\GIT\Test\AudioAot\AudioAot.csproj::TargetFramework=net46]

Regression?

No response

Known Workarounds

Change Project to single Target

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
  </PropertyGroup>

</Project>

Configuration

net8 windows10 x64 no no

Other information

And check main project with reference to project with other common (netstandard2.0) targets: <TargetFrameworks>netstandard2.0;net46</TargetFrameworks>

It should also compile correctly.

dotnet-policy-service[bot] commented 1 month ago

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas See info in area-owners.md if you want to be subscribed.

jkotas commented 1 month ago

C:\Program Files\dotnet\sdk\8.0.400\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.FrameworkReferenceResolution.targe ts(90,5): error NETSDK1207: Ahead-of-time compilation is not supported for the target framework. [C:\repro\repro.csproj ::TargetFramework=net46]

agocke commented 1 month ago

This is currently by-design. The fix is to avoid setting PublishAot via -p as that will apply to all target frameworks.

Instead, put the following in your project file:

<PublishAot Condition="'$(TargetFramework)' != 'net46'">true</PublishAot>

Now AOT will be skipped for net46, where it is not supported.

Quake4 commented 1 month ago

Then what is the meaning of the -f key? This is an indication of the target platform. Where is the logic?