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