73914
|
1 |
/* Title: Tools/Setup/isabelle/setup/Build_Scala.java
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Build Isabelle/Scala modules.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle.setup;
|
|
8 |
|
|
9 |
|
|
10 |
import java.io.File;
|
|
11 |
import java.io.IOException;
|
|
12 |
import java.math.BigInteger;
|
|
13 |
import java.nio.file.Files;
|
|
14 |
import java.nio.file.Path;
|
73918
|
15 |
import java.nio.file.StandardCopyOption;
|
73914
|
16 |
import java.security.MessageDigest;
|
|
17 |
import java.security.NoSuchAlgorithmException;
|
|
18 |
import java.util.Comparator;
|
|
19 |
import java.util.HashMap;
|
|
20 |
import java.util.LinkedList;
|
|
21 |
import java.util.List;
|
|
22 |
import java.util.Map;
|
|
23 |
import java.util.Properties;
|
|
24 |
import java.util.stream.Stream;
|
|
25 |
|
|
26 |
|
|
27 |
public class Build_Scala
|
|
28 |
{
|
|
29 |
public static class Context
|
|
30 |
{
|
|
31 |
private final Path component_dir;
|
|
32 |
private Properties props;
|
|
33 |
|
|
34 |
public Context(Path dir) throws IOException
|
|
35 |
{
|
|
36 |
component_dir = dir;
|
|
37 |
props = new Properties();
|
|
38 |
Path path = component_dir.resolve("etc/scala.props");
|
|
39 |
if (Files.exists(path)) { props.load(Files.newBufferedReader(path)); }
|
|
40 |
}
|
|
41 |
|
73917
|
42 |
@Override public String toString() { return component_dir.toString(); }
|
73914
|
43 |
|
73917
|
44 |
public String component_name() { return component_dir.toFile().getName(); }
|
|
45 |
public String name() { return props.getProperty("name", component_name()); }
|
|
46 |
public String description() { return props.getProperty("description", name()); }
|
73915
|
47 |
|
73917
|
48 |
public String lib_name() { return props.getProperty("lib", "lib") + "/" + name(); }
|
|
49 |
public String jar_name() { return lib_name() + ".jar"; }
|
|
50 |
public String shasum_name() { return lib_name() + ".shasum"; }
|
73915
|
51 |
|
73917
|
52 |
public String main() { return props.getProperty("main", ""); }
|
73914
|
53 |
|
73917
|
54 |
public String[] sources() { return props.getProperty("sources", "").split("\\s+"); }
|
73918
|
55 |
public String[] resources() { return props.getProperty("resources", "").split("\\s+"); }
|
73917
|
56 |
public String[] services() { return props.getProperty("services", "").split("\\s+"); }
|
73914
|
57 |
|
73917
|
58 |
public Path path(String file) { return component_dir.resolve(file); }
|
|
59 |
public boolean exists(String file) { return Files.exists(path(file)); }
|
73914
|
60 |
|
73918
|
61 |
public String resource_name(String s)
|
|
62 |
{
|
|
63 |
int i = s.indexOf(':');
|
|
64 |
return i > 0 ? s.substring(0, i) : s;
|
|
65 |
}
|
|
66 |
|
|
67 |
public String resource_target(String s)
|
|
68 |
{
|
|
69 |
int i = s.indexOf(':');
|
|
70 |
return i > 0 ? s.substring(i + 1) : s;
|
|
71 |
}
|
|
72 |
|
73914
|
73 |
public String shasum(String file)
|
|
74 |
throws IOException, NoSuchAlgorithmException
|
|
75 |
{
|
|
76 |
if (exists(file)) {
|
|
77 |
MessageDigest sha = MessageDigest.getInstance("SHA");
|
|
78 |
sha.update(Files.readAllBytes(path(file)));
|
|
79 |
String digest = new BigInteger(1, sha.digest()).toString(16);
|
|
80 |
return digest + " *" + file + "\n";
|
|
81 |
}
|
|
82 |
else { return ""; }
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
public static void build_scala(Context context, boolean fresh)
|
|
87 |
throws IOException, InterruptedException, NoSuchAlgorithmException
|
|
88 |
{
|
73915
|
89 |
String jar_name = context.jar_name();
|
|
90 |
String shasum_name = context.shasum_name();
|
73914
|
91 |
|
73916
|
92 |
String[] sources = context.sources();
|
73918
|
93 |
String[] resources = context.resources();
|
73914
|
94 |
|
73916
|
95 |
if (sources.length == 0) {
|
73914
|
96 |
Files.deleteIfExists(context.path(jar_name));
|
|
97 |
Files.deleteIfExists(context.path(shasum_name));
|
|
98 |
}
|
|
99 |
else {
|
|
100 |
String shasum_old =
|
|
101 |
context.exists(shasum_name) ? Files.readString(context.path(shasum_name)) : "";
|
|
102 |
String shasum_sources;
|
|
103 |
{
|
|
104 |
StringBuilder _shasum = new StringBuilder();
|
73917
|
105 |
for (String s : sources) { _shasum.append(context.shasum(s)); }
|
73918
|
106 |
for (String s : resources) {
|
|
107 |
_shasum.append(context.shasum(context.resource_name(s)));
|
|
108 |
}
|
73914
|
109 |
shasum_sources = _shasum.toString();
|
|
110 |
}
|
|
111 |
if (fresh || !shasum_old.equals(context.shasum(jar_name) + shasum_sources)) {
|
|
112 |
System.out.println("### Building " + context.description() + " ...");
|
|
113 |
|
|
114 |
String java_home = Environment.getenv("JAVA_HOME");
|
|
115 |
String scala_home = Environment.getenv("SCALA_HOME");
|
|
116 |
String scalac_options = Environment.getenv("ISABELLE_SCALAC_OPTIONS");
|
|
117 |
String isabelle_class_path = Environment.getenv("ISABELLE_CLASSPATH");
|
|
118 |
|
|
119 |
if (java_home.isEmpty()) {
|
|
120 |
throw new RuntimeException("Unknown JAVA_HOME -- Java unavailable");
|
|
121 |
}
|
|
122 |
if (scala_home.isEmpty()) {
|
|
123 |
throw new RuntimeException("Unknown SCALA_HOME -- Scala unavailable");
|
|
124 |
}
|
|
125 |
|
|
126 |
Path build_dir = Files.createTempDirectory("isabelle");
|
|
127 |
try {
|
|
128 |
/* classpath */
|
|
129 |
|
|
130 |
List<String> classpath = new LinkedList<String>();
|
|
131 |
for (String s : isabelle_class_path.split(":", -1)) {
|
|
132 |
classpath.add(Environment.platform_path(s));
|
|
133 |
}
|
|
134 |
|
|
135 |
Map<String,String> env = new HashMap<String,String>(Environment.settings());
|
|
136 |
env.put("CLASSPATH", String.join(File.pathSeparator, classpath));
|
|
137 |
|
|
138 |
|
73918
|
139 |
/* compile sources */
|
73914
|
140 |
|
|
141 |
List<String> cmd = new LinkedList<String>();
|
|
142 |
Environment.Exec_Result res;
|
|
143 |
|
|
144 |
cmd.add(Environment.platform_path(scala_home + "/bin/scalac"));
|
|
145 |
for (String s : scalac_options.split("\\s+")) { cmd.add(s); }
|
|
146 |
cmd.add("-d");
|
|
147 |
cmd.add(build_dir.toString());
|
|
148 |
for (String s : sources) { cmd.add(context.path(s).toString()); }
|
|
149 |
|
|
150 |
res = Environment.exec_process(cmd, build_dir.toFile(), env, false);
|
|
151 |
if (!res.ok()) throw new RuntimeException(res.err());
|
|
152 |
|
|
153 |
|
73918
|
154 |
/* copy resources */
|
|
155 |
|
|
156 |
for (String s : context.resources()) {
|
|
157 |
String name = context.resource_name(s);
|
|
158 |
String target = context.resource_target(s);
|
|
159 |
Path file_name = Path.of(name).normalize().getFileName();
|
|
160 |
Path target_path = Path.of(target).normalize();
|
|
161 |
|
|
162 |
Path target_dir;
|
|
163 |
Path target_file;
|
|
164 |
{
|
|
165 |
if (target.endsWith("/") || target.endsWith("/.")) {
|
|
166 |
target_dir = build_dir.resolve(target_path);
|
|
167 |
target_file = target_dir.resolve(file_name);
|
|
168 |
}
|
|
169 |
else {
|
|
170 |
target_file = build_dir.resolve(target_path);
|
|
171 |
target_dir = target_file.getParent();
|
|
172 |
}
|
|
173 |
}
|
|
174 |
Files.createDirectories(target_dir);
|
|
175 |
Files.copy(context.path(name), target_file,
|
|
176 |
StandardCopyOption.COPY_ATTRIBUTES);
|
|
177 |
}
|
|
178 |
|
|
179 |
|
73914
|
180 |
/* jar */
|
|
181 |
|
|
182 |
cmd.clear();
|
|
183 |
cmd.add(Environment.platform_path(java_home + "/bin/jar"));
|
|
184 |
cmd.add("-c");
|
|
185 |
cmd.add("-f");
|
|
186 |
cmd.add(context.path(jar_name).toString());
|
|
187 |
if (!context.main().isEmpty()) {
|
|
188 |
cmd.add("-e");
|
|
189 |
cmd.add(context.main());
|
|
190 |
}
|
|
191 |
cmd.add(".");
|
|
192 |
|
|
193 |
res = Environment.exec_process(cmd, build_dir.toFile(), env, false);
|
|
194 |
if (!res.ok()) throw new RuntimeException(res.err());
|
|
195 |
|
|
196 |
|
|
197 |
/* shasum */
|
|
198 |
|
|
199 |
String shasum = context.shasum(jar_name) + shasum_sources;
|
|
200 |
Files.writeString(context.path(shasum_name), shasum);
|
|
201 |
}
|
|
202 |
finally {
|
|
203 |
try (Stream<Path> walk = Files.walk(build_dir)) {
|
|
204 |
walk.sorted(Comparator.reverseOrder())
|
|
205 |
.map(Path::toFile)
|
|
206 |
.forEach(File::delete);
|
|
207 |
}
|
|
208 |
}
|
|
209 |
}
|
|
210 |
}
|
|
211 |
}
|
|
212 |
}
|