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