-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
build.fsx
297 lines (246 loc) · 9.5 KB
/
build.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#r @"paket:
source https://api.nuget.org/v3/index.json
framework: net6.0
nuget FSharp.Core
nuget Mono.Cecil
nuget System.IO.Compression.ZipFile
nuget Graphviz.DotLanguage
nuget Fake.Core.Target
nuget Fake.Core.Process
nuget Fake.Core.ReleaseNotes
nuget Fake.IO.FileSystem
nuget Fake.DotNet.Cli
nuget Fake.DotNet.MSBuild
nuget Fake.DotNet.AssemblyInfoFile
nuget Fake.DotNet.Paket
nuget Fake.Tools.Git
nuget Fake.Api.GitHub //"
#if !FAKE
#load "./.fake/build.fsx/intellisense.fsx"
#endif
open System
open System.IO
open System.Collections.Generic
open System.Text.RegularExpressions
open Microsoft.FSharp.Core
open Mono.Cecil
open Fake
open Fake.Core.TargetOperators
open Fake.Core
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
open Fake.DotNet
open DotLang.CodeAnalysis.Syntax
Target.initEnvironment()
// --------------------------------------------------------------------------------------
// Provide project-specific details
let [<Literal>]root = __SOURCE_DIRECTORY__
// Read additional information from the release notes document
let release = ReleaseNotes.load "RELEASE_NOTES.md"
// --------------------------------------------------------------------------------------
// IKVM.NET compilation helpers
type TargetRuntime =
| Net_461
| NetCore_3_1
override this.ToString() =
match this with
| Net_461 -> "net461"
| NetCore_3_1 -> "netcoreapp3.1"
// Location of IKVM Compiler
let ikvmRootFolder = root </> "paket-files" </> "github.com"
let getIkmvcFolder = function
| Net_461 -> ikvmRootFolder </> "tools-ikvmc-net461/any"
| NetCore_3_1 -> ikvmRootFolder </> "tools-ikvmc-netcoreapp3.1/win7-x64"
let getIkvmRuntimeFolder = function
| Net_461 -> ikvmRootFolder </> "bin-net461"
| NetCore_3_1 -> ikvmRootFolder </> "bin-netcoreapp3.1"
type IKVMcTask(jar:string, version:string) =
member __.JarFile = jar
member __.Version = version
member __.DllFile = Path.ChangeExtension(Path.GetFileName(jar), ".dll")
member val Dependencies = List.empty<IKVMcTask> with get, set
member this.GetDllReferences() =
seq {
for t in this.Dependencies do
yield! t.GetDllReferences()
yield this.DllFile
}
let timeOut = TimeSpan.FromSeconds(120.0)
let IKVMCompile framework workingDirectory keyFile tasks =
let origKeyFile =
if (File.Exists keyFile) then
Path.GetFileName(keyFile)
else keyFile
let command = (getIkmvcFolder framework) </> "ikvmc.exe"
let ikvmc args =
CreateProcess.fromRawCommandLine command args
|> CreateProcess.withWorkingDirectory (DirectoryInfo(workingDirectory).FullName)
|> CreateProcess.withTimeout timeOut
|> CreateProcess.ensureExitCode
|> Proc.run
|> ignore
let newKeyFile =
if (File.Exists keyFile) then
let file = workingDirectory @@ (Path.GetFileName(keyFile))
File.Copy(keyFile, file, true)
Path.GetFileName(file)
else keyFile
let bprintf = Microsoft.FSharp.Core.Printf.bprintf
let cache = HashSet<_>()
let rec compile (task:IKVMcTask) =
let getIKVMCommandLineArgs() =
let dependencies =
task.Dependencies
|> Seq.collect (fun x ->
compile x
x.GetDllReferences()
)
|> Seq.distinct
let sb = Text.StringBuilder()
bprintf sb " -out:%s" task.DllFile
if not <| String.IsNullOrEmpty(task.Version)
then task.Version |> bprintf sb " -version:%s"
bprintf sb " -keyfile:%s" origKeyFile
//bprintf sb " -debug" // Not supported on Mono
if framework = NetCore_3_1 then
bprintf sb " -nostdlib"
!! (sprintf "%s/refs/*.dll" (getIkmvcFolder framework))
|> Seq.iter (fun lib -> bprintf sb " -r:%s" lib)
let runtime = getIkvmRuntimeFolder framework
bprintf sb " -runtime:%s/IKVM.Runtime.dll" runtime
dependencies |> Seq.iter (bprintf sb " -r:%s")
bprintf sb " \"%s\"" task.JarFile
sb.ToString()
if cache.Contains task.JarFile
then
Trace.tracefn "Task '%s' already compiled" task.JarFile
else
//File.Copy(task.JarFile, workingDirectory @@ (Path.GetFileName(task.JarFile)) ,true)
ikvmc <| getIKVMCommandLineArgs()
if (File.Exists(newKeyFile)) then
let key = FileInfo(newKeyFile).FullName
|> File.ReadAllBytes
ModuleDefinition
.ReadModule(task.DllFile, ReaderParameters(InMemory=true))
.Write(task.DllFile, WriterParameters(StrongNameKeyBlob=key))
cache.Add(task.JarFile) |> ignore
tasks |> Seq.iter compile
/// Infer dependencies between *.jar files
let buildJDepsGraph dotFile jarFolder (jars:string seq)=
"-filter:package -dotoutput dot " + String.Join(" ", jars)
|> CreateProcess.fromRawCommandLine "jdeps"
|> CreateProcess.withWorkingDirectory jarFolder
|> CreateProcess.ensureExitCode
|> Proc.run
|> ignore
jarFolder </> "dot/summary.dot"
|> Shell.copyFile dotFile
let loadJDepsGraph dotFile (jars:Set<string>) =
let parser = dotFile |> File.ReadAllText |> Parser
parser.Parse().Graphs.[0].Statements
|> Seq.cast<EdgeStatementSyntax>
|> Seq.map (fun x->
(x.Left :?> NodeStatementSyntax).Identifier.IdentifierToken.StringValue,
(x.Right :?> NodeStatementSyntax).Identifier.IdentifierToken.StringValue)
|> Seq.filter (fun (x,y) -> jars.Contains x && jars.Contains y)
|> Seq.groupBy fst
|> Seq.map (fun (x,xs)-> x, xs |> Seq.map snd |> Seq.toList)
|> Map.ofSeq
/// Build IKVm task with proper dependencies order (topological sort)
let createCompilationGraph dotFile jarFolder (jars:Set<string>) =
let deps = loadJDepsGraph dotFile jars
let cache = Dictionary<_,_>()
let pattern = Regex("-(?<version>[0-9.]*).jar$", RegexOptions.Compiled)
let getVersion str =
let m = pattern.Match(str)
if m.Success
then m.Groups.["version"].Value |> Some
else None
let rec getTask name =
match cache.TryGetValue name with
| true, task -> task
| _ ->
let deps =
Map.tryFind name deps
|> Option.defaultValue []
|> List.map getTask
let ver = getVersion name
|> Option.defaultValue release.AssemblyVersion
let task = IKVMcTask(jarFolder </> name, ver, Dependencies = deps)
cache.Add(name, task)
task
seq {
for jar in jars do
if not <| cache.ContainsKey jar
then yield getTask jar
} |> Seq.toList
let createNuGetPackage template =
Paket.pack(fun p ->
{ p with
ToolType = Fake.DotNet.ToolType.CreateLocalTool()
TemplateFile = template
OutputPath = "bin"
Version = release.NugetVersion
ReleaseNotes = String.toLines release.Notes})
let keyFile = @"./nuget/OpenNLP.snk"
// --------------------------------------------------------------------------------------
// Clean build results
Target.create "Clean" (fun _ ->
Shell.cleanDirs ["bin"; "temp"; "TestResults"]
)
// --------------------------------------------------------------------------------------
// Compile Stanford.NLP.CoreNLP and build NuGet package
let openNLPDir = root </> "paket-files/archive.apache.org/apache-opennlp-1.9.4/lib"
let frameworks = [Net_461; NetCore_3_1]
Target.create "Compile" (fun _ ->
// Get *.jar file for compilation
let jars =
Directory.GetFiles(openNLPDir, "*.jar")
|> Array.map (Path.GetFileName)
|> Set.ofArray
if (jars.IsEmpty)
then failwith "Found 0 *.jar files"
let dotFile = "nuget/OpenNLP.dot"
if not <| File.Exists dotFile then
buildJDepsGraph dotFile openNLPDir jars
for framework in frameworks do
Trace.trace $"Compiling for {framework}"
let ikvmDir = $"bin/lib/{framework}"
Shell.mkdir ikvmDir
createCompilationGraph dotFile openNLPDir jars
|> IKVMCompile framework ikvmDir keyFile
)
Target.create "NuGet" (fun _ ->
root </> "nuget/OpenNLP.template"
|> createNuGetPackage
)
// --------------------------------------------------------------------------------------
// Build and run test projects
let dotnet cmd args =
let result = DotNet.exec id cmd args
if not result.OK
then failwithf "Failed: %A" result.Errors
Target.create "BuildTests" (fun _ ->
dotnet "build" "OpenNLP.NET.sln -c Release"
)
Target.create "RunTests" (fun _ ->
for framework in frameworks do
Trace.trace $"Running tests for {framework}"
!! $"tests/**/bin/Release/{framework}/*.Tests.dll"
|> Seq.iter (fun lib ->
let logFileName = $"""${framework}-{Path.GetFileNameWithoutExtension(lib)}-TestResults.trx"""
dotnet "test" $"{lib} -c Release --no-build --logger:\"console;verbosity=normal\" --logger:\"trx;LogFileName={logFileName}\""
)
)
// --------------------------------------------------------------------------------------
// Run all targets by default. Invoke 'build <Target>' to override
Target.create "All" ignore
Target.create "Release" ignore
"Clean"
==> "Compile"
==> "NuGet"
==> "BuildTests"
==> "RunTests"
==> "All"
Target.runOrDefault "All"