src/Pure/System/build.scala
author wenzelm
Wed, 18 Jul 2012 16:24:16 +0200
changeset 48336 3c55bfad22eb
parent 48335 2f923e994056
child 48337 9c7f8e5805b4
permissions -rw-r--r--
more tight treatment of reset_name;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48276
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/System/build.scala
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
     3
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
     4
Build and manage Isabelle sessions.
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
     5
*/
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
     6
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
     7
package isabelle
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
     8
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
     9
48335
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    10
import java.io.File
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    11
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    12
48276
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    13
object Build
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    14
{
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    15
  /* command line entry point */
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    16
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    17
  private object Bool
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    18
  {
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    19
    def unapply(s: String): Option[Boolean] =
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    20
      s match {
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    21
        case "true" => Some(true)
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    22
        case "false" => Some(false)
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    23
        case _ => None
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    24
      }
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    25
  }
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    26
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    27
  def main(args: Array[String])
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    28
  {
48335
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    29
    def bad_args(): Nothing = error("Bad arguments: " + args.toString)
48276
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    30
48335
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    31
    val rc =
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    32
      try {
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    33
        args.toList match {
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    34
          case Bool(all_sessions) :: Bool(build_images) :: Bool(list_only) :: rest =>
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    35
            rest.indexWhere(_ == "\n") match {
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    36
              case -1 => bad_args()
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    37
              case i =>
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    38
                val (options, rest1) = rest.splitAt(i)
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    39
                val sessions = rest1.tail
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    40
                build(all_sessions, build_images, list_only, options, sessions)
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    41
            }
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    42
          case _ => bad_args()
48276
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    43
        }
48335
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    44
      }
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    45
      catch {
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    46
        case exn: Throwable => java.lang.System.err.println(Exn.message(exn)); 2
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    47
      }
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    48
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    49
    sys.exit(rc)
48276
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    50
  }
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    51
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    52
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    53
  /* build */
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    54
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    55
  def build(all_sessions: Boolean, build_images: Boolean, list_only: Boolean,
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    56
    options: List[String], sessions: List[String]): Int =
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    57
  {
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    58
    println("options = " + options.toString)
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    59
    println("sessions = " + sessions.toString)
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    60
48335
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    61
    find_sessions() foreach println
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    62
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    63
    0
48276
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
    64
  }
48280
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
    65
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
    66
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
    67
  /* session information */
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
    68
48334
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    69
  val ROOT_NAME = "ROOT"
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    70
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    71
  type Options = List[(String, Option[String])]
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    72
48280
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
    73
  case class Session_Info(
48334
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    74
    dir: Path,
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    75
    name: String,
48336
3c55bfad22eb more tight treatment of reset_name;
wenzelm
parents: 48335
diff changeset
    76
    reset_name: Boolean,
48334
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    77
    parent: String,
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    78
    description: String,
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    79
    options: Options,
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    80
    theories: List[(Options, String)],
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    81
    files: List[String])
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    82
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    83
  private object Parser extends Parse.Parser
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    84
  {
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    85
    val SESSION = "session"
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    86
    val IN = "in"
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    87
    val DESCRIPTION = "description"
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    88
    val OPTIONS = "options"
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    89
    val THEORIES = "theories"
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    90
    val FILES = "files"
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    91
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    92
    val syntax =
48336
3c55bfad22eb more tight treatment of reset_name;
wenzelm
parents: 48335
diff changeset
    93
      Outer_Syntax.empty + "!" + "(" + ")" + "+" + "," + "=" + "[" + "]" +
3c55bfad22eb more tight treatment of reset_name;
wenzelm
parents: 48335
diff changeset
    94
        SESSION + IN + DESCRIPTION + OPTIONS + THEORIES + FILES
48334
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    95
48335
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
    96
    def session_info(dir: Path): Parser[Session_Info] =
48334
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    97
    {
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    98
      val session_name = atom("session name", _.is_name)
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
    99
      val theory_name = atom("theory name", _.is_name)
48280
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
   100
48334
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   101
      val option =
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   102
        name ~ opt(keyword("=") ~! name ^^ { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   103
      val options: Parser[Options] =
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   104
        keyword("[") ~> repsep(option, keyword(",")) <~ keyword("]")
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   105
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   106
      val theories =
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   107
        keyword(THEORIES) ~! ((options | success(Nil)) ~ rep1(theory_name)) ^^
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   108
          { case _ ~ (x ~ y) => (x, y) }
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   109
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   110
      ((keyword(SESSION) ~! session_name) ^^ { case _ ~ x => x }) ~
48336
3c55bfad22eb more tight treatment of reset_name;
wenzelm
parents: 48335
diff changeset
   111
        (keyword("!") ^^^ true | success(false)) ~
3c55bfad22eb more tight treatment of reset_name;
wenzelm
parents: 48335
diff changeset
   112
        (opt(keyword(IN) ~! string ^^ { case _ ~ x => Path.explode(x) })) ~
48334
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   113
        (keyword("=") ~> session_name <~ keyword("+")) ~
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   114
        (keyword(DESCRIPTION) ~! text ^^ { case _ ~ x => x } | success("")) ~
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   115
        (keyword(OPTIONS) ~! options ^^ { case _ ~ x => x } | success(Nil)) ~
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   116
        rep(theories) ~
48336
3c55bfad22eb more tight treatment of reset_name;
wenzelm
parents: 48335
diff changeset
   117
        (keyword(FILES) ~! rep1(path) ^^ { case _ ~ x => x } | success(Nil)) ^^
48334
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   118
          { case a ~ b ~ c ~ d ~ e ~ f ~ g ~ h =>
48336
3c55bfad22eb more tight treatment of reset_name;
wenzelm
parents: 48335
diff changeset
   119
              val dir1 = dir + c.getOrElse(Path.basic(a))
3c55bfad22eb more tight treatment of reset_name;
wenzelm
parents: 48335
diff changeset
   120
              val thys = g.map({ case (x, ys) => ys.map(y => (x, y)) }).flatten
3c55bfad22eb more tight treatment of reset_name;
wenzelm
parents: 48335
diff changeset
   121
              Session_Info(dir1, a, b, d, e, f, thys, h) }
48334
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   122
    }
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   123
48335
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
   124
    def parse_entries(dir: Path, root: File): List[Session_Info] =
48334
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   125
    {
48335
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
   126
      val toks = syntax.scan(Standard_System.read_file(root))
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
   127
      parse_all(rep(session_info(dir)), Token.reader(toks, root.toString)) match {
48334
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   128
        case Success(result, _) => result
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   129
        case bad => error(bad.toString)
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   130
      }
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   131
    }
8dff9933e72a added parser for Session_Info;
wenzelm
parents: 48280
diff changeset
   132
  }
48280
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
   133
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
   134
  def find_sessions(): List[Session_Info] =
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
   135
  {
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
   136
    for {
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
   137
      dir <- Isabelle_System.components()
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
   138
      root = Isabelle_System.platform_file(dir + Path.basic(ROOT_NAME))
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
   139
      if root.isFile
48335
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
   140
      entry <- Parser.parse_entries(dir, root)
2f923e994056 more informative errors;
wenzelm
parents: 48334
diff changeset
   141
    } yield entry
48280
7d86239986c2 basic support for session ROOT files, with examples for FOL and ZF;
wenzelm
parents: 48276
diff changeset
   142
  }
48276
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
   143
}
4bd480886813 basic setup for Isabelle build tool;
wenzelm
parents:
diff changeset
   144