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