dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.7k stars 1.06k forks source link

How to set TargetFramework* properties so it builds UAP off Windows? #11753

Open AArnott opened 4 years ago

AArnott commented 4 years ago

I have a class library that contains p/invokes to Windows Store-banned APIs. Today this project multi-targets netstandard2.0 and uap10.0 in order to conditionally exclude these banned p/invokes when compiling for store apps. But this prevents me from building my project off Windows.

So I dropped using the MSBuild.Sdk.Extras and tried my own homemade version:

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

  <PropertyGroup>
    <TargetFramework>uap10.0</TargetFramework>
    <TargetFrameworkIdentifier>.NETStandard</TargetFrameworkIdentifier>
    <TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
  </PropertyGroup>

</Project>

The idea being to just treat it like an ordinary project targeting netstandard2.0, except give it a uap10.0 TargetFramework name so that when packing it goes to a lib\uap10.0 folder.

But when doing this, dotnet build fails with this:

 dotnet build
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 33.46 ms for /home/andrew/git/tf_tests/tf_tests.csproj.
/usr/share/dotnet/sdk/3.1.201/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(234,5): error NETSDK1005: Assets file '/home/andrew/git/tf_tests/obj/project.assets.json' doesn't have a target for '.NETStandard,Version=v2.0'. Ensure that restore has run and that you have included 'netstandard2.0' in the TargetFrameworks for your project. [/home/andrew/git/tf_tests/tf_tests.csproj]

Build FAILED.
wli3 commented 4 years ago

@clairernovotny you might know?

clairernovotny commented 4 years ago

What API's are those? FWIW, I thought Windows Store was done banning APIs

clairernovotny commented 4 years ago

You can't build UAP off of windows, AFAIK. NuGet also complains about duplicate monikers -- and doing two builds of .NET Standard, v2.0 will cause problems. Virtually everything ignores the TFM itself.

AArnott commented 4 years ago

I have not heard that the Store isn't banning APIs any more. docs.microsoft.com still shows APIs as desktop only. Unless the Store were to somehow retroactively un-ban use of these APIs for all future submissions of apps even down to Win8, we need to need to exclude these p/invoke APIs from store apps.

You can't build UAP off of windows

I'm sure we can target the UAP TFM, given we don't actually need anything UAP specific and could totally spoof up all the .targets if we had to. I'm just looking for something much easier if it exists.

jalbertSyncroTech commented 4 years ago

@AArnott don't know if this link is still the best approach, but I found this github comment that links to a sdk-style .csproj configured to build UWP https://github.com/dotnet/sdk/issues/1408#issuecomment-330063812

AArnott commented 4 years ago

Thanks for the tip, @jalbertSyncroTech. That looks promising and I'll give it a try.