-
Notifications
You must be signed in to change notification settings - Fork 820
[WIP] Auto-generate IlLink.Substitutions.xml to Remove F# Metadata Resources #18592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0cd324b
e01d09d
3912a52
ab29090
3aff8db
793060a
d7202c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
||
namespace FSharp.Build | ||
|
||
open System | ||
open System.IO | ||
open System.Text | ||
open Microsoft.Build.Framework | ||
open Microsoft.Build.Utilities | ||
|
||
/// <summary> | ||
/// MSBuild task that generates ILLink.Substitutions.xml file to remove F# metadata resources during IL linking. | ||
/// </summary> | ||
type GenerateILLinkSubstitutions() = | ||
inherit Task() | ||
|
||
/// <summary> | ||
/// Assembly name to use when generating resource names to be removed. | ||
/// </summary> | ||
[<Required>] | ||
member val AssemblyName = "" with get, set | ||
|
||
/// <summary> | ||
/// Intermediate output path for storing the generated file. | ||
/// </summary> | ||
[<Required>] | ||
member val IntermediateOutputPath = "" with get, set | ||
|
||
/// <summary> | ||
/// Generated embedded resource items. | ||
/// </summary> | ||
[<o>] | ||
Check failure on line 32 in src/FSharp.Build/GenerateILLinkSubstitutions.fs
|
||
member val GeneratedItems = [| |] : ITaskItem[] with get, set | ||
|
||
override this.Execute() = | ||
try | ||
// Define the resource prefixes that need to be removed | ||
let resourcePrefixes = | ||
[| | ||
// Signature variants | ||
yield! [| for dataType in [| "Data"; "DataB" |] do | ||
for compression in [| ""; "Compressed" |] do | ||
yield $"FSharpSignature{compression}{dataType}" |] | ||
|
||
// Optimization variants | ||
yield! [| for dataType in [| "Data"; "DataB" |] do | ||
for compression in [| ""; "Compressed" |] do | ||
yield $"FSharpOptimization{compression}{dataType}" |] | ||
|
||
// Info variants | ||
yield "FSharpOptimizationInfo" | ||
yield "FSharpSignatureInfo" | ||
|] | ||
|
||
// Generate the XML content | ||
let sb = StringBuilder(4096) // pre-allocate capacity | ||
sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>") |> ignore | ||
sb.AppendLine("<linker>") |> ignore | ||
sb.AppendLine($" <assembly fullname=\"{this.AssemblyName}\">") |> ignore | ||
|
||
// Add each resource entry with proper closing tag on the same line | ||
for prefix in resourcePrefixes do | ||
sb.AppendLine($" <resource name=\"{prefix}.{this.AssemblyName}\" action=\"remove\"></resource>") |> ignore | ||
|
||
// Close assembly and linker tags | ||
sb.AppendLine(" </assembly>") |> ignore | ||
sb.AppendLine("</linker>") |> ignore | ||
|
||
let xmlContent = sb.ToString() | ||
|
||
// Create a file in the intermediate output path | ||
let outputFileName = Path.Combine(this.IntermediateOutputPath, "ILLink.Substitutions.xml") | ||
Directory.CreateDirectory(this.IntermediateOutputPath) |> ignore | ||
File.WriteAllText(outputFileName, xmlContent) | ||
|
||
// Create a TaskItem for the generated file | ||
let item = TaskItem(outputFileName) :> ITaskItem | ||
item.SetMetadata("LogicalName", "ILLink.Substitutions.xml") | ||
|
||
this.GeneratedItems <- [| item |] | ||
true | ||
with ex -> | ||
this.Log.LogErrorFromException(ex, true) | ||
false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>net9.0</TargetFrameworks> | ||
<LangVersion>preview</LangVersion> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference> | ||
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder> | ||
<DisableImplicitLibraryPacksFolder>true</DisableImplicitLibraryPacksFolder> | ||
<PublishTrimmed>true</PublishTrimmed> | ||
<RuntimeIdentifier>win-x64</RuntimeIdentifier> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You also need to make sure that the new targets and freshly build FSharpBuildAssemblyFile is used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've updated the test project to explicitly set the FSharpBuildAssemblyFile property to point to the freshly built FSharp.Build.dll, and added imports for the custom targets files directly from the repository to ensure it uses the latest version with our new task. These changes are in commit 74bce89. |
||
<DotnetFscCompilerPath>$(MSBuildThisFileDirectory)../../../../artifacts/bin/fsc/Release/net9.0/fsc.dll</DotnetFscCompilerPath> | ||
<Fsc_DotNET_DotnetFscCompilerPath>$(MSBuildThisFileDirectory)../../../../artifacts/bin/fsc/Release/net9.0/fsc.dll</Fsc_DotNET_DotnetFscCompilerPath> | ||
<FSharpBuildAssemblyFile>$(MSBuildThisFileDirectory)../../../../artifacts/bin/FSharp.Build/Release/net9.0/FSharp.Build.dll</FSharpBuildAssemblyFile> | ||
<FSharpPreferNetFrameworkTools>False</FSharpPreferNetFrameworkTools> | ||
<FSharpPrefer64BitTools>True</FSharpPrefer64BitTools> | ||
</PropertyGroup> | ||
|
||
<!-- Import the custom targets file directly from the repository to use the latest version --> | ||
<Import Project="$(MSBuildThisFileDirectory)../../../../src/FSharp.Build/Microsoft.FSharp.NetSdk.props" /> | ||
<Import Project="$(MSBuildThisFileDirectory)../../../../src/FSharp.Build/Microsoft.FSharp.NetSdk.targets" /> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\Program.fs" /> | ||
</ItemGroup> | ||
|
||
<Import Project="$(MSBuildThisFileDirectory)../../../../eng/Versions.props" /> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FSharp.Core" Version="$(FSharpCorePreviewPackageVersionValue)"/> | ||
</ItemGroup> | ||
|
||
</Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type
o
is not defined, this is wrong and causes a build failure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot