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