src/Pure/System/build.scala
author wenzelm
Thu Jul 19 20:39:49 2012 +0200 (2012-07-19)
changeset 48354 aa1e730c3fdd
parent 48352 7fbf98ee265f
child 48361 63bdba7c1366
permissions -rw-r--r--
allow catalog entries to be commented-out;
     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
    11 
    12 import scala.collection.mutable
    13 import scala.annotation.tailrec
    14 
    15 
    16 object Build
    17 {
    18   /** session information **/
    19 
    20   type Options = List[(String, Option[String])]
    21 
    22   object Session
    23   {
    24     object Key
    25     {
    26       object Ordering extends scala.math.Ordering[Key]
    27       {
    28         def compare(key1: Key, key2: Key): Int =
    29           key1.order compare key2.order match {
    30             case 0 => key1.name compare key2.name
    31             case ord => ord
    32           }
    33       }
    34     }
    35 
    36     sealed case class Key(name: String, order: Int)
    37     {
    38       override def toString: String = name
    39     }
    40 
    41     sealed case class Info(
    42       dir: Path,
    43       description: String,
    44       options: Options,
    45       theories: List[(Options, String)],
    46       files: List[String])
    47 
    48     object Queue
    49     {
    50       val empty: Queue = new Queue()
    51     }
    52 
    53     final class Queue private(
    54       keys: Map[String, Key] = Map.empty,
    55       graph: Graph[Key, Info] = Graph.empty(Key.Ordering))
    56     {
    57       def defined(name: String): Boolean = keys.isDefinedAt(name)
    58 
    59       def + (key: Key, info: Info, parent: Option[String]): Queue =
    60       {
    61         val keys1 =
    62           if (defined(key.name)) error("Duplicate session: " + quote(key.name))
    63           else keys + (key.name -> key)
    64 
    65         val graph1 =
    66           try {
    67             graph.new_node(key, info).add_deps_acyclic(key, parent.toList.map(keys(_)))
    68           }
    69           catch {
    70             case exn: Graph.Cycles[_] =>
    71               error(cat_lines(exn.cycles.map(cycle =>
    72                 "Cyclic session dependency of " +
    73                   cycle.map(key => quote(key.toString)).mkString(" via "))))
    74           }
    75         new Queue(keys1, graph1)
    76       }
    77 
    78       def topological_order: List[(Key, Info)] =
    79         graph.topological_order.map(key => (key, graph.get_node(key)))
    80     }
    81   }
    82 
    83 
    84   /* parsing */
    85 
    86   private case class Session_Entry(
    87     name: String,
    88     reset: Boolean,
    89     order: Int,
    90     path: Option[String],
    91     parent: Option[String],
    92     description: String,
    93     options: Options,
    94     theories: List[(Options, List[String])],
    95     files: List[String])
    96 
    97   private object Parser extends Parse.Parser
    98   {
    99     val SESSION = "session"
   100     val IN = "in"
   101     val DESCRIPTION = "description"
   102     val OPTIONS = "options"
   103     val THEORIES = "theories"
   104     val FILES = "files"
   105 
   106     val syntax =
   107       Outer_Syntax.empty + "!" + "(" + ")" + "+" + "," + "=" + "[" + "]" +
   108         SESSION + IN + DESCRIPTION + OPTIONS + THEORIES + FILES
   109 
   110     val session_entry: Parser[Session_Entry] =
   111     {
   112       val session_name = atom("session name", _.is_name)
   113       val theory_name = atom("theory name", _.is_name)
   114 
   115       val option =
   116         name ~ opt(keyword("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
   117       val options = keyword("[") ~> repsep(option, keyword(",")) <~ keyword("]")
   118 
   119       val theories =
   120         keyword(THEORIES) ~! ((options | success(Nil)) ~ rep1(theory_name)) ^^
   121           { case _ ~ (x ~ y) => (x, y) }
   122 
   123       ((keyword(SESSION) ~! session_name) ^^ { case _ ~ x => x }) ~
   124         (keyword("!") ^^^ true | success(false)) ~
   125         (keyword("(") ~! (nat <~ keyword(")")) ^^ { case _ ~ x => x } | success(Integer.MAX_VALUE)) ~
   126         (opt(keyword(IN) ~! string ^^ { case _ ~ x => x })) ~
   127         (keyword("=") ~> opt(session_name <~ keyword("+"))) ~
   128         (keyword(DESCRIPTION) ~! text ^^ { case _ ~ x => x } | success("")) ~
   129         (keyword(OPTIONS) ~! options ^^ { case _ ~ x => x } | success(Nil)) ~
   130         rep(theories) ~
   131         (keyword(FILES) ~! rep1(path) ^^ { case _ ~ x => x } | success(Nil)) ^^
   132           { case a ~ b ~ c ~ d ~ e ~ f ~ g ~ h ~ i => Session_Entry(a, b, c, d, e, f, g, h, i) }
   133     }
   134 
   135     def parse_entries(root: File): List[Session_Entry] =
   136     {
   137       val toks = syntax.scan(Standard_System.read_file(root))
   138       parse_all(rep(session_entry), Token.reader(toks, root.toString)) match {
   139         case Success(result, _) => result
   140         case bad => error(bad.toString)
   141       }
   142     }
   143   }
   144 
   145 
   146   /* find sessions */
   147 
   148   private def sessions_root(dir: Path, root: File, sessions: Session.Queue): Session.Queue =
   149   {
   150     (sessions /: Parser.parse_entries(root))((sessions1, entry) =>
   151       try {
   152         if (entry.name == "") error("Bad session name")
   153 
   154         val full_name =
   155           if (entry.name == "RAW" || entry.name == "Pure") {
   156             if (entry.parent.isDefined) error("Illegal parent session")
   157             else entry.name
   158           }
   159           else
   160             entry.parent match {
   161               case Some(parent_name) if sessions1.defined(parent_name) =>
   162                 if (entry.reset) entry.name
   163                 else parent_name + "-" + entry.name
   164               case _ => error("Bad parent session")
   165             }
   166 
   167         val path =
   168           entry.path match {
   169             case Some(p) => Path.explode(p)
   170             case None => Path.basic(entry.name)
   171           }
   172 
   173         val key = Session.Key(full_name, entry.order)
   174         val info = Session.Info(dir + path, entry.description, entry.options,
   175           entry.theories.map({ case (x, ys) => ys.map(y => (x, y)) }).flatten, entry.files)
   176 
   177         sessions1 + (key, info, entry.parent)
   178       }
   179       catch {
   180         case ERROR(msg) =>
   181           error(msg + "\nThe error(s) above occurred in session entry " +
   182             quote(entry.name) + " (file " + quote(root.toString) + ")")
   183       })
   184   }
   185 
   186   private def sessions_dir(strict: Boolean, dir: Path, sessions: Session.Queue): Session.Queue =
   187   {
   188     val root = Isabelle_System.platform_file(dir + Path.basic("ROOT"))
   189     if (root.isFile) sessions_root(dir, root, sessions)
   190     else if (strict) error("Bad session root file: " + quote(root.toString))
   191     else sessions
   192   }
   193 
   194   private def sessions_catalog(dir: Path, catalog: File, sessions: Session.Queue): Session.Queue =
   195   {
   196     val dirs =
   197       split_lines(Standard_System.read_file(catalog)).
   198         filterNot(line => line == "" || line.startsWith("#"))
   199     (sessions /: dirs)((sessions1, dir1) =>
   200       try {
   201         val dir2 = dir + Path.explode(dir1)
   202         if (Isabelle_System.platform_file(dir2).isDirectory) sessions_dir(true, dir2, sessions1)
   203         else error("Bad session directory: " + dir2.toString)
   204       }
   205       catch {
   206         case ERROR(msg) =>
   207           error(msg + "\nThe error(s) above occurred in session catalog " + quote(catalog.toString))
   208       })
   209   }
   210 
   211   def find_sessions(more_dirs: List[Path]): Session.Queue =
   212   {
   213     var sessions = Session.Queue.empty
   214 
   215     for (dir <- Isabelle_System.components()) {
   216       sessions = sessions_dir(false, dir, sessions)
   217 
   218       val catalog = Isabelle_System.platform_file(dir + Path.explode("etc/sessions"))
   219       if (catalog.isFile)
   220         sessions = sessions_catalog(dir, catalog, sessions)
   221     }
   222 
   223     for (dir <- more_dirs) sessions = sessions_dir(true, dir, sessions)
   224 
   225     sessions
   226   }
   227 
   228 
   229 
   230   /** build **/
   231 
   232   def build(all_sessions: Boolean, build_images: Boolean, list_only: Boolean,
   233     more_dirs: List[Path], options: List[String], sessions: List[String]): Int =
   234   {
   235     println("more_dirs = " + more_dirs.toString)
   236     println("options = " + options.toString)
   237     println("sessions = " + sessions.toString)
   238 
   239     for ((key, info) <- find_sessions(more_dirs).topological_order)
   240       println(key.name + " in " + info.dir)
   241 
   242     0
   243   }
   244 
   245 
   246   /* command line entry point */
   247 
   248   def main(args: Array[String])
   249   {
   250     Command_Line.tool {
   251       args.toList match {
   252         case
   253           Properties.Value.Boolean(all_sessions) ::
   254           Properties.Value.Boolean(build_images) ::
   255           Properties.Value.Boolean(list_only) ::
   256           Command_Line.Chunks(more_dirs, options, sessions) =>
   257             build(all_sessions, build_images, list_only,
   258               more_dirs.map(Path.explode), options, sessions)
   259         case _ => error("Bad arguments:\n" + cat_lines(args))
   260       }
   261     }
   262   }
   263 }
   264