src/Tools/Setup/isabelle/setup/Build.java
changeset 74018 9d6c9a55f450
parent 73987 fc363a3b690a
child 74020 bad67fa41e71
equal deleted inserted replaced
74017:b4e6b82fdb9e 74018:9d6c9a55f450
     6 
     6 
     7 package isabelle.setup;
     7 package isabelle.setup;
     8 
     8 
     9 
     9 
    10 import java.io.BufferedOutputStream;
    10 import java.io.BufferedOutputStream;
       
    11 import java.io.ByteArrayOutputStream;
       
    12 import java.io.CharArrayWriter;
    11 import java.io.File;
    13 import java.io.File;
    12 import java.io.IOException;
    14 import java.io.IOException;
       
    15 import java.io.PrintStream;
    13 import java.math.BigInteger;
    16 import java.math.BigInteger;
    14 import java.nio.charset.StandardCharsets;
    17 import java.nio.charset.StandardCharsets;
    15 import java.nio.file.Files;
    18 import java.nio.file.Files;
    16 import java.nio.file.Path;
    19 import java.nio.file.Path;
    17 import java.nio.file.StandardCopyOption;
    20 import java.nio.file.StandardCopyOption;
    35 import javax.tools.JavaFileObject;
    38 import javax.tools.JavaFileObject;
    36 import javax.tools.StandardJavaFileManager;
    39 import javax.tools.StandardJavaFileManager;
    37 import javax.tools.ToolProvider;
    40 import javax.tools.ToolProvider;
    38 
    41 
    39 import scala.tools.nsc.MainClass;
    42 import scala.tools.nsc.MainClass;
       
    43 import scala.tools.nsc.reporters.ConsoleReporter;
       
    44 import scala.util.control.Exception;
    40 
    45 
    41 
    46 
    42 public class Build
    47 public class Build
    43 {
    48 {
    44     /** context **/
    49     /** context **/
   191     {
   196     {
   192         if (options != null) {
   197         if (options != null) {
   193             for (String s : options.split("\\s+")) {
   198             for (String s : options.split("\\s+")) {
   194                 if (!s.isEmpty()) { options_list.add(s); }
   199                 if (!s.isEmpty()) { options_list.add(s); }
   195             }
   200             }
       
   201         }
       
   202     }
       
   203 
       
   204     private static void compiler_result(boolean ok, String out, String what)
       
   205     {
       
   206         if (ok) { if (!out.isEmpty()) { System.err.println(out); } }
       
   207         else {
       
   208             String msg = "Failed to compile " + what + (out.isEmpty() ? "" : ":\n" + out);
       
   209             throw new RuntimeException(msg);
   196         }
   210         }
   197     }
   211     }
   198 
   212 
   199     public static void compile_scala_sources(
   213     public static void compile_scala_sources(
   200         Path target_dir, String more_options, List<Path> deps, List<Path> sources)
   214         Path target_dir, String more_options, List<Path> deps, List<Path> sources)
   213         for (Path p : sources) {
   227         for (Path p : sources) {
   214             args.add(p.toString());
   228             args.add(p.toString());
   215             if (p.toString().endsWith(".scala")) { scala_sources = true; }
   229             if (p.toString().endsWith(".scala")) { scala_sources = true; }
   216         }
   230         }
   217         if (scala_sources) {
   231         if (scala_sources) {
   218             MainClass main = new MainClass();
   232             boolean ok = false;
   219             boolean ok = main.process(args.toArray(String[]::new));
   233 
   220             if (!ok) throw new RuntimeException("Failed to compile Scala sources");
   234             PrintStream out_orig = System.out;
       
   235             PrintStream err_orig = System.err;
       
   236             ByteArrayOutputStream out = new ByteArrayOutputStream();
       
   237             PrintStream out_stream = new PrintStream(out);
       
   238 
       
   239             // Single-threaded context!
       
   240             try {
       
   241                 System.setOut(out_stream);
       
   242                 System.setErr(out_stream);
       
   243                 ok = new MainClass().process(args.toArray(String[]::new));
       
   244             }
       
   245             finally {
       
   246                 System.setOut(out_orig);
       
   247                 System.setErr(err_orig);
       
   248             }
       
   249             compiler_result(ok, out.toString(), "Scala sources");
   221         }
   250         }
   222     }
   251     }
   223 
   252 
   224     public static void compile_java_sources(
   253     public static void compile_java_sources(
   225         Path target_dir, String more_options, List<Path> deps, List<Path> sources)
   254         Path target_dir, String more_options, List<Path> deps, List<Path> sources)
   243                 for (JavaFileObject o : file_manager.getJavaFileObjectsFromPaths(List.of(p))) {
   272                 for (JavaFileObject o : file_manager.getJavaFileObjectsFromPaths(List.of(p))) {
   244                     java_sources.add(o);
   273                     java_sources.add(o);
   245                 }
   274                 }
   246             }
   275             }
   247         }
   276         }
       
   277 
   248         if (!java_sources.isEmpty()) {
   278         if (!java_sources.isEmpty()) {
   249             boolean ok = compiler.getTask(null, file_manager, null, options, null, java_sources).call();
   279             CharArrayWriter out = new CharArrayWriter();
   250             if (!ok) throw new RuntimeException("Failed to compile Java sources");
   280             boolean ok = compiler.getTask(out, file_manager, null, options, null, java_sources).call();
       
   281             compiler_result(ok, out.toString(), "Java sources");
   251         }
   282         }
   252     }
   283     }
   253 
   284 
   254 
   285 
   255     /** shasum for jar content **/
   286     /** shasum for jar content **/