]> git.proxmox.com Git - cargo.git/blob - tests/test_cargo_compile_custom_build.rs
Implement overrides via local cargo configuration
[cargo.git] / tests / test_cargo_compile_custom_build.rs
1 use support::{project, execs};
2 use support::{COMPILING, RUNNING};
3 use hamcrest::{assert_that};
4
5 fn setup() {
6 }
7
8 test!(custom_build_script_failed {
9 let p = project("foo")
10 .file("Cargo.toml", r#"
11 [project]
12
13 name = "foo"
14 version = "0.5.0"
15 authors = ["wycats@example.com"]
16 build = "build.rs"
17 "#)
18 .file("src/main.rs", r#"
19 fn main() {}
20 "#)
21 .file("build.rs", r#"
22 fn main() {
23 std::os::set_exit_status(101);
24 }
25 "#);
26 assert_that(p.cargo_process("build").arg("-v"),
27 execs().with_status(101)
28 .with_stdout(format!("\
29 {compiling} foo v0.5.0 ({url})
30 {running} `rustc build.rs --crate-name build-script-build --crate-type bin [..]`
31 ",
32 url = p.url(), compiling = COMPILING, running = RUNNING))
33 .with_stderr(format!("\
34 Failed to run custom build command for `foo v0.5.0 ({})`
35 Process didn't exit successfully: `[..]build[..]build-script-build` (status=101)",
36 p.url())));
37 })
38
39 test!(custom_build_env_vars {
40 let p = project("foo")
41 .file("Cargo.toml", r#"
42 [project]
43
44 name = "foo"
45 version = "0.5.0"
46 authors = ["wycats@example.com"]
47
48 [features]
49 bar_feat = ["bar/foo"]
50
51 [dependencies.bar]
52 path = "bar"
53 "#)
54 .file("src/main.rs", r#"
55 fn main() {}
56 "#)
57 .file("bar/Cargo.toml", r#"
58 [project]
59
60 name = "bar"
61 version = "0.5.0"
62 authors = ["wycats@example.com"]
63 build = "build.rs"
64
65 [features]
66 foo = []
67 "#)
68 .file("bar/src/lib.rs", r#"
69 pub fn hello() {}
70 "#);
71
72 let file_content = format!(r#"
73 use std::os;
74 use std::io::fs::PathExtensions;
75 fn main() {{
76 let _target = os::getenv("TARGET").unwrap();
77
78 let _ncpus = os::getenv("NUM_JOBS").unwrap();
79
80 let out = os::getenv("CARGO_MANIFEST_DIR").unwrap();
81 let p1 = Path::new(out);
82 let p2 = os::make_absolute(&Path::new(file!()).dir_path().dir_path());
83 assert!(p1 == p2, "{{}} != {{}}", p1.display(), p2.display());
84
85 let opt = os::getenv("OPT_LEVEL").unwrap();
86 assert_eq!(opt.as_slice(), "0");
87
88 let opt = os::getenv("PROFILE").unwrap();
89 assert_eq!(opt.as_slice(), "compile");
90
91 let debug = os::getenv("DEBUG").unwrap();
92 assert_eq!(debug.as_slice(), "true");
93
94 let out = os::getenv("OUT_DIR").unwrap();
95 assert!(out.as_slice().starts_with(r"{0}"));
96 assert!(Path::new(out).is_dir());
97
98 let _feat = os::getenv("CARGO_FEATURE_FOO").unwrap();
99 }}
100 "#,
101 p.root().join("target").join("native").display());
102
103 let p = p.file("bar/build.rs", file_content);
104
105
106 assert_that(p.cargo_process("build").arg("--features").arg("bar_feat"),
107 execs().with_status(0));
108 })
109
110 test!(custom_build_script_wrong_rustc_flags {
111 let p = project("foo")
112 .file("Cargo.toml", r#"
113 [project]
114
115 name = "foo"
116 version = "0.5.0"
117 authors = ["wycats@example.com"]
118 build = "build.rs"
119 "#)
120 .file("src/main.rs", r#"
121 fn main() {}
122 "#)
123 .file("build.rs", r#"
124 fn main() {
125 println!("cargo:rustc-flags=-aaa -bbb");
126 }
127 "#);
128
129 assert_that(p.cargo_process("build"),
130 execs().with_status(101)
131 .with_stderr(format!("\
132 Only `-l` and `-L` flags are allowed in build script of `foo v0.5.0 ({})`: \
133 `-aaa -bbb`",
134 p.url())));
135 })
136
137 /*
138 test!(custom_build_script_rustc_flags {
139 let p = project("foo")
140 .file("Cargo.toml", r#"
141 [project]
142
143 name = "bar"
144 version = "0.5.0"
145 authors = ["wycats@example.com"]
146
147 [dependencies.foo]
148 path = "foo"
149 "#)
150 .file("src/main.rs", r#"
151 fn main() {}
152 "#)
153 .file("foo/Cargo.toml", r#"
154 [project]
155
156 name = "foo"
157 version = "0.5.0"
158 authors = ["wycats@example.com"]
159 build = "build.rs"
160 "#)
161 .file("foo/src/lib.rs", r#"
162 "#)
163 .file("foo/build.rs", r#"
164 fn main() {
165 println!("cargo:rustc-flags=-l nonexistinglib -L /dummy/path1 -L /dummy/path2");
166 }
167 "#);
168
169 // TODO: TEST FAILS BECAUSE OF WRONG STDOUT (but otherwise, the build works)
170 assert_that(p.cargo_process("build").arg("--verbose"),
171 execs().with_status(101)
172 .with_stdout(format!("\
173 {compiling} bar v0.5.0 ({url})
174 {running} `rustc {dir}{sep}src{sep}lib.rs --crate-name test --crate-type lib -g \
175 -C metadata=[..] \
176 -C extra-filename=-[..] \
177 --out-dir {dir}{sep}target \
178 --dep-info [..] \
179 -L {dir}{sep}target \
180 -L {dir}{sep}target{sep}deps`
181 ",
182 running = RUNNING, compiling = COMPILING, sep = path::SEP,
183 dir = p.root().display(),
184 url = p.url(),
185 )));
186 })
187 */
188
189 test!(links_no_build_cmd {
190 let p = project("foo")
191 .file("Cargo.toml", r#"
192 [project]
193 name = "foo"
194 version = "0.5.0"
195 authors = []
196 links = "a"
197 "#)
198 .file("src/lib.rs", "");
199
200 assert_that(p.cargo_process("build"),
201 execs().with_status(101)
202 .with_stderr("\
203 package `foo v0.5.0 (file://[..])` specifies that it links to `a` but does \
204 not have a custom build script
205 "));
206 })
207
208 test!(links_duplicates {
209 let p = project("foo")
210 .file("Cargo.toml", r#"
211 [project]
212 name = "foo"
213 version = "0.5.0"
214 authors = []
215 links = "a"
216 build = "build.rs"
217
218 [dependencies.a]
219 path = "a"
220 "#)
221 .file("src/lib.rs", "")
222 .file("build.rs", "")
223 .file("a/Cargo.toml", r#"
224 [project]
225 name = "a"
226 version = "0.5.0"
227 authors = []
228 links = "a"
229 build = "build.rs"
230 "#)
231 .file("a/src/lib.rs", "")
232 .file("a/build.rs", "");
233
234 assert_that(p.cargo_process("build"),
235 execs().with_status(101)
236 .with_stderr("\
237 native library `a` is being linked to by more than one package, and can only be \
238 linked to by one package
239
240 foo v0.5.0 (file://[..])
241 a v0.5.0 (file://[..])
242 "));
243 })
244
245 test!(overrides_and_links {
246 let (_, target) = ::cargo::ops::rustc_version().unwrap();
247
248 let p = project("foo")
249 .file("Cargo.toml", r#"
250 [project]
251 name = "foo"
252 version = "0.5.0"
253 authors = []
254 build = "build.rs"
255
256 [dependencies.a]
257 path = "a"
258 "#)
259 .file("src/lib.rs", "")
260 .file("build.rs", r#"
261 use std::os;
262 fn main() {
263 assert_eq!(os::getenv("DEP_FOO_FOO").unwrap().as_slice(), "bar");
264 assert_eq!(os::getenv("DEP_FOO_BAR").unwrap().as_slice(), "baz");
265 }
266 "#)
267 .file(".cargo/config", format!(r#"
268 [target.{}.foo]
269 rustc-flags = "-l foo -L bar"
270 foo = "bar"
271 bar = "baz"
272 "#, target).as_slice())
273 .file("a/Cargo.toml", r#"
274 [project]
275 name = "a"
276 version = "0.5.0"
277 authors = []
278 links = "foo"
279 build = "build.rs"
280 "#)
281 .file("a/src/lib.rs", "")
282 .file("a/build.rs", "not valid rust code");
283
284 assert_that(p.cargo_process("build").arg("-v"),
285 execs().with_status(0)
286 .with_stdout("\
287 Compiling a v0.5.0 (file://[..])
288 Running `rustc [..] --crate-name a [..]`
289 Compiling foo v0.5.0 (file://[..])
290 Running `rustc build.rs [..]`
291 Running `rustc [..] --crate-name foo [..]`
292 "));
293 })
294