src/Pure/System/build.scala
author wenzelm
Fri Jul 20 22:29:25 2012 +0200 (2012-07-20)
changeset 48409 0d2114eb412a
parent 48373 527e2bad7cca
child 48411 5b3440850d36
permissions -rw-r--r--
more explicit java.io.{File => JFile};
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@48409
    10
import java.io.{File => JFile}
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@48350
    29
          key1.order compare key2.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
      description: String,
wenzelm@48349
    44
      options: Options,
wenzelm@48349
    45
      theories: List[(Options, String)],
wenzelm@48349
    46
      files: List[String])
wenzelm@48349
    47
wenzelm@48349
    48
    object Queue
wenzelm@48349
    49
    {
wenzelm@48349
    50
      val empty: Queue = new Queue()
wenzelm@48349
    51
    }
wenzelm@48349
    52
wenzelm@48349
    53
    final class Queue private(
wenzelm@48349
    54
      keys: Map[String, Key] = Map.empty,
wenzelm@48349
    55
      graph: Graph[Key, Info] = Graph.empty(Key.Ordering))
wenzelm@48349
    56
    {
wenzelm@48351
    57
      def defined(name: String): Boolean = keys.isDefinedAt(name)
wenzelm@48349
    58
wenzelm@48351
    59
      def + (key: Key, info: Info, parent: Option[String]): Queue =
wenzelm@48349
    60
      {
wenzelm@48349
    61
        val keys1 =
wenzelm@48351
    62
          if (defined(key.name)) error("Duplicate session: " + quote(key.name))
wenzelm@48351
    63
          else keys + (key.name -> key)
wenzelm@48349
    64
wenzelm@48349
    65
        val graph1 =
wenzelm@48349
    66
          try {
wenzelm@48351
    67
            graph.new_node(key, info).add_deps_acyclic(key, parent.toList.map(keys(_)))
wenzelm@48349
    68
          }
wenzelm@48349
    69
          catch {
wenzelm@48349
    70
            case exn: Graph.Cycles[_] =>
wenzelm@48349
    71
              error(cat_lines(exn.cycles.map(cycle =>
wenzelm@48349
    72
                "Cyclic session dependency of " +
wenzelm@48349
    73
                  cycle.map(key => quote(key.toString)).mkString(" via "))))
wenzelm@48349
    74
          }
wenzelm@48349
    75
        new Queue(keys1, graph1)
wenzelm@48349
    76
      }
wenzelm@48349
    77
wenzelm@48363
    78
      def required(names: List[String]): Queue =
wenzelm@48363
    79
      {
wenzelm@48363
    80
        val req = graph.all_preds(names.map(keys(_))).map(_.name).toSet
wenzelm@48363
    81
        val keys1 = keys -- keys.keySet.filter(name => !req(name))
wenzelm@48363
    82
        val graph1 = graph.restrict(key => keys1.isDefinedAt(key.name))
wenzelm@48363
    83
        new Queue(keys1, graph1)
wenzelm@48363
    84
      }
wenzelm@48363
    85
wenzelm@48351
    86
      def topological_order: List[(Key, Info)] =
wenzelm@48351
    87
        graph.topological_order.map(key => (key, graph.get_node(key)))
wenzelm@48349
    88
    }
wenzelm@48349
    89
  }
wenzelm@48334
    90
wenzelm@48337
    91
wenzelm@48337
    92
  /* parsing */
wenzelm@48337
    93
wenzelm@48337
    94
  private case class Session_Entry(
wenzelm@48337
    95
    name: String,
wenzelm@48337
    96
    reset: Boolean,
wenzelm@48349
    97
    order: Int,
wenzelm@48337
    98
    path: Option[String],
wenzelm@48347
    99
    parent: Option[String],
wenzelm@48337
   100
    description: String,
wenzelm@48337
   101
    options: Options,
wenzelm@48337
   102
    theories: List[(Options, List[String])],
wenzelm@48337
   103
    files: List[String])
wenzelm@48337
   104
wenzelm@48334
   105
  private object Parser extends Parse.Parser
wenzelm@48334
   106
  {
wenzelm@48334
   107
    val SESSION = "session"
wenzelm@48334
   108
    val IN = "in"
wenzelm@48334
   109
    val DESCRIPTION = "description"
wenzelm@48334
   110
    val OPTIONS = "options"
wenzelm@48334
   111
    val THEORIES = "theories"
wenzelm@48334
   112
    val FILES = "files"
wenzelm@48334
   113
wenzelm@48334
   114
    val syntax =
wenzelm@48336
   115
      Outer_Syntax.empty + "!" + "(" + ")" + "+" + "," + "=" + "[" + "]" +
wenzelm@48336
   116
        SESSION + IN + DESCRIPTION + OPTIONS + THEORIES + FILES
wenzelm@48334
   117
wenzelm@48337
   118
    val session_entry: Parser[Session_Entry] =
wenzelm@48334
   119
    {
wenzelm@48334
   120
      val session_name = atom("session name", _.is_name)
wenzelm@48334
   121
      val theory_name = atom("theory name", _.is_name)
wenzelm@48280
   122
wenzelm@48334
   123
      val option =
wenzelm@48334
   124
        name ~ opt(keyword("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
wenzelm@48337
   125
      val options = keyword("[") ~> repsep(option, keyword(",")) <~ keyword("]")
wenzelm@48334
   126
wenzelm@48334
   127
      val theories =
wenzelm@48334
   128
        keyword(THEORIES) ~! ((options | success(Nil)) ~ rep1(theory_name)) ^^
wenzelm@48334
   129
          { case _ ~ (x ~ y) => (x, y) }
wenzelm@48334
   130
wenzelm@48334
   131
      ((keyword(SESSION) ~! session_name) ^^ { case _ ~ x => x }) ~
wenzelm@48336
   132
        (keyword("!") ^^^ true | success(false)) ~
wenzelm@48349
   133
        (keyword("(") ~! (nat <~ keyword(")")) ^^ { case _ ~ x => x } | success(Integer.MAX_VALUE)) ~
wenzelm@48337
   134
        (opt(keyword(IN) ~! string ^^ { case _ ~ x => x })) ~
wenzelm@48347
   135
        (keyword("=") ~> opt(session_name <~ keyword("+"))) ~
wenzelm@48334
   136
        (keyword(DESCRIPTION) ~! text ^^ { case _ ~ x => x } | success("")) ~
wenzelm@48334
   137
        (keyword(OPTIONS) ~! options ^^ { case _ ~ x => x } | success(Nil)) ~
wenzelm@48347
   138
        rep(theories) ~
wenzelm@48336
   139
        (keyword(FILES) ~! rep1(path) ^^ { case _ ~ x => x } | success(Nil)) ^^
wenzelm@48349
   140
          { case a ~ b ~ c ~ d ~ e ~ f ~ g ~ h ~ i => Session_Entry(a, b, c, d, e, f, g, h, i) }
wenzelm@48334
   141
    }
wenzelm@48334
   142
wenzelm@48409
   143
    def parse_entries(root: JFile): List[Session_Entry] =
wenzelm@48334
   144
    {
wenzelm@48335
   145
      val toks = syntax.scan(Standard_System.read_file(root))
wenzelm@48337
   146
      parse_all(rep(session_entry), Token.reader(toks, root.toString)) match {
wenzelm@48334
   147
        case Success(result, _) => result
wenzelm@48334
   148
        case bad => error(bad.toString)
wenzelm@48334
   149
      }
wenzelm@48334
   150
    }
wenzelm@48334
   151
  }
wenzelm@48280
   152
wenzelm@48337
   153
wenzelm@48341
   154
  /* find sessions */
wenzelm@48337
   155
wenzelm@48361
   156
  private val ROOT = Path.explode("ROOT")
wenzelm@48361
   157
  private val SESSIONS = Path.explode("etc/sessions")
wenzelm@48361
   158
wenzelm@48364
   159
  private def is_pure(name: String): Boolean = name == "RAW" || name == "Pure"
wenzelm@48364
   160
wenzelm@48409
   161
  private def sessions_root(dir: Path, root: JFile, queue: Session.Queue): Session.Queue =
wenzelm@48280
   162
  {
wenzelm@48361
   163
    (queue /: Parser.parse_entries(root))((queue1, entry) =>
wenzelm@48337
   164
      try {
wenzelm@48347
   165
        if (entry.name == "") error("Bad session name")
wenzelm@48347
   166
wenzelm@48337
   167
        val full_name =
wenzelm@48364
   168
          if (is_pure(entry.name)) {
wenzelm@48347
   169
            if (entry.parent.isDefined) error("Illegal parent session")
wenzelm@48347
   170
            else entry.name
wenzelm@48347
   171
          }
wenzelm@48347
   172
          else
wenzelm@48347
   173
            entry.parent match {
wenzelm@48361
   174
              case Some(parent_name) if queue1.defined(parent_name) =>
wenzelm@48347
   175
                if (entry.reset) entry.name
wenzelm@48351
   176
                else parent_name + "-" + entry.name
wenzelm@48351
   177
              case _ => error("Bad parent session")
wenzelm@48347
   178
            }
wenzelm@48339
   179
wenzelm@48337
   180
        val path =
wenzelm@48337
   181
          entry.path match {
wenzelm@48337
   182
            case Some(p) => Path.explode(p)
wenzelm@48337
   183
            case None => Path.basic(entry.name)
wenzelm@48337
   184
          }
wenzelm@48339
   185
wenzelm@48351
   186
        val key = Session.Key(full_name, entry.order)
wenzelm@48351
   187
        val info = Session.Info(dir + path, entry.description, entry.options,
wenzelm@48351
   188
          entry.theories.map({ case (x, ys) => ys.map(y => (x, y)) }).flatten, entry.files)
wenzelm@48351
   189
wenzelm@48361
   190
        queue1 + (key, info, entry.parent)
wenzelm@48337
   191
      }
wenzelm@48337
   192
      catch {
wenzelm@48337
   193
        case ERROR(msg) =>
wenzelm@48337
   194
          error(msg + "\nThe error(s) above occurred in session entry " +
wenzelm@48370
   195
            quote(entry.name) + Position.str_of(Position.file(root)))
wenzelm@48352
   196
      })
wenzelm@48352
   197
  }
wenzelm@48352
   198
wenzelm@48361
   199
  private def sessions_dir(strict: Boolean, dir: Path, queue: Session.Queue): Session.Queue =
wenzelm@48352
   200
  {
wenzelm@48373
   201
    val root = (dir + ROOT).file
wenzelm@48361
   202
    if (root.isFile) sessions_root(dir, root, queue)
wenzelm@48352
   203
    else if (strict) error("Bad session root file: " + quote(root.toString))
wenzelm@48361
   204
    else queue
wenzelm@48352
   205
  }
wenzelm@48352
   206
wenzelm@48409
   207
  private def sessions_catalog(dir: Path, catalog: JFile, queue: Session.Queue): Session.Queue =
wenzelm@48352
   208
  {
wenzelm@48354
   209
    val dirs =
wenzelm@48354
   210
      split_lines(Standard_System.read_file(catalog)).
wenzelm@48354
   211
        filterNot(line => line == "" || line.startsWith("#"))
wenzelm@48361
   212
    (queue /: dirs)((queue1, dir1) =>
wenzelm@48352
   213
      try {
wenzelm@48352
   214
        val dir2 = dir + Path.explode(dir1)
wenzelm@48373
   215
        if (dir2.file.isDirectory) sessions_dir(true, dir2, queue1)
wenzelm@48352
   216
        else error("Bad session directory: " + dir2.toString)
wenzelm@48337
   217
      }
wenzelm@48352
   218
      catch {
wenzelm@48352
   219
        case ERROR(msg) =>
wenzelm@48352
   220
          error(msg + "\nThe error(s) above occurred in session catalog " + quote(catalog.toString))
wenzelm@48352
   221
      })
wenzelm@48352
   222
  }
wenzelm@48352
   223
wenzelm@48352
   224
  def find_sessions(more_dirs: List[Path]): Session.Queue =
wenzelm@48352
   225
  {
wenzelm@48361
   226
    var queue = Session.Queue.empty
wenzelm@48352
   227
wenzelm@48352
   228
    for (dir <- Isabelle_System.components()) {
wenzelm@48361
   229
      queue = sessions_dir(false, dir, queue)
wenzelm@48352
   230
wenzelm@48373
   231
      val catalog = (dir + SESSIONS).file
wenzelm@48352
   232
      if (catalog.isFile)
wenzelm@48361
   233
        queue = sessions_catalog(dir, catalog, queue)
wenzelm@48337
   234
    }
wenzelm@48352
   235
wenzelm@48361
   236
    for (dir <- more_dirs) queue = sessions_dir(true, dir, queue)
wenzelm@48352
   237
wenzelm@48361
   238
    queue
wenzelm@48280
   239
  }
wenzelm@48341
   240
wenzelm@48341
   241
wenzelm@48341
   242
wenzelm@48341
   243
  /** build **/
wenzelm@48341
   244
wenzelm@48373
   245
  private def echo(msg: String) { java.lang.System.out.println(msg) }
wenzelm@48373
   246
  private def echo_n(msg: String) { java.lang.System.out.print(msg) }
wenzelm@48373
   247
wenzelm@48364
   248
  private def build_job(build_images: Boolean,  // FIXME
wenzelm@48364
   249
    key: Session.Key, info: Session.Info): Isabelle_System.Bash_Job =
wenzelm@48364
   250
  {
wenzelm@48373
   251
    val cwd = info.dir.file
wenzelm@48364
   252
    val script =
wenzelm@48373
   253
      if (is_pure(key.name)) "./build " + (if (build_images) "-b " else "") + key.name
wenzelm@48364
   254
      else """echo "Bad session" >&2; exit 2"""
wenzelm@48364
   255
    new Isabelle_System.Bash_Job(cwd, null, script)
wenzelm@48364
   256
  }
wenzelm@48364
   257
wenzelm@48341
   258
  def build(all_sessions: Boolean, build_images: Boolean, list_only: Boolean,
wenzelm@48341
   259
    more_dirs: List[Path], options: List[String], sessions: List[String]): Int =
wenzelm@48341
   260
  {
wenzelm@48363
   261
    val full_queue = find_sessions(more_dirs)
wenzelm@48368
   262
    val build_options = (Options.init() /: options)(_.define_simple(_))
wenzelm@48368
   263
wenzelm@48363
   264
    sessions.filter(name => !full_queue.defined(name)) match {
wenzelm@48363
   265
      case Nil =>
wenzelm@48363
   266
      case bad => error("Undefined session(s): " + commas_quote(bad))
wenzelm@48363
   267
    }
wenzelm@48363
   268
wenzelm@48363
   269
    val required_queue =
wenzelm@48363
   270
      if (all_sessions) full_queue
wenzelm@48363
   271
      else full_queue.required(sessions)
wenzelm@48363
   272
wenzelm@48373
   273
    // prepare browser info dir
wenzelm@48373
   274
    if (build_options.bool("browser_info") &&
wenzelm@48373
   275
      !Path.explode("$ISABELLE_BROWSER_INFO/index.html").file.isFile)
wenzelm@48373
   276
    {
wenzelm@48373
   277
      Path.explode("$ISABELLE_BROWSER_INFO").file.mkdirs()
wenzelm@48373
   278
      Standard_System.copy_file(Path.explode("$ISABELLE_HOME/lib/logo/isabelle.gif").file,
wenzelm@48373
   279
        Path.explode("$ISABELLE_BROWSER_INFO/isabelle.gif").file)
wenzelm@48373
   280
      Standard_System.write_file(Path.explode("$ISABELLE_BROWSER_INFO/index.html").file,
wenzelm@48373
   281
        Standard_System.read_file(
wenzelm@48373
   282
          Path.explode("$ISABELLE_HOME/lib/html/library_index_header.template").file) +
wenzelm@48373
   283
        Standard_System.read_file(
wenzelm@48373
   284
          Path.explode("$ISABELLE_HOME/lib/html/library_index_content.template").file) +
wenzelm@48373
   285
        Standard_System.read_file(
wenzelm@48373
   286
          Path.explode("$ISABELLE_HOME/lib/html/library_index_footer.template").file))
wenzelm@48364
   287
    }
wenzelm@48341
   288
wenzelm@48373
   289
    // prepare log dir
wenzelm@48373
   290
    val log_dir = Path.explode("$ISABELLE_OUTPUT/log")
wenzelm@48373
   291
    log_dir.file.mkdirs()
wenzelm@48373
   292
wenzelm@48373
   293
    // run jobs
wenzelm@48373
   294
    val rcs =
wenzelm@48373
   295
      for ((key, info) <- required_queue.topological_order) yield
wenzelm@48373
   296
      {
wenzelm@48373
   297
        if (list_only) { echo(key.name + " in " + info.dir); 0 }
wenzelm@48373
   298
        else {
wenzelm@48373
   299
          if (build_images) echo("Building " + key.name + "...")
wenzelm@48373
   300
          else echo("Running " + key.name + "...")
wenzelm@48373
   301
wenzelm@48373
   302
          val (out, err, rc) = build_job(build_images, key, info).join
wenzelm@48373
   303
          echo_n(err)
wenzelm@48373
   304
wenzelm@48373
   305
          val log = log_dir + Path.basic(key.name)
wenzelm@48373
   306
          if (rc == 0) {
wenzelm@48373
   307
            Standard_System.write_file(log.ext("gz").file, out, true)
wenzelm@48373
   308
          }
wenzelm@48373
   309
          else {
wenzelm@48373
   310
            Standard_System.write_file(log.file, out)
wenzelm@48373
   311
            echo(key.name + " FAILED")
wenzelm@48373
   312
            echo("(see also " + log.file + ")")
wenzelm@48373
   313
            val lines = split_lines(out)
wenzelm@48373
   314
            val tail = lines.drop(lines.length - 20 max 0)
wenzelm@48373
   315
            echo("\n" + cat_lines(tail))
wenzelm@48373
   316
          }
wenzelm@48373
   317
          rc
wenzelm@48373
   318
        }
wenzelm@48373
   319
      }
wenzelm@48373
   320
    (0 /: rcs)(_ max _)
wenzelm@48341
   321
  }
wenzelm@48341
   322
wenzelm@48341
   323
wenzelm@48346
   324
  /* command line entry point */
wenzelm@48341
   325
wenzelm@48341
   326
  def main(args: Array[String])
wenzelm@48341
   327
  {
wenzelm@48346
   328
    Command_Line.tool {
wenzelm@48346
   329
      args.toList match {
wenzelm@48346
   330
        case
wenzelm@48346
   331
          Properties.Value.Boolean(all_sessions) ::
wenzelm@48346
   332
          Properties.Value.Boolean(build_images) ::
wenzelm@48346
   333
          Properties.Value.Boolean(list_only) ::
wenzelm@48346
   334
          Command_Line.Chunks(more_dirs, options, sessions) =>
wenzelm@48346
   335
            build(all_sessions, build_images, list_only,
wenzelm@48346
   336
              more_dirs.map(Path.explode), options, sessions)
wenzelm@48346
   337
        case _ => error("Bad arguments:\n" + cat_lines(args))
wenzelm@48341
   338
      }
wenzelm@48346
   339
    }
wenzelm@48341
   340
  }
wenzelm@48276
   341
}
wenzelm@48276
   342