Skip to content

Commit 90b7e8a

Browse files
authored
Merge pull request #40 from slyshykO/fix-native-aot
Fix NativeAOT startup with embedded trimming descriptor
2 parents d06cfcd + ff47b6e commit 90b7e8a

9 files changed

Lines changed: 117 additions & 3 deletions

File tree

‎.github/workflows/dotnet.yml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ jobs:
1919
- name: Setup .NET
2020
uses: actions/setup-dotnet@v6
2121
with:
22-
dotnet-version: 6.0.x
22+
dotnet-version: |
23+
6.0.x
24+
10.0.x
2325
- name: Run CI
2426
run: dotnet fsi src/build.fsx
27+
- name: Test NativeAOT package consumer
28+
run: dotnet fsi src/build.fsx -- -p NativeAOT

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ nupkg
4343
_UpgradeReport_Files/
4444
[Pp]ackages/
4545

46+
.vscode/
47+
4648
Thumbs.db
4749
Desktop.ini
4850
.DS_Store
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<linker>
2+
<!-- SafeInputLists looks up ListModule.OfArray by name during initialization. -->
3+
<assembly fullname="FSharp.Core">
4+
<type fullname="Microsoft.FSharp.Collections.ListModule">
5+
<method name="OfArray" />
6+
</type>
7+
</assembly>
8+
</linker>

‎src/FSharp.SystemCommandLine/FSharp.SystemCommandLine.fsproj‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
<Compile Include="CommandBuilders.fs" />
2020
</ItemGroup>
2121

22+
<ItemGroup>
23+
<EmbeddedResource Include="FSharp.SystemCommandLine.TrimmerRoots.xml"
24+
LogicalName="ILLink.Descriptors.xml" />
25+
</ItemGroup>
26+
2227
<ItemGroup>
2328
<PackageReference Include="System.CommandLine" Version="2.0.0" />
2429
</ItemGroup>

‎src/TestConsole/TestConsole.fsproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFrameworks>net6.0;net10.0</TargetFrameworks>
66
</PropertyGroup>
77

88
<ItemGroup>

‎src/TestNativeAot/Program.fs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
open System
2+
open System.Runtime.CompilerServices
3+
open FSharp.SystemCommandLine
4+
open Input
5+
6+
[<EntryPoint>]
7+
let main argv =
8+
if RuntimeFeature.IsDynamicCodeSupported then
9+
failwith "This smoke test must run as a NativeAOT executable."
10+
11+
rootCommand argv {
12+
description "NativeAOT startup and scalar/array parsing regression test"
13+
inputs (argument<string array> "words" |> arity ZeroOrMore, option<int> "--count")
14+
setAction (fun (words, count) ->
15+
// Avoid printf's own reflection requirements so this tests the library's input path.
16+
Console.WriteLine("words=" + String.Join(",", words) + ";count=" + count.ToString())
17+
)
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<PublishAot>true</PublishAot>
6+
<IsPackable>false</IsPackable>
7+
<RestoreAdditionalProjectSources>$(MSBuildThisFileDirectory)obj/packages</RestoreAdditionalProjectSources>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Compile Include="Program.fs" />
12+
<!-- Consume the package, including its embedded descriptor, without application roots. -->
13+
<PackageReference Include="FSharp.SystemCommandLine" Version="$(SmokePackageVersion)" />
14+
</ItemGroup>
15+
</Project>

‎src/TestNativeAot/global.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"sdk": {
3+
"version": "10.0.100",
4+
"rollForward": "latestFeature",
5+
"allowPrerelease": false
6+
}
7+
}

‎src/build.fsx‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#r "nuget: Fun.Build, 1.1.17"
22

33
open Fun.Build
4+
open System
5+
open System.Diagnostics
6+
open System.IO
7+
open System.Runtime.InteropServices
48

59
let src = __SOURCE_DIRECTORY__
610
let fsproj = $"{src}/FSharp.SystemCommandLine/FSharp.SystemCommandLine.fsproj"
@@ -19,6 +23,57 @@ pipeline "Build" {
1923
runIfOnlySpecified false
2024
}
2125

26+
// Use a unique package version so repeated local runs cannot reuse a cached build.
27+
let smokeDir = Path.Combine(src, "TestNativeAot")
28+
let smokePackages = Path.Combine(smokeDir, "obj", "packages")
29+
let smokeOutput = Path.Combine(smokeDir, "bin", "native-aot")
30+
let smokeVersion = "0.0.0-aot-" + Guid.NewGuid().ToString("N")
31+
let smokeExecutable =
32+
Path.Combine(smokeOutput, if OperatingSystem.IsWindows() then "TestNativeAot.exe" else "TestNativeAot")
33+
34+
let checkNativeAot args expectedExitCode expectedOutput =
35+
let startInfo = ProcessStartInfo(smokeExecutable)
36+
startInfo.UseShellExecute <- false
37+
startInfo.RedirectStandardOutput <- true
38+
startInfo.RedirectStandardError <- true
39+
for arg in args do startInfo.ArgumentList.Add(arg)
40+
use child = Process.Start(startInfo)
41+
let stdout = child.StandardOutput.ReadToEndAsync()
42+
let stderr = child.StandardError.ReadToEndAsync()
43+
if not (child.WaitForExit(30_000)) then
44+
child.Kill(true)
45+
failwithf "NativeAOT smoke test timed out: %A" args
46+
let output = stdout.GetAwaiter().GetResult()
47+
let errors = stderr.GetAwaiter().GetResult()
48+
if child.ExitCode <> expectedExitCode || not (output.Contains(expectedOutput: string)) then
49+
failwithf "NativeAOT smoke test %A: expected exit %d and output containing %A; got exit %d.\n%s\n%s"
50+
args expectedExitCode expectedOutput child.ExitCode output errors
51+
printfn "NativeAOT smoke test passed: %A" args
52+
53+
pipeline "NativeAOT" {
54+
// The consumer's global.json selects .NET 10 for both pack and publish.
55+
workingDir smokeDir
56+
57+
stage "Pack library for NativeAOT" {
58+
run $"dotnet pack \"{fsproj}\" --configuration Release --output \"{smokePackages}\" -p:PackageVersion={smokeVersion}"
59+
}
60+
61+
stage "Publish NativeAOT package consumer" {
62+
run $"dotnet publish TestNativeAot.fsproj --configuration Release --runtime {RuntimeInformation.RuntimeIdentifier} --output \"{smokeOutput}\" -p:SmokePackageVersion={smokeVersion}"
63+
}
64+
65+
stage "Run NativeAOT smoke tests" {
66+
run (fun _ ->
67+
checkNativeAot [ "--help" ] 0 "--count"
68+
checkNativeAot [ "hello"; "world"; "--count"; "2" ] 0 "words=hello,world;count=2"
69+
checkNativeAot [] 0 "words=;count=0"
70+
checkNativeAot [ "--count"; "invalid" ] 1 ""
71+
)
72+
}
73+
74+
runIfOnlySpecified true
75+
}
76+
2277
open System.Xml.Linq
2378

2479
pipeline "Publish" {
@@ -39,4 +94,4 @@ pipeline "Publish" {
3994
runIfOnlySpecified true
4095
}
4196

42-
tryPrintPipelineCommandHelp ()
97+
tryPrintPipelineCommandHelp ()

0 commit comments

Comments
 (0)