From 002bd8699fdec359e631ed0717e095f50fa61495 Mon Sep 17 00:00:00 2001
From: ncave <777696+ncave@users.noreply.github.com>
Date: Mon, 26 Oct 2020 22:11:04 -0700
Subject: [PATCH] Export metadata
---
.vscode/launch.json | 18 ++++
eng/Versions.props | 4 +-
fcs/build.sh | 5 ++
fcs/fcs-export/Program.fs | 88 +++++++++++++++++++
fcs/fcs-export/fcs-export.fsproj | 24 +++++
src/buildtools/buildtools.targets | 4 +-
src/fsharp/CompilerImports.fs | 32 +++++++
.../FSharp.Compiler.Service.fsproj | 1 +
src/fsharp/absil/ilwrite.fs | 6 ++
9 files changed, 178 insertions(+), 4 deletions(-)
create mode 100644 .vscode/launch.json
create mode 100644 fcs/build.sh
create mode 100644 fcs/fcs-export/Program.fs
create mode 100644 fcs/fcs-export/fcs-export.fsproj
diff --git a/.vscode/launch.json b/.vscode/launch.json
new file mode 100644
index 000000000000..86874819df83
--- /dev/null
+++ b/.vscode/launch.json
@@ -0,0 +1,18 @@
+{
+ // Use IntelliSense to learn about possible attributes.
+ // Hover to view descriptions of existing attributes.
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+ "version": "0.2.0",
+ "configurations": [
+ {
+ "name": ".NET Core Launch (console)",
+ "type": "coreclr",
+ "request": "launch",
+ "program": "${workspaceFolder}/artifacts/bin/fcs-export/Debug/netcoreapp3.1/fcs-export.dll",
+ "args": [],
+ "cwd": "${workspaceFolder}/fcs/fcs-export",
+ "console": "internalConsole",
+ "stopAtEntry": false
+ }
+ ]
+}
\ No newline at end of file
diff --git a/eng/Versions.props b/eng/Versions.props
index 4d4f0b03af3d..b4fcb9799107 100644
--- a/eng/Versions.props
+++ b/eng/Versions.props
@@ -68,7 +68,7 @@
4.5.1
- 5.0.0-preview.8.20407.11
+ 1.7.1
4.3.0
4.3.0
4.0.0
@@ -81,7 +81,7 @@
4.3.0
4.3.0
4.3.0
- 5.0.0-preview.8.20407.11
+ 1.8.1
4.3.0
1.5.0
4.3.0
diff --git a/fcs/build.sh b/fcs/build.sh
new file mode 100644
index 000000000000..c1330e403a80
--- /dev/null
+++ b/fcs/build.sh
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+
+dotnet build -c Release src/buildtools/buildtools.proj
+dotnet build -c Release src/fsharp/FSharp.Compiler.Service
+dotnet run -c Release -p fcs/fcs-export
diff --git a/fcs/fcs-export/Program.fs b/fcs/fcs-export/Program.fs
new file mode 100644
index 000000000000..7d40871cc716
--- /dev/null
+++ b/fcs/fcs-export/Program.fs
@@ -0,0 +1,88 @@
+open System.IO
+open System.Collections.Generic
+open FSharp.Compiler
+open FSharp.Compiler.SourceCodeServices
+
+let readRefs (folder : string) (projectFile: string) =
+ let runProcess (workingDir: string) (exePath: string) (args: string) =
+ let psi = System.Diagnostics.ProcessStartInfo()
+ psi.FileName <- exePath
+ psi.WorkingDirectory <- workingDir
+ psi.RedirectStandardOutput <- false
+ psi.RedirectStandardError <- false
+ psi.Arguments <- args
+ psi.CreateNoWindow <- true
+ psi.UseShellExecute <- false
+
+ use p = new System.Diagnostics.Process()
+ p.StartInfo <- psi
+ p.Start() |> ignore
+ p.WaitForExit()
+
+ let exitCode = p.ExitCode
+ exitCode, ()
+
+ let runCmd exePath args = runProcess folder exePath (args |> String.concat " ")
+ let msbuildExec = Dotnet.ProjInfo.Inspect.dotnetMsbuild runCmd
+ let result = Dotnet.ProjInfo.Inspect.getProjectInfo ignore msbuildExec Dotnet.ProjInfo.Inspect.getFscArgs projectFile
+ match result with
+ | Ok(Dotnet.ProjInfo.Inspect.GetResult.FscArgs x) ->
+ x
+ |> List.filter (fun s -> s.StartsWith("-r:"))
+ |> List.map (fun s -> s.Replace("-r:", ""))
+ | _ -> []
+
+let mkStandardProjectReferences () =
+ let file = "fcs-export.fsproj"
+ let projDir = __SOURCE_DIRECTORY__
+ readRefs projDir file
+
+let mkProjectCommandLineArgsForScript (dllName, fileNames) =
+ [| yield "--simpleresolution"
+ yield "--noframework"
+ yield "--debug:full"
+ yield "--define:DEBUG"
+ yield "--optimize-"
+ yield "--out:" + dllName
+ yield "--doc:test.xml"
+ yield "--warn:3"
+ yield "--fullpaths"
+ yield "--flaterrors"
+ yield "--target:library"
+ for x in fileNames do
+ yield x
+ let references = mkStandardProjectReferences ()
+ for r in references do
+ yield "-r:" + r
+ |]
+
+let checker = FSharpChecker.Create()
+
+let parseAndCheckScript (file, input) =
+ let dllName = Path.ChangeExtension(file, ".dll")
+ let projName = Path.ChangeExtension(file, ".fsproj")
+ let args = mkProjectCommandLineArgsForScript (dllName, [file])
+ printfn "file: %s" file
+ args |> Array.iter (printfn "args: %s")
+ let projectOptions = checker.GetProjectOptionsFromCommandLineArgs (projName, args)
+ let parseRes, typedRes = checker.ParseAndCheckFileInProject(file, 0, input, projectOptions) |> Async.RunSynchronously
+
+ if parseRes.Errors.Length > 0 then
+ printfn "---> Parse Input = %A" input
+ printfn "---> Parse Error = %A" parseRes.Errors
+
+ match typedRes with
+ | FSharpCheckFileAnswer.Succeeded(res) -> parseRes, res
+ | res -> failwithf "Parsing did not finish... (%A)" res
+
+[]
+let main argv =
+ ignore argv
+ printfn "Exporting metadata..."
+ let file = "/temp/test.fsx"
+ let input = "let a = 42"
+ let sourceText = FSharp.Compiler.Text.SourceText.ofString input
+ // parse script just to export metadata
+ let parseRes, typedRes = parseAndCheckScript(file, sourceText)
+ printfn "Exporting is done."
+ 0
diff --git a/fcs/fcs-export/fcs-export.fsproj b/fcs/fcs-export/fcs-export.fsproj
new file mode 100644
index 000000000000..75d2f32b46ea
--- /dev/null
+++ b/fcs/fcs-export/fcs-export.fsproj
@@ -0,0 +1,24 @@
+
+
+
+ Exe
+ netcoreapp3.1
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/buildtools/buildtools.targets b/src/buildtools/buildtools.targets
index 86346fc2a156..25effd1d61e2 100644
--- a/src/buildtools/buildtools.targets
+++ b/src/buildtools/buildtools.targets
@@ -20,7 +20,7 @@
BeforeTargets="CoreCompile">
- $(ArtifactsDir)\Bootstrap\fslex\fslex.dll
+ $(ArtifactsDir)\bin\fslex\Release\netcoreapp3.1\fslex.dll
@@ -44,7 +44,7 @@
BeforeTargets="CoreCompile">
- $(ArtifactsDir)\Bootstrap\fsyacc\fsyacc.dll
+ $(ArtifactsDir)\bin\fsyacc\Release\netcoreapp3.1\fsyacc.dll
diff --git a/src/fsharp/CompilerImports.fs b/src/fsharp/CompilerImports.fs
index 3db33b94f9bd..34dcb42317cf 100644
--- a/src/fsharp/CompilerImports.fs
+++ b/src/fsharp/CompilerImports.fs
@@ -1782,6 +1782,38 @@ and [] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
global_g <- Some tcGlobals
#endif
frameworkTcImports.SetTcGlobals tcGlobals
+
+#if EXPORT_METADATA
+ let metadataPath = __SOURCE_DIRECTORY__ + "/../../temp/metadata/"
+ let writeMetadata (dllInfo: ImportedBinary) =
+ let outfile = Path.GetFullPath(metadataPath + Path.GetFileName(dllInfo.FileName))
+ let ilModule = dllInfo.RawMetadata.TryGetILModuleDef().Value
+ try
+ let args: ILBinaryWriter.options = {
+ ilg = ilGlobals
+ pdbfile = None
+ emitTailcalls = false
+ deterministic = false
+ showTimes = false
+ portablePDB = false
+ embeddedPDB = false
+ embedAllSource = false
+ embedSourceList = []
+ sourceLink = ""
+ checksumAlgorithm = tcConfig.checksumAlgorithm
+ signer = None
+ dumpDebugInfo = false
+ pathMap = tcConfig.pathMap }
+ ILBinaryWriter.WriteILBinary (outfile, args, ilModule, id)
+ with Failure msg ->
+ printfn "Export error: %s" msg
+
+ let! dllinfos, _ccuinfos = frameworkTcImports.RegisterAndImportReferencedAssemblies (ctok, tcResolutions.GetAssemblyResolutions())
+ dllinfos |> List.iter writeMetadata
+ let! dllinfos, _ccuinfos = frameworkTcImports.RegisterAndImportReferencedAssemblies (ctok, tcAltResolutions.GetAssemblyResolutions())
+ dllinfos |> List.iter writeMetadata
+#endif
+
return tcGlobals, frameworkTcImports
}
diff --git a/src/fsharp/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj b/src/fsharp/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj
index 2f7d9d26d304..ff2b64d0ca63 100644
--- a/src/fsharp/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj
+++ b/src/fsharp/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj
@@ -5,6 +5,7 @@
netstandard2.0
$(NoWarn);44;62;69;65;54;61;75;62;9;2003;NU5125
true
+ $(DefineConstants);EXPORT_METADATA
$(DefineConstants);COMPILER_SERVICE_AS_DLL
$(DefineConstants);COMPILER
$(DefineConstants);ENABLE_MONO_SUPPORT
diff --git a/src/fsharp/absil/ilwrite.fs b/src/fsharp/absil/ilwrite.fs
index 16bd6118d398..12421442e40b 100644
--- a/src/fsharp/absil/ilwrite.fs
+++ b/src/fsharp/absil/ilwrite.fs
@@ -2444,6 +2444,9 @@ let GenMethodDefAsRow cenv env midx (md: ILMethodDef) =
if cenv.entrypoint <> None then failwith "duplicate entrypoint"
else cenv.entrypoint <- Some (true, midx)
let codeAddr =
+#if EXPORT_METADATA
+ 0x0000
+#else
(match md.Body.Contents with
| MethodBody.IL ilmbody ->
let addr = cenv.nextCodeAddr
@@ -2489,6 +2492,7 @@ let GenMethodDefAsRow cenv env midx (md: ILMethodDef) =
| MethodBody.Native ->
failwith "cannot write body of native method - Abstract IL cannot roundtrip mixed native/managed binaries"
| _ -> 0x0000)
+#endif
UnsharedRow
[| ULong codeAddr
@@ -3465,6 +3469,7 @@ let writeBinaryAndReportMappings (outfile,
match signer, modul.Manifest with
| Some _, _ -> signer
| _, None -> signer
+#if !EXPORT_METADATA
| None, Some {PublicKey=Some pubkey} ->
(dprintn "Note: The output assembly will be delay-signed using the original public"
dprintn "Note: key. In order to load it you will need to either sign it with"
@@ -3474,6 +3479,7 @@ let writeBinaryAndReportMappings (outfile,
dprintn "Note: private key when converting the assembly, assuming you have access to"
dprintn "Note: it."
Some (ILStrongNameSigner.OpenPublicKey pubkey))
+#endif
| _ -> signer
let modul =