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