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