-
Notifications
You must be signed in to change notification settings - Fork 0
/
defs.bzl
82 lines (77 loc) · 2.88 KB
/
defs.bzl
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
load("@bazel_skylib//lib:paths.bzl", "paths")
def my_latex_gen(name, main, deps, options = {"bibtex": True}):
"""Generate PDF file & SHA512 checksums for input files.
Args:
name: the name of the target.
main: the latex main file.
deps: the dependencies.
options: extra options.
"""
out = paths.replace_extension(main, ".pdf")
latexmk_bibtex_str = ""
if options["bibtex"]:
latexmk_bibtex_str = ""
else:
latexmk_bibtex_str = "-bibtex-"
cmd_bash = "latexmk -cd -xelatex " + latexmk_bibtex_str + " -latexoption=\"-shell-escape -interaction=errorstopmode -halt-on-error\" $(location " + main + ") && " + \
"mv \"$$(dirname $(location " + main + "))/" + out + "\" \"$@\""
cmd_ps = "latexmk.exe -cd -xelatex " + latexmk_bibtex_str + " -latexoption=\"-shell-escape -interaction=errorstopmode -halt-on-error\" $(location " + main + "); if ($$?) { " + \
"mv \"$$(Split-Path -Parent $(location " + main + "))/" + out + "\" \"$@\" } "
cmd_bat = "latexmk.exe -g -MSWinBackSlash -cd -xelatex " + latexmk_bibtex_str + " -latexoption=\"-shell-escape -interaction=errorstopmode -halt-on-error\" $(location " + main + ") && " + \
"move /Y $(location " + main + ")\\..\\" + out + " $@"
native.genrule(
name = name,
srcs = [main] + deps,
outs = [out],
cmd_bash = cmd_bash,
# cmd_ps = cmd_ps,
cmd_bat = cmd_bat,
local = 1,
)
checksum_inputs = [main, out] + deps
checksum_output = paths.replace_extension(main, ".sha512")
checksum_cmd = " ".join(
[
"sha512sum",
"$(location " + out + ")",
"$(location " + main + ")",
] + [
"$(location " + d + ")"
for d in deps
if not d.startswith("//")
] + [
"$(locations " + d + ")"
for d in deps
if d.startswith("//")
] + [
" > \"$@\"",
],
)
checksum_ps1 = (
"Get-Item (@(" +
", ".join(
[
"\"$(location " + out + ")\"",
"\"$(location " + main + ")\"",
] + [
"\"$(location " + d + ")\""
for d in deps
if not d.startswith("//")
],
) + ")" +
" ".join([
" + \"$(locations " + d + ")\" -split \" \""
for d in deps
if d.startswith("//")
]) + ")" +
" | Get-FileHash -Algorithm SHA512 " +
" | %{ $$_.Hash.ToLower() + \" \" + ((Resolve-Path -Relative $$_.Path) -replace \"\\.\\\\\", \"\" -replace \"\\\\\", \"/\") }" +
" | Set-Content -Encoding UTF8 \"$@\""
)
native.genrule(
name = name + "_checksum",
srcs = checksum_inputs,
outs = [checksum_output],
cmd_bash = checksum_cmd,
cmd_ps = checksum_ps1,
)