author | wenzelm |
Fri, 01 Apr 2022 23:19:12 +0200 | |
changeset 75394 | 42267c650205 |
parent 75393 | 87ebf5a50283 |
child 75654 | 21164fd15e3d |
permissions | -rw-r--r-- |
74055 | 1 |
/* Title: Pure/Tools/scala_build.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Manage and build Isabelle/Scala/Java components. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
import java.util.{Properties => JProperties} |
|
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
11 |
import java.io.{ByteArrayOutputStream, PrintStream} |
74055 | 12 |
import java.nio.file.Files |
13 |
||
14 |
import scala.jdk.CollectionConverters._ |
|
15 |
||
16 |
||
75393 | 17 |
object Scala_Build { |
18 |
class Context private[Scala_Build](java_context: isabelle.setup.Build.Context) { |
|
74057 | 19 |
override def toString: String = java_context.toString |
20 |
||
75393 | 21 |
def is_module(path: Path): Boolean = { |
74057 | 22 |
val module_name = java_context.module_name() |
74056 | 23 |
module_name.nonEmpty && File.eq(java_context.path(module_name).toFile, path.file) |
24 |
} |
|
25 |
||
75393 | 26 |
def module_result: Option[Path] = { |
74062 | 27 |
java_context.module_result() match { |
28 |
case "" => None |
|
29 |
case module => Some(File.path(java_context.path(module).toFile)) |
|
30 |
} |
|
31 |
} |
|
32 |
||
74056 | 33 |
def sources: List[Path] = |
34 |
java_context.sources().asScala.toList.map(s => File.path(java_context.path(s).toFile)) |
|
35 |
||
36 |
def requirements: List[Path] = |
|
37 |
(for { |
|
38 |
s <- java_context.requirements().asScala.iterator |
|
39 |
p <- java_context.requirement_paths(s).asScala.iterator |
|
40 |
} yield (File.path(p.toFile))).toList |
|
41 |
||
75393 | 42 |
def build(fresh: Boolean = false): String = { |
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
43 |
val output0 = new ByteArrayOutputStream |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
44 |
val output = new PrintStream(output0) |
75393 | 45 |
def get_output(): String = { |
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
46 |
output.flush() |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
47 |
Library.trim_line(output0.toString(UTF8.charset)) |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
48 |
} |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
49 |
try { |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
50 |
Console.withOut(output) { |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
51 |
Console.withErr(output) { |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
52 |
isabelle.setup.Build.build(output, java_context, fresh) |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
53 |
} |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
54 |
} |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
55 |
get_output() |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
56 |
} |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
57 |
catch { case ERROR(msg) => cat_error(get_output(), msg) } |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
58 |
} |
74056 | 59 |
} |
74055 | 60 |
|
61 |
def context(dir: Path, |
|
62 |
component: Boolean = false, |
|
74060 | 63 |
no_title: Boolean = false, |
74057 | 64 |
do_build: Boolean = false, |
75393 | 65 |
module: Option[Path] = None |
66 |
): Context = { |
|
74055 | 67 |
val props_name = |
68 |
if (component) isabelle.setup.Build.COMPONENT_BUILD_PROPS |
|
69 |
else isabelle.setup.Build.BUILD_PROPS |
|
70 |
val props_path = dir + Path.explode(props_name) |
|
71 |
||
72 |
val props = new JProperties |
|
73 |
props.load(Files.newBufferedReader(props_path.java_path)) |
|
74060 | 74 |
if (no_title) props.remove(isabelle.setup.Build.TITLE) |
74057 | 75 |
if (do_build) props.remove(isabelle.setup.Build.NO_BUILD) |
76 |
if (module.isDefined) props.put(isabelle.setup.Build.MODULE, File.standard_path(module.get)) |
|
74055 | 77 |
|
74056 | 78 |
new Context(new isabelle.setup.Build.Context(dir.java_path, props, props_path.implode)) |
74055 | 79 |
} |
80 |
||
81 |
def build(dir: Path, |
|
82 |
fresh: Boolean = false, |
|
83 |
component: Boolean = false, |
|
74060 | 84 |
no_title: Boolean = false, |
74057 | 85 |
do_build: Boolean = false, |
75393 | 86 |
module: Option[Path] = None |
87 |
): String = { |
|
74060 | 88 |
context(dir, component = component, no_title = no_title, do_build = do_build, module = module) |
89 |
.build(fresh = fresh) |
|
90 |
} |
|
91 |
||
75393 | 92 |
sealed case class Result(output: String, jar_bytes: Bytes, jar_path: Option[Path]) { |
93 |
def write(): Unit = { |
|
74062 | 94 |
if (jar_path.isDefined) { |
95 |
Isabelle_System.make_directory(jar_path.get.dir) |
|
96 |
Bytes.write(jar_path.get, jar_bytes) |
|
97 |
} |
|
98 |
} |
|
99 |
} |
|
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
100 |
|
75393 | 101 |
def build_result(dir: Path, component: Boolean = false): Result = { |
75394 | 102 |
Isabelle_System.with_tmp_file("result", "jar") { tmp_file => |
74061
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
103 |
val output = |
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
wenzelm
parents:
74060
diff
changeset
|
104 |
build(dir, component = component, no_title = true, do_build = true, module = Some(tmp_file)) |
74062 | 105 |
val jar_bytes = Bytes.read(tmp_file) |
106 |
val jar_path = context(dir, component = component).module_result |
|
107 |
Result(output, jar_bytes, jar_path) |
|
75394 | 108 |
} |
74055 | 109 |
} |
110 |
||
111 |
def component_contexts(): List[Context] = |
|
74056 | 112 |
isabelle.setup.Build.component_contexts().asScala.toList.map(new Context(_)) |
74055 | 113 |
} |