| author | wenzelm | 
| Wed, 03 Aug 2022 11:43:14 +0200 | |
| changeset 75740 | d22ae56ca00c | 
| parent 75687 | c8dc5d1adc7b | 
| child 75869 | ee2f93fa2440 | 
| permissions | -rw-r--r-- | 
| 74055 | 1  | 
/* Title: Pure/Tools/scala_build.scala  | 
2  | 
Author: Makarius  | 
|
3  | 
||
| 75661 | 4  | 
Manage and build Isabelle/Scala/Java modules.  | 
| 74055 | 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 {
 | 
| 75654 | 50  | 
isabelle.setup.Build.build(output, java_context, fresh)  | 
| 
74061
 
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
 
wenzelm 
parents: 
74060 
diff
changeset
 | 
51  | 
get_output()  | 
| 
 
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
 
wenzelm 
parents: 
74060 
diff
changeset
 | 
52  | 
}  | 
| 
 
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
 
wenzelm 
parents: 
74060 
diff
changeset
 | 
53  | 
      catch { case ERROR(msg) => cat_error(get_output(), msg) }
 | 
| 
 
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
 
wenzelm 
parents: 
74060 
diff
changeset
 | 
54  | 
}  | 
| 74056 | 55  | 
}  | 
| 74055 | 56  | 
|
57  | 
def context(dir: Path,  | 
|
58  | 
component: Boolean = false,  | 
|
| 74060 | 59  | 
no_title: Boolean = false,  | 
| 74057 | 60  | 
do_build: Boolean = false,  | 
| 75393 | 61  | 
module: Option[Path] = None  | 
62  | 
  ): Context = {
 | 
|
| 74055 | 63  | 
val props_name =  | 
64  | 
if (component) isabelle.setup.Build.COMPONENT_BUILD_PROPS  | 
|
65  | 
else isabelle.setup.Build.BUILD_PROPS  | 
|
66  | 
val props_path = dir + Path.explode(props_name)  | 
|
67  | 
||
68  | 
val props = new JProperties  | 
|
69  | 
props.load(Files.newBufferedReader(props_path.java_path))  | 
|
| 74060 | 70  | 
if (no_title) props.remove(isabelle.setup.Build.TITLE)  | 
| 74057 | 71  | 
if (do_build) props.remove(isabelle.setup.Build.NO_BUILD)  | 
72  | 
if (module.isDefined) props.put(isabelle.setup.Build.MODULE, File.standard_path(module.get))  | 
|
| 74055 | 73  | 
|
| 74056 | 74  | 
new Context(new isabelle.setup.Build.Context(dir.java_path, props, props_path.implode))  | 
| 74055 | 75  | 
}  | 
76  | 
||
| 75393 | 77  | 
  sealed case class Result(output: String, jar_bytes: Bytes, jar_path: Option[Path]) {
 | 
78  | 
    def write(): Unit = {
 | 
|
| 74062 | 79  | 
      if (jar_path.isDefined) {
 | 
80  | 
Isabelle_System.make_directory(jar_path.get.dir)  | 
|
81  | 
Bytes.write(jar_path.get, jar_bytes)  | 
|
82  | 
}  | 
|
83  | 
}  | 
|
84  | 
}  | 
|
| 
74061
 
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
 
wenzelm 
parents: 
74060 
diff
changeset
 | 
85  | 
|
| 75393 | 86  | 
  def build_result(dir: Path, component: Boolean = false): Result = {
 | 
| 75394 | 87  | 
    Isabelle_System.with_tmp_file("result", "jar") { tmp_file =>
 | 
| 
74061
 
203dfa8bc0fc
clarified compiler output: allow multithreaded execution;
 
wenzelm 
parents: 
74060 
diff
changeset
 | 
88  | 
val output =  | 
| 75657 | 89  | 
context(dir, component = component, no_title = true, do_build = true,  | 
90  | 
module = Some(tmp_file)).build()  | 
|
| 74062 | 91  | 
val jar_bytes = Bytes.read(tmp_file)  | 
92  | 
val jar_path = context(dir, component = component).module_result  | 
|
93  | 
Result(output, jar_bytes, jar_path)  | 
|
| 75394 | 94  | 
}  | 
| 74055 | 95  | 
}  | 
96  | 
||
| 
75660
 
45d3497c0baa
support for Isabelle/Scala/Java modules in Isabelle/ML;
 
wenzelm 
parents: 
75657 
diff
changeset
 | 
97  | 
  object Scala_Fun extends Scala.Fun("scala_build") with Scala.Bytes_Fun {
 | 
| 
 
45d3497c0baa
support for Isabelle/Scala/Java modules in Isabelle/ML;
 
wenzelm 
parents: 
75657 
diff
changeset
 | 
98  | 
val here = Scala_Project.here  | 
| 
 
45d3497c0baa
support for Isabelle/Scala/Java modules in Isabelle/ML;
 
wenzelm 
parents: 
75657 
diff
changeset
 | 
99  | 
def invoke(args: List[Bytes]): List[Bytes] =  | 
| 
 
45d3497c0baa
support for Isabelle/Scala/Java modules in Isabelle/ML;
 
wenzelm 
parents: 
75657 
diff
changeset
 | 
100  | 
      args match {
 | 
| 75687 | 101  | 
case List(dir) =>  | 
102  | 
val result = build_result(Path.explode(dir.text))  | 
|
| 
75660
 
45d3497c0baa
support for Isabelle/Scala/Java modules in Isabelle/ML;
 
wenzelm 
parents: 
75657 
diff
changeset
 | 
103  | 
val jar_name =  | 
| 
 
45d3497c0baa
support for Isabelle/Scala/Java modules in Isabelle/ML;
 
wenzelm 
parents: 
75657 
diff
changeset
 | 
104  | 
            result.jar_path match {
 | 
| 
 
45d3497c0baa
support for Isabelle/Scala/Java modules in Isabelle/ML;
 
wenzelm 
parents: 
75657 
diff
changeset
 | 
105  | 
case Some(path) => path.file_name  | 
| 75678 | 106  | 
case None => "scala_build.jar"  | 
| 
75660
 
45d3497c0baa
support for Isabelle/Scala/Java modules in Isabelle/ML;
 
wenzelm 
parents: 
75657 
diff
changeset
 | 
107  | 
}  | 
| 75678 | 108  | 
          List(Bytes("classpath/" + jar_name), result.jar_bytes, Bytes(result.output))
 | 
| 
75660
 
45d3497c0baa
support for Isabelle/Scala/Java modules in Isabelle/ML;
 
wenzelm 
parents: 
75657 
diff
changeset
 | 
109  | 
        case _ => error("Bad arguments")
 | 
| 
 
45d3497c0baa
support for Isabelle/Scala/Java modules in Isabelle/ML;
 
wenzelm 
parents: 
75657 
diff
changeset
 | 
110  | 
}  | 
| 
 
45d3497c0baa
support for Isabelle/Scala/Java modules in Isabelle/ML;
 
wenzelm 
parents: 
75657 
diff
changeset
 | 
111  | 
}  | 
| 
 
45d3497c0baa
support for Isabelle/Scala/Java modules in Isabelle/ML;
 
wenzelm 
parents: 
75657 
diff
changeset
 | 
112  | 
|
| 74055 | 113  | 
def component_contexts(): List[Context] =  | 
| 74056 | 114  | 
isabelle.setup.Build.component_contexts().asScala.toList.map(new Context(_))  | 
| 74055 | 115  | 
}  |