src/Pure/System/build.scala
author wenzelm
Thu Jul 26 12:59:09 2012 +0200 (2012-07-26)
changeset 48511 37999ee01156
parent 48509 4854ced3e9d7
child 48528 784c6f63d79c
permissions -rw-r--r--
remove old output heaps, to ensure that result is valid wrt. check_stamps;
tuned signature;
     1 /*  Title:      Pure/System/build.scala
     2     Author:     Makarius
     3 
     4 Build and manage Isabelle sessions.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import java.io.{File => JFile, BufferedInputStream, FileInputStream,
    11   BufferedReader, InputStreamReader, IOException}
    12 import java.util.zip.GZIPInputStream
    13 
    14 import scala.collection.mutable
    15 import scala.annotation.tailrec
    16 
    17 
    18 object Build
    19 {
    20   /** session information **/
    21 
    22   object Session
    23   {
    24     /* Info */
    25 
    26     sealed case class Info(
    27       base_name: String,
    28       groups: List[String],
    29       dir: Path,
    30       parent: Option[String],
    31       parent_base_name: Option[String],
    32       description: String,
    33       options: Options,
    34       theories: List[(Options, List[Path])],
    35       files: List[Path],
    36       digest: SHA1.Digest)
    37 
    38 
    39     /* Queue */
    40 
    41     object Queue
    42     {
    43       val empty: Queue = new Queue()
    44     }
    45 
    46     final class Queue private(graph: Graph[String, Info] = Graph.string)
    47       extends PartialFunction[String, Info]
    48     {
    49       def apply(name: String): Info = graph.get_node(name)
    50       def isDefinedAt(name: String): Boolean = graph.defined(name)
    51 
    52       def is_inner(name: String): Boolean = !graph.is_maximal(name)
    53 
    54       def is_empty: Boolean = graph.is_empty
    55 
    56       def + (name: String, info: Info): Queue =
    57         new Queue(
    58           try { graph.new_node(name, info).add_deps_acyclic(name, info.parent.toList) }
    59           catch {
    60             case _: Graph.Duplicate[_] => error("Duplicate session: " + quote(name))
    61             case exn: Graph.Cycles[_] =>
    62               error(cat_lines(exn.cycles.map(cycle =>
    63                 "Cyclic session dependency of " +
    64                   cycle.map(c => quote(c.toString)).mkString(" via "))))
    65           })
    66 
    67       def - (name: String): Queue = new Queue(graph.del_node(name))
    68 
    69       def required(groups: List[String], names: List[String]): Queue =
    70       {
    71         val selected_group = groups.toSet
    72         val selected_name = names.toSet
    73         val selected =
    74           graph.keys.filter(name =>
    75             selected_name(name) || apply(name).groups.exists(selected_group)).toList
    76         new Queue(graph.restrict(graph.all_preds(selected).toSet))
    77       }
    78 
    79       def dequeue(skip: String => Boolean): Option[(String, Info)] =
    80       {
    81         val it = graph.entries.dropWhile(
    82           { case (name, (_, (deps, _))) => !deps.isEmpty || skip(name) })
    83         if (it.hasNext) { val (name, (info, _)) = it.next; Some((name, info)) }
    84         else None
    85       }
    86 
    87       def topological_order: List[(String, Info)] =
    88         graph.topological_order.map(name => (name, graph.get_node(name)))
    89     }
    90   }
    91 
    92 
    93   /* parsing */
    94 
    95   private case class Session_Entry(
    96     name: String,
    97     this_name: Boolean,
    98     groups: List[String],
    99     path: Option[String],
   100     parent: Option[String],
   101     description: String,
   102     options: List[Options.Spec],
   103     theories: List[(List[Options.Spec], List[String])],
   104     files: List[String])
   105 
   106   private object Parser extends Parse.Parser
   107   {
   108     val SESSION = "session"
   109     val IN = "in"
   110     val DESCRIPTION = "description"
   111     val OPTIONS = "options"
   112     val THEORIES = "theories"
   113     val FILES = "files"
   114 
   115     val syntax =
   116       Outer_Syntax.empty + "!" + "(" + ")" + "+" + "," + "=" + "[" + "]" +
   117         SESSION + IN + DESCRIPTION + OPTIONS + THEORIES + FILES
   118 
   119     val session_entry: Parser[Session_Entry] =
   120     {
   121       val session_name = atom("session name", _.is_name)
   122 
   123       val option =
   124         name ~ opt(keyword("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
   125       val options = keyword("[") ~> repsep(option, keyword(",")) <~ keyword("]")
   126 
   127       val theories =
   128         keyword(THEORIES) ~! ((options | success(Nil)) ~ rep1(theory_name)) ^^
   129           { case _ ~ (x ~ y) => (x, y) }
   130 
   131       ((keyword(SESSION) ~! session_name) ^^ { case _ ~ x => x }) ~
   132         (keyword("!") ^^^ true | success(false)) ~
   133         (keyword("(") ~! (rep1(name) <~ keyword(")")) ^^ { case _ ~ x => x } | success(Nil)) ~
   134         (opt(keyword(IN) ~! string ^^ { case _ ~ x => x })) ~
   135         (keyword("=") ~> opt(session_name <~ keyword("+"))) ~
   136         (keyword(DESCRIPTION) ~! text ^^ { case _ ~ x => x } | success("")) ~
   137         (keyword(OPTIONS) ~! options ^^ { case _ ~ x => x } | success(Nil)) ~
   138         rep(theories) ~
   139         (keyword(FILES) ~! rep1(path) ^^ { case _ ~ x => x } | success(Nil)) ^^
   140           { case a ~ b ~ c ~ d ~ e ~ f ~ g ~ h ~ i => Session_Entry(a, b, c, d, e, f, g, h, i) }
   141     }
   142 
   143     def parse_entries(root: JFile): List[Session_Entry] =
   144     {
   145       val toks = syntax.scan(File.read(root))
   146       parse_all(rep(session_entry), Token.reader(toks, root.toString)) match {
   147         case Success(result, _) => result
   148         case bad => error(bad.toString)
   149       }
   150     }
   151   }
   152 
   153 
   154   /* find sessions */
   155 
   156   private val ROOT = Path.explode("ROOT")
   157   private val SESSIONS = Path.explode("etc/sessions")
   158 
   159   private def is_pure(name: String): Boolean = name == "RAW" || name == "Pure"
   160 
   161   private def sessions_root(options: Options, dir: Path, root: JFile, queue: Session.Queue)
   162     : Session.Queue =
   163   {
   164     (queue /: Parser.parse_entries(root))((queue1, entry) =>
   165       try {
   166         if (entry.name == "") error("Bad session name")
   167 
   168         val (full_name, parent_base_name) =
   169           if (is_pure(entry.name)) {
   170             if (entry.parent.isDefined) error("Illegal parent session")
   171             else (entry.name, None: Option[String])
   172           }
   173           else
   174             entry.parent match {
   175               case Some(parent_name) if queue1.isDefinedAt(parent_name) =>
   176                 val full_name =
   177                   if (entry.this_name) entry.name
   178                   else parent_name + "-" + entry.name
   179                 val parent_base_name = Some(queue1(parent_name).base_name)
   180                 (full_name, parent_base_name)
   181               case _ => error("Bad parent session")
   182             }
   183 
   184         val path =
   185           entry.path match {
   186             case Some(p) => Path.explode(p)
   187             case None => Path.basic(entry.name)
   188           }
   189 
   190         val session_options = options ++ entry.options
   191 
   192         val theories =
   193           entry.theories.map({ case (opts, thys) =>
   194             (session_options ++ opts, thys.map(Path.explode(_))) })
   195         val files = entry.files.map(Path.explode(_))
   196         val digest = SHA1.digest((full_name, entry.parent, entry.options, entry.theories).toString)
   197 
   198         val info =
   199           Session.Info(entry.name, entry.groups, dir + path, entry.parent, parent_base_name,
   200             entry.description, session_options, theories, files, digest)
   201 
   202         queue1 + (full_name, info)
   203       }
   204       catch {
   205         case ERROR(msg) =>
   206           error(msg + "\nThe error(s) above occurred in session entry " +
   207             quote(entry.name) + Position.str_of(Position.file(root)))
   208       })
   209   }
   210 
   211   private def sessions_dir(options: Options, strict: Boolean, dir: Path, queue: Session.Queue)
   212     : Session.Queue =
   213   {
   214     val root = (dir + ROOT).file
   215     if (root.isFile) sessions_root(options, dir, root, queue)
   216     else if (strict) error("Bad session root file: " + quote(root.toString))
   217     else queue
   218   }
   219 
   220   private def sessions_catalog(options: Options, dir: Path, catalog: JFile, queue: Session.Queue)
   221     : Session.Queue =
   222   {
   223     val dirs =
   224       split_lines(File.read(catalog)).filterNot(line => line == "" || line.startsWith("#"))
   225     (queue /: dirs)((queue1, dir1) =>
   226       try {
   227         val dir2 = dir + Path.explode(dir1)
   228         if (dir2.file.isDirectory) sessions_dir(options, true, dir2, queue1)
   229         else error("Bad session directory: " + dir2.toString)
   230       }
   231       catch {
   232         case ERROR(msg) =>
   233           error(msg + "\nThe error(s) above occurred in session catalog " + quote(catalog.toString))
   234       })
   235   }
   236 
   237   def find_sessions(options: Options, more_dirs: List[Path],
   238     all_sessions: Boolean, session_groups: List[String], sessions: List[String]): Session.Queue =
   239   {
   240     var queue = Session.Queue.empty
   241 
   242     for (dir <- Isabelle_System.components()) {
   243       queue = sessions_dir(options, false, dir, queue)
   244 
   245       val catalog = (dir + SESSIONS).file
   246       if (catalog.isFile)
   247         queue = sessions_catalog(options, dir, catalog, queue)
   248     }
   249 
   250     for (dir <- more_dirs) queue = sessions_dir(options, true, dir, queue)
   251 
   252     sessions.filter(name => !queue.isDefinedAt(name)) match {
   253       case Nil =>
   254       case bad => error("Undefined session(s): " + commas_quote(bad))
   255     }
   256 
   257     if (all_sessions) queue else queue.required(session_groups, sessions)
   258   }
   259 
   260 
   261 
   262   /** build **/
   263 
   264   private def echo(msg: String) { java.lang.System.out.println(msg) }
   265   private def sleep(): Unit = Thread.sleep(500)
   266 
   267 
   268   /* source dependencies */
   269 
   270   sealed case class Node(
   271     loaded_theories: Set[String],
   272     sources: List[(Path, SHA1.Digest)])
   273 
   274   sealed case class Deps(deps: Map[String, Node])
   275   {
   276     def sources(name: String): List[SHA1.Digest] = deps(name).sources.map(_._2)
   277   }
   278 
   279   def dependencies(verbose: Boolean, queue: Session.Queue): Deps =
   280     Deps((Map.empty[String, Node] /: queue.topological_order)(
   281       { case (deps, (name, info)) =>
   282           val preloaded =
   283             info.parent match {
   284               case None => Set.empty[String]
   285               case Some(parent) => deps(parent).loaded_theories
   286             }
   287           val thy_info = new Thy_Info(new Thy_Load(preloaded))
   288 
   289           if (verbose) echo("Checking " + name)
   290 
   291           val thy_deps =
   292             thy_info.dependencies(
   293               info.theories.map(_._2).flatten.
   294                 map(thy => Document.Node.Name(info.dir + Thy_Load.thy_path(thy))))
   295 
   296           val loaded_theories = preloaded ++ thy_deps.map(_._1.theory)
   297 
   298           val all_files =
   299             thy_deps.map({ case (n, h) =>
   300               val thy = Path.explode(n.node).expand
   301               val uses =
   302                 h match {
   303                   case Exn.Res(d) =>
   304                     d.uses.map(p => (Path.explode(n.dir) + Path.explode(p._1)).expand)
   305                   case _ => Nil
   306                 }
   307               thy :: uses
   308             }).flatten ::: info.files.map(file => info.dir + file)
   309           val sources =
   310             try { all_files.map(p => (p, SHA1.digest(p))) }
   311             catch {
   312               case ERROR(msg) =>
   313                 error(msg + "\nThe error(s) above occurred in session " + quote(name))
   314             }
   315 
   316           deps + (name -> Node(loaded_theories, sources))
   317       }))
   318 
   319 
   320   /* jobs */
   321 
   322   private class Job(cwd: JFile, env: Map[String, String], script: String, args: String,
   323     output: Path, do_output: Boolean)
   324   {
   325     private val args_file = File.tmp_file("args")
   326     private val env1 = env + ("ARGS_FILE" -> Isabelle_System.posix_path(args_file.getPath))
   327     File.write(args_file, args)
   328 
   329     private val (thread, result) =
   330       Simple_Thread.future("build") { Isabelle_System.bash_env(cwd, env1, script) }
   331 
   332     def terminate: Unit = thread.interrupt
   333     def is_finished: Boolean = result.is_finished
   334     def join: (String, String, Int) = { val res = result.join; args_file.delete; res }
   335     def output_path: Option[Path] = if (do_output) Some(output) else None
   336   }
   337 
   338   private def start_job(name: String, info: Session.Info, output: Path, do_output: Boolean,
   339     options: Options, timing: Boolean, verbose: Boolean, browser_info: Path): Job =
   340   {
   341     // global browser info dir
   342     if (options.bool("browser_info") && !(browser_info + Path.explode("index.html")).file.isFile)
   343     {
   344       browser_info.file.mkdirs()
   345       File.copy(Path.explode("~~/lib/logo/isabelle.gif"),
   346         browser_info + Path.explode("isabelle.gif"))
   347       File.write(browser_info + Path.explode("index.html"),
   348         File.read(Path.explode("~~/lib/html/library_index_header.template")) +
   349         File.read(Path.explode("~~/lib/html/library_index_content.template")) +
   350         File.read(Path.explode("~~/lib/html/library_index_footer.template")))
   351     }
   352 
   353     val parent = info.parent.getOrElse("")
   354     val parent_base_name = info.parent_base_name.getOrElse("")
   355 
   356     val cwd = info.dir.file
   357     val env =
   358       Map("INPUT" -> parent, "TARGET" -> name, "OUTPUT" -> Isabelle_System.standard_path(output))
   359     val script =
   360       if (is_pure(name)) {
   361         if (do_output) "./build " + name + " \"$OUTPUT\""
   362         else """ rm -f "$OUTPUT"; ./build """ + name
   363       }
   364       else {
   365         """
   366         . "$ISABELLE_HOME/lib/scripts/timestart.bash"
   367         """ +
   368           (if (do_output)
   369             """
   370             "$ISABELLE_PROCESS" -e "Build.build \"$ARGS_FILE\";" -q -w "$INPUT" "$OUTPUT"
   371             """
   372           else
   373             """
   374             rm -f "$OUTPUT"; "$ISABELLE_PROCESS" -e "Build.build \"$ARGS_FILE\";" -r -q "$INPUT"
   375             """) +
   376         """
   377         RC="$?"
   378 
   379         . "$ISABELLE_HOME/lib/scripts/timestop.bash"
   380 
   381         if [ "$RC" -eq 0 ]; then
   382           echo "Finished $TARGET ($TIMES_REPORT)" >&2
   383         fi
   384 
   385         exit "$RC"
   386         """
   387       }
   388     val args_xml =
   389     {
   390       import XML.Encode._
   391           pair(bool, pair(Options.encode, pair(bool, pair(bool, pair(Path.encode, pair(string,
   392             pair(string, pair(string, list(pair(Options.encode, list(Path.encode)))))))))))(
   393           (do_output, (options, (timing, (verbose, (browser_info, (parent_base_name,
   394             (name, (info.base_name, info.theories)))))))))
   395     }
   396     new Job(cwd, env, script, YXML.string_of_body(args_xml), output, do_output)
   397   }
   398 
   399 
   400   /* log files and corresponding heaps */
   401 
   402   private val LOG = Path.explode("log")
   403   private def log(name: String): Path = LOG + Path.basic(name)
   404   private def log_gz(name: String): Path = log(name).ext("gz")
   405 
   406   private def sources_stamp(digests: List[SHA1.Digest]): String =
   407     digests.map(_.toString).sorted.mkString("sources: ", " ", "")
   408 
   409   private def heap_stamp(output: Option[Path]): String =
   410   {
   411     "heap: " +
   412       (output match {
   413         case Some(path) =>
   414           val file = path.file
   415           if (file.isFile) file.length.toString + " " + file.lastModified.toString
   416           else "-"
   417         case None => "-"
   418       })
   419   }
   420 
   421   private def check_stamps(dir: Path, name: String): Option[(String, Boolean)] =
   422   {
   423     val file = (dir + log_gz(name)).file
   424     if (file.isFile) {
   425       val stream = new GZIPInputStream (new BufferedInputStream(new FileInputStream(file)))
   426       val reader = new BufferedReader(new InputStreamReader(stream, Standard_System.charset))
   427       val (s, h) = try { (reader.readLine, reader.readLine) } finally { reader.close }
   428       if (s != null && s.startsWith("sources: ") && h != null && h.startsWith("heap: ") &&
   429           h == heap_stamp(Some(dir + Path.basic(name)))) Some((s, h != "heap: -"))
   430       else None
   431     }
   432     else None
   433   }
   434 
   435 
   436   /* build */
   437 
   438   def build(
   439     all_sessions: Boolean = false,
   440     build_heap: Boolean = false,
   441     more_dirs: List[Path] = Nil,
   442     session_groups: List[String] = Nil,
   443     max_jobs: Int = 1,
   444     no_build: Boolean = false,
   445     build_options: List[String] = Nil,
   446     system_mode: Boolean = false,
   447     timing: Boolean = false,
   448     verbose: Boolean = false,
   449     sessions: List[String] = Nil): Int =
   450   {
   451     val options = (Options.init() /: build_options)(_.define_simple(_))
   452     val queue = find_sessions(options, more_dirs, all_sessions, session_groups, sessions)
   453     val deps = dependencies(verbose, queue)
   454 
   455     def make_stamp(name: String): String =
   456       sources_stamp(queue(name).digest :: deps.sources(name))
   457 
   458     val (input_dirs, output_dir, browser_info) =
   459       if (system_mode) {
   460         val output_dir = Path.explode("~~/heaps/$ML_IDENTIFIER")
   461         (List(output_dir), output_dir, Path.explode("~~/browser_info"))
   462       }
   463       else {
   464         val output_dir = Path.explode("$ISABELLE_OUTPUT")
   465         (output_dir :: Isabelle_System.find_logics_dirs(), output_dir,
   466          Path.explode("$ISABELLE_BROWSER_INFO"))
   467       }
   468 
   469     // prepare log dir
   470     (output_dir + LOG).file.mkdirs()
   471 
   472     // scheduler loop
   473     @tailrec def loop(
   474       pending: Session.Queue,
   475       running: Map[String, Job],
   476       results: Map[String, Int]): Map[String, Int] =
   477     {
   478       if (pending.is_empty) results
   479       else if (running.exists({ case (_, job) => job.is_finished }))
   480       { // finish job
   481         val (name, job) = running.find({ case (_, job) => job.is_finished }).get
   482 
   483         val (out, err, rc) = job.join
   484         echo(Library.trim_line(err))
   485 
   486         if (rc == 0) {
   487           val sources = make_stamp(name)
   488           val heap = heap_stamp(job.output_path)
   489           File.write_gzip(output_dir + log_gz(name), sources + "\n" + heap + "\n" + out)
   490         }
   491         else {
   492           File.write(output_dir + log(name), out)
   493           echo(name + " FAILED")
   494           echo("(see also " + log(name).file.toString + ")")
   495           val lines = split_lines(out)
   496           val tail = lines.drop(lines.length - 20 max 0)
   497           echo("\n" + cat_lines(tail))
   498         }
   499         loop(pending - name, running - name, results + (name -> rc))
   500       }
   501       else if (running.size < (max_jobs max 1))
   502       { // check/start next job
   503         pending.dequeue(running.isDefinedAt(_)) match {
   504           case Some((name, info)) =>
   505             val output = output_dir + Path.basic(name)
   506             val do_output = build_heap || queue.is_inner(name)
   507 
   508             val current =
   509             {
   510               input_dirs.find(dir => (dir + log_gz(name)).file.isFile) match {
   511                 case Some(dir) =>
   512                   check_stamps(dir, name) match {
   513                     case Some((s, h)) => s == make_stamp(name) && (h || !do_output)
   514                     case None => false
   515                   }
   516                 case None => false
   517               }
   518             }
   519             if (current || no_build)
   520               loop(pending - name, running, results + (name -> (if (current) 0 else 1)))
   521             else if (info.parent.map(results(_)).forall(_ == 0)) {
   522               echo((if (do_output) "Building " else "Running ") + name + " ...")
   523               val job =
   524                 start_job(name, info, output, do_output, info.options, timing, verbose, browser_info)
   525               loop(pending, running + (name -> job), results)
   526             }
   527             else {
   528               echo(name + " CANCELLED")
   529               loop(pending - name, running, results + (name -> 1))
   530             }
   531           case None => sleep(); loop(pending, running, results)
   532         }
   533       }
   534       else { sleep(); loop(pending, running, results) }
   535     }
   536 
   537     val results = loop(queue, Map.empty, Map.empty)
   538     val rc = (0 /: results)({ case (rc1, (_, rc2)) => rc1 max rc2 })
   539     if (rc != 0 && (verbose || !no_build)) {
   540       val unfinished = (for ((name, r) <- results.iterator if r != 0) yield name).toList.sorted
   541       echo("Unfinished session(s): " + commas(unfinished))
   542     }
   543     rc
   544   }
   545 
   546 
   547   /* command line entry point */
   548 
   549   def main(args: Array[String])
   550   {
   551     Command_Line.tool {
   552       args.toList match {
   553         case
   554           Properties.Value.Boolean(all_sessions) ::
   555           Properties.Value.Boolean(build_heap) ::
   556           Properties.Value.Int(max_jobs) ::
   557           Properties.Value.Boolean(no_build) ::
   558           Properties.Value.Boolean(system_mode) ::
   559           Properties.Value.Boolean(timing) ::
   560           Properties.Value.Boolean(verbose) ::
   561           Command_Line.Chunks(more_dirs, session_groups, build_options, sessions) =>
   562             build(all_sessions, build_heap, more_dirs.map(Path.explode), session_groups,
   563               max_jobs, no_build, build_options, system_mode, timing, verbose, sessions)
   564         case _ => error("Bad arguments:\n" + cat_lines(args))
   565       }
   566     }
   567   }
   568 }
   569