Skip to content

Commit 97fca8e

Browse files
authored
Merge pull request #11141 from unoplatform/mergify/bp/release/stable/4.7/pr-11126
docs: Add an article to enable faster single-targetframework builds in VS 2022 (backport #11126)
2 parents 311329f + 6995383 commit 97fca8e

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
uid: Build.Solution.TargetFramework-override
3+
---
4+
# Adjust an Uno Solution for a faster build with Visual Studio 2022
5+
6+
The Uno Platform template provides a cross-targeted Class library that includes multiple target frameworks. While building with the command line `dotnet build -f net7.0-ios` only builds the application's head and the class library for `net7.0-ios`, Visual Studio builds all the target frameworks, [regardless of the project head's target framework](https://developercommunity.visualstudio.com/t/Building-a-cross-targeted-project-with-m/651372).
7+
8+
Considering that during development, it is common to work on a single platform at a given time, here's a suggested set of modifications that can be performed on the solution to restrict the active build platform:
9+
10+
1. Let's create a set of solution filters to ensure that individual project heads can be loaded:
11+
12+
1. Create a new app template with **iOS**, **Android**, **WebAssembly** and **Windows** targets selected.
13+
1. Right click on the **.Mobile** and **.Wasm** projects and select **Unload Project**
14+
1. On the top level Solution node, right-click to select **Save As Solution Filter**, name the filter **MyApp-Windows-Only.slnf**
15+
1. Right-click on the **Mobile** project, select **Reload Project**
16+
1. Unload the **.Windows** project, then save a new solution filter called **MyApp-Mobile-Only.slnf**
17+
1. Repeat the operation with the **.Wasm** project, with a solution filter called **MyApp-Wasm-Only.slnf**
18+
19+
These solution filters will prevent Visual Studio to restore NuGet packages for TargetFrameworks that will be ignored by the configuration done below.
20+
21+
1. Now, next to the solution file, create a file named `targetframework-override.props`:
22+
23+
```xml
24+
<Project>
25+
<Import Project="solution-config.props" Condition="exists('solution-config.props')" />
26+
27+
<!-- Override the TargetFrameworks list with the one specified in MyAppTargetFrameworkOverride -->
28+
<PropertyGroup Condition="'$(MyAppTargetFrameworkOverride)'!=''">
29+
<TargetFrameworks>$(MyAppTargetFrameworkOverride)</TargetFrameworks>
30+
</PropertyGroup>
31+
</Project>
32+
```
33+
34+
1. Also next to the solution file, create a file named `solution-config.props.sample`:
35+
36+
```xml
37+
<Project>
38+
<PropertyGroup>
39+
<!--
40+
Uncomment the following line to enable single-target framework builds
41+
in order to get faster performance when debugging for a single platform.
42+
43+
Once this file is modified, use the appropriate solution filter to avoid
44+
NuGet restore issues.
45+
46+
Available target frameworks can be found in the project heads of your solution.
47+
-->
48+
<!-- <MyAppTargetFrameworkOverride>net7.0-ios</MyAppTargetFrameworkOverride> -->
49+
</PropertyGroup>
50+
</Project>
51+
```
52+
53+
1. Next, in all projects of the solution which are cross-targeted (with multiple TargetFrameworks values), add the following lines right after the `PropertyGroup` which defines `<TargetFrameworks>`:
54+
55+
```xml
56+
<!-- Import the TargetFramework override configuration -->
57+
<Import Project="../../targetframework-override.props" />
58+
```
59+
60+
The file should then look like this:
61+
62+
```xml
63+
<Project Sdk="Microsoft.NET.Sdk">
64+
<PropertyGroup>
65+
<TargetFrameworks>net7.0-windows10.0.18362;net7.0;net7.0-ios;net7.0-android</TargetFrameworks>
66+
</PropertyGroup>
67+
</Project>
68+
<!-- Import the TargetFramework override configuration -->
69+
<Import Project="../../targetframework-override.props" />
70+
```
71+
72+
> [!NOTE]
73+
> If the template is created with `dotnet new`, the path will instead be `../targetframework-override.props`
74+
75+
1. Create a copy of the file `solution-config.props.sample` next to itself, and name it `solution-config.props`
76+
1. If using git, add this specific file to the `.gitignore` so it never gets committed. This way, each developer can keep their own version of the file without corrupting the repository.
77+
1. Commit your changes to the repository.
78+
79+
At this point, your solution is ready for single-TargetFramework use.
80+
81+
For example, to work on `net7.0-ios`:
82+
83+
1. Before opening the solution, open the `solution-config.props` file and uncomment `MyAppTargetFrameworkOverride` to contain `net7.0-ios`
84+
1. Open the `MyApp-Mobile-Only.slnf` solution filter in Visual Studio 2022
85+
1. You should only see the **.Mobile** and **Class Library** projects in your solution
86+
1. When building and debugging the app, you'll only now build for the target specified in `solution-config.props`.
87+
88+
> [!IMPORTANT]
89+
> When changing the `MyAppTargetFrameworkOverride` value, make sure to close the solution and reload it so the build system recognizes properly the change.

doc/articles/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@
394394
href: wpf-migration.md
395395
- name: Creating a cross-targeted library
396396
href: cross-targeted-libraries.md
397+
- name: Faster Single Target App Builds
398+
href: solution-building-single-targetframework.md
397399
- name: Hosting a WebAssembly app
398400
href: how-to-host-a-webassembly-app.md
399401
- name: Updating to WinUI 3.0

doc/articles/uno-app-solution-structure.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ Dependencies in Uno solutions can be added preferably in the app's **Class Libra
2222
## Further information
2323

2424
See additional guides on handling platform-specific [C# code](platform-specific-csharp.md) and [XAML markup](platform-specific-xaml.md) in an Uno Platform project.
25+
26+
The Uno Platform solution also [can be further optimized](xref:Build.Solution.TargetFramework-override) to build larger projects with Visual Studio 2022.

0 commit comments

Comments
 (0)