/
EclCompile.java
202 lines (183 loc) · 6.5 KB
/
EclCompile.java
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
package org.hpccsystems.clienttools;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
public class EclCompile
{
/**
* @param ecl - the ecl text to compile
* @param eclccInstallDir - the installation dir of eclcc/eclcc.exe
* @param tempdirname - the directory to use to read/write temporary files during ecl compilation
* @param includes - a collection of additional directories to include when compiling ecl
* @param flags - flag to use when compiling ecl (-legacy, -syntax, etc)
* @param saveTempFiles - whether or not to delete the temp files once you're finished; true by default.
* @return a hashmap containing the compiled ecl archive package, log file information, and error information
* @throws Exception
*/
public static HashMap<String, String> compileECL(String ecl, String eclccInstallDir, String tempdirname, List<String> includes, List<String> flags, Boolean saveTempFiles) throws Exception
{
if (saveTempFiles == null)
{
saveTempFiles = false;
}
HashMap<String, String> results = new HashMap<String, String>();
// check for compiler
if (!new File(eclccInstallDir).exists())
{
throw new Exception("Eclcc install dir " + eclccInstallDir + " not found");
}
String c = "";
if (System.getProperty("os.name").startsWith("Windows"))
{
c = "eclcc.exe";
}
else
{
c = "eclcc";
}
File cf = new File(eclccInstallDir, c);
if (!cf.exists())
{
throw new Exception("Eclcc compiler " + cf.getAbsolutePath() + " not found");
}
else if (!cf.canExecute())
{
throw new Exception("You do not have execute rights on eclcc compiler " + cf.getAbsolutePath());
}
// check for tempdir
File tempdir = new File(tempdirname);
if (!tempdir.exists())
{
throw new Exception("Temp dir " + tempdirname + " not found");
}
if (!tempdir.canWrite())
{
throw new Exception("You do not have write permissions to temp dir " + tempdirname);
}
// create files needed to compile ecl
String prefix = "HPCCWSClient." + String.valueOf((new Date()).getTime()) + ".";
File tempfilein = File.createTempFile(prefix, ".in", tempdir);
File tempfileout = File.createTempFile(prefix, ".out", tempdir);
File tempfilelog = File.createTempFile(prefix, ".log", tempdir);
try
{
Files.write(tempfilein.toPath(), ecl.getBytes());
}
catch (Exception e)
{
e.printStackTrace();
throw new Exception("Error writing ecl to temp file " + tempfilein.getAbsolutePath() + ": " + e.getMessage());
}
try
{
// set compile flags
ArrayList<String> paramsAL = new ArrayList<String>();
paramsAL.add(cf.getAbsolutePath());
paramsAL.add("-E");
paramsAL.add("-v");
if (flags != null)
{
for (String flag : flags)
{
if (!paramsAL.contains(flag))
{
paramsAL.add(flag);
}
}
}
paramsAL.add("--logfile");
paramsAL.add(tempfilelog.getAbsolutePath());
// set include dirs
if (includes != null)
{
for (String include : includes)
{
if (!paramsAL.contains(include))
{
paramsAL.add("-I");
paramsAL.add(include);
}
}
}
// set output and input files
paramsAL.add("-o");
paramsAL.add(tempfileout.getAbsolutePath());
paramsAL.add(tempfilein.getAbsolutePath());
String[] params = new String[paramsAL.size()];
paramsAL.toArray(params);
// fire off compile process
ProcessBuilder pb = new ProcessBuilder(params);
results.put("Command Line", pb.command().toString());
pb.redirectErrorStream(true); // merge stdout, stderr of process
pb.directory(new File(eclccInstallDir));
Process p = pb.start();
try
{
Thread.sleep(1000);
}
catch (InterruptedException ex)
{
Thread.currentThread().interrupt();
}
String errorText = "";
InputStream in = null;
BufferedReader br = null;
try
{
in = p.getErrorStream();
br = new BufferedReader(new InputStreamReader(in));
String lineErr;
while ((lineErr = br.readLine()) != null)
{
errorText += lineErr + "\r\n";
}
}
catch (Exception e)
{
e.printStackTrace();
throw new Exception("Error reading compile errors:"
+ e.getMessage());
}
finally
{
try
{
if (br != null)
{
br.close();
}
if (in != null)
{
in.close();
}
}
catch (Exception ex)
{}
}
results.put("Error", errorText);
String compiled_ecl = new String(Files.readAllBytes(tempfileout.toPath()));
results.put("Compiled ECL", compiled_ecl);
String log = new String(Files.readAllBytes(tempfilelog.toPath()));
results.put("Log", log);
if (errorText.equals("") && !compiled_ecl.equals("")
&& !saveTempFiles)
{
Files.delete(tempfilein.toPath());
Files.delete(tempfileout.toPath());
Files.delete(tempfilelog.toPath());
}
}
catch (Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
return results;
}
}