Skip to content

Commit 4ff164a

Browse files
Merge pull request #10386 from ghogen/msbuild-generated-files
Add section for handling generated files
2 parents 07a2d68 + 7e21e07 commit 4ff164a

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

docs/msbuild/customize-your-build.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ The location of the solution file is irrelevant to *Directory.Build.props*.
7070

7171
Properties that are set in *Directory.Build.props* can be overridden elsewhere in the project file or in imported files, so you should think of the settings in *Directory.Build.props* as specifying the defaults for your projects.
7272

73-
*Directory.Build.targets* is imported from *Microsoft.Common.targets* after importing *.targets* files from NuGet packages. So, it can override properties and targets defined in most of the build logic, or set properties for all your projects regardless of what the individual projects set.
73+
*Directory.Build.targets* is imported from *Microsoft.Common.targets* after importing `.targets` files from NuGet packages. So, it can override properties and targets defined in most of the build logic, or set properties for all your projects regardless of what the individual projects set.
7474

7575
When you need to set a property or define a target for an individual project that overrides any prior settings, put that logic in the project file after the final import. In order to do this in an SDK-style project, you first have to replace the SDK-style attribute with the equivalent imports. See [How to use MSBuild project SDKs](how-to-use-project-sdk.md).
7676

@@ -114,29 +114,29 @@ Or more simply: the first *Directory.Build.props* that doesn't import anything i
114114

115115
MSBuild is import-order dependent, and the last definition of a property (or a `UsingTask` or target) is the definition used.
116116

117-
When using explicit imports, you can import from a *.props* or *.targets* file at any point. Here is the widely used convention:
117+
When using explicit imports, you can import from a `.props` or `.targets` file at any point. Here is the widely used convention:
118118

119-
- *.props* files are imported early in the import order.
119+
- `.props` files are imported early in the import order.
120120

121-
- *.targets* files are imported late in the build order.
121+
- `.targets` files are imported late in the build order.
122122

123123
This convention is enforced by `<Project Sdk="SdkName">` imports (that is, the import of *Sdk.props* comes first, before all of the contents of the file, then *Sdk.targets* comes last, after all of the contents of the file).
124124

125125
When deciding where to put the properties, use the following general guidelines:
126126

127127
- For many properties, it doesn't matter where they're defined, because they're not overwritten and will be read only at execution time.
128128

129-
- For behavior that might be customized in an individual project, set defaults in *.props* files.
129+
- For behavior that might be customized in an individual project, set defaults in `.props` files.
130130

131-
- Avoid setting dependent properties in *.props* files by reading the value of a possibly customized property, because the customization won't happen until MSBuild reads the user's project.
131+
- Avoid setting dependent properties in `.props` files by reading the value of a possibly customized property, because the customization won't happen until MSBuild reads the user's project.
132132

133-
- Set dependent properties in *.targets* files, because they'll pick up customizations from individual projects.
133+
- Set dependent properties in `.targets` files, because they'll pick up customizations from individual projects.
134134

135-
- If you need to override properties, do it in a *.targets* file, after all user-project customizations have had a chance to take effect. Be cautious when using derived properties; derived properties may need to be overridden as well.
135+
- If you need to override properties, do it in a `.targets` file, after all user-project customizations have had a chance to take effect. Be cautious when using derived properties; derived properties may need to be overridden as well.
136136

137-
- Include items in *.props* files (conditioned on a property). All properties are considered before any item, so user-project property customizations get picked up, and this gives the user's project the opportunity to `Remove` or `Update` any item brought in by the import.
137+
- Include items in `.props` files (conditioned on a property). All properties are considered before any item, so user-project property customizations get picked up, and this gives the user's project the opportunity to `Remove` or `Update` any item brought in by the import.
138138

139-
- Define targets in *.targets* files. However, if the *.targets* file is imported by an SDK, remember that this scenario makes overriding the target more difficult because the user's project doesn't have a place to override it by default.
139+
- Define targets in `.targets` files. However, if the `.targets` file is imported by an SDK, remember that this scenario makes overriding the target more difficult because the user's project doesn't have a place to override it by default.
140140

141141
- If possible, prefer customizing properties at evaluation time over changing properties inside a target. This guideline makes it easier to load a project and understand what it's doing.
142142

@@ -190,6 +190,26 @@ If you need different behaviors depending on the .NET language (C#, Visual Basic
190190
</PropertyGroup>
191191
```
192192

193+
## Handle generated files
194+
195+
In any given build, files that get generated during the build behave differently from static files (such as source files). For this reason, it's important to understand [How MSBuild Builds Projects](build-process-overview.md). The two phases are the [evaluation phase](build-process-overview.md#evaluation-phase) and the [execution phase](build-process-overview.md#execution-phase). During the evaluation phase, MSBuild reads your project, imports everything, creates properties, expands globs for items, and sets up the build process. During the execution phase, MSBuild performs the build by running targets and tasks with the data it parsed during the evaluation phase.
196+
197+
Files generated during execution don't exist during the evaluation phase, therefore they aren't included in the build process. To solve this problem, you must manually add the generated files into the build process. The recommended way to do this is by adding the new file to the `Content` or `None` items before the `BeforeBuild` target, as in the following example:
198+
199+
```xml
200+
<Target Name="MyTarget" BeforeTargets="BeforeBuild">
201+
202+
<!-- Some logic that generates your file goes here -->
203+
<!-- Generated files should be placed in $(IntermediateOutputPath) -->
204+
205+
<ItemGroup>
206+
<None Include="$(IntermediateOutputPath)my-generated-file.xyz" CopyToOutputDirectory="PreserveNewest"/>
207+
</ItemGroup>
208+
</Target>
209+
```
210+
211+
Adding your generated file to `None` or `Content` is sufficient for the build process to see it. You also want to ensure it gets added at the right time. Ideally, your target runs before `BeforeBuild`. `AssignTargetPaths` is another possible target, as it is the final opportunity to modify `None` and `Content` items (among others) before they are transformed into new items. See [Common Item Types](common-msbuild-project-items.md).
212+
193213
## Customize the solution build
194214

195215
> [!IMPORTANT]
@@ -245,17 +265,17 @@ If you have a dedicated build server and want to ensure that certain targets alw
245265
246266
## Customize C++ builds
247267

248-
For C++ projects, the previously mentioned custom *.targets* and *.props* files cannot be used in the same way to override default settings. *Directory.Build.props* is imported by *Microsoft.Common.props*, which is imported in `Microsoft.Cpp.Default.props` while most of the defaults are defined in *Microsoft.Cpp.props* and for a number of properties a "if not yet defined" condition cannot be used, as the property is already defined, but the default needs to be different for particular project properties defined in `PropertyGroup` with `Label="Configuration"` (see [.vcxproj and .props file structure](/cpp/build/reference/vcxproj-file-structure)).
268+
For C++ projects, the previously mentioned custom `.targets` and `.props` files cannot be used in the same way to override default settings. *Directory.Build.props* is imported by *Microsoft.Common.props*, which is imported in `Microsoft.Cpp.Default.props` while most of the defaults are defined in *Microsoft.Cpp.props* and for a number of properties a "if not yet defined" condition cannot be used, as the property is already defined, but the default needs to be different for particular project properties defined in `PropertyGroup` with `Label="Configuration"` (see [.vcxproj and .props file structure](/cpp/build/reference/vcxproj-file-structure)).
249269

250-
But, you can use the following properties to specify *.props* file(s) to be automatically imported before/after *Microsoft.Cpp.\** files:
270+
But, you can use the following properties to specify `.props` file(s) to be automatically imported before/after *Microsoft.Cpp.\** files:
251271

252272
- ForceImportAfterCppDefaultProps
253273
- ForceImportBeforeCppProps
254274
- ForceImportAfterCppProps
255275
- ForceImportBeforeCppTargets
256276
- ForceImportAfterCppTargets
257277

258-
To customize the default values of properties for all C++ builds, create another *.props* file (say, *MyProps.props*), and define the `ForceImportAfterCppProps` property in `Directory.Build.props` pointing to it:
278+
To customize the default values of properties for all C++ builds, create another `.props` file (say, *MyProps.props*), and define the `ForceImportAfterCppProps` property in `Directory.Build.props` pointing to it:
259279

260280
```xml
261281
<PropertyGroup>

0 commit comments

Comments
 (0)