]> git.lizzy.rs Git - rust.git/blob - mk/rt.mk
Rollup merge of #38299 - achanda:ctrl-c, r=brson
[rust.git] / mk / rt.mk
1 # Copyright 2014 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 ################################################################################
12 # Native libraries built as part of the rust build process
13 #
14 # This portion of the rust build system is meant to keep track of native
15 # dependencies and how to build them. It is currently required that all native
16 # dependencies are built as static libraries, as slinging around dynamic
17 # libraries isn't exactly the most fun thing to do.
18 #
19 # This section should need minimal modification to add new libraries. The
20 # relevant variables are:
21 #
22 #   NATIVE_LIBS
23 #       This is a list of all native libraries which are built as part of the
24 #       build process. It will build all libraries into RT_OUTPUT_DIR with the
25 #       appropriate name of static library as dictated by the target platform
26 #
27 #   NATIVE_DEPS_<lib>
28 #       This is a list of files relative to the src/rt directory which are
29 #       needed to build the native library. Each file will be compiled to an
30 #       object file, and then all the object files will be assembled into an
31 #       archive (static library). The list contains files of any extension
32 #
33 # If adding a new library, you should update the NATIVE_LIBS list, and then list
34 # the required files below it. The list of required files is a list of files
35 # that's per-target so you're allowed to conditionally add files based on the
36 # target.
37 ################################################################################
38 NATIVE_LIBS := hoedown miniz rust_test_helpers
39
40 # A macro to add a generic implementation of intrinsics iff a arch optimized implementation is not
41 # already in the list.
42 # $(1) is the target
43 # $(2) is the intrinsic
44 define ADD_INTRINSIC
45   ifeq ($$(findstring X,$$(foreach intrinsic,$$(COMPRT_OBJS_$(1)),$$(if $$(findstring $(2),$$(intrinsic)),X,))),)
46     COMPRT_OBJS_$(1) += $(2)
47   endif
48 endef
49
50 # $(1) is the target triple
51 define NATIVE_LIBRARIES
52
53 NATIVE_DEPS_hoedown_$(1) := hoedown/src/autolink.c \
54                         hoedown/src/buffer.c \
55                         hoedown/src/document.c \
56                         hoedown/src/escape.c \
57                         hoedown/src/html.c \
58                         hoedown/src/html_blocks.c \
59                         hoedown/src/html_smartypants.c \
60                         hoedown/src/stack.c \
61                         hoedown/src/version.c
62 NATIVE_DEPS_miniz_$(1) = miniz.c
63 NATIVE_DEPS_rust_test_helpers_$(1) := rust_test_helpers.c
64
65 ################################################################################
66 # You shouldn't find it that necessary to edit anything below this line.
67 ################################################################################
68
69 # While we're defining the native libraries for each target, we define some
70 # common rules used to build files for various targets.
71
72 RT_OUTPUT_DIR_$(1) := $(1)/rt
73
74 $$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.c $$(MKFILE_DEPS)
75         @mkdir -p $$(@D)
76         @$$(call E, compile: $$@)
77         $$(Q)$$(call CFG_COMPILE_C_$(1), $$@, \
78                 $$(call CFG_CC_INCLUDE_$(1),$$(S)src/rt/hoedown/src) \
79                 $$(call CFG_CC_INCLUDE_$(1),$$(S)src/rt) \
80                  $$(RUNTIME_CFLAGS_$(1))) $$<
81
82 $$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.S $$(MKFILE_DEPS) \
83             $$(LLVM_CONFIG_$$(CFG_BUILD))
84         @mkdir -p $$(@D)
85         @$$(call E, compile: $$@)
86         $$(Q)$$(call CFG_ASSEMBLE_$(1),$$@,$$<)
87
88 # On MSVC targets the compiler's default include path (e.g. where to find system
89 # headers) is specified by the INCLUDE environment variable. This may not be set
90 # so the ./configure script scraped the relevant values and this is the location
91 # that we put them into cl.exe's environment.
92 ifeq ($$(findstring msvc,$(1)),msvc)
93 $$(RT_OUTPUT_DIR_$(1))/%.o: \
94         export INCLUDE := $$(CFG_MSVC_INCLUDE_PATH_$$(HOST_$(1)))
95 $(1)/rustllvm/%.o: \
96         export INCLUDE := $$(CFG_MSVC_INCLUDE_PATH_$$(HOST_$(1)))
97 endif
98 endef
99
100 $(foreach target,$(CFG_TARGET),$(eval $(call NATIVE_LIBRARIES,$(target))))
101
102 # A macro for devining how to build third party libraries listed above (based
103 # on their dependencies).
104 #
105 # $(1) is the target
106 # $(2) is the lib name
107 define THIRD_PARTY_LIB
108
109 OBJS_$(2)_$(1) := $$(NATIVE_DEPS_$(2)_$(1):%=$$(RT_OUTPUT_DIR_$(1))/%)
110 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.c=.o)
111 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.cpp=.o)
112 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.S=.o)
113 NATIVE_$(2)_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),$(2))
114 $$(RT_OUTPUT_DIR_$(1))/$$(NATIVE_$(2)_$(1)): $$(OBJS_$(2)_$(1))
115         @$$(call E, link: $$@)
116         $$(Q)$$(call CFG_CREATE_ARCHIVE_$(1),$$@) $$^
117
118 endef
119
120 $(foreach target,$(CFG_TARGET), \
121  $(eval $(call RUNTIME_RULES,$(target))))
122 $(foreach lib,$(NATIVE_LIBS), \
123  $(foreach target,$(CFG_TARGET), \
124   $(eval $(call THIRD_PARTY_LIB,$(target),$(lib)))))
125
126
127 ################################################################################
128 # Building third-party targets with external build systems
129 #
130 # This location is meant for dependencies which have external build systems. It
131 # is still assumed that the output of each of these steps is a static library
132 # in the correct location.
133 ################################################################################
134
135 define DEF_THIRD_PARTY_TARGETS
136
137 # $(1) is the target triple
138
139 ifeq ($$(CFG_WINDOWSY_$(1)),1)
140   # A bit of history here, this used to be --enable-lazy-lock added in #14006
141   # which was filed with jemalloc in jemalloc/jemalloc#83 which was also
142   # reported to MinGW: http://sourceforge.net/p/mingw-w64/bugs/395/
143   #
144   # When updating jemalloc to 4.0, however, it was found that binaries would
145   # exit with the status code STATUS_RESOURCE_NOT_OWNED indicating that a thread
146   # was unlocking a mutex it never locked. Disabling this "lazy lock" option
147   # seems to fix the issue, but it was enabled by default for MinGW targets in
148   # 13473c7 for jemalloc.
149   #
150   # As a result of all that, force disabling lazy lock on Windows, and after
151   # reading some code it at least *appears* that the initialization of mutexes
152   # is otherwise ok in jemalloc, so shouldn't cause problems hopefully...
153   #
154   # tl;dr: make windows behave like other platforms by disabling lazy locking,
155   #        but requires passing an option due to a historical default with
156   #        jemalloc.
157   JEMALLOC_ARGS_$(1) := --disable-lazy-lock
158 else ifeq ($(OSTYPE_$(1)), apple-ios)
159   JEMALLOC_ARGS_$(1) := --disable-tls
160 else ifeq ($(findstring android, $(OSTYPE_$(1))), android)
161   # We force android to have prefixed symbols because apparently replacement of
162   # the libc allocator doesn't quite work. When this was tested (unprefixed
163   # symbols), it was found that the `realpath` function in libc would allocate
164   # with libc malloc (not jemalloc malloc), and then the standard library would
165   # free with jemalloc free, causing a segfault.
166   #
167   # If the test suite passes, however, without symbol prefixes then we should be
168   # good to go!
169   JEMALLOC_ARGS_$(1) := --disable-tls --with-jemalloc-prefix=je_
170 else ifeq ($(findstring dragonfly, $(OSTYPE_$(1))), dragonfly)
171   JEMALLOC_ARGS_$(1) := --with-jemalloc-prefix=je_
172 endif
173
174 ifdef CFG_ENABLE_DEBUG_JEMALLOC
175   JEMALLOC_ARGS_$(1) += --enable-debug --enable-fill
176 endif
177
178 ################################################################################
179 # jemalloc
180 ################################################################################
181
182 ifdef CFG_ENABLE_FAST_MAKE
183 JEMALLOC_DEPS := $(S)/.gitmodules
184 else
185 JEMALLOC_DEPS := $(wildcard \
186                    $(S)src/jemalloc/* \
187                    $(S)src/jemalloc/*/* \
188                    $(S)src/jemalloc/*/*/* \
189                    $(S)src/jemalloc/*/*/*/*)
190 endif
191
192 # See #17183 for details, this file is touched during the build process so we
193 # don't want to consider it as a dependency.
194 JEMALLOC_DEPS := $(filter-out $(S)src/jemalloc/VERSION,$(JEMALLOC_DEPS))
195
196 JEMALLOC_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc)
197 ifeq ($$(CFG_WINDOWSY_$(1)),1)
198   JEMALLOC_REAL_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc_s)
199 else
200   JEMALLOC_REAL_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc_pic)
201 endif
202 JEMALLOC_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(JEMALLOC_NAME_$(1))
203 JEMALLOC_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/jemalloc
204 JEMALLOC_LOCAL_$(1) := $$(JEMALLOC_BUILD_DIR_$(1))/lib/$$(JEMALLOC_REAL_NAME_$(1))
205
206 $$(JEMALLOC_LOCAL_$(1)): $$(JEMALLOC_DEPS) $$(MKFILE_DEPS)
207         @$$(call E, make: jemalloc)
208         cd "$$(JEMALLOC_BUILD_DIR_$(1))"; "$(S)src/jemalloc/configure" \
209                 $$(JEMALLOC_ARGS_$(1)) $(CFG_JEMALLOC_FLAGS) \
210                 --build=$$(CFG_GNU_TRIPLE_$(CFG_BUILD)) --host=$$(CFG_GNU_TRIPLE_$(1)) \
211                 CC="$$(CC_$(1)) $$(CFG_JEMALLOC_CFLAGS_$(1))" \
212                 AR="$$(AR_$(1))" \
213                 RANLIB="$$(AR_$(1)) s" \
214                 CPPFLAGS="-I $(S)src/rt/" \
215                 EXTRA_CFLAGS="-g1 -ffunction-sections -fdata-sections"
216         $$(Q)$$(MAKE) -C "$$(JEMALLOC_BUILD_DIR_$(1))" build_lib_static
217
218 ifeq ($(1),$$(CFG_BUILD))
219 ifneq ($$(CFG_JEMALLOC_ROOT),)
220 $$(JEMALLOC_LIB_$(1)): $$(CFG_JEMALLOC_ROOT)/libjemalloc_pic.a
221         @$$(call E, copy: jemalloc)
222         $$(Q)cp $$< $$@
223 else
224 $$(JEMALLOC_LIB_$(1)): $$(JEMALLOC_LOCAL_$(1))
225         $$(Q)cp $$< $$@
226 endif
227 else
228 $$(JEMALLOC_LIB_$(1)): $$(JEMALLOC_LOCAL_$(1))
229         $$(Q)cp $$< $$@
230 endif
231
232 ################################################################################
233 # compiler-rt
234 ################################################################################
235
236 # Everything below is a manual compilation of compiler-rt, disregarding its
237 # build system. See comments in `src/bootstrap/native.rs` for more information.
238
239 COMPRT_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),compiler-rt)
240 COMPRT_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(COMPRT_NAME_$(1))
241 COMPRT_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/compiler-rt
242
243 # We must avoid compiling both a generic implementation (e.g. `floatdidf.c) and an arch optimized
244 # implementation (e.g. `x86_64/floatdidf.S) of the same symbol (e.g. `floatdidf) because that causes
245 # linker errors. To avoid that, we first add all the arch optimized implementations and then add the
246 # generic implementations if and only if its arch optimized version is not already in the list. This
247 # last part is handled by the ADD_INTRINSIC macro.
248
249 COMPRT_OBJS_$(1) :=
250
251 ifeq ($$(findstring msvc,$(1)),)
252 ifeq ($$(findstring x86_64,$(1)),x86_64)
253 COMPRT_OBJS_$(1) += \
254       x86_64/chkstk.o \
255       x86_64/chkstk2.o \
256       x86_64/floatdidf.o \
257       x86_64/floatdisf.o \
258       x86_64/floatdixf.o \
259       x86_64/floatundidf.o \
260       x86_64/floatundisf.o \
261       x86_64/floatundixf.o
262 endif
263
264 ifeq ($$(findstring i686,$$(patsubts i%86,i686,$(1))),i686)
265 COMPRT_OBJS_$(1) += \
266       i386/ashldi3.o \
267       i386/ashrdi3.o \
268       i386/chkstk.o \
269       i386/chkstk2.o \
270       i386/divdi3.o \
271       i386/floatdidf.o \
272       i386/floatdisf.o \
273       i386/floatdixf.o \
274       i386/floatundidf.o \
275       i386/floatundisf.o \
276       i386/floatundixf.o \
277       i386/lshrdi3.o \
278       i386/moddi3.o \
279       i386/muldi3.o \
280       i386/udivdi3.o \
281       i386/umoddi3.o
282 endif
283
284 else
285
286 ifeq ($$(findstring x86_64,$(1)),x86_64)
287 COMPRT_OBJS_$(1) += \
288       x86_64/floatdidf.o \
289       x86_64/floatdisf.o \
290       x86_64/floatdixf.o
291 endif
292
293 endif
294
295 # Generic ARM sources, nothing compiles on iOS though
296 ifeq ($$(findstring arm,$(1)),arm)
297 ifeq ($$(findstring ios,$(1)),)
298 COMPRT_OBJS_$(1) += \
299   arm/aeabi_cdcmp.o \
300   arm/aeabi_cdcmpeq_check_nan.o \
301   arm/aeabi_cfcmp.o \
302   arm/aeabi_cfcmpeq_check_nan.o \
303   arm/aeabi_dcmp.o \
304   arm/aeabi_div0.o \
305   arm/aeabi_drsub.o \
306   arm/aeabi_fcmp.o \
307   arm/aeabi_frsub.o \
308   arm/aeabi_idivmod.o \
309   arm/aeabi_ldivmod.o \
310   arm/aeabi_memcmp.o \
311   arm/aeabi_memcpy.o \
312   arm/aeabi_memmove.o \
313   arm/aeabi_memset.o \
314   arm/aeabi_uidivmod.o \
315   arm/aeabi_uldivmod.o \
316   arm/bswapdi2.o \
317   arm/bswapsi2.o \
318   arm/clzdi2.o \
319   arm/clzsi2.o \
320   arm/comparesf2.o \
321   arm/divmodsi4.o \
322   arm/divsi3.o \
323   arm/modsi3.o \
324   arm/switch16.o \
325   arm/switch32.o \
326   arm/switch8.o \
327   arm/switchu8.o \
328   arm/sync_synchronize.o \
329   arm/udivmodsi4.o \
330   arm/udivsi3.o \
331   arm/umodsi3.o
332 endif
333 endif
334
335 # Thumb sources
336 ifeq ($$(findstring armv7,$(1)),armv7)
337 COMPRT_OBJS_$(1) += \
338   arm/sync_fetch_and_add_4.o \
339   arm/sync_fetch_and_add_8.o \
340   arm/sync_fetch_and_and_4.o \
341   arm/sync_fetch_and_and_8.o \
342   arm/sync_fetch_and_max_4.o \
343   arm/sync_fetch_and_max_8.o \
344   arm/sync_fetch_and_min_4.o \
345   arm/sync_fetch_and_min_8.o \
346   arm/sync_fetch_and_nand_4.o \
347   arm/sync_fetch_and_nand_8.o \
348   arm/sync_fetch_and_or_4.o \
349   arm/sync_fetch_and_or_8.o \
350   arm/sync_fetch_and_sub_4.o \
351   arm/sync_fetch_and_sub_8.o \
352   arm/sync_fetch_and_umax_4.o \
353   arm/sync_fetch_and_umax_8.o \
354   arm/sync_fetch_and_umin_4.o \
355   arm/sync_fetch_and_umin_8.o \
356   arm/sync_fetch_and_xor_4.o \
357   arm/sync_fetch_and_xor_8.o
358 endif
359
360 # VFP sources
361 ifeq ($$(findstring eabihf,$(1)),eabihf)
362 COMPRT_OBJS_$(1) += \
363   arm/adddf3vfp.o \
364   arm/addsf3vfp.o \
365   arm/divdf3vfp.o \
366   arm/divsf3vfp.o \
367   arm/eqdf2vfp.o \
368   arm/eqsf2vfp.o \
369   arm/extendsfdf2vfp.o \
370   arm/fixdfsivfp.o \
371   arm/fixsfsivfp.o \
372   arm/fixunsdfsivfp.o \
373   arm/fixunssfsivfp.o \
374   arm/floatsidfvfp.o \
375   arm/floatsisfvfp.o \
376   arm/floatunssidfvfp.o \
377   arm/floatunssisfvfp.o \
378   arm/gedf2vfp.o \
379   arm/gesf2vfp.o \
380   arm/gtdf2vfp.o \
381   arm/gtsf2vfp.o \
382   arm/ledf2vfp.o \
383   arm/lesf2vfp.o \
384   arm/ltdf2vfp.o \
385   arm/ltsf2vfp.o \
386   arm/muldf3vfp.o \
387   arm/mulsf3vfp.o \
388   arm/negdf2vfp.o \
389   arm/negsf2vfp.o \
390   arm/nedf2vfp.o \
391   arm/nesf2vfp.o \
392   arm/restore_vfp_d8_d15_regs.o \
393   arm/save_vfp_d8_d15_regs.o \
394   arm/subdf3vfp.o \
395   arm/subsf3vfp.o \
396   arm/truncdfsf2vfp.o \
397   arm/unorddf2vfp.o \
398   arm/unordsf2vfp.o
399 endif
400
401 $(foreach intrinsic,absvdi2.o \
402   absvsi2.o \
403   adddf3.o \
404   addsf3.o \
405   addvdi3.o \
406   addvsi3.o \
407   apple_versioning.o \
408   ashldi3.o \
409   ashrdi3.o \
410   clear_cache.o \
411   clzdi2.o \
412   clzsi2.o \
413   cmpdi2.o \
414   comparedf2.o \
415   comparesf2.o \
416   ctzdi2.o \
417   ctzsi2.o \
418   divdc3.o \
419   divdf3.o \
420   divdi3.o \
421   divmoddi4.o \
422   divmodsi4.o \
423   divsc3.o \
424   divsf3.o \
425   divsi3.o \
426   divxc3.o \
427   extendsfdf2.o \
428   extendhfsf2.o \
429   ffsdi2.o \
430   fixdfdi.o \
431   fixdfsi.o \
432   fixsfdi.o \
433   fixsfsi.o \
434   fixunsdfdi.o \
435   fixunsdfsi.o \
436   fixunssfdi.o \
437   fixunssfsi.o \
438   fixunsxfdi.o \
439   fixunsxfsi.o \
440   fixxfdi.o \
441   floatdidf.o \
442   floatdisf.o \
443   floatdixf.o \
444   floatsidf.o \
445   floatsisf.o \
446   floatundidf.o \
447   floatundisf.o \
448   floatundixf.o \
449   floatunsidf.o \
450   floatunsisf.o \
451   int_util.o \
452   lshrdi3.o \
453   moddi3.o \
454   modsi3.o \
455   muldc3.o \
456   muldf3.o \
457   muldi3.o \
458   mulodi4.o \
459   mulosi4.o \
460   muloti4.o \
461   mulsc3.o \
462   mulsf3.o \
463   mulvdi3.o \
464   mulvsi3.o \
465   mulxc3.o \
466   negdf2.o \
467   negdi2.o \
468   negsf2.o \
469   negvdi2.o \
470   negvsi2.o \
471   paritydi2.o \
472   paritysi2.o \
473   popcountdi2.o \
474   popcountsi2.o \
475   powidf2.o \
476   powisf2.o \
477   powixf2.o \
478   subdf3.o \
479   subsf3.o \
480   subvdi3.o \
481   subvsi3.o \
482   truncdfhf2.o \
483   truncdfsf2.o \
484   truncsfhf2.o \
485   ucmpdi2.o \
486   udivdi3.o \
487   udivmoddi4.o \
488   udivmodsi4.o \
489   udivsi3.o \
490   umoddi3.o \
491   umodsi3.o,
492   $(call ADD_INTRINSIC,$(1),$(intrinsic)))
493
494 ifeq ($$(findstring ios,$(1)),)
495 $(foreach intrinsic,absvti2.o \
496   addtf3.o \
497   addvti3.o \
498   ashlti3.o \
499   ashrti3.o \
500   clzti2.o \
501   cmpti2.o \
502   ctzti2.o \
503   divtf3.o \
504   divti3.o \
505   ffsti2.o \
506   fixdfti.o \
507   fixsfti.o \
508   fixunsdfti.o \
509   fixunssfti.o \
510   fixunsxfti.o \
511   fixxfti.o \
512   floattidf.o \
513   floattisf.o \
514   floattixf.o \
515   floatuntidf.o \
516   floatuntisf.o \
517   floatuntixf.o \
518   lshrti3.o \
519   modti3.o \
520   multf3.o \
521   multi3.o \
522   mulvti3.o \
523   negti2.o \
524   negvti2.o \
525   parityti2.o \
526   popcountti2.o \
527   powitf2.o \
528   subtf3.o \
529   subvti3.o \
530   trampoline_setup.o \
531   ucmpti2.o \
532   udivmodti4.o \
533   udivti3.o \
534   umodti3.o,
535   $(call ADD_INTRINSIC,$(1),$(intrinsic)))
536 endif
537
538 ifeq ($$(findstring apple,$(1)),apple)
539 $(foreach intrinsic,atomic_flag_clear.o \
540   atomic_flag_clear_explicit.o \
541   atomic_flag_test_and_set.o \
542   atomic_flag_test_and_set_explicit.o \
543   atomic_signal_fence.o \
544   atomic_thread_fence.o,
545   $(call ADD_INTRINSIC,$(1),$(intrinsic)))
546 endif
547
548 ifeq ($$(findstring windows,$(1)),)
549 $(call ADD_INTRINSIC,$(1),emutls.o)
550 endif
551
552 ifeq ($$(findstring msvc,$(1)),)
553
554 ifeq ($$(findstring freebsd,$(1)),)
555 $(call ADD_INTRINSIC,$(1),gcc_personality_v0.o)
556 endif
557 endif
558
559 ifeq ($$(findstring aarch64,$(1)),aarch64)
560 $(foreach intrinsic,comparetf2.o \
561   extenddftf2.o \
562   extendsftf2.o \
563   fixtfdi.o \
564   fixtfsi.o \
565   fixtfti.o \
566   fixunstfdi.o \
567   fixunstfsi.o \
568   fixunstfti.o \
569   floatditf.o \
570   floatsitf.o \
571   floatunditf.o \
572   floatunsitf.o \
573   multc3.o \
574   trunctfdf2.o \
575   trunctfsf2.o,
576   $(call ADD_INTRINSIC,$(1),$(intrinsic)))
577 endif
578
579 ifeq ($$(findstring msvc,$(1)),msvc)
580 $$(COMPRT_BUILD_DIR_$(1))/%.o: CFLAGS += -Zl -D__func__=__FUNCTION__
581 else
582 $$(COMPRT_BUILD_DIR_$(1))/%.o: CFLAGS += -fno-builtin -fvisibility=hidden \
583         -fomit-frame-pointer -ffreestanding
584 endif
585
586 COMPRT_OBJS_$(1) := $$(COMPRT_OBJS_$(1):%=$$(COMPRT_BUILD_DIR_$(1))/%)
587
588 $$(COMPRT_BUILD_DIR_$(1))/%.o: $(S)src/compiler-rt/lib/builtins/%.c
589         @mkdir -p $$(@D)
590         @$$(call E, compile: $$@)
591         $$(Q)$$(call CFG_COMPILE_C_$(1),$$@,$$<)
592
593 $$(COMPRT_BUILD_DIR_$(1))/%.o: $(S)src/compiler-rt/lib/builtins/%.S \
594             $$(LLVM_CONFIG_$$(CFG_BUILD))
595         @mkdir -p $$(@D)
596         @$$(call E, compile: $$@)
597         $$(Q)$$(call CFG_ASSEMBLE_$(1),$$@,$$<)
598
599 ifeq ($$(findstring msvc,$(1)),msvc)
600 $$(COMPRT_BUILD_DIR_$(1))/%.o: \
601         export INCLUDE := $$(CFG_MSVC_INCLUDE_PATH_$$(HOST_$(1)))
602 endif
603
604 ifeq ($$(findstring emscripten,$(1)),emscripten)
605 # FIXME: emscripten doesn't use compiler-rt and can't build it without
606 # further hacks
607 COMPRT_OBJS_$(1) :=
608 endif
609
610 $$(COMPRT_LIB_$(1)): $$(COMPRT_OBJS_$(1))
611         @$$(call E, link: $$@)
612         $$(Q)$$(call CFG_CREATE_ARCHIVE_$(1),$$@) $$^
613
614 ################################################################################
615 # libbacktrace
616 #
617 # We use libbacktrace on linux to get symbols in backtraces, but only on linux.
618 # Elsewhere we use other system utilities, so this library is only built on
619 # linux.
620 ################################################################################
621
622 BACKTRACE_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),backtrace)
623 BACKTRACE_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(BACKTRACE_NAME_$(1))
624 BACKTRACE_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/libbacktrace
625
626 # We don't use this on platforms that aren't linux-based (with the exception of
627 # msys2/mingw builds on windows, which use it to read the dwarf debug
628 # information) so just make the file available, the compilation of libstd won't
629 # actually build it.
630 ifeq ($$(findstring darwin,$$(OSTYPE_$(1))),darwin)
631 # See comment above
632 $$(BACKTRACE_LIB_$(1)):
633         touch $$@
634
635 else ifeq ($$(findstring ios,$$(OSTYPE_$(1))),ios)
636 # See comment above
637 $$(BACKTRACE_LIB_$(1)):
638         touch $$@
639 else ifeq ($$(findstring msvc,$(1)),msvc)
640 # See comment above
641 $$(BACKTRACE_LIB_$(1)):
642         touch $$@
643 else ifeq ($$(findstring emscripten,$(1)),emscripten)
644 # FIXME: libbacktrace doesn't understand the emscripten triple
645 $$(BACKTRACE_LIB_$(1)):
646         touch $$@
647 else
648
649 ifdef CFG_ENABLE_FAST_MAKE
650 BACKTRACE_DEPS := $(S)/.gitmodules
651 else
652 BACKTRACE_DEPS := $(wildcard $(S)src/libbacktrace/*)
653 endif
654
655 # We need to export CFLAGS because otherwise it doesn't pick up cross compile
656 # builds. If libbacktrace doesn't realize this, it will attempt to read 64-bit
657 # elf headers when compiled for a 32-bit system, yielding blank backtraces.
658 #
659 # This also removes the -Werror flag specifically to prevent errors during
660 # configuration.
661 #
662 # Down below you'll also see echos into the config.h generated by the
663 # ./configure script. This is done to force libbacktrace to *not* use the
664 # atomic/sync functionality because it pulls in unnecessary dependencies and we
665 # never use it anyway.
666 #
667 # We also use `env PWD=` to clear the PWD environment variable, and then
668 # execute the command in a new shell. This is necessary to workaround a
669 # buildbot/msys2 bug: the shell is launched with PWD set to a windows-style path,
670 # which results in all further uses of `pwd` also printing a windows-style path,
671 # which breaks libbacktrace's configure script. Clearing PWD within the same
672 # shell is not sufficient.
673
674 $$(BACKTRACE_BUILD_DIR_$(1))/Makefile: $$(BACKTRACE_DEPS) $$(MKFILE_DEPS)
675         @$$(call E, configure: libbacktrace for $(1))
676         $$(Q)rm -rf $$(BACKTRACE_BUILD_DIR_$(1))
677         $$(Q)mkdir -p $$(BACKTRACE_BUILD_DIR_$(1))
678         $$(Q)(cd $$(BACKTRACE_BUILD_DIR_$(1)) && env \
679               PWD= \
680               CC="$$(CC_$(1))" \
681               AR="$$(AR_$(1))" \
682               RANLIB="$$(AR_$(1)) s" \
683               CFLAGS="$$(CFG_GCCISH_CFLAGS_$(1)) -Wno-error -fno-stack-protector" \
684               $(S)src/libbacktrace/configure --build=$(CFG_GNU_TRIPLE_$(CFG_BUILD)) --host=$(CFG_GNU_TRIPLE_$(1)))
685         $$(Q)echo '#undef HAVE_ATOMIC_FUNCTIONS' >> \
686               $$(BACKTRACE_BUILD_DIR_$(1))/config.h
687         $$(Q)echo '#undef HAVE_SYNC_FUNCTIONS' >> \
688               $$(BACKTRACE_BUILD_DIR_$(1))/config.h
689
690 $$(BACKTRACE_LIB_$(1)): $$(BACKTRACE_BUILD_DIR_$(1))/Makefile $$(MKFILE_DEPS)
691         @$$(call E, make: libbacktrace)
692         $$(Q)$$(MAKE) -C $$(BACKTRACE_BUILD_DIR_$(1)) \
693                 INCDIR=$(S)src/libbacktrace
694         $$(Q)cp $$(BACKTRACE_BUILD_DIR_$(1))/.libs/libbacktrace.a $$@
695
696 endif
697
698 ################################################################################
699 # libc/libunwind for musl
700 #
701 # When we're building a musl-like target we're going to link libc/libunwind
702 # statically into the standard library and liblibc, so we need to make sure
703 # they're in a location that we can find
704 ################################################################################
705
706 ifeq ($$(findstring musl,$(1)),musl)
707 $$(RT_OUTPUT_DIR_$(1))/%: $$(CFG_MUSL_ROOT)/lib/%
708         cp $$^ $$@
709 else
710 # Ask gcc where it is
711 $$(RT_OUTPUT_DIR_$(1))/%:
712         cp $$(shell $$(CC_$(1)) -print-file-name=$$(@F)) $$@
713 endif
714
715 endef
716
717 # Instantiate template for all stages/targets
718 $(foreach target,$(CFG_TARGET), \
719      $(eval $(call DEF_THIRD_PARTY_TARGETS,$(target))))