]>
Commit | Line | Data |
---|---|---|
83571aee EH |
1 | //! Tests for rustc plugins. |
2 | ||
9115b2c3 AC |
3 | use cargo_test_support::{basic_manifest, project}; |
4 | use cargo_test_support::{is_nightly, rustc_host}; | |
f3e43bfc | 5 | |
0e0d9688 | 6 | #[cargo_test] |
6950bbb0 | 7 | fn plugin_to_the_max() { |
1e682848 | 8 | if !is_nightly() { |
50277e88 | 9 | // plugins are unstable |
1e682848 AC |
10 | return; |
11 | } | |
23762e45 | 12 | |
7fe2fbc8 | 13 | let foo = project() |
1e682848 AC |
14 | .file( |
15 | "Cargo.toml", | |
16 | r#" | |
6f8c7d5a EH |
17 | [package] |
18 | name = "foo" | |
19 | version = "0.0.1" | |
20 | authors = [] | |
f3e43bfc | 21 | |
6f8c7d5a EH |
22 | [lib] |
23 | name = "foo_lib" | |
6a2a2c95 | 24 | |
6f8c7d5a EH |
25 | [dependencies.bar] |
26 | path = "../bar" | |
27 | "#, | |
fecb7246 AC |
28 | ) |
29 | .file( | |
1e682848 AC |
30 | "src/main.rs", |
31 | r#" | |
6f8c7d5a EH |
32 | #![feature(plugin)] |
33 | #![plugin(bar)] | |
34 | extern crate foo_lib; | |
f3e43bfc | 35 | |
6f8c7d5a EH |
36 | fn main() { foo_lib::foo(); } |
37 | "#, | |
fecb7246 AC |
38 | ) |
39 | .file( | |
1e682848 AC |
40 | "src/foo_lib.rs", |
41 | r#" | |
6f8c7d5a EH |
42 | #![feature(plugin)] |
43 | #![plugin(bar)] | |
6a2a2c95 | 44 | |
6f8c7d5a EH |
45 | pub fn foo() {} |
46 | "#, | |
fecb7246 AC |
47 | ) |
48 | .build(); | |
85984a87 DW |
49 | let _bar = project() |
50 | .at("bar") | |
1e682848 AC |
51 | .file( |
52 | "Cargo.toml", | |
53 | r#" | |
6f8c7d5a EH |
54 | [package] |
55 | name = "bar" | |
56 | version = "0.0.1" | |
57 | authors = [] | |
58 | ||
59 | [lib] | |
60 | name = "bar" | |
61 | plugin = true | |
62 | ||
63 | [dependencies.baz] | |
64 | path = "../baz" | |
65 | "#, | |
fecb7246 AC |
66 | ) |
67 | .file( | |
1e682848 AC |
68 | "src/lib.rs", |
69 | r#" | |
6f8c7d5a | 70 | #![feature(plugin_registrar, rustc_private)] |
f3e43bfc | 71 | |
6f8c7d5a EH |
72 | extern crate baz; |
73 | extern crate rustc_driver; | |
f3e43bfc | 74 | |
6f8c7d5a | 75 | use rustc_driver::plugin::Registry; |
f3e43bfc | 76 | |
6f8c7d5a EH |
77 | #[plugin_registrar] |
78 | pub fn foo(_reg: &mut Registry) { | |
79 | println!("{}", baz::baz()); | |
80 | } | |
81 | "#, | |
fecb7246 AC |
82 | ) |
83 | .build(); | |
85984a87 DW |
84 | let _baz = project() |
85 | .at("baz") | |
1e682848 AC |
86 | .file( |
87 | "Cargo.toml", | |
88 | r#" | |
6f8c7d5a EH |
89 | [package] |
90 | name = "baz" | |
91 | version = "0.0.1" | |
92 | authors = [] | |
93 | ||
94 | [lib] | |
95 | name = "baz" | |
96 | crate_type = ["dylib"] | |
97 | "#, | |
fecb7246 AC |
98 | ) |
99 | .file("src/lib.rs", "pub fn baz() -> i32 { 1 }") | |
d43ee1dd | 100 | .build(); |
f3e43bfc | 101 | |
85984a87 DW |
102 | foo.cargo("build").run(); |
103 | foo.cargo("doc").run(); | |
6950bbb0 | 104 | } |
0c8f6dba | 105 | |
0e0d9688 | 106 | #[cargo_test] |
6950bbb0 | 107 | fn plugin_with_dynamic_native_dependency() { |
1e682848 | 108 | if !is_nightly() { |
50277e88 | 109 | // plugins are unstable |
1e682848 AC |
110 | return; |
111 | } | |
23762e45 | 112 | |
85984a87 | 113 | let build = project() |
83ff57a4 | 114 | .at("builder") |
1e682848 AC |
115 | .file( |
116 | "Cargo.toml", | |
117 | r#" | |
6f8c7d5a EH |
118 | [package] |
119 | name = "builder" | |
120 | version = "0.0.1" | |
121 | authors = [] | |
122 | ||
123 | [lib] | |
124 | name = "builder" | |
125 | crate-type = ["dylib"] | |
126 | "#, | |
fecb7246 AC |
127 | ) |
128 | .file("src/lib.rs", "#[no_mangle] pub extern fn foo() {}") | |
d43ee1dd | 129 | .build(); |
0c8f6dba | 130 | |
85984a87 | 131 | let foo = project() |
1e682848 AC |
132 | .file( |
133 | "Cargo.toml", | |
134 | r#" | |
6f8c7d5a EH |
135 | [package] |
136 | name = "foo" | |
137 | version = "0.0.1" | |
138 | authors = [] | |
139 | ||
140 | [dependencies.bar] | |
141 | path = "bar" | |
142 | "#, | |
fecb7246 AC |
143 | ) |
144 | .file( | |
1e682848 AC |
145 | "src/main.rs", |
146 | r#" | |
6f8c7d5a EH |
147 | #![feature(plugin)] |
148 | #![plugin(bar)] | |
0c8f6dba | 149 | |
6f8c7d5a EH |
150 | fn main() {} |
151 | "#, | |
fecb7246 AC |
152 | ) |
153 | .file( | |
1e682848 AC |
154 | "bar/Cargo.toml", |
155 | r#" | |
6f8c7d5a EH |
156 | [package] |
157 | name = "bar" | |
158 | version = "0.0.1" | |
159 | authors = [] | |
160 | build = 'build.rs' | |
161 | ||
162 | [lib] | |
163 | name = "bar" | |
164 | plugin = true | |
165 | "#, | |
fecb7246 AC |
166 | ) |
167 | .file( | |
1e682848 AC |
168 | "bar/build.rs", |
169 | r#" | |
6f8c7d5a EH |
170 | use std::env; |
171 | use std::fs; | |
172 | use std::path::PathBuf; | |
173 | ||
174 | fn main() { | |
175 | let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); | |
176 | let root = PathBuf::from(env::var("BUILDER_ROOT").unwrap()); | |
177 | let file = format!("{}builder{}", | |
178 | env::consts::DLL_PREFIX, | |
179 | env::consts::DLL_SUFFIX); | |
180 | let src = root.join(&file); | |
181 | let dst = out_dir.join(&file); | |
182 | fs::copy(src, dst).unwrap(); | |
183 | if cfg!(target_env = "msvc") { | |
184 | fs::copy(root.join("builder.dll.lib"), | |
185 | out_dir.join("builder.dll.lib")).unwrap(); | |
186 | } | |
187 | println!("cargo:rustc-flags=-L {}", out_dir.display()); | |
83ff57a4 | 188 | } |
6f8c7d5a | 189 | "#, |
fecb7246 AC |
190 | ) |
191 | .file( | |
1e682848 AC |
192 | "bar/src/lib.rs", |
193 | r#" | |
6f8c7d5a | 194 | #![feature(plugin_registrar, rustc_private)] |
0c8f6dba | 195 | |
6f8c7d5a EH |
196 | extern crate rustc_driver; |
197 | use rustc_driver::plugin::Registry; | |
0c8f6dba | 198 | |
6f8c7d5a EH |
199 | #[cfg_attr(not(target_env = "msvc"), link(name = "builder"))] |
200 | #[cfg_attr(target_env = "msvc", link(name = "builder.dll"))] | |
201 | extern { fn foo(); } | |
0c8f6dba | 202 | |
6f8c7d5a EH |
203 | #[plugin_registrar] |
204 | pub fn bar(_reg: &mut Registry) { | |
205 | unsafe { foo() } | |
206 | } | |
207 | "#, | |
fecb7246 AC |
208 | ) |
209 | .build(); | |
b8158cfd | 210 | |
85984a87 | 211 | build.cargo("build").run(); |
b8158cfd | 212 | |
83ff57a4 EH |
213 | let root = build.root().join("target").join("debug"); |
214 | foo.cargo("build -v").env("BUILDER_ROOT", root).run(); | |
6950bbb0 | 215 | } |
175693a2 | 216 | |
0e0d9688 | 217 | #[cargo_test] |
6950bbb0 | 218 | fn plugin_integration() { |
7fe2fbc8 | 219 | let p = project() |
1e682848 AC |
220 | .file( |
221 | "Cargo.toml", | |
222 | r#" | |
6f8c7d5a EH |
223 | [package] |
224 | name = "foo" | |
225 | version = "0.0.1" | |
226 | authors = [] | |
227 | build = "build.rs" | |
228 | ||
229 | [lib] | |
230 | name = "foo" | |
231 | plugin = true | |
232 | doctest = false | |
233 | "#, | |
fecb7246 AC |
234 | ) |
235 | .file("build.rs", "fn main() {}") | |
175693a2 | 236 | .file("src/lib.rs", "") |
d43ee1dd NK |
237 | .file("tests/it_works.rs", "") |
238 | .build(); | |
175693a2 | 239 | |
85984a87 | 240 | p.cargo("test -v").run(); |
6950bbb0 | 241 | } |
b6609ac6 | 242 | |
0e0d9688 | 243 | #[cargo_test] |
6950bbb0 | 244 | fn doctest_a_plugin() { |
7fe2fbc8 | 245 | let p = project() |
1e682848 AC |
246 | .file( |
247 | "Cargo.toml", | |
248 | r#" | |
6f8c7d5a EH |
249 | [package] |
250 | name = "foo" | |
251 | version = "0.0.1" | |
252 | authors = [] | |
253 | ||
254 | [dependencies] | |
255 | bar = { path = "bar" } | |
256 | "#, | |
fecb7246 AC |
257 | ) |
258 | .file("src/lib.rs", "#[macro_use] extern crate bar;") | |
1e682848 AC |
259 | .file( |
260 | "bar/Cargo.toml", | |
261 | r#" | |
6f8c7d5a EH |
262 | [package] |
263 | name = "bar" | |
264 | version = "0.0.1" | |
265 | authors = [] | |
266 | ||
267 | [lib] | |
268 | name = "bar" | |
269 | plugin = true | |
270 | "#, | |
fecb7246 AC |
271 | ) |
272 | .file("bar/src/lib.rs", "pub fn bar() {}") | |
d43ee1dd | 273 | .build(); |
b6609ac6 | 274 | |
85984a87 | 275 | p.cargo("test -v").run(); |
6950bbb0 | 276 | } |
e2c0a9d8 JA |
277 | |
278 | // See #1515 | |
0e0d9688 | 279 | #[cargo_test] |
35b924db | 280 | fn native_plugin_dependency_with_custom_linker() { |
763ba535 | 281 | let target = rustc_host(); |
e2c0a9d8 | 282 | |
7fe2fbc8 | 283 | let _foo = project() |
1e682848 AC |
284 | .file( |
285 | "Cargo.toml", | |
286 | r#" | |
6f8c7d5a EH |
287 | [package] |
288 | name = "foo" | |
289 | version = "0.0.1" | |
290 | authors = [] | |
291 | ||
292 | [lib] | |
293 | plugin = true | |
294 | "#, | |
fecb7246 AC |
295 | ) |
296 | .file("src/lib.rs", "") | |
d43ee1dd | 297 | .build(); |
e2c0a9d8 | 298 | |
85984a87 DW |
299 | let bar = project() |
300 | .at("bar") | |
1e682848 AC |
301 | .file( |
302 | "Cargo.toml", | |
303 | r#" | |
6f8c7d5a EH |
304 | [package] |
305 | name = "bar" | |
306 | version = "0.0.1" | |
307 | authors = [] | |
308 | ||
309 | [dependencies.foo] | |
310 | path = "../foo" | |
311 | "#, | |
fecb7246 AC |
312 | ) |
313 | .file("src/lib.rs", "") | |
1e682848 AC |
314 | .file( |
315 | ".cargo/config", | |
316 | &format!( | |
317 | r#" | |
6f8c7d5a EH |
318 | [target.{}] |
319 | linker = "nonexistent-linker" | |
320 | "#, | |
1e682848 AC |
321 | target |
322 | ), | |
fecb7246 AC |
323 | ) |
324 | .build(); | |
e2c0a9d8 | 325 | |
85984a87 DW |
326 | bar.cargo("build --verbose") |
327 | .with_status(101) | |
328 | .with_stderr_contains( | |
1e682848 | 329 | "\ |
29cdad4f | 330 | [COMPILING] foo v0.0.1 ([..]) |
35b924db | 331 | [RUNNING] `rustc [..] -C linker=nonexistent-linker [..]` |
0e087768 | 332 | [ERROR] [..]linker[..] |
1e682848 | 333 | ", |
fecb7246 AC |
334 | ) |
335 | .run(); | |
6950bbb0 | 336 | } |
e9c6b0a0 | 337 | |
0e0d9688 | 338 | #[cargo_test] |
e9c6b0a0 AC |
339 | fn panic_abort_plugins() { |
340 | if !is_nightly() { | |
50277e88 | 341 | // requires rustc_private |
1e682848 | 342 | return; |
e9c6b0a0 AC |
343 | } |
344 | ||
f8c9928c | 345 | let p = project() |
1e682848 AC |
346 | .file( |
347 | "Cargo.toml", | |
348 | r#" | |
6f8c7d5a EH |
349 | [package] |
350 | name = "foo" | |
351 | version = "0.0.1" | |
352 | authors = [] | |
e9c6b0a0 | 353 | |
6f8c7d5a EH |
354 | [profile.dev] |
355 | panic = 'abort' | |
e9c6b0a0 | 356 | |
6f8c7d5a EH |
357 | [dependencies] |
358 | bar = { path = "bar" } | |
359 | "#, | |
fecb7246 AC |
360 | ) |
361 | .file("src/lib.rs", "") | |
1e682848 | 362 | .file( |
f8c9928c | 363 | "bar/Cargo.toml", |
1e682848 | 364 | r#" |
6f8c7d5a EH |
365 | [package] |
366 | name = "bar" | |
367 | version = "0.0.1" | |
368 | authors = [] | |
369 | ||
370 | [lib] | |
371 | plugin = true | |
372 | "#, | |
fecb7246 AC |
373 | ) |
374 | .file( | |
f8c9928c | 375 | "bar/src/lib.rs", |
1e682848 | 376 | r#" |
6f8c7d5a EH |
377 | #![feature(rustc_private)] |
378 | extern crate rustc_ast; | |
379 | "#, | |
fecb7246 AC |
380 | ) |
381 | .build(); | |
e9c6b0a0 | 382 | |
85984a87 | 383 | p.cargo("build").run(); |
e9c6b0a0 AC |
384 | } |
385 | ||
0e0d9688 | 386 | #[cargo_test] |
e9c6b0a0 AC |
387 | fn shared_panic_abort_plugins() { |
388 | if !is_nightly() { | |
50277e88 | 389 | // requires rustc_private |
1e682848 | 390 | return; |
e9c6b0a0 AC |
391 | } |
392 | ||
f8c9928c | 393 | let p = project() |
1e682848 AC |
394 | .file( |
395 | "Cargo.toml", | |
396 | r#" | |
6f8c7d5a EH |
397 | [package] |
398 | name = "foo" | |
399 | version = "0.0.1" | |
400 | authors = [] | |
401 | ||
402 | [profile.dev] | |
403 | panic = 'abort' | |
404 | ||
405 | [dependencies] | |
406 | bar = { path = "bar" } | |
407 | baz = { path = "baz" } | |
408 | "#, | |
fecb7246 AC |
409 | ) |
410 | .file("src/lib.rs", "extern crate baz;") | |
1e682848 | 411 | .file( |
f8c9928c | 412 | "bar/Cargo.toml", |
1e682848 | 413 | r#" |
6f8c7d5a EH |
414 | [package] |
415 | name = "bar" | |
416 | version = "0.0.1" | |
417 | authors = [] | |
e9c6b0a0 | 418 | |
6f8c7d5a EH |
419 | [lib] |
420 | plugin = true | |
e9c6b0a0 | 421 | |
6f8c7d5a EH |
422 | [dependencies] |
423 | baz = { path = "../baz" } | |
424 | "#, | |
fecb7246 AC |
425 | ) |
426 | .file( | |
f8c9928c | 427 | "bar/src/lib.rs", |
1e682848 | 428 | r#" |
6f8c7d5a EH |
429 | #![feature(rustc_private)] |
430 | extern crate rustc_ast; | |
431 | extern crate baz; | |
432 | "#, | |
fecb7246 AC |
433 | ) |
434 | .file("baz/Cargo.toml", &basic_manifest("baz", "0.0.1")) | |
f8c9928c | 435 | .file("baz/src/lib.rs", "") |
d43ee1dd | 436 | .build(); |
e9c6b0a0 | 437 | |
3fd28143 | 438 | p.cargo("build -v").run(); |
e9c6b0a0 | 439 | } |