]> git.lizzy.rs Git - rust.git/blob - src/libcompiler_builtins/build.rs
libcompiler_builtins: Don't build emutls.c
[rust.git] / src / libcompiler_builtins / build.rs
1 // Copyright 2016 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 //! Compiles the `compiler-rt` library, or at least the builtins part of it.
12 //!
13 //! Note that while compiler-rt has a build system associated with it, we
14 //! specifically don't use it here. The compiler-rt build system, written in
15 //! CMake, is actually *very* difficult to work with in terms of getting it to
16 //! compile on all the relevant platforms we want it to compile on. In the end
17 //! it became so much pain to work with local patches, work around the oddities
18 //! of the build system, etc, that we're just building everything by hand now.
19 //!
20 //! In general compiler-rt is just a bunch of intrinsics that are in practice
21 //! *very* stable. We just need to make sure that all the relevant functions and
22 //! such are compiled somewhere and placed in an object file somewhere.
23 //! Eventually, these should all be written in Rust!
24 //!
25 //! So below you'll find a listing of every single file in the compiler-rt repo
26 //! that we're compiling. We just reach in and compile with the `gcc` crate
27 //! which should have all the relevant flags and such already configured.
28 //!
29 //! The risk here is that if we update compiler-rt we may need to compile some
30 //! new intrinsics, but to be honest we surely don't use all of the intrinsics
31 //! listed below today so the likelihood of us actually needing a new intrinsic
32 //! is quite low. The failure case is also just that someone reports a link
33 //! error (if any) and then we just add it to the list. Overall, that cost is
34 //! far far less than working with compiler-rt's build system over time.
35
36 extern crate gcc;
37
38 use std::collections::BTreeMap;
39 use std::env;
40 use std::path::Path;
41
42 struct Sources {
43     // SYMBOL -> PATH TO SOURCE
44     map: BTreeMap<&'static str, &'static str>,
45 }
46
47 impl Sources {
48     fn new() -> Sources {
49         Sources { map: BTreeMap::new() }
50     }
51
52     fn extend(&mut self, sources: &[&'static str]) {
53         // NOTE Some intrinsics have both a generic implementation (e.g.
54         // `floatdidf.c`) and an arch optimized implementation
55         // (`x86_64/floatdidf.c`). In those cases, we keep the arch optimized
56         // implementation and discard the generic implementation. If we don't
57         // and keep both implementations, the linker will yell at us about
58         // duplicate symbols!
59         for &src in sources {
60             let symbol = Path::new(src).file_stem().unwrap().to_str().unwrap();
61             if src.contains("/") {
62                 // Arch-optimized implementation (preferred)
63                 self.map.insert(symbol, src);
64             } else {
65                 // Generic implementation
66                 if !self.map.contains_key(symbol) {
67                     self.map.insert(symbol, src);
68                 }
69             }
70         }
71     }
72 }
73
74 fn main() {
75     let target = env::var("TARGET").expect("TARGET was not set");
76
77     // Emscripten's runtime includes all the builtins
78     if target.contains("emscripten") {
79         return;
80     }
81
82     let cfg = &mut gcc::Config::new();
83
84     if target.contains("msvc") {
85         // Don't pull in extra libraries on MSVC
86         cfg.flag("/Zl");
87
88         // Emulate C99 and C++11's __func__ for MSVC prior to 2013 CTP
89         cfg.define("__func__", Some("__FUNCTION__"));
90     } else {
91         // Turn off various features of gcc and such, mostly copying
92         // compiler-rt's build system already
93         cfg.flag("-fno-builtin");
94         cfg.flag("-fvisibility=hidden");
95         cfg.flag("-fomit-frame-pointer");
96         cfg.flag("-ffreestanding");
97         cfg.define("VISIBILITY_HIDDEN", None);
98     }
99
100     let mut sources = Sources::new();
101     sources.extend(&["absvdi2.c",
102                      "absvsi2.c",
103                      "adddf3.c",
104                      "addsf3.c",
105                      "addvdi3.c",
106                      "addvsi3.c",
107                      "apple_versioning.c",
108                      "ashldi3.c",
109                      "ashrdi3.c",
110                      "clear_cache.c",
111                      "clzdi2.c",
112                      "clzsi2.c",
113                      "cmpdi2.c",
114                      "comparedf2.c",
115                      "comparesf2.c",
116                      "ctzdi2.c",
117                      "ctzsi2.c",
118                      "divdc3.c",
119                      "divdf3.c",
120                      "divdi3.c",
121                      "divmoddi4.c",
122                      "divmodsi4.c",
123                      "divsc3.c",
124                      "divsf3.c",
125                      "divsi3.c",
126                      "divxc3.c",
127                      "extendsfdf2.c",
128                      "extendhfsf2.c",
129                      "ffsdi2.c",
130                      "fixdfdi.c",
131                      "fixdfsi.c",
132                      "fixsfdi.c",
133                      "fixsfsi.c",
134                      "fixunsdfdi.c",
135                      "fixunsdfsi.c",
136                      "fixunssfdi.c",
137                      "fixunssfsi.c",
138                      "fixunsxfdi.c",
139                      "fixunsxfsi.c",
140                      "fixxfdi.c",
141                      "floatdidf.c",
142                      "floatdisf.c",
143                      "floatdixf.c",
144                      "floatsidf.c",
145                      "floatsisf.c",
146                      "floatundidf.c",
147                      "floatundisf.c",
148                      "floatundixf.c",
149                      "floatunsidf.c",
150                      "floatunsisf.c",
151                      "int_util.c",
152                      "lshrdi3.c",
153                      "moddi3.c",
154                      "modsi3.c",
155                      "muldc3.c",
156                      "muldf3.c",
157                      "muldi3.c",
158                      "mulodi4.c",
159                      "mulosi4.c",
160                      "muloti4.c",
161                      "mulsc3.c",
162                      "mulsf3.c",
163                      "mulvdi3.c",
164                      "mulvsi3.c",
165                      "mulxc3.c",
166                      "negdf2.c",
167                      "negdi2.c",
168                      "negsf2.c",
169                      "negvdi2.c",
170                      "negvsi2.c",
171                      "paritydi2.c",
172                      "paritysi2.c",
173                      "popcountdi2.c",
174                      "popcountsi2.c",
175                      "powidf2.c",
176                      "powisf2.c",
177                      "powixf2.c",
178                      "subdf3.c",
179                      "subsf3.c",
180                      "subvdi3.c",
181                      "subvsi3.c",
182                      "truncdfhf2.c",
183                      "truncdfsf2.c",
184                      "truncsfhf2.c",
185                      "ucmpdi2.c",
186                      "udivdi3.c",
187                      "udivmoddi4.c",
188                      "udivmodsi4.c",
189                      "udivsi3.c",
190                      "umoddi3.c",
191                      "umodsi3.c"]);
192
193     if !target.contains("ios") {
194         sources.extend(&["absvti2.c",
195                          "addtf3.c",
196                          "addvti3.c",
197                          "ashlti3.c",
198                          "ashrti3.c",
199                          "clzti2.c",
200                          "cmpti2.c",
201                          "ctzti2.c",
202                          "divtf3.c",
203                          "divti3.c",
204                          "ffsti2.c",
205                          "fixdfti.c",
206                          "fixsfti.c",
207                          "fixunsdfti.c",
208                          "fixunssfti.c",
209                          "fixunsxfti.c",
210                          "fixxfti.c",
211                          "floattidf.c",
212                          "floattisf.c",
213                          "floattixf.c",
214                          "floatuntidf.c",
215                          "floatuntisf.c",
216                          "floatuntixf.c",
217                          "lshrti3.c",
218                          "modti3.c",
219                          "multf3.c",
220                          "multi3.c",
221                          "mulvti3.c",
222                          "negti2.c",
223                          "negvti2.c",
224                          "parityti2.c",
225                          "popcountti2.c",
226                          "powitf2.c",
227                          "subtf3.c",
228                          "subvti3.c",
229                          "trampoline_setup.c",
230                          "ucmpti2.c",
231                          "udivmodti4.c",
232                          "udivti3.c",
233                          "umodti3.c"]);
234     }
235
236     if target.contains("apple") {
237         sources.extend(&["atomic_flag_clear.c",
238                          "atomic_flag_clear_explicit.c",
239                          "atomic_flag_test_and_set.c",
240                          "atomic_flag_test_and_set_explicit.c",
241                          "atomic_signal_fence.c",
242                          "atomic_thread_fence.c"]);
243     }
244
245     if target.contains("msvc") {
246         if target.contains("x86_64") {
247             sources.extend(&["x86_64/floatdidf.c", "x86_64/floatdisf.c", "x86_64/floatdixf.c"]);
248         }
249     } else {
250         if !target.contains("freebsd") {
251             sources.extend(&["gcc_personality_v0.c"]);
252         }
253
254         if target.contains("x86_64") {
255             sources.extend(&["x86_64/chkstk.S",
256                              "x86_64/chkstk2.S",
257                              "x86_64/floatdidf.c",
258                              "x86_64/floatdisf.c",
259                              "x86_64/floatdixf.c",
260                              "x86_64/floatundidf.S",
261                              "x86_64/floatundisf.S",
262                              "x86_64/floatundixf.S"]);
263         }
264
265         if target.contains("i386") || target.contains("i586") || target.contains("i686") {
266             sources.extend(&["i386/ashldi3.S",
267                              "i386/ashrdi3.S",
268                              "i386/chkstk.S",
269                              "i386/chkstk2.S",
270                              "i386/divdi3.S",
271                              "i386/floatdidf.S",
272                              "i386/floatdisf.S",
273                              "i386/floatdixf.S",
274                              "i386/floatundidf.S",
275                              "i386/floatundisf.S",
276                              "i386/floatundixf.S",
277                              "i386/lshrdi3.S",
278                              "i386/moddi3.S",
279                              "i386/muldi3.S",
280                              "i386/udivdi3.S",
281                              "i386/umoddi3.S"]);
282         }
283     }
284
285     if target.contains("arm") && !target.contains("ios") {
286         sources.extend(&["arm/aeabi_cdcmp.S",
287                          "arm/aeabi_cdcmpeq_check_nan.c",
288                          "arm/aeabi_cfcmp.S",
289                          "arm/aeabi_cfcmpeq_check_nan.c",
290                          "arm/aeabi_dcmp.S",
291                          "arm/aeabi_div0.c",
292                          "arm/aeabi_drsub.c",
293                          "arm/aeabi_fcmp.S",
294                          "arm/aeabi_frsub.c",
295                          "arm/aeabi_idivmod.S",
296                          "arm/aeabi_ldivmod.S",
297                          "arm/aeabi_memcmp.S",
298                          "arm/aeabi_memcpy.S",
299                          "arm/aeabi_memmove.S",
300                          "arm/aeabi_memset.S",
301                          "arm/aeabi_uidivmod.S",
302                          "arm/aeabi_uldivmod.S",
303                          "arm/bswapdi2.S",
304                          "arm/bswapsi2.S",
305                          "arm/clzdi2.S",
306                          "arm/clzsi2.S",
307                          "arm/comparesf2.S",
308                          "arm/divmodsi4.S",
309                          "arm/divsi3.S",
310                          "arm/modsi3.S",
311                          "arm/switch16.S",
312                          "arm/switch32.S",
313                          "arm/switch8.S",
314                          "arm/switchu8.S",
315                          "arm/sync_synchronize.S",
316                          "arm/udivmodsi4.S",
317                          "arm/udivsi3.S",
318                          "arm/umodsi3.S"]);
319     }
320
321     if target.contains("armv7") {
322         sources.extend(&["arm/sync_fetch_and_add_4.S",
323                          "arm/sync_fetch_and_add_8.S",
324                          "arm/sync_fetch_and_and_4.S",
325                          "arm/sync_fetch_and_and_8.S",
326                          "arm/sync_fetch_and_max_4.S",
327                          "arm/sync_fetch_and_max_8.S",
328                          "arm/sync_fetch_and_min_4.S",
329                          "arm/sync_fetch_and_min_8.S",
330                          "arm/sync_fetch_and_nand_4.S",
331                          "arm/sync_fetch_and_nand_8.S",
332                          "arm/sync_fetch_and_or_4.S",
333                          "arm/sync_fetch_and_or_8.S",
334                          "arm/sync_fetch_and_sub_4.S",
335                          "arm/sync_fetch_and_sub_8.S",
336                          "arm/sync_fetch_and_umax_4.S",
337                          "arm/sync_fetch_and_umax_8.S",
338                          "arm/sync_fetch_and_umin_4.S",
339                          "arm/sync_fetch_and_umin_8.S",
340                          "arm/sync_fetch_and_xor_4.S",
341                          "arm/sync_fetch_and_xor_8.S"]);
342     }
343
344     if target.contains("eabihf") {
345         sources.extend(&["arm/adddf3vfp.S",
346                          "arm/addsf3vfp.S",
347                          "arm/divdf3vfp.S",
348                          "arm/divsf3vfp.S",
349                          "arm/eqdf2vfp.S",
350                          "arm/eqsf2vfp.S",
351                          "arm/extendsfdf2vfp.S",
352                          "arm/fixdfsivfp.S",
353                          "arm/fixsfsivfp.S",
354                          "arm/fixunsdfsivfp.S",
355                          "arm/fixunssfsivfp.S",
356                          "arm/floatsidfvfp.S",
357                          "arm/floatsisfvfp.S",
358                          "arm/floatunssidfvfp.S",
359                          "arm/floatunssisfvfp.S",
360                          "arm/gedf2vfp.S",
361                          "arm/gesf2vfp.S",
362                          "arm/gtdf2vfp.S",
363                          "arm/gtsf2vfp.S",
364                          "arm/ledf2vfp.S",
365                          "arm/lesf2vfp.S",
366                          "arm/ltdf2vfp.S",
367                          "arm/ltsf2vfp.S",
368                          "arm/muldf3vfp.S",
369                          "arm/mulsf3vfp.S",
370                          "arm/negdf2vfp.S",
371                          "arm/negsf2vfp.S",
372                          "arm/nedf2vfp.S",
373                          "arm/nesf2vfp.S",
374                          "arm/restore_vfp_d8_d15_regs.S",
375                          "arm/save_vfp_d8_d15_regs.S",
376                          "arm/subdf3vfp.S",
377                          "arm/subsf3vfp.S",
378                          "arm/truncdfsf2vfp.S",
379                          "arm/unorddf2vfp.S",
380                          "arm/unordsf2vfp.S"]);
381     }
382
383     if target.contains("aarch64") {
384         sources.extend(&["comparetf2.c",
385                          "extenddftf2.c",
386                          "extendsftf2.c",
387                          "fixtfdi.c",
388                          "fixtfsi.c",
389                          "fixtfti.c",
390                          "fixunstfdi.c",
391                          "fixunstfsi.c",
392                          "fixunstfti.c",
393                          "floatditf.c",
394                          "floatsitf.c",
395                          "floatunditf.c",
396                          "floatunsitf.c",
397                          "multc3.c",
398                          "trunctfdf2.c",
399                          "trunctfsf2.c"]);
400     }
401
402     for src in sources.map.values() {
403         cfg.file(Path::new("../compiler-rt/lib/builtins").join(src));
404     }
405
406     cfg.compile("libcompiler-rt.a");
407 }