forked from tectonic-typesetting/tectonic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.rs
255 lines (212 loc) · 8.83 KB
/
compile.rs
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2016-2021 the Tectonic Project
// Licensed under the MIT License.
//! Standalone compilation of TeX documents. This implements the "classic" /
//! "V1" / "rustc-like" Tectonic command-line interface, as well as the
//! `compile` subcommand of the "V2" / "cargo-like" interface.
use std::{
path::{Path, PathBuf},
str::FromStr,
};
use structopt::StructOpt;
use tectonic_bridge_core::{SecuritySettings, SecurityStance};
use tectonic::{
config::PersistentConfig,
driver::{OutputFormat, PassSetting, ProcessingSession, ProcessingSessionBuilder},
errmsg,
errors::{ErrorKind, Result},
status::StatusBackend,
tt_error, tt_note,
unstable_opts::{UnstableArg, UnstableOptions},
};
#[derive(Debug, StructOpt)]
pub struct CompileOptions {
/// The file to process, or "-" to process the standard input stream
#[structopt(name = "input")]
input: String,
/// The name of the "format" file used to initialize the TeX engine
#[structopt(long, short, name = "path", default_value = "latex")]
format: String,
/// Use this directory or Zip-format bundle file to find resource files instead of the default
#[structopt(takes_value(true), parse(from_os_str), long, short, name = "file_path")]
bundle: Option<PathBuf>,
/// Use this URL to find resource files instead of the default
#[structopt(takes_value(true), long, short, name = "url")]
// TODO add URL validation
web_bundle: Option<String>,
/// Use only resource files cached locally
#[structopt(short = "C", long)]
only_cached: bool,
/// The kind of output to generate
#[structopt(long, name = "format", default_value = "pdf", possible_values(&["pdf", "html", "xdv", "aux", "fmt"]))]
outfmt: String,
/// Write Makefile-format rules expressing the dependencies of this run to <dest_path>
#[structopt(long, name = "dest_path")]
makefile_rules: Option<PathBuf>,
/// Which engines to run
#[structopt(long, default_value = "default", possible_values(&["default", "tex", "bibtex_first"]))]
pass: String,
/// Rerun the TeX engine exactly this many times after the first
#[structopt(name = "count", long = "reruns", short = "r")]
reruns: Option<usize>,
/// Keep the intermediate files generated during processing
#[structopt(short, long)]
keep_intermediates: bool,
/// Keep the log files generated during processing
#[structopt(long)]
keep_logs: bool,
/// Generate SyncTeX data
#[structopt(long)]
synctex: bool,
/// Tell the engine that no file at <hide_path> exists, if it tries to read it
#[structopt(long, name = "hide_path")]
hide: Option<Vec<PathBuf>>,
/// Print the engine's chatter during processing
#[structopt(long = "print", short)]
print_stdout: bool,
/// The directory in which to place output files [default: the directory containing <input>]
#[structopt(name = "outdir", short, long, parse(from_os_str))]
outdir: Option<PathBuf>,
/// Automatically create the output directory if it doesn't exist and outdir is specified
#[structopt(name = "outdir-create", long, short = "-O", requires("outdir"))]
outdir_create: bool,
/// Input is untrusted -- disable all known-insecure features
#[structopt(long)]
untrusted: bool,
/// Unstable options. Pass -Zhelp to show a list
#[structopt(name = "option", short = "Z", number_of_values = 1)]
unstable: Vec<UnstableArg>,
}
impl CompileOptions {
pub fn execute(self, config: PersistentConfig, status: &mut dyn StatusBackend) -> Result<i32> {
let unstable = UnstableOptions::from_unstable_args(self.unstable.into_iter());
// Default to allowing insecure since it would be super duper annoying
// to have to pass `--trusted` every time to build a personal document
// that uses shell-escape! This default can be overridden by setting the
// environment variable TECTONIC_UNTRUSTED_MODE to a nonempty value.
let stance = if self.untrusted {
SecurityStance::DisableInsecures
} else {
SecurityStance::MaybeAllowInsecures
};
let mut sess_builder =
ProcessingSessionBuilder::new_with_security(SecuritySettings::new(stance));
let format_path = self.format;
let deterministic_mode = unstable.deterministic_mode;
sess_builder
.unstables(unstable)
.format_name(&format_path)
.keep_logs(self.keep_logs)
.keep_intermediates(self.keep_intermediates)
.format_cache_path(config.format_cache_path()?)
.synctex(self.synctex);
sess_builder.output_format(OutputFormat::from_str(&self.outfmt).unwrap());
let pass = PassSetting::from_str(&self.pass).unwrap();
sess_builder.pass(pass);
if let Some(s) = self.reruns {
sess_builder.reruns(s);
}
if let Some(p) = self.makefile_rules {
sess_builder.makefile_output_path(p);
}
// Input and path setup
let input_path = self.input;
if input_path == "-" {
// Don't provide an input path to the ProcessingSession, so it will default to stdin.
sess_builder.tex_input_name("texput.tex");
sess_builder.output_dir(Path::new(""));
tt_note!(
status,
"reading from standard input; outputs will appear under the base name \"texput\""
);
} else {
let input_path = Path::new(&input_path);
sess_builder.primary_input_path(input_path);
if let Some(fname) = input_path.file_name() {
sess_builder.tex_input_name(&fname.to_string_lossy());
} else {
return Err(errmsg!(
"can't figure out a basename for input path \"{}\"",
input_path.to_string_lossy()
));
};
if let Some(par) = input_path.parent() {
sess_builder.output_dir(par);
} else {
return Err(errmsg!(
"can't figure out a parent directory for input path \"{}\"",
input_path.to_string_lossy()
));
}
}
if let Some(output_dir) = self.outdir {
if output_dir.is_file() {
return Err(errmsg!(
"output directory \"{}\" is a file, not a directory",
&output_dir.display()
));
} else if !output_dir.exists() && !self.outdir_create {
return Err(errmsg!(
"output directory \"{}\" does not exist",
&output_dir.display()
));
}
if self.outdir_create {
if let Some(create_dir_error) = std::fs::create_dir(&output_dir).err() {
return Err(errmsg!(
"failed to create output directory at \"{}\", error: {}",
&output_dir.display(),
create_dir_error
));
}
}
sess_builder.output_dir(output_dir);
}
// Set up the rest of I/O.
sess_builder.print_stdout(self.print_stdout);
if let Some(items) = self.hide {
for v in items {
sess_builder.hide(v);
}
}
let only_cached = self.only_cached;
if only_cached {
tt_note!(status, "using only cached resource files");
}
if let Some(path) = self.bundle {
sess_builder.bundle(config.make_local_file_provider(path, status)?);
} else if let Some(u) = self.web_bundle {
sess_builder.bundle(config.make_cached_url_provider(&u, only_cached, None, status)?);
} else {
sess_builder.bundle(config.default_bundle(only_cached, status)?);
}
sess_builder.build_date_from_env(deterministic_mode);
run_and_report(sess_builder, status).map(|_| 0)
}
}
pub(crate) fn run_and_report(
sess_builder: ProcessingSessionBuilder,
status: &mut dyn StatusBackend,
) -> Result<ProcessingSession> {
let mut sess = sess_builder.create(status)?;
let result = sess.run(status);
if let Err(e) = &result {
if let ErrorKind::EngineError(engine) = e.kind() {
let output = sess.get_stdout_content();
if output.is_empty() {
tt_error!(
status,
"something bad happened inside {}, but no output was logged",
engine
);
} else {
tt_error!(
status,
"something bad happened inside {}; its output follows:\n",
engine
);
status.dump_error_logs(&output);
}
}
}
result.map(|_| sess)
}