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