Commit 76c076f
authored
Add support for Project Specific RegisterTaskObject. (#199)
Context: dotnet/maui#11605
Context: dotnet/maui#11387 (comment)
Context: http://github.com/xamarin/xamarin-android/commit/8bc7a3e84f95e70fe12790ac31ecd97957771cb2
In dotnet/maui#11605, when `$(AndroidEnableMarshalMethods)`=True
(dotnet/android@8bc7a3e8), the build may fail if the `.sln`
contains more than one Android "App" project. We tracked this down
to "undesired sharing" between project builds; the `obj` provided to
[`IBuildEngine4.RegisterTaskObject()`][0] can be visible across
project builds. Consider [`<GeneratePackageManagerJava/>`][1]:
var marshalMethodsState = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<MarshalMethodsState> (".:!MarshalMethods!:.", RegisteredTaskObjectLifetime.Build);
Further consider a `.sln` with two "App" projects, as the "App"
project build hits `<GeneratePackageManagerJava/>`.
The lifetime of `.Build` is *not* tied to the the `Build` target of a
given `.csproj`; rather, it's for the *build process*. This can
result in:
1. `dotnet build Project.sln` is run; `Project.sln` references
`App1.csproj` and `App2.csproj`.
2. `App1.csproj` is built.
3. `App1.csproj` calls `<GeneratePackageManagerJava/>`.
4. `App2.csproj` is later built as part in the process, and *also*
calls `<GeneratePackageManagerJava/>`.
In particular note the key within `<GeneratePackageManagerJava/>`:
`".:!MarshalMethods!:."`. This value is unchanged, and means that
that when `App2.csproj` is built, it will be using the same key as
was used with `App1.csproj`, and thus could be inadvertently using
data intended for `App1.csproj`!
This would result build errors:
…\Xamarin.Android.Common.targets(1717,3): error XAGPM7009: System.InvalidOperationException: Unable to translate assembly name 'Xamarin.AndroidX.Activity' to its index
…\Xamarin.Android.Common.targets(1717,3): error XAGPM7009: at Xamarin.Android.Tasks.MarshalMethodsNativeAssemblyGenerator.WriteNativeMethods(LlvmIrGenerator generator, Dictionary`2 asmNameToIndex, LlvmIrVariableReference get_function_pointer_ref)
…\Xamarin.Android.Common.targets(1717,3): error XAGPM7009: at Xamarin.Android.Tasks.MarshalMethodsNativeAssemblyGenerator.Write(LlvmIrGenerator generator)
…\Xamarin.Android.Common.targets(1717,3): error XAGPM7009: at Xamarin.Android.Tasks.LLVMIR.LlvmIrComposer.Write(AndroidTargetArch arch, StreamWriter output, String fileName)
…\Xamarin.Android.Common.targets(1717,3): error XAGPM7009: at Xamarin.Android.Tasks.GeneratePackageManagerJava.AddEnvironment()
…\Xamarin.Android.Common.targets(1717,3): error XAGPM7009: at Xamarin.Android.Tasks.GeneratePackageManagerJava.RunTask()
The sharing of `RegisterTaskObject` data across project builds is
rarely desirable. There are a few instances where it is safe to share
the registered objects between projects, e.g. `java` version
information (keyed on `java` path). However, most of the time it is
specific to the project that is being built. Historically we have
probably got away with this because "most" users only have one project.
Update the `MSBuildExtensions` extension methods to include an
additional `RegisterTaskObjectKeyFlags` parameter:
[Flags]
public enum RegisterTaskObjectKeyFlags {
None = 0,
IncludeProjectFile = 1 << 0,
}
Allowing:
var marshalMethodsState = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<MarshalMethodsState> (
".:!MarshalMethods!:.",
RegisteredTaskObjectLifetime.Build,
RegisterTaskObjectKeyFlags.IncludeProjectFile
);
When `RegisterTaskObjectKeyFlags.IncludeProjectFile` is specified,
then [`IBuildEngine.ProjectFileOfTaskNode`][2] is used as part of
the key with `RegisterTaskObject()`. This helps ensure that builds
in different `.csproj` files will result in different keys.
The previous `MSBuildExtensions.GetRegisteredTaskObjectAssemblyLocal()`
and related overloads have been updated so that
`RegisterTaskObjectKeyFlags.IncludeProjectFile` is used by default.
[0]: https://learn.microsoft.com/en-us/dotnet/api/microsoft.build.framework.ibuildengine4.registertaskobject?view=msbuild-17-netcore
[1]: https://github.com/xamarin/xamarin-android/blob/c92ae5eb9fdcb3a2fd7c20f5b42dddf8b3ea781a/src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs#L407
[2]: https://learn.microsoft.com/en-us/dotnet/api/microsoft.build.framework.ibuildengine.projectfileoftasknode?view=msbuild-17-netcore1 parent 9f02d77 commit 76c076f
1 file changed
+58
-6
lines changedLines changed: 58 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
16 | 22 | | |
17 | 23 | | |
18 | 24 | | |
| |||
252 | 258 | | |
253 | 259 | | |
254 | 260 | | |
| 261 | + | |
255 | 262 | | |
256 | | - | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
257 | 270 | | |
258 | 271 | | |
259 | 272 | | |
260 | 273 | | |
| 274 | + | |
261 | 275 | | |
262 | | - | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
263 | 284 | | |
264 | 285 | | |
265 | 286 | | |
266 | 287 | | |
| 288 | + | |
267 | 289 | | |
268 | | - | |
269 | | - | |
| 290 | + | |
270 | 291 | | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
271 | 298 | | |
272 | 299 | | |
273 | 300 | | |
274 | 301 | | |
| 302 | + | |
275 | 303 | | |
276 | | - | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
277 | 311 | | |
278 | 312 | | |
279 | 313 | | |
280 | 314 | | |
| 315 | + | |
281 | 316 | | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
282 | 323 | | |
283 | | - | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
284 | 336 | | |
285 | 337 | | |
0 commit comments