src/Pure/Admin/build_doc.scala
author wenzelm
Fri, 08 Apr 2022 16:42:52 +0200
changeset 75421 3c710067b178
parent 75394 42267c650205
child 75758 5ad049a5f6a8
permissions -rw-r--r--
back to more ambitious scala-3.1.1 (see 8b7497992301);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64161
2b1128e95dfb explicit indication of Admin tools;
wenzelm
parents: 63805
diff changeset
     1
/*  Title:      Pure/Admin/build_doc.scala
56429
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
     3
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
     4
Build Isabelle documentation.
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
     5
*/
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
     6
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
     7
package isabelle
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
     8
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74814
diff changeset
    10
object Build_Doc {
56429
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    11
  /* build_doc */
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    12
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    13
  def build_doc(
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    14
    options: Options,
71726
a5fda30edae2 clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents: 69854
diff changeset
    15
    progress: Progress = new Progress,
56429
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    16
    all_docs: Boolean = false,
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    17
    max_jobs: Int = 1,
73512
e52a9b208481 support sequential LaTeX jobs: more robust when TeX installation is self-installing packages etc.;
wenzelm
parents: 73340
diff changeset
    18
    sequential: Boolean = false,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74814
diff changeset
    19
    docs: List[String] = Nil
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74814
diff changeset
    20
  ): Unit = {
72578
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    21
    val store = Sessions.store(options)
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    22
67026
687c822ee5e3 tuned signature;
wenzelm
parents: 67023
diff changeset
    23
    val sessions_structure = Sessions.load_structure(options)
72578
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    24
    val selected =
56429
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    25
      for {
72578
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    26
        session <- sessions_structure.build_topological_order
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    27
        info = sessions_structure(session)
56429
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    28
        if info.groups.contains("doc")
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    29
        doc = info.options.string("document_variants")
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    30
        if all_docs || docs.contains(doc)
72578
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    31
      } yield (doc, session)
56429
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    32
72578
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    33
    val documents = selected.map(_._1)
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    34
    val selection = Sessions.Selection(sessions = selected.map(_._2))
56429
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    35
72578
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    36
    docs.filter(doc => !documents.contains(doc)) match {
56429
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    37
      case Nil =>
56432
wenzelm
parents: 56430
diff changeset
    38
      case bad => error("No documentation session for " + commas_quote(bad))
56429
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    39
    }
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    40
72578
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    41
    progress.echo("Build started for sessions " + commas_quote(selection.sessions))
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    42
    Build.build(options, selection = selection, progress = progress, max_jobs = max_jobs).ok ||
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    43
      error("Build failed")
56429
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    44
72578
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    45
    progress.echo("Build started for documentation " + commas_quote(documents))
72675
cc1347c8c804 clarified document output;
wenzelm
parents: 72652
diff changeset
    46
    val doc_options = options + "document=pdf"
72578
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
    47
    val deps = Sessions.load_structure(doc_options).selection_deps(selection)
72597
e8d7dc1c229c more parallelism;
wenzelm
parents: 72594
diff changeset
    48
e8d7dc1c229c more parallelism;
wenzelm
parents: 72594
diff changeset
    49
    val errs =
72675
cc1347c8c804 clarified document output;
wenzelm
parents: 72652
diff changeset
    50
      Par_List.map[(String, String), Option[String]](
cc1347c8c804 clarified document output;
wenzelm
parents: 72652
diff changeset
    51
      {
cc1347c8c804 clarified document output;
wenzelm
parents: 72652
diff changeset
    52
        case (doc, session) =>
cc1347c8c804 clarified document output;
wenzelm
parents: 72652
diff changeset
    53
          try {
74814
79fa9f2d02fa more interrupts;
wenzelm
parents: 73719
diff changeset
    54
            progress.expose_interrupt()
72897
86eff7a823f3 tuned messages;
wenzelm
parents: 72854
diff changeset
    55
            progress.echo("Documentation " + quote(doc) + " ...")
72683
b5e6f0d137a7 clarified signature: prefer Database_Context;
wenzelm
parents: 72675
diff changeset
    56
72854
6c660f05f70c clarified signature;
wenzelm
parents: 72763
diff changeset
    57
            using(store.open_database_context())(db_context =>
73719
d4c7b88f56a0 misc tuning and clarification;
wenzelm
parents: 73718
diff changeset
    58
              Document_Build.build_documents(Document_Build.context(session, deps, db_context),
72685
a7877e14e7f8 proper output directory (amending cc1347c8c804);
wenzelm
parents: 72683
diff changeset
    59
                output_pdf = Some(Path.explode("~~/doc"))))
72675
cc1347c8c804 clarified document output;
wenzelm
parents: 72652
diff changeset
    60
            None
cc1347c8c804 clarified document output;
wenzelm
parents: 72652
diff changeset
    61
          }
cc1347c8c804 clarified document output;
wenzelm
parents: 72652
diff changeset
    62
          catch {
cc1347c8c804 clarified document output;
wenzelm
parents: 72652
diff changeset
    63
            case Exn.Interrupt.ERROR(msg) =>
cc1347c8c804 clarified document output;
wenzelm
parents: 72652
diff changeset
    64
              val sep = if (msg.contains('\n')) "\n" else " "
72897
86eff7a823f3 tuned messages;
wenzelm
parents: 72854
diff changeset
    65
              Some("Documentation " + quote(doc) + " failed:" + sep + msg)
72675
cc1347c8c804 clarified document output;
wenzelm
parents: 72652
diff changeset
    66
          }
73512
e52a9b208481 support sequential LaTeX jobs: more robust when TeX installation is self-installing packages etc.;
wenzelm
parents: 73340
diff changeset
    67
      }, selected, sequential = sequential).flatten
72597
e8d7dc1c229c more parallelism;
wenzelm
parents: 72594
diff changeset
    68
e8d7dc1c229c more parallelism;
wenzelm
parents: 72594
diff changeset
    69
    if (errs.nonEmpty) error(cat_lines(errs))
56429
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    70
  }
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    71
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    72
62838
c91ca9935280 prefer internal tool;
wenzelm
parents: 62641
diff changeset
    73
  /* Isabelle tool wrapper */
56429
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
    74
64161
2b1128e95dfb explicit indication of Admin tools;
wenzelm
parents: 63805
diff changeset
    75
  val isabelle_tool =
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    76
    Isabelle_Tool("build_doc", "build Isabelle documentation", Scala_Project.here,
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    77
      { args =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    78
        var all_docs = false
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    79
        var max_jobs = 1
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    80
        var sequential = false
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    81
        var options = Options.init()
62435
2c390ad93bc8 moved getopts to Scala;
wenzelm
parents: 61276
diff changeset
    82
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    83
        val getopts =
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    84
          Getopts("""
64310
wenzelm
parents: 64220
diff changeset
    85
Usage: isabelle build_doc [OPTIONS] [DOCS ...]
62435
2c390ad93bc8 moved getopts to Scala;
wenzelm
parents: 61276
diff changeset
    86
2c390ad93bc8 moved getopts to Scala;
wenzelm
parents: 61276
diff changeset
    87
  Options are:
2c390ad93bc8 moved getopts to Scala;
wenzelm
parents: 61276
diff changeset
    88
    -a           select all documentation sessions
2c390ad93bc8 moved getopts to Scala;
wenzelm
parents: 61276
diff changeset
    89
    -j INT       maximum number of parallel jobs (default 1)
69854
cc0b3e177b49 system option "system_heaps" supersedes various command-line options for "system build mode";
wenzelm
parents: 69277
diff changeset
    90
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
73512
e52a9b208481 support sequential LaTeX jobs: more robust when TeX installation is self-installing packages etc.;
wenzelm
parents: 73340
diff changeset
    91
    -s           sequential LaTeX jobs
62435
2c390ad93bc8 moved getopts to Scala;
wenzelm
parents: 61276
diff changeset
    92
2c390ad93bc8 moved getopts to Scala;
wenzelm
parents: 61276
diff changeset
    93
  Build Isabelle documentation from documentation sessions with
2c390ad93bc8 moved getopts to Scala;
wenzelm
parents: 61276
diff changeset
    94
  suitable document_variants entry.
2c390ad93bc8 moved getopts to Scala;
wenzelm
parents: 61276
diff changeset
    95
""",
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    96
            "a" -> (_ => all_docs = true),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    97
            "j:" -> (arg => max_jobs = Value.Int.parse(arg)),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    98
            "o:" -> (arg => options = options + arg),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    99
            "s" -> (_ => sequential = true))
62838
c91ca9935280 prefer internal tool;
wenzelm
parents: 62641
diff changeset
   100
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   101
        val docs = getopts(args)
62435
2c390ad93bc8 moved getopts to Scala;
wenzelm
parents: 61276
diff changeset
   102
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   103
        if (!all_docs && docs.isEmpty) getopts.usage()
62435
2c390ad93bc8 moved getopts to Scala;
wenzelm
parents: 61276
diff changeset
   104
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   105
        val progress = new Console_Progress()
72578
3e8395f9093a clarified build_doc, based on Present.build_documents;
wenzelm
parents: 71981
diff changeset
   106
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   107
        progress.interrupt_handler {
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   108
          build_doc(options, progress = progress, all_docs = all_docs, max_jobs = max_jobs,
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   109
            sequential = sequential, docs = docs)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   110
        }
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   111
      })
56429
bc61161a5bd0 re-implemented build_doc in Isabelle/Scala;
wenzelm
parents:
diff changeset
   112
}