-
Notifications
You must be signed in to change notification settings - Fork 3
/
polybuild.ps1
116 lines (82 loc) · 2.81 KB
/
polybuild.ps1
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
<#
.SYNOPSIS
Use Poly/ML to compile a Standard ML program defined in a .mlb file
.EXAMPLE
polybuild example.mlb
Compile the Standard ML program defined in example.mlb using Poly/ML.
The environment variable POLY_DIR must be set to a directory
containing the Poly/ML library files (PolyLib.lib, PolyLib.dll,
PolyMainLib.lib) and executable (PolyML.exe), and the clang compiler
must be in the path, for example because we are running in a
sufficiently recent Visual Studio command prompt with clang support.
#>
# NB just installing Poly/ML for Windows is not enough to get the
# Poly/ML libraries, because the export libraries such as PolyLib.lib
# are not included in the installation.
#
# To build them, check out Poly/ML from git or whatever, then go to
# its repo in a VC command prompt and
#
# > msbuild polyml.sln /p:Configuration=Release '/t:PolyLib;PolyML;PolyMainLib'
#
# then set the x64\Release subdir of the Poly/ML dir as $env:POLY_DIR.
#
# You will also need to ensure PolyLib.dll is available at runtime
# (e.g. in the same place as your executable).
Set-StrictMode -Version 2.0
$ErrorActionPreference = "Stop"
if ($args.Count -lt 1) {
"Usage: polybuild [-o output.exe] file.mlb"
exit 1
}
if ($args[0] -eq "-o") {
$output_file = $args[1]
$mlb = $args[2]
} else {
$mlb = $args[0]
$output_file = $mlb -replace ".mlb",""
$output_file = "$output_file.exe"
}
if ($mlb -notmatch "[.]mlb$") {
"Error: argument must be a .mlb file"
exit 1
}
$poly_libdir = $env:POLY_DIR
if (! $poly_libdir -or ! (Test-Path -PathType Container $poly_libdir)) {
"Error: POLY_DIR environment variable must be set to a valid directory"
exit 1
}
"Poly/ML libdir is $poly_libdir"
$mydir = Split-Path -Parent $PSCommandPath
. $mydir/smlbuild-include.ps1
$lines = @(processMLB $mlb)
if ($lines -match "^Error: ") {
$lines -match "^Error: "
exit 1
}
$tmpfile_in = ([System.IO.Path]::GetTempFileName()) -replace "[.]tmp",".sml"
$tmpfile_out = ([System.IO.Path]::GetTempFileName()) -replace "[.]tmp",".obj"
$outro = @"
val _ = PolyML.export("$tmpfile_out", main);
val _ = OS.Process.exit (OS.Process.success);
"@ -split "[\r\n]+"
$script = @()
$script += $lines
$script += $outro
$script -replace "\\","\\" | Out-File -Encoding "ASCII" $tmpfile_in
$polyml = "$poly_libdir\polyml"
cat $tmpfile_in | & $polyml -q --error-exit | Out-Host
if (-not $?) {
del $tmpfile_in
if (Test-Path $tmpfile_out) {
del $tmpfile_out
}
exit $LastExitCode
}
del $tmpfile_in
clang -O3 $tmpfile_out -o $output_file -Xlinker /subsystem:console -Xlinker /entry:WinMainCRTStartup -Xlinker /ltcg -L"$poly_libdir" -lPolyLib -lPolyMainLib
if (-not $?) {
del $tmpfile_out
exit $LastExitCode
}
del $tmpfile_out