]> git.proxmox.com Git - rustc.git/blob - src/librustdoc/lib.rs
9c393d6f897372531de74158e42e40d56f4251a9
[rustc.git] / src / librustdoc / lib.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Do not remove on snapshot creation. Needed for bootstrap. (Issue #22364)
12 #![cfg_attr(stage0, feature(custom_attribute))]
13 #![crate_name = "rustdoc"]
14 #![unstable(feature = "rustdoc")]
15 #![staged_api]
16 #![crate_type = "dylib"]
17 #![crate_type = "rlib"]
18 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
19 html_favicon_url = "http://www.rust-lang.org/favicon.ico",
20 html_root_url = "http://doc.rust-lang.org/nightly/",
21 html_playground_url = "http://play.rust-lang.org/")]
22
23 #![feature(box_patterns)]
24 #![feature(box_syntax)]
25 #![feature(collections)]
26 #![feature(exit_status)]
27 #![feature(set_stdio)]
28 #![feature(libc)]
29 #![feature(rustc_private)]
30 #![feature(staged_api)]
31 #![feature(std_misc)]
32 #![feature(test)]
33 #![feature(unicode)]
34 #![feature(path_ext)]
35 #![feature(path_relative_from)]
36 #![feature(slice_patterns)]
37
38 extern crate arena;
39 extern crate getopts;
40 extern crate libc;
41 extern crate rustc;
42 extern crate rustc_trans;
43 extern crate rustc_driver;
44 extern crate rustc_resolve;
45 extern crate rustc_lint;
46 extern crate rustc_back;
47 extern crate serialize;
48 extern crate syntax;
49 extern crate test as testing;
50 extern crate rustc_unicode;
51 #[macro_use] extern crate log;
52
53 extern crate serialize as rustc_serialize; // used by deriving
54
55 use std::cell::RefCell;
56 use std::collections::HashMap;
57 use std::env;
58 use std::fs::File;
59 use std::io::{self, Read, Write};
60 use std::path::PathBuf;
61 use std::rc::Rc;
62 use std::sync::mpsc::channel;
63
64 use externalfiles::ExternalHtml;
65 use serialize::Decodable;
66 use serialize::json::{self, Json};
67 use rustc::session::search_paths::SearchPaths;
68
69 // reexported from `clean` so it can be easily updated with the mod itself
70 pub use clean::SCHEMA_VERSION;
71
72 #[macro_use]
73 pub mod externalfiles;
74
75 pub mod clean;
76 pub mod core;
77 pub mod doctree;
78 pub mod fold;
79 pub mod html {
80 pub mod highlight;
81 pub mod escape;
82 pub mod item_type;
83 pub mod format;
84 pub mod layout;
85 pub mod markdown;
86 pub mod render;
87 pub mod toc;
88 }
89 pub mod markdown;
90 pub mod passes;
91 pub mod plugins;
92 pub mod visit_ast;
93 pub mod test;
94 mod flock;
95
96 type Pass = (&'static str, // name
97 fn(clean::Crate) -> plugins::PluginResult, // fn
98 &'static str); // description
99
100 const PASSES: &'static [Pass] = &[
101 ("strip-hidden", passes::strip_hidden,
102 "strips all doc(hidden) items from the output"),
103 ("unindent-comments", passes::unindent_comments,
104 "removes excess indentation on comments in order for markdown to like it"),
105 ("collapse-docs", passes::collapse_docs,
106 "concatenates all document attributes into one document attribute"),
107 ("strip-private", passes::strip_private,
108 "strips all private items from a crate which cannot be seen externally"),
109 ];
110
111 const DEFAULT_PASSES: &'static [&'static str] = &[
112 "strip-hidden",
113 "strip-private",
114 "collapse-docs",
115 "unindent-comments",
116 ];
117
118 thread_local!(pub static ANALYSISKEY: Rc<RefCell<Option<core::CrateAnalysis>>> = {
119 Rc::new(RefCell::new(None))
120 });
121
122 struct Output {
123 krate: clean::Crate,
124 json_plugins: Vec<plugins::PluginJson>,
125 passes: Vec<String>,
126 }
127
128 pub fn main() {
129 const STACK_SIZE: usize = 32000000; // 32MB
130 let res = std::thread::Builder::new().stack_size(STACK_SIZE).spawn(move || {
131 let s = env::args().collect::<Vec<_>>();
132 main_args(&s)
133 }).unwrap().join().unwrap();
134 env::set_exit_status(res as i32);
135 }
136
137 pub fn opts() -> Vec<getopts::OptGroup> {
138 use getopts::*;
139 vec!(
140 optflag("h", "help", "show this help message"),
141 optflag("V", "version", "print rustdoc's version"),
142 optflag("v", "verbose", "use verbose output"),
143 optopt("r", "input-format", "the input type of the specified file",
144 "[rust|json]"),
145 optopt("w", "output-format", "the output type to write",
146 "[html|json]"),
147 optopt("o", "output", "where to place the output", "PATH"),
148 optopt("", "crate-name", "specify the name of this crate", "NAME"),
149 optmulti("L", "library-path", "directory to add to crate search path",
150 "DIR"),
151 optmulti("", "cfg", "pass a --cfg to rustc", ""),
152 optmulti("", "extern", "pass an --extern to rustc", "NAME=PATH"),
153 optmulti("", "plugin-path", "directory to load plugins from", "DIR"),
154 optmulti("", "passes", "list of passes to also run, you might want \
155 to pass it multiple times; a value of `list` \
156 will print available passes",
157 "PASSES"),
158 optmulti("", "plugins", "space separated list of plugins to also load",
159 "PLUGINS"),
160 optflag("", "no-defaults", "don't run the default passes"),
161 optflag("", "test", "run code examples as tests"),
162 optmulti("", "test-args", "arguments to pass to the test runner",
163 "ARGS"),
164 optopt("", "target", "target triple to document", "TRIPLE"),
165 optmulti("", "markdown-css", "CSS files to include via <link> in a rendered Markdown file",
166 "FILES"),
167 optmulti("", "html-in-header",
168 "files to include inline in the <head> section of a rendered Markdown file \
169 or generated documentation",
170 "FILES"),
171 optmulti("", "html-before-content",
172 "files to include inline between <body> and the content of a rendered \
173 Markdown file or generated documentation",
174 "FILES"),
175 optmulti("", "html-after-content",
176 "files to include inline between the content and </body> of a rendered \
177 Markdown file or generated documentation",
178 "FILES"),
179 optopt("", "markdown-playground-url",
180 "URL to send code snippets to", "URL"),
181 optflag("", "markdown-no-toc", "don't include table of contents")
182 )
183 }
184
185 pub fn usage(argv0: &str) {
186 println!("{}",
187 getopts::usage(&format!("{} [options] <input>", argv0),
188 &opts()));
189 }
190
191 pub fn main_args(args: &[String]) -> isize {
192 let matches = match getopts::getopts(args.tail(), &opts()) {
193 Ok(m) => m,
194 Err(err) => {
195 println!("{}", err);
196 return 1;
197 }
198 };
199 if matches.opt_present("h") || matches.opt_present("help") {
200 usage(&args[0]);
201 return 0;
202 } else if matches.opt_present("version") {
203 rustc_driver::version("rustdoc", &matches);
204 return 0;
205 }
206
207 if matches.opt_strs("passes") == ["list"] {
208 println!("Available passes for running rustdoc:");
209 for &(name, _, description) in PASSES {
210 println!("{:>20} - {}", name, description);
211 }
212 println!("{}", "\nDefault passes for rustdoc:"); // FIXME: #9970
213 for &name in DEFAULT_PASSES {
214 println!("{:>20}", name);
215 }
216 return 0;
217 }
218
219 if matches.free.is_empty() {
220 println!("expected an input file to act on");
221 return 1;
222 } if matches.free.len() > 1 {
223 println!("only one input file may be specified");
224 return 1;
225 }
226 let input = &matches.free[0];
227
228 let mut libs = SearchPaths::new();
229 for s in &matches.opt_strs("L") {
230 libs.add_path(s);
231 }
232 let externs = match parse_externs(&matches) {
233 Ok(ex) => ex,
234 Err(err) => {
235 println!("{}", err);
236 return 1;
237 }
238 };
239
240 let test_args = matches.opt_strs("test-args");
241 let test_args: Vec<String> = test_args.iter()
242 .flat_map(|s| s.split_whitespace())
243 .map(|s| s.to_string())
244 .collect();
245
246 let should_test = matches.opt_present("test");
247 let markdown_input = input.ends_with(".md") || input.ends_with(".markdown");
248
249 let output = matches.opt_str("o").map(|s| PathBuf::from(&s));
250 let cfgs = matches.opt_strs("cfg");
251
252 let external_html = match ExternalHtml::load(
253 &matches.opt_strs("html-in-header"),
254 &matches.opt_strs("html-before-content"),
255 &matches.opt_strs("html-after-content")) {
256 Some(eh) => eh,
257 None => return 3
258 };
259 let crate_name = matches.opt_str("crate-name");
260
261 match (should_test, markdown_input) {
262 (true, true) => {
263 return markdown::test(input, libs, externs, test_args)
264 }
265 (true, false) => {
266 return test::run(input, cfgs, libs, externs, test_args, crate_name)
267 }
268 (false, true) => return markdown::render(input,
269 output.unwrap_or(PathBuf::from("doc")),
270 &matches, &external_html,
271 !matches.opt_present("markdown-no-toc")),
272 (false, false) => {}
273 }
274
275 let out = match acquire_input(input, externs, &matches) {
276 Ok(out) => out,
277 Err(s) => {
278 println!("input error: {}", s);
279 return 1;
280 }
281 };
282 let Output { krate, json_plugins, passes, } = out;
283 info!("going to format");
284 match matches.opt_str("w").as_ref().map(|s| &**s) {
285 Some("html") | None => {
286 match html::render::run(krate, &external_html,
287 output.unwrap_or(PathBuf::from("doc")),
288 passes.into_iter().collect()) {
289 Ok(()) => {}
290 Err(e) => panic!("failed to generate documentation: {}", e),
291 }
292 }
293 Some("json") => {
294 match json_output(krate, json_plugins,
295 output.unwrap_or(PathBuf::from("doc.json"))) {
296 Ok(()) => {}
297 Err(e) => panic!("failed to write json: {}", e),
298 }
299 }
300 Some(s) => {
301 println!("unknown output format: {}", s);
302 return 1;
303 }
304 }
305
306 return 0;
307 }
308
309 /// Looks inside the command line arguments to extract the relevant input format
310 /// and files and then generates the necessary rustdoc output for formatting.
311 fn acquire_input(input: &str,
312 externs: core::Externs,
313 matches: &getopts::Matches) -> Result<Output, String> {
314 match matches.opt_str("r").as_ref().map(|s| &**s) {
315 Some("rust") => Ok(rust_input(input, externs, matches)),
316 Some("json") => json_input(input),
317 Some(s) => Err(format!("unknown input format: {}", s)),
318 None => {
319 if input.ends_with(".json") {
320 json_input(input)
321 } else {
322 Ok(rust_input(input, externs, matches))
323 }
324 }
325 }
326 }
327
328 /// Extracts `--extern CRATE=PATH` arguments from `matches` and
329 /// returns a `HashMap` mapping crate names to their paths or else an
330 /// error message.
331 fn parse_externs(matches: &getopts::Matches) -> Result<core::Externs, String> {
332 let mut externs = HashMap::new();
333 for arg in &matches.opt_strs("extern") {
334 let mut parts = arg.splitn(2, '=');
335 let name = match parts.next() {
336 Some(s) => s,
337 None => {
338 return Err("--extern value must not be empty".to_string());
339 }
340 };
341 let location = match parts.next() {
342 Some(s) => s,
343 None => {
344 return Err("--extern value must be of the format `foo=bar`".to_string());
345 }
346 };
347 let name = name.to_string();
348 externs.entry(name).or_insert(vec![]).push(location.to_string());
349 }
350 Ok(externs)
351 }
352
353 /// Interprets the input file as a rust source file, passing it through the
354 /// compiler all the way through the analysis passes. The rustdoc output is then
355 /// generated from the cleaned AST of the crate.
356 ///
357 /// This form of input will run all of the plug/cleaning passes
358 #[allow(deprecated)] // for old Path in plugin manager
359 fn rust_input(cratefile: &str, externs: core::Externs, matches: &getopts::Matches) -> Output {
360 let mut default_passes = !matches.opt_present("no-defaults");
361 let mut passes = matches.opt_strs("passes");
362 let mut plugins = matches.opt_strs("plugins");
363
364 // First, parse the crate and extract all relevant information.
365 let mut paths = SearchPaths::new();
366 for s in &matches.opt_strs("L") {
367 paths.add_path(s);
368 }
369 let cfgs = matches.opt_strs("cfg");
370 let triple = matches.opt_str("target");
371
372 let cr = PathBuf::from(cratefile);
373 info!("starting to run rustc");
374
375 let (tx, rx) = channel();
376 std::thread::spawn(move || {
377 use rustc::session::config::Input;
378
379 tx.send(core::run_core(paths, cfgs, externs, Input::File(cr),
380 triple)).unwrap();
381 }).join().map_err(|_| "rustc failed").unwrap();
382 let (mut krate, analysis) = rx.recv().unwrap();
383 info!("finished with rustc");
384 let mut analysis = Some(analysis);
385 ANALYSISKEY.with(|s| {
386 *s.borrow_mut() = analysis.take();
387 });
388
389 match matches.opt_str("crate-name") {
390 Some(name) => krate.name = name,
391 None => {}
392 }
393
394 // Process all of the crate attributes, extracting plugin metadata along
395 // with the passes which we are supposed to run.
396 match krate.module.as_ref().unwrap().doc_list() {
397 Some(nested) => {
398 for inner in nested {
399 match *inner {
400 clean::Word(ref x)
401 if "no_default_passes" == *x => {
402 default_passes = false;
403 }
404 clean::NameValue(ref x, ref value)
405 if "passes" == *x => {
406 for pass in value.split_whitespace() {
407 passes.push(pass.to_string());
408 }
409 }
410 clean::NameValue(ref x, ref value)
411 if "plugins" == *x => {
412 for p in value.split_whitespace() {
413 plugins.push(p.to_string());
414 }
415 }
416 _ => {}
417 }
418 }
419 }
420 None => {}
421 }
422 if default_passes {
423 for name in DEFAULT_PASSES.iter().rev() {
424 passes.insert(0, name.to_string());
425 }
426 }
427
428 // Load all plugins/passes into a PluginManager
429 let path = matches.opt_str("plugin-path")
430 .unwrap_or("/tmp/rustdoc/plugins".to_string());
431 let mut pm = plugins::PluginManager::new(PathBuf::from(path));
432 for pass in &passes {
433 let plugin = match PASSES.iter()
434 .position(|&(p, _, _)| {
435 p == *pass
436 }) {
437 Some(i) => PASSES[i].1,
438 None => {
439 error!("unknown pass {}, skipping", *pass);
440 continue
441 },
442 };
443 pm.add_plugin(plugin);
444 }
445 info!("loading plugins...");
446 for pname in plugins {
447 pm.load_plugin(pname);
448 }
449
450 // Run everything!
451 info!("Executing passes/plugins");
452 let (krate, json) = pm.run_plugins(krate);
453 return Output { krate: krate, json_plugins: json, passes: passes, };
454 }
455
456 /// This input format purely deserializes the json output file. No passes are
457 /// run over the deserialized output.
458 fn json_input(input: &str) -> Result<Output, String> {
459 let mut bytes = Vec::new();
460 match File::open(input).and_then(|mut f| f.read_to_end(&mut bytes)) {
461 Ok(_) => {}
462 Err(e) => return Err(format!("couldn't open {}: {}", input, e)),
463 };
464 match json::from_reader(&mut &bytes[..]) {
465 Err(s) => Err(format!("{:?}", s)),
466 Ok(Json::Object(obj)) => {
467 let mut obj = obj;
468 // Make sure the schema is what we expect
469 match obj.remove(&"schema".to_string()) {
470 Some(Json::String(version)) => {
471 if version != SCHEMA_VERSION {
472 return Err(format!(
473 "sorry, but I only understand version {}",
474 SCHEMA_VERSION))
475 }
476 }
477 Some(..) => return Err("malformed json".to_string()),
478 None => return Err("expected a schema version".to_string()),
479 }
480 let krate = match obj.remove(&"crate".to_string()) {
481 Some(json) => {
482 let mut d = json::Decoder::new(json);
483 Decodable::decode(&mut d).unwrap()
484 }
485 None => return Err("malformed json".to_string()),
486 };
487 // FIXME: this should read from the "plugins" field, but currently
488 // Json doesn't implement decodable...
489 let plugin_output = Vec::new();
490 Ok(Output { krate: krate, json_plugins: plugin_output, passes: Vec::new(), })
491 }
492 Ok(..) => {
493 Err("malformed json input: expected an object at the \
494 top".to_string())
495 }
496 }
497 }
498
499 /// Outputs the crate/plugin json as a giant json blob at the specified
500 /// destination.
501 fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
502 dst: PathBuf) -> io::Result<()> {
503 // {
504 // "schema": version,
505 // "crate": { parsed crate ... },
506 // "plugins": { output of plugins ... }
507 // }
508 let mut json = std::collections::BTreeMap::new();
509 json.insert("schema".to_string(), Json::String(SCHEMA_VERSION.to_string()));
510 let plugins_json = res.into_iter()
511 .filter_map(|opt| {
512 match opt {
513 None => None,
514 Some((string, json)) => {
515 Some((string.to_string(), json))
516 }
517 }
518 }).collect();
519
520 // FIXME #8335: yuck, Rust -> str -> JSON round trip! No way to .encode
521 // straight to the Rust JSON representation.
522 let crate_json_str = format!("{}", json::as_json(&krate));
523 let crate_json = match json::from_str(&crate_json_str) {
524 Ok(j) => j,
525 Err(e) => panic!("Rust generated JSON is invalid: {:?}", e)
526 };
527
528 json.insert("crate".to_string(), crate_json);
529 json.insert("plugins".to_string(), Json::Object(plugins_json));
530
531 let mut file = try!(File::create(&dst));
532 write!(&mut file, "{}", Json::Object(json))
533 }