summaryrefslogtreecommitdiff
path: root/latex.bzl
blob: d49639704b734c140439ebb8dc33378a7ef31aca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""Defines latex_document(...) macro.
"""

def _latex_pdf_impl(ctx):
    """Rule to build a single PDF file.
    """
    texlive_path = ctx.var.get("TEXLIVE_FULL_DIR", None)
    if texlive_path == None:
        fail("Please run setup_texlive.sh to set TEXLIVE_FULL_DIR.")
    ctx.actions.run(
        mnemonic = "LaTeX",
        executable = "python",
        use_default_shell_env = True,
        arguments = [
            ctx.files._run_script[0].path,
            texlive_path,
            ctx.files._latexrun[0].path,
            ctx.label.name,
            ctx.files.main[0].path,
            ctx.outputs.out.path,
        ] + [src.path for src in ctx.files.srcs],
        inputs = depset(
            direct = (ctx.files.main + ctx.files.srcs + ctx.files._latexrun +
                      ctx.files._run_script),
        ),
        outputs = [ctx.outputs.out],
    )

_latex_pdf = rule(
    attrs = {
        "main": attr.label(allow_files = True),
        "srcs": attr.label_list(allow_files = True),
        "_latexrun": attr.label(
            allow_files = True,
            default = "@bazel_latex_latexrun//:latexrun",
        ),
        "_run_script": attr.label(
            allow_files = True,
            default = "@bazel_latex//:run_latex.py",
        ),
    },
    outputs = {"out": "%{name}.pdf"},
    implementation = _latex_pdf_impl,
)

def _arxivable_impl(ctx):
    """Rule to run arxiv-latex-cleaner and produce a .tar.gz of the sources.
    """
    texlive_path = ctx.var.get("TEXLIVE_FULL_DIR", None)
    if texlive_path == None:
        fail("Please run setup_texlive.sh to set TEXLIVE_FULL_DIR.")
    ctx.actions.run(
        mnemonic = "Cleaning",
        executable = "python",
        use_default_shell_env = True,
        arguments = [
            ctx.files._run_script[0].path,
            texlive_path,
            ctx.files._latexrun[0].path,
            ctx.files.main[0].path.replace(".tex", ""),
            ctx.files.main[0].path,
            "--",
        ] + [src.path for src in ctx.files.srcs] + [
            "--",
            ctx.files._arxiv_script[0].path,
            ctx.outputs.out.path,
        ],
        inputs = depset(
            direct = (ctx.files.main + ctx.files.srcs + ctx.files._latexrun +
                      ctx.files._run_script + ctx.files._arxiv_script +
                      ctx.files._arxiv_latex_cleaner),
        ),
        outputs = [ctx.outputs.out],
    )

_arxivable = rule(
    attrs = {
        "main": attr.label(allow_files = True),
        "srcs": attr.label_list(allow_files = True),
        "_latexrun": attr.label(
            allow_files = True,
            default = "@bazel_latex_latexrun//:latexrun",
        ),
        "_run_script": attr.label(
            allow_files = True,
            default = "@bazel_latex//:run_latex.py",
        ),
        "_arxiv_script": attr.label(
            allow_files = True,
            default = "@bazel_latex//:get_arxivable.sh",
        ),
        "_arxiv_latex_cleaner": attr.label(
            allow_files = True,
            default = "@arxiv_latex_cleaner//:all",
        ),
    },
    outputs = {"out": "%{name}.tar.gz"},
    implementation = _arxivable_impl,
)

def latex_document(name, main, srcs = []):
    """Given a TeX file, add rules for compiling and archiving it.
    """

    # PDF generation.
    _latex_pdf(
        name = name,
        srcs = srcs,
        main = main,
    )

    # Convenience rule for viewing PDFs.
    native.sh_binary(
        name = name + "_view",
        srcs = ["@bazel_latex//:view_pdf.sh"],
        data = [name + ".pdf"],
    )

    # Copy the PDF into the main working directory.
    native.sh_binary(
        name = name + "_getpdf",
        srcs = ["@bazel_latex//:get_file.sh"],
        args = [name + ".pdf"],
        data = [name + ".pdf"],
    )

    # Create an arXiv-ready version of the source.
    _arxivable(
        name = name + "_arxivable",
        srcs = srcs,
        main = main,
    )

    # Copy the .tar.gz into the main working directory.
    native.sh_binary(
        name = name + "_getarxivable",
        srcs = ["@bazel_latex//:get_file.sh"],
        args = [name + "_arxivable.tar.gz"],
        data = [name + "_arxivable.tar.gz"],
    )
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback