13
13
**/
14
14
15
15
using System ;
16
+ using System . Collections ;
16
17
using System . Collections . Generic ;
17
- using System . Linq ;
18
- using System . Text ;
18
+ using System . Diagnostics ;
19
19
using System . IO ;
20
20
using clojure . lang ;
21
- using System . Collections ;
22
- using System . Diagnostics ;
23
21
24
22
namespace BootstrapCompile
25
23
{
26
24
static class Compile
27
25
{
28
-
29
26
const string PATH_PROP = "CLOJURE_COMPILE_PATH" ;
30
27
const string REFLECTION_WARNING_PROP = "CLOJURE_COMPILE_WARN_ON_REFLECTION" ;
31
28
const string UNCHECKED_MATH_PROP = "CLOJURE_COMPILE_UNCHECKED_MATH" ;
32
29
33
30
static void Main ( string [ ] args )
34
31
{
35
- TextWriter outTW = ( TextWriter ) RT . OutVar . deref ( ) ;
36
- TextWriter errTW = RT . errPrintWriter ( ) ;
32
+ string assemblyName ;
33
+ List < string > fileNames ;
34
+
35
+ if ( ! ParseArgs ( args , out assemblyName , out fileNames ) )
36
+ return ;
37
+
38
+ DoCompile ( assemblyName , fileNames ) ;
39
+ }
40
+
41
+ static bool ParseArgs ( string [ ] args , out string assemblyName , out List < String > fileNames )
42
+ {
43
+ assemblyName = null ;
44
+ fileNames = null ;
45
+
46
+ if ( args . Length == 0 )
47
+ {
48
+ PrintUsage ( ) ;
49
+ return false ;
50
+ }
51
+
52
+ if ( args [ 0 ] . Equals ( "-o" ) )
53
+ {
54
+ if ( args . Length == 1 )
55
+ {
56
+ Console . WriteLine ( "-o option must be followed by a filename" ) ;
57
+ PrintUsage ( ) ;
58
+ return false ;
59
+ }
60
+
61
+ if ( args . Length == 2 )
62
+ {
63
+ Console . WriteLine ( "Need filenames to compile" ) ;
64
+ PrintUsage ( ) ;
65
+ return false ;
66
+ }
67
+ assemblyName = args [ 1 ] ;
68
+ fileNames = new List < String > ( args . Length - 2 ) ;
37
69
70
+ for ( int i = 2 ; i < args . Length ; i ++ )
71
+ fileNames . Add ( args [ i ] ) ;
72
+
73
+ return true ;
74
+ }
75
+ else
76
+ {
77
+ assemblyName = null ;
78
+ fileNames = new List < String > ( args ) ;
79
+ return true ;
80
+ }
81
+ }
82
+
83
+ static void PrintUsage ( )
84
+ {
85
+ Console . WriteLine ( "Usage: Clojure.Compile [-o assemblyName] fileNames..." ) ;
86
+ }
87
+
88
+ static void DoCompile ( string assemblyName , List < String > fileNames )
89
+ {
38
90
string path = Environment . GetEnvironmentVariable ( PATH_PROP ) ;
39
91
40
92
path = path ?? "." ;
41
93
42
- string warnVal = Environment . GetEnvironmentVariable ( REFLECTION_WARNING_PROP ) ;
94
+ string warnVal = Environment . GetEnvironmentVariable ( REFLECTION_WARNING_PROP ) ;
43
95
bool warnOnReflection = warnVal == null ? false : warnVal . Equals ( "true" ) ;
44
96
string mathVal = Environment . GetEnvironmentVariable ( UNCHECKED_MATH_PROP ) ;
45
97
bool uncheckedMath = mathVal == null ? false : mathVal . Equals ( "true" ) ;
@@ -49,14 +101,19 @@ static void Main(string[] args)
49
101
{
50
102
String name = ( String ) kv . Key ;
51
103
String v = ( String ) kv . Value ;
52
- if ( name . StartsWith ( "CLOJURE_COMPILER_" ) )
104
+ if ( name . StartsWith ( "CLOJURE_COMPILER_" ) )
53
105
{
54
106
compilerOptions = RT . assoc ( compilerOptions
55
- , RT . keyword ( null , name . Substring ( 1 + name . LastIndexOf ( '_' ) ) )
56
- , RT . readString ( v ) ) ;
107
+ , RT . keyword ( null , name . Substring ( 1 + name . LastIndexOf ( '_' ) ) )
108
+ , RT . readString ( v ) ) ;
57
109
}
58
110
}
59
111
112
+ // Even though these are not used in all paths, we need this early
113
+ // to get RT initialized before Compiler.
114
+ TextWriter outTW = ( TextWriter ) RT . OutVar . deref ( ) ;
115
+ TextWriter errTW = RT . errPrintWriter ( ) ;
116
+
60
117
try
61
118
{
62
119
Var . pushThreadBindings ( RT . map (
@@ -66,39 +123,39 @@ static void Main(string[] args)
66
123
Compiler . CompilerOptionsVar , compilerOptions
67
124
) ) ;
68
125
69
- Stopwatch sw = new Stopwatch ( ) ;
70
-
71
- foreach ( string lib in args )
126
+ if ( String . IsNullOrWhiteSpace ( assemblyName ) )
72
127
{
73
- sw . Reset ( ) ;
74
- sw . Start ( ) ;
75
- outTW . Write ( "Compiling {0} to {1}" , lib , path ) ;
76
- outTW . Flush ( ) ;
77
- Compiler . CompileVar . invoke ( Symbol . intern ( lib ) ) ;
78
- sw . Stop ( ) ;
79
- outTW . WriteLine ( " -- {0} milliseconds." , sw . ElapsedMilliseconds ) ;
128
+ Stopwatch sw = new Stopwatch ( ) ;
129
+ foreach ( string lib in fileNames )
130
+ {
131
+ sw . Reset ( ) ;
132
+ sw . Start ( ) ;
133
+ outTW . Write ( "Compiling {0} to {1}" , lib , path ) ;
134
+ outTW . Flush ( ) ;
135
+ Compiler . CompileVar . invoke ( Symbol . intern ( lib ) ) ;
136
+ sw . Stop ( ) ;
137
+ outTW . WriteLine ( " -- {0} milliseconds." , sw . ElapsedMilliseconds ) ;
138
+ }
139
+ }
140
+ else
141
+ {
142
+ bool hasMain = assemblyName . ToLower ( ) . Trim ( ) . EndsWith ( ".exe" ) ;
143
+ RT . Compile ( fileNames , Path . GetFileNameWithoutExtension ( assemblyName ) , hasMain ) ;
80
144
}
81
- }
82
- catch ( Exception e )
83
- {
84
- errTW . WriteLine ( e . ToString ( ) ) ;
85
- Environment . Exit ( 1 ) ;
86
145
}
87
146
finally
88
147
{
89
148
Var . popThreadBindings ( ) ;
90
- try {
149
+ try
150
+ {
91
151
outTW . Flush ( ) ;
92
152
outTW . Close ( ) ;
93
153
}
94
- catch ( IOException e )
154
+ catch ( IOException e )
95
155
{
96
156
errTW . WriteLine ( e . StackTrace ) ;
97
157
}
98
158
}
99
-
100
-
101
-
102
159
}
103
160
}
104
161
}
0 commit comments