|
| 1 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +namespace FSharp.Build |
| 4 | + |
| 5 | +open System |
| 6 | +open System.IO |
| 7 | +open System.Text |
| 8 | +open Microsoft.Build.Framework |
| 9 | +open Microsoft.Build.Utilities |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// MSBuild task that generates ILLink.Substitutions.xml file to remove F# metadata resources during IL linking. |
| 13 | +/// </summary> |
| 14 | +type GenerateILLinkSubstitutions() = |
| 15 | + inherit Task() |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Assembly name to use when generating resource names to be removed. |
| 19 | + /// </summary> |
| 20 | + [<Required>] |
| 21 | + member val AssemblyName = "" with get, set |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Intermediate output path for storing the generated file. |
| 25 | + /// </summary> |
| 26 | + [<Required>] |
| 27 | + member val IntermediateOutputPath = "" with get, set |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Generated embedded resource items. |
| 31 | + /// </summary> |
| 32 | + [<Output>] |
| 33 | + member val GeneratedItems = [||]: ITaskItem[] with get, set |
| 34 | + |
| 35 | + override this.Execute() = |
| 36 | + try |
| 37 | + // Define the resource prefixes that need to be removed |
| 38 | + let resourcePrefixes = |
| 39 | + [| |
| 40 | + // Signature variants |
| 41 | + yield! |
| 42 | + [| |
| 43 | + for dataType in [| "Data"; "DataB" |] do |
| 44 | + for compression in [| ""; "Compressed" |] do |
| 45 | + yield $"FSharpSignature{compression}{dataType}" |
| 46 | + |] |
| 47 | + |
| 48 | + // Optimization variants |
| 49 | + yield! |
| 50 | + [| |
| 51 | + for dataType in [| "Data"; "DataB" |] do |
| 52 | + for compression in [| ""; "Compressed" |] do |
| 53 | + yield $"FSharpOptimization{compression}{dataType}" |
| 54 | + |] |
| 55 | + |
| 56 | + // Info variants |
| 57 | + yield "FSharpOptimizationInfo" |
| 58 | + yield "FSharpSignatureInfo" |
| 59 | + |] |
| 60 | + |
| 61 | + // Generate the XML content |
| 62 | + let sb = StringBuilder(4096) // pre-allocate capacity |
| 63 | + sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>") |> ignore |
| 64 | + sb.AppendLine("<linker>") |> ignore |
| 65 | + sb.AppendLine($" <assembly fullname=\"{this.AssemblyName}\">") |> ignore |
| 66 | + |
| 67 | + // Add each resource entry with proper closing tag on the same line |
| 68 | + for prefix in resourcePrefixes do |
| 69 | + sb.AppendLine($" <resource name=\"{prefix}.{this.AssemblyName}\" action=\"remove\"></resource>") |
| 70 | + |> ignore |
| 71 | + |
| 72 | + // Close assembly and linker tags |
| 73 | + sb.AppendLine(" </assembly>") |> ignore |
| 74 | + sb.AppendLine("</linker>") |> ignore |
| 75 | + |
| 76 | + let xmlContent = sb.ToString() |
| 77 | + |
| 78 | + // Create a file in the intermediate output path |
| 79 | + let outputFileName = |
| 80 | + Path.Combine(this.IntermediateOutputPath, "ILLink.Substitutions.xml") |
| 81 | + |
| 82 | + Directory.CreateDirectory(this.IntermediateOutputPath) |> ignore |
| 83 | + File.WriteAllText(outputFileName, xmlContent) |
| 84 | + |
| 85 | + // Create a TaskItem for the generated file |
| 86 | + let item = TaskItem(outputFileName) :> ITaskItem |
| 87 | + item.SetMetadata("LogicalName", "ILLink.Substitutions.xml") |
| 88 | + |
| 89 | + this.GeneratedItems <- [| item |] |
| 90 | + true |
| 91 | + with ex -> |
| 92 | + this.Log.LogErrorFromException(ex, true) |
| 93 | + false |
0 commit comments