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 |
|
73922
|
10 |
import java.io.BufferedOutputStream;
|
73914
|
11 |
import java.io.File;
|
|
12 |
import java.io.IOException;
|
73922
|
13 |
import java.io.OutputStream;
|
73914
|
14 |
import java.math.BigInteger;
|
|
15 |
import java.nio.file.Files;
|
|
16 |
import java.nio.file.Path;
|
73918
|
17 |
import java.nio.file.StandardCopyOption;
|
73914
|
18 |
import java.security.MessageDigest;
|
|
19 |
import java.security.NoSuchAlgorithmException;
|
|
20 |
import java.util.Comparator;
|
|
21 |
import java.util.HashMap;
|
|
22 |
import java.util.LinkedList;
|
|
23 |
import java.util.List;
|
73919
|
24 |
import java.util.Locale;
|
73914
|
25 |
import java.util.Map;
|
|
26 |
import java.util.Properties;
|
73922
|
27 |
import java.util.jar.Attributes;
|
|
28 |
import java.util.jar.JarEntry;
|
|
29 |
import java.util.jar.JarOutputStream;
|
|
30 |
import java.util.jar.Manifest;
|
73914
|
31 |
import java.util.stream.Stream;
|
|
32 |
|
|
33 |
|
|
34 |
public class Build_Scala
|
|
35 |
{
|
73922
|
36 |
/** component directory context **/
|
|
37 |
|
73914
|
38 |
public static class Context
|
|
39 |
{
|
|
40 |
private final Path component_dir;
|
|
41 |
private Properties props;
|
|
42 |
|
|
43 |
public Context(Path dir) throws IOException
|
|
44 |
{
|
|
45 |
component_dir = dir;
|
|
46 |
props = new Properties();
|
|
47 |
Path path = component_dir.resolve("etc/scala.props");
|
|
48 |
if (Files.exists(path)) { props.load(Files.newBufferedReader(path)); }
|
|
49 |
}
|
|
50 |
|
73917
|
51 |
@Override public String toString() { return component_dir.toString(); }
|
73914
|
52 |
|
73917
|
53 |
public String component_name() { return component_dir.toFile().getName(); }
|
|
54 |
public String name() { return props.getProperty("name", component_name()); }
|
|
55 |
public String description() { return props.getProperty("description", name()); }
|
73915
|
56 |
|
73917
|
57 |
public String lib_name() { return props.getProperty("lib", "lib") + "/" + name(); }
|
|
58 |
public String jar_name() { return lib_name() + ".jar"; }
|
|
59 |
public String shasum_name() { return lib_name() + ".shasum"; }
|
73915
|
60 |
|
73917
|
61 |
public String main() { return props.getProperty("main", ""); }
|
73914
|
62 |
|
73917
|
63 |
public String[] sources() { return props.getProperty("sources", "").split("\\s+"); }
|
73918
|
64 |
public String[] resources() { return props.getProperty("resources", "").split("\\s+"); }
|
73917
|
65 |
public String[] services() { return props.getProperty("services", "").split("\\s+"); }
|
73914
|
66 |
|
73917
|
67 |
public Path path(String file) { return component_dir.resolve(file); }
|
|
68 |
public boolean exists(String file) { return Files.exists(path(file)); }
|
73914
|
69 |
|
73921
|
70 |
public String item_name(String s)
|
73918
|
71 |
{
|
|
72 |
int i = s.indexOf(':');
|
|
73 |
return i > 0 ? s.substring(0, i) : s;
|
|
74 |
}
|
|
75 |
|
73921
|
76 |
public String item_target(String s)
|
73918
|
77 |
{
|
|
78 |
int i = s.indexOf(':');
|
|
79 |
return i > 0 ? s.substring(i + 1) : s;
|
|
80 |
}
|
|
81 |
|
73914
|
82 |
public String shasum(String file)
|
|
83 |
throws IOException, NoSuchAlgorithmException
|
|
84 |
{
|
|
85 |
if (exists(file)) {
|
|
86 |
MessageDigest sha = MessageDigest.getInstance("SHA");
|
|
87 |
sha.update(Files.readAllBytes(path(file)));
|
73919
|
88 |
String digest = String.format(Locale.ROOT, "%040x", new BigInteger(1, sha.digest()));
|
73914
|
89 |
return digest + " *" + file + "\n";
|
|
90 |
}
|
|
91 |
else { return ""; }
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
73922
|
95 |
|
|
96 |
|
|
97 |
/** create jar **/
|
|
98 |
|
|
99 |
public static void create_jar(Path dir, String main, Path jar)
|
|
100 |
throws IOException
|
|
101 |
{
|
|
102 |
Files.deleteIfExists(jar);
|
|
103 |
|
|
104 |
Manifest manifest = new Manifest();
|
|
105 |
Attributes attributes = manifest.getMainAttributes();
|
|
106 |
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
|
|
107 |
attributes.put(new Attributes.Name("Created-By"),
|
|
108 |
System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")");
|
|
109 |
if (!main.isEmpty()) { attributes.put(Attributes.Name.MAIN_CLASS, main); }
|
|
110 |
|
|
111 |
try (JarOutputStream out =
|
|
112 |
new JarOutputStream(new BufferedOutputStream(Files.newOutputStream(jar)), manifest))
|
|
113 |
{
|
|
114 |
for (Path path : Files.walk(dir).sorted().toArray(Path[]::new)) {
|
|
115 |
boolean is_dir = Files.isDirectory(path);
|
|
116 |
boolean is_file = Files.isRegularFile(path);
|
|
117 |
if (is_dir || is_file) {
|
|
118 |
String name = Environment.slashes(dir.relativize(path).toString());
|
|
119 |
JarEntry entry = new JarEntry(is_dir ? name + "/" : name);
|
|
120 |
entry.setTime(path.toFile().lastModified());
|
|
121 |
out.putNextEntry(entry);
|
|
122 |
if (is_file) { out.write(Files.readAllBytes(path)); }
|
|
123 |
out.closeEntry();
|
|
124 |
}
|
|
125 |
}
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
/** build scala **/
|
|
132 |
|
73914
|
133 |
public static void build_scala(Context context, boolean fresh)
|
|
134 |
throws IOException, InterruptedException, NoSuchAlgorithmException
|
|
135 |
{
|
73915
|
136 |
String jar_name = context.jar_name();
|
|
137 |
String shasum_name = context.shasum_name();
|
73914
|
138 |
|
73916
|
139 |
String[] sources = context.sources();
|
73918
|
140 |
String[] resources = context.resources();
|
73914
|
141 |
|
73916
|
142 |
if (sources.length == 0) {
|
73914
|
143 |
Files.deleteIfExists(context.path(jar_name));
|
|
144 |
Files.deleteIfExists(context.path(shasum_name));
|
|
145 |
}
|
|
146 |
else {
|
|
147 |
String shasum_old =
|
|
148 |
context.exists(shasum_name) ? Files.readString(context.path(shasum_name)) : "";
|
|
149 |
String shasum_sources;
|
|
150 |
{
|
|
151 |
StringBuilder _shasum = new StringBuilder();
|
73918
|
152 |
for (String s : resources) {
|
73921
|
153 |
_shasum.append(context.shasum(context.item_name(s)));
|
73918
|
154 |
}
|
73920
|
155 |
for (String s : sources) { _shasum.append(context.shasum(s)); }
|
73914
|
156 |
shasum_sources = _shasum.toString();
|
|
157 |
}
|
|
158 |
if (fresh || !shasum_old.equals(context.shasum(jar_name) + shasum_sources)) {
|
|
159 |
System.out.println("### Building " + context.description() + " ...");
|
|
160 |
|
|
161 |
String java_home = Environment.getenv("JAVA_HOME");
|
|
162 |
String scala_home = Environment.getenv("SCALA_HOME");
|
|
163 |
String scalac_options = Environment.getenv("ISABELLE_SCALAC_OPTIONS");
|
|
164 |
String isabelle_class_path = Environment.getenv("ISABELLE_CLASSPATH");
|
|
165 |
|
|
166 |
if (java_home.isEmpty()) {
|
|
167 |
throw new RuntimeException("Unknown JAVA_HOME -- Java unavailable");
|
|
168 |
}
|
|
169 |
if (scala_home.isEmpty()) {
|
|
170 |
throw new RuntimeException("Unknown SCALA_HOME -- Scala unavailable");
|
|
171 |
}
|
|
172 |
|
|
173 |
Path build_dir = Files.createTempDirectory("isabelle");
|
|
174 |
try {
|
|
175 |
/* classpath */
|
|
176 |
|
|
177 |
List<String> classpath = new LinkedList<String>();
|
|
178 |
for (String s : isabelle_class_path.split(":", -1)) {
|
|
179 |
classpath.add(Environment.platform_path(s));
|
|
180 |
}
|
|
181 |
|
|
182 |
Map<String,String> env = new HashMap<String,String>(Environment.settings());
|
|
183 |
env.put("CLASSPATH", String.join(File.pathSeparator, classpath));
|
|
184 |
|
|
185 |
|
73918
|
186 |
/* compile sources */
|
73914
|
187 |
|
|
188 |
List<String> cmd = new LinkedList<String>();
|
|
189 |
Environment.Exec_Result res;
|
|
190 |
|
|
191 |
cmd.add(Environment.platform_path(scala_home + "/bin/scalac"));
|
|
192 |
for (String s : scalac_options.split("\\s+")) { cmd.add(s); }
|
|
193 |
cmd.add("-d");
|
|
194 |
cmd.add(build_dir.toString());
|
|
195 |
for (String s : sources) { cmd.add(context.path(s).toString()); }
|
|
196 |
|
|
197 |
res = Environment.exec_process(cmd, build_dir.toFile(), env, false);
|
|
198 |
if (!res.ok()) throw new RuntimeException(res.err());
|
|
199 |
|
|
200 |
|
73918
|
201 |
/* copy resources */
|
|
202 |
|
|
203 |
for (String s : context.resources()) {
|
73921
|
204 |
String name = context.item_name(s);
|
|
205 |
String target = context.item_target(s);
|
73918
|
206 |
Path file_name = Path.of(name).normalize().getFileName();
|
|
207 |
Path target_path = Path.of(target).normalize();
|
|
208 |
|
|
209 |
Path target_dir;
|
|
210 |
Path target_file;
|
|
211 |
{
|
|
212 |
if (target.endsWith("/") || target.endsWith("/.")) {
|
|
213 |
target_dir = build_dir.resolve(target_path);
|
|
214 |
target_file = target_dir.resolve(file_name);
|
|
215 |
}
|
|
216 |
else {
|
|
217 |
target_file = build_dir.resolve(target_path);
|
|
218 |
target_dir = target_file.getParent();
|
|
219 |
}
|
|
220 |
}
|
|
221 |
Files.createDirectories(target_dir);
|
|
222 |
Files.copy(context.path(name), target_file,
|
|
223 |
StandardCopyOption.COPY_ATTRIBUTES);
|
|
224 |
}
|
|
225 |
|
|
226 |
|
73922
|
227 |
/* packaging */
|
73914
|
228 |
|
73922
|
229 |
create_jar(build_dir, context.main(), context.path(jar_name));
|
73914
|
230 |
|
|
231 |
String shasum = context.shasum(jar_name) + shasum_sources;
|
|
232 |
Files.writeString(context.path(shasum_name), shasum);
|
|
233 |
}
|
|
234 |
finally {
|
|
235 |
try (Stream<Path> walk = Files.walk(build_dir)) {
|
|
236 |
walk.sorted(Comparator.reverseOrder())
|
|
237 |
.map(Path::toFile)
|
|
238 |
.forEach(File::delete);
|
|
239 |
}
|
|
240 |
}
|
|
241 |
}
|
|
242 |
}
|
|
243 |
}
|
|
244 |
}
|