1 extern crate cc;
2 extern crate tempdir;
3 
4 use std::env;
5 use support::Test;
6 
7 mod support;
8 
9 #[test]
gnu_smoke()10 fn gnu_smoke() {
11     let test = Test::gnu();
12     test.gcc().file("foo.c").compile("foo");
13 
14     test.cmd(0)
15         .must_have("-O2")
16         .must_have("foo.c")
17         .must_not_have("-g")
18         .must_have("-c")
19         .must_have("-ffunction-sections")
20         .must_have("-fdata-sections");
21     test.cmd(1).must_have(test.td.path().join("foo.o"));
22 }
23 
24 #[test]
gnu_opt_level_1()25 fn gnu_opt_level_1() {
26     let test = Test::gnu();
27     test.gcc().opt_level(1).file("foo.c").compile("foo");
28 
29     test.cmd(0).must_have("-O1").must_not_have("-O2");
30 }
31 
32 #[test]
gnu_opt_level_s()33 fn gnu_opt_level_s() {
34     let test = Test::gnu();
35     test.gcc().opt_level_str("s").file("foo.c").compile("foo");
36 
37     test.cmd(0)
38         .must_have("-Os")
39         .must_not_have("-O1")
40         .must_not_have("-O2")
41         .must_not_have("-O3")
42         .must_not_have("-Oz");
43 }
44 
45 #[test]
gnu_debug()46 fn gnu_debug() {
47     let test = Test::gnu();
48     test.gcc().debug(true).file("foo.c").compile("foo");
49     test.cmd(0).must_have("-g");
50 }
51 
52 #[test]
gnu_warnings_into_errors()53 fn gnu_warnings_into_errors() {
54     let test = Test::gnu();
55     test.gcc()
56         .warnings_into_errors(true)
57         .file("foo.c")
58         .compile("foo");
59 
60     test.cmd(0).must_have("-Werror");
61 }
62 
63 #[test]
gnu_warnings()64 fn gnu_warnings() {
65     let test = Test::gnu();
66     test.gcc()
67         .warnings(true)
68         .flag("-Wno-missing-field-initializers")
69         .file("foo.c")
70         .compile("foo");
71 
72     test.cmd(0).must_have("-Wall").must_have("-Wextra");
73 }
74 
75 #[test]
gnu_extra_warnings0()76 fn gnu_extra_warnings0() {
77     let test = Test::gnu();
78     test.gcc()
79         .warnings(true)
80         .extra_warnings(false)
81         .flag("-Wno-missing-field-initializers")
82         .file("foo.c")
83         .compile("foo");
84 
85     test.cmd(0).must_have("-Wall").must_not_have("-Wextra");
86 }
87 
88 #[test]
gnu_extra_warnings1()89 fn gnu_extra_warnings1() {
90     let test = Test::gnu();
91     test.gcc()
92         .warnings(false)
93         .extra_warnings(true)
94         .flag("-Wno-missing-field-initializers")
95         .file("foo.c")
96         .compile("foo");
97 
98     test.cmd(0).must_not_have("-Wall").must_have("-Wextra");
99 }
100 
101 #[test]
gnu_warnings_overridable()102 fn gnu_warnings_overridable() {
103     let test = Test::gnu();
104     test.gcc()
105         .warnings(true)
106         .flag("-Wno-missing-field-initializers")
107         .file("foo.c")
108         .compile("foo");
109 
110     test.cmd(0)
111         .must_have_in_order("-Wall", "-Wno-missing-field-initializers");
112 }
113 
114 #[test]
gnu_no_warnings_if_cflags()115 fn gnu_no_warnings_if_cflags() {
116     env::set_var("CFLAGS", "-Wflag-does-not-exist");
117     let test = Test::gnu();
118     test.gcc().file("foo.c").compile("foo");
119 
120     test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
121     env::set_var("CFLAGS", "");
122 }
123 
124 #[test]
gnu_no_warnings_if_cxxflags()125 fn gnu_no_warnings_if_cxxflags() {
126     env::set_var("CXXFLAGS", "-Wflag-does-not-exist");
127     let test = Test::gnu();
128     test.gcc().file("foo.c").compile("foo");
129 
130     test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
131     env::set_var("CXXFLAGS", "");
132 }
133 
134 #[test]
gnu_x86_64()135 fn gnu_x86_64() {
136     for vendor in &["unknown-linux-gnu", "apple-darwin"] {
137         let target = format!("x86_64-{}", vendor);
138         let test = Test::gnu();
139         test.gcc()
140             .target(&target)
141             .host(&target)
142             .file("foo.c")
143             .compile("foo");
144 
145         test.cmd(0).must_have("-fPIC").must_have("-m64");
146     }
147 }
148 
149 #[test]
gnu_x86_64_no_pic()150 fn gnu_x86_64_no_pic() {
151     for vendor in &["unknown-linux-gnu", "apple-darwin"] {
152         let target = format!("x86_64-{}", vendor);
153         let test = Test::gnu();
154         test.gcc()
155             .pic(false)
156             .target(&target)
157             .host(&target)
158             .file("foo.c")
159             .compile("foo");
160 
161         test.cmd(0).must_not_have("-fPIC");
162     }
163 }
164 
165 #[test]
gnu_i686()166 fn gnu_i686() {
167     for vendor in &["unknown-linux-gnu", "apple-darwin"] {
168         let target = format!("i686-{}", vendor);
169         let test = Test::gnu();
170         test.gcc()
171             .target(&target)
172             .host(&target)
173             .file("foo.c")
174             .compile("foo");
175 
176         test.cmd(0).must_have("-m32");
177     }
178 }
179 
180 #[test]
gnu_i686_pic()181 fn gnu_i686_pic() {
182     for vendor in &["unknown-linux-gnu", "apple-darwin"] {
183         let target = format!("i686-{}", vendor);
184         let test = Test::gnu();
185         test.gcc()
186             .pic(true)
187             .target(&target)
188             .host(&target)
189             .file("foo.c")
190             .compile("foo");
191 
192         test.cmd(0).must_have("-fPIC");
193     }
194 }
195 
196 #[test]
gnu_x86_64_no_plt()197 fn gnu_x86_64_no_plt() {
198     let target = "x86_64-unknown-linux-gnu";
199     let test = Test::gnu();
200     test.gcc()
201         .pic(true)
202         .use_plt(false)
203         .target(&target)
204         .host(&target)
205         .file("foo.c")
206         .compile("foo");
207     test.cmd(0).must_have("-fno-plt");
208 }
209 
210 #[test]
gnu_set_stdlib()211 fn gnu_set_stdlib() {
212     let test = Test::gnu();
213     test.gcc()
214         .cpp_set_stdlib(Some("foo"))
215         .file("foo.c")
216         .compile("foo");
217 
218     test.cmd(0).must_not_have("-stdlib=foo");
219 }
220 
221 #[test]
gnu_include()222 fn gnu_include() {
223     let test = Test::gnu();
224     test.gcc().include("foo/bar").file("foo.c").compile("foo");
225 
226     test.cmd(0).must_have("-I").must_have("foo/bar");
227 }
228 
229 #[test]
gnu_define()230 fn gnu_define() {
231     let test = Test::gnu();
232     test.gcc()
233         .define("FOO", "bar")
234         .define("BAR", None)
235         .file("foo.c")
236         .compile("foo");
237 
238     test.cmd(0).must_have("-DFOO=bar").must_have("-DBAR");
239 }
240 
241 #[test]
gnu_compile_assembly()242 fn gnu_compile_assembly() {
243     let test = Test::gnu();
244     test.gcc().file("foo.S").compile("foo");
245     test.cmd(0).must_have("foo.S");
246 }
247 
248 #[test]
gnu_shared()249 fn gnu_shared() {
250     let test = Test::gnu();
251     test.gcc()
252         .file("foo.c")
253         .shared_flag(true)
254         .static_flag(false)
255         .compile("foo");
256 
257     test.cmd(0).must_have("-shared").must_not_have("-static");
258 }
259 
260 #[test]
gnu_flag_if_supported()261 fn gnu_flag_if_supported() {
262     if cfg!(windows) {
263         return;
264     }
265     let test = Test::gnu();
266     test.gcc()
267         .file("foo.c")
268         .flag("-v")
269         .flag_if_supported("-Wall")
270         .flag_if_supported("-Wflag-does-not-exist")
271         .flag_if_supported("-std=c++11")
272         .compile("foo");
273 
274     test.cmd(0)
275         .must_have("-v")
276         .must_have("-Wall")
277         .must_not_have("-Wflag-does-not-exist")
278         .must_not_have("-std=c++11");
279 }
280 
281 #[test]
gnu_flag_if_supported_cpp()282 fn gnu_flag_if_supported_cpp() {
283     if cfg!(windows) {
284         return;
285     }
286     let test = Test::gnu();
287     test.gcc()
288         .cpp(true)
289         .file("foo.cpp")
290         .flag_if_supported("-std=c++11")
291         .compile("foo");
292 
293     test.cmd(0).must_have("-std=c++11");
294 }
295 
296 #[test]
gnu_static()297 fn gnu_static() {
298     let test = Test::gnu();
299     test.gcc()
300         .file("foo.c")
301         .shared_flag(false)
302         .static_flag(true)
303         .compile("foo");
304 
305     test.cmd(0).must_have("-static").must_not_have("-shared");
306 }
307 
308 #[test]
msvc_smoke()309 fn msvc_smoke() {
310     let test = Test::msvc();
311     test.gcc().file("foo.c").compile("foo");
312 
313     test.cmd(0)
314         .must_have("/O2")
315         .must_have("foo.c")
316         .must_not_have("/Z7")
317         .must_have("/c")
318         .must_have("/MD");
319     test.cmd(1).must_have(test.td.path().join("foo.o"));
320 }
321 
322 #[test]
msvc_opt_level_0()323 fn msvc_opt_level_0() {
324     let test = Test::msvc();
325     test.gcc().opt_level(0).file("foo.c").compile("foo");
326 
327     test.cmd(0).must_not_have("/O2");
328 }
329 
330 #[test]
msvc_debug()331 fn msvc_debug() {
332     let test = Test::msvc();
333     test.gcc().debug(true).file("foo.c").compile("foo");
334     test.cmd(0).must_have("/Z7");
335 }
336 
337 #[test]
msvc_include()338 fn msvc_include() {
339     let test = Test::msvc();
340     test.gcc().include("foo/bar").file("foo.c").compile("foo");
341 
342     test.cmd(0).must_have("/I").must_have("foo/bar");
343 }
344 
345 #[test]
msvc_define()346 fn msvc_define() {
347     let test = Test::msvc();
348     test.gcc()
349         .define("FOO", "bar")
350         .define("BAR", None)
351         .file("foo.c")
352         .compile("foo");
353 
354     test.cmd(0).must_have("/DFOO=bar").must_have("/DBAR");
355 }
356 
357 #[test]
msvc_static_crt()358 fn msvc_static_crt() {
359     let test = Test::msvc();
360     test.gcc().static_crt(true).file("foo.c").compile("foo");
361 
362     test.cmd(0).must_have("/MT");
363 }
364 
365 #[test]
msvc_no_static_crt()366 fn msvc_no_static_crt() {
367     let test = Test::msvc();
368     test.gcc().static_crt(false).file("foo.c").compile("foo");
369 
370     test.cmd(0).must_have("/MD");
371 }
372