src/Pure/System/build.scala
author wenzelm
Thu Jul 19 11:47:49 2012 +0200 (2012-07-19)
changeset 48344 8dc904c45945
parent 48341 752de4e10162
child 48346 e2382bede914
permissions -rw-r--r--
prefer general Properties.Value.Boolean;
     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   case class Session_Info(
    23     dir: Path,
    24     name: String,
    25     parent: String,
    26     description: String,
    27     options: Options,
    28     theories: List[(Options, String)],
    29     files: List[String])
    30 
    31   private val pure_info =
    32     Session_Info(Path.current, "Pure", "", "", Nil, List((Nil, "Pure")), Nil)
    33 
    34 
    35   /* parsing */
    36 
    37   val ROOT_NAME = "ROOT"
    38 
    39   private case class Session_Entry(
    40     name: String,
    41     reset: Boolean,
    42     path: Option[String],
    43     parent: String,
    44     description: String,
    45     options: Options,
    46     theories: List[(Options, List[String])],
    47     files: List[String])
    48 
    49   private object Parser extends Parse.Parser
    50   {
    51     val SESSION = "session"
    52     val IN = "in"
    53     val DESCRIPTION = "description"
    54     val OPTIONS = "options"
    55     val THEORIES = "theories"
    56     val FILES = "files"
    57 
    58     val syntax =
    59       Outer_Syntax.empty + "!" + "(" + ")" + "+" + "," + "=" + "[" + "]" +
    60         SESSION + IN + DESCRIPTION + OPTIONS + THEORIES + FILES
    61 
    62     val session_entry: Parser[Session_Entry] =
    63     {
    64       val session_name = atom("session name", _.is_name)
    65       val theory_name = atom("theory name", _.is_name)
    66 
    67       val option =
    68         name ~ opt(keyword("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
    69       val options = keyword("[") ~> repsep(option, keyword(",")) <~ keyword("]")
    70 
    71       val theories =
    72         keyword(THEORIES) ~! ((options | success(Nil)) ~ rep1(theory_name)) ^^
    73           { case _ ~ (x ~ y) => (x, y) }
    74 
    75       ((keyword(SESSION) ~! session_name) ^^ { case _ ~ x => x }) ~
    76         (keyword("!") ^^^ true | success(false)) ~
    77         (opt(keyword(IN) ~! string ^^ { case _ ~ x => x })) ~
    78         (keyword("=") ~> session_name <~ keyword("+")) ~
    79         (keyword(DESCRIPTION) ~! text ^^ { case _ ~ x => x } | success("")) ~
    80         (keyword(OPTIONS) ~! options ^^ { case _ ~ x => x } | success(Nil)) ~
    81         rep1(theories) ~
    82         (keyword(FILES) ~! rep1(path) ^^ { case _ ~ x => x } | success(Nil)) ^^
    83           { case a ~ b ~ c ~ d ~ e ~ f ~ g ~ h => Session_Entry(a, b, c, d, e, f, g, h) }
    84     }
    85 
    86     def parse_entries(root: File): List[Session_Entry] =
    87     {
    88       val toks = syntax.scan(Standard_System.read_file(root))
    89       parse_all(rep(session_entry), Token.reader(toks, root.toString)) match {
    90         case Success(result, _) => result
    91         case bad => error(bad.toString)
    92       }
    93     }
    94   }
    95 
    96 
    97   /* find sessions */
    98 
    99   def find_sessions(more_dirs: List[Path]): List[Session_Info] =
   100   {
   101     val infos = new mutable.ListBuffer[Session_Info]
   102     infos += pure_info
   103 
   104     for {
   105       (dir, strict) <- Isabelle_System.components().map((_, false)) ++ more_dirs.map((_, true))
   106       root = Isabelle_System.platform_file(dir + Path.basic(ROOT_NAME))
   107       _ = (strict && !root.isFile && error("Bad session root file: " + quote(root.toString)))
   108       if root.isFile
   109       entry <- Parser.parse_entries(root)
   110     }
   111     {
   112       try {
   113         val parent =
   114           infos.find(_.name == entry.parent) getOrElse
   115            error("Unknown parent session: " + quote(entry.parent))
   116         val full_name =
   117           if (entry.reset) entry.name
   118           else parent.name + "-" + entry.name
   119 
   120         if (entry.name == "") error("Bad session name")
   121         if (infos.exists(_.name == full_name))
   122           error("Duplicate session name: " + quote(full_name))
   123 
   124         val path =
   125           entry.path match {
   126             case Some(p) => Path.explode(p)
   127             case None => Path.basic(entry.name)
   128           }
   129         val thys = entry.theories.map({ case (x, ys) => ys.map(y => (x, y)) }).flatten
   130         val info =
   131           Session_Info(dir + path, full_name, entry.parent, entry.description,
   132             entry.options, thys, entry.files)
   133 
   134         infos += info
   135       }
   136       catch {
   137         case ERROR(msg) =>
   138           error(msg + "\nThe error(s) above occurred in session entry " +
   139             quote(entry.name) + " (file " + quote(root.toString) + ")")
   140       }
   141     }
   142     infos.toList
   143   }
   144 
   145 
   146 
   147   /** build **/
   148 
   149   def build(all_sessions: Boolean, build_images: Boolean, list_only: Boolean,
   150     more_dirs: List[Path], options: List[String], sessions: List[String]): Int =
   151   {
   152     println("more_dirs = " + more_dirs.toString)
   153     println("options = " + options.toString)
   154     println("sessions = " + sessions.toString)
   155 
   156     find_sessions(more_dirs) foreach println
   157 
   158     0
   159   }
   160 
   161 
   162 
   163   /** command line entry point **/
   164 
   165   private object Chunks
   166   {
   167     private def chunks(list: List[String]): List[List[String]] =
   168       list.indexWhere(_ == "\n") match {
   169         case -1 => List(list)
   170         case i =>
   171           val (chunk, rest) = list.splitAt(i)
   172           chunk :: chunks(rest.tail)
   173       }
   174     def unapplySeq(list: List[String]): Option[List[List[String]]] = Some(chunks(list))
   175   }
   176 
   177   def main(args: Array[String])
   178   {
   179     val rc =
   180       try {
   181         args.toList match {
   182           case
   183             Properties.Value.Boolean(all_sessions) ::
   184             Properties.Value.Boolean(build_images) ::
   185             Properties.Value.Boolean(list_only) ::
   186             Chunks(more_dirs, options, sessions) =>
   187               build(all_sessions, build_images, list_only,
   188                 more_dirs.map(Path.explode), options, sessions)
   189           case _ => error("Bad arguments:\n" + cat_lines(args))
   190         }
   191       }
   192       catch {
   193         case exn: Throwable => java.lang.System.err.println(Exn.message(exn)); 2
   194       }
   195     sys.exit(rc)
   196   }
   197 }
   198