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