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.
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.
11 ################################################################################
12 # Native libraries built as part of the rust build process
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.
19 # This section should need minimal modification to add new libraries. The
20 # relevant variables are:
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
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
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
37 ################################################################################
38 NATIVE_LIBS := hoedown miniz rust_test_helpers
40 # A macro to add a generic implementation of intrinsics iff a arch optimized implementation is not
41 # already in the list.
43 # $(2) is the intrinsic
45 ifeq ($$(findstring X,$$(foreach intrinsic,$$(COMPRT_OBJS_$(1)),$$(if $$(findstring $(2),$$(intrinsic)),X,))),)
46 COMPRT_OBJS_$(1) += $(2)
50 # $(1) is the target triple
51 define NATIVE_LIBRARIES
53 NATIVE_DEPS_hoedown_$(1) := hoedown/src/autolink.c \
54 hoedown/src/buffer.c \
55 hoedown/src/document.c \
56 hoedown/src/escape.c \
58 hoedown/src/html_blocks.c \
59 hoedown/src/html_smartypants.c \
62 NATIVE_DEPS_miniz_$(1) = miniz.c
63 NATIVE_DEPS_rust_test_helpers_$(1) := rust_test_helpers.c
65 ################################################################################
66 # You shouldn't find it that necessary to edit anything below this line.
67 ################################################################################
69 # While we're defining the native libraries for each target, we define some
70 # common rules used to build files for various targets.
72 RT_OUTPUT_DIR_$(1) := $(1)/rt
74 $$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.c $$(MKFILE_DEPS)
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))) $$<
82 $$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.S $$(MKFILE_DEPS) \
83 $$(LLVM_CONFIG_$$(CFG_BUILD))
85 @$$(call E, compile: $$@)
86 $$(Q)$$(call CFG_ASSEMBLE_$(1),$$@,$$<)
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)))
96 export INCLUDE := $$(CFG_MSVC_INCLUDE_PATH_$$(HOST_$(1)))
100 $(foreach target,$(CFG_TARGET),$(eval $(call NATIVE_LIBRARIES,$(target))))
102 # A macro for devining how to build third party libraries listed above (based
103 # on their dependencies).
106 # $(2) is the lib name
107 define THIRD_PARTY_LIB
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),$$@) $$^
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)))))
127 ################################################################################
128 # Building third-party targets with external build systems
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 ################################################################################
135 define DEF_THIRD_PARTY_TARGETS
137 # $(1) is the target triple
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/
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.
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...
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
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.
167 # If the test suite passes, however, without symbol prefixes then we should be
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_
174 ifdef CFG_ENABLE_DEBUG_JEMALLOC
175 JEMALLOC_ARGS_$(1) += --enable-debug --enable-fill
178 ################################################################################
180 ################################################################################
182 ifdef CFG_ENABLE_FAST_MAKE
183 JEMALLOC_DEPS := $(S)/.gitmodules
185 JEMALLOC_DEPS := $(wildcard \
187 $(S)src/jemalloc/*/* \
188 $(S)src/jemalloc/*/*/* \
189 $(S)src/jemalloc/*/*/*/*)
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))
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)
200 JEMALLOC_REAL_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc_pic)
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))
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))" \
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
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)
224 $$(JEMALLOC_LIB_$(1)): $$(JEMALLOC_LOCAL_$(1))
228 $$(JEMALLOC_LIB_$(1)): $$(JEMALLOC_LOCAL_$(1))
232 ################################################################################
234 ################################################################################
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.
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
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.
251 ifeq ($$(findstring msvc,$(1)),)
252 ifeq ($$(findstring x86_64,$(1)),x86_64)
253 COMPRT_OBJS_$(1) += \
259 x86_64/floatundidf.o \
260 x86_64/floatundisf.o \
264 ifeq ($$(findstring i686,$$(patsubts i%86,i686,$(1))),i686)
265 COMPRT_OBJS_$(1) += \
286 ifeq ($$(findstring x86_64,$(1)),x86_64)
287 COMPRT_OBJS_$(1) += \
295 # Generic ARM sources, nothing compiles on iOS though
296 ifeq ($$(findstring arm,$(1)),arm)
297 ifeq ($$(findstring ios,$(1)),)
298 COMPRT_OBJS_$(1) += \
300 arm/aeabi_cdcmpeq_check_nan.o \
302 arm/aeabi_cfcmpeq_check_nan.o \
308 arm/aeabi_idivmod.o \
309 arm/aeabi_ldivmod.o \
312 arm/aeabi_memmove.o \
314 arm/aeabi_uidivmod.o \
315 arm/aeabi_uldivmod.o \
328 arm/sync_synchronize.o \
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
361 ifeq ($$(findstring eabihf,$(1)),eabihf)
362 COMPRT_OBJS_$(1) += \
369 arm/extendsfdf2vfp.o \
372 arm/fixunsdfsivfp.o \
373 arm/fixunssfsivfp.o \
376 arm/floatunssidfvfp.o \
377 arm/floatunssisfvfp.o \
392 arm/restore_vfp_d8_d15_regs.o \
393 arm/save_vfp_d8_d15_regs.o \
396 arm/truncdfsf2vfp.o \
401 $(foreach intrinsic,absvdi2.o \
492 $(call ADD_INTRINSIC,$(1),$(intrinsic)))
494 ifeq ($$(findstring ios,$(1)),)
495 $(foreach intrinsic,absvti2.o \
535 $(call ADD_INTRINSIC,$(1),$(intrinsic)))
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)))
548 ifeq ($$(findstring windows,$(1)),)
549 $(call ADD_INTRINSIC,$(1),emutls.o)
552 ifeq ($$(findstring msvc,$(1)),)
554 ifeq ($$(findstring freebsd,$(1)),)
555 ifeq ($$(findstring netbsd,$(1)),)
556 $(call ADD_INTRINSIC,$(1),gcc_personality_v0.o)
561 ifeq ($$(findstring aarch64,$(1)),aarch64)
562 $(foreach intrinsic,comparetf2.o \
578 $(call ADD_INTRINSIC,$(1),$(intrinsic)))
581 ifeq ($$(findstring msvc,$(1)),msvc)
582 $$(COMPRT_BUILD_DIR_$(1))/%.o: CFLAGS += -Zl -D__func__=__FUNCTION__
584 $$(COMPRT_BUILD_DIR_$(1))/%.o: CFLAGS += -fno-builtin -fvisibility=hidden \
585 -fomit-frame-pointer -ffreestanding
588 COMPRT_OBJS_$(1) := $$(COMPRT_OBJS_$(1):%=$$(COMPRT_BUILD_DIR_$(1))/%)
590 $$(COMPRT_BUILD_DIR_$(1))/%.o: $(S)src/compiler-rt/lib/builtins/%.c
592 @$$(call E, compile: $$@)
593 $$(Q)$$(call CFG_COMPILE_C_$(1),$$@,$$<)
595 $$(COMPRT_BUILD_DIR_$(1))/%.o: $(S)src/compiler-rt/lib/builtins/%.S \
596 $$(LLVM_CONFIG_$$(CFG_BUILD))
598 @$$(call E, compile: $$@)
599 $$(Q)$$(call CFG_ASSEMBLE_$(1),$$@,$$<)
601 ifeq ($$(findstring msvc,$(1)),msvc)
602 $$(COMPRT_BUILD_DIR_$(1))/%.o: \
603 export INCLUDE := $$(CFG_MSVC_INCLUDE_PATH_$$(HOST_$(1)))
606 ifeq ($$(findstring emscripten,$(1)),emscripten)
607 # FIXME: emscripten doesn't use compiler-rt and can't build it without
612 $$(COMPRT_LIB_$(1)): $$(COMPRT_OBJS_$(1))
613 @$$(call E, link: $$@)
614 $$(Q)$$(call CFG_CREATE_ARCHIVE_$(1),$$@) $$^
616 ################################################################################
619 # We use libbacktrace on linux to get symbols in backtraces, but only on linux.
620 # Elsewhere we use other system utilities, so this library is only built on
622 ################################################################################
624 BACKTRACE_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),backtrace)
625 BACKTRACE_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(BACKTRACE_NAME_$(1))
626 BACKTRACE_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/libbacktrace
628 # We don't use this on platforms that aren't linux-based (with the exception of
629 # msys2/mingw builds on windows, which use it to read the dwarf debug
630 # information) so just make the file available, the compilation of libstd won't
632 ifeq ($$(findstring darwin,$$(OSTYPE_$(1))),darwin)
634 $$(BACKTRACE_LIB_$(1)):
637 else ifeq ($$(findstring ios,$$(OSTYPE_$(1))),ios)
639 $$(BACKTRACE_LIB_$(1)):
641 else ifeq ($$(findstring msvc,$(1)),msvc)
643 $$(BACKTRACE_LIB_$(1)):
645 else ifeq ($$(findstring emscripten,$(1)),emscripten)
646 # FIXME: libbacktrace doesn't understand the emscripten triple
647 $$(BACKTRACE_LIB_$(1)):
651 ifdef CFG_ENABLE_FAST_MAKE
652 BACKTRACE_DEPS := $(S)/.gitmodules
654 BACKTRACE_DEPS := $(wildcard $(S)src/libbacktrace/*)
657 # We need to export CFLAGS because otherwise it doesn't pick up cross compile
658 # builds. If libbacktrace doesn't realize this, it will attempt to read 64-bit
659 # elf headers when compiled for a 32-bit system, yielding blank backtraces.
661 # This also removes the -Werror flag specifically to prevent errors during
664 # Down below you'll also see echos into the config.h generated by the
665 # ./configure script. This is done to force libbacktrace to *not* use the
666 # atomic/sync functionality because it pulls in unnecessary dependencies and we
667 # never use it anyway.
669 # We also use `env PWD=` to clear the PWD environment variable, and then
670 # execute the command in a new shell. This is necessary to workaround a
671 # buildbot/msys2 bug: the shell is launched with PWD set to a windows-style path,
672 # which results in all further uses of `pwd` also printing a windows-style path,
673 # which breaks libbacktrace's configure script. Clearing PWD within the same
674 # shell is not sufficient.
676 $$(BACKTRACE_BUILD_DIR_$(1))/Makefile: $$(BACKTRACE_DEPS) $$(MKFILE_DEPS)
677 @$$(call E, configure: libbacktrace for $(1))
678 $$(Q)rm -rf $$(BACKTRACE_BUILD_DIR_$(1))
679 $$(Q)mkdir -p $$(BACKTRACE_BUILD_DIR_$(1))
680 $$(Q)(cd $$(BACKTRACE_BUILD_DIR_$(1)) && env \
684 RANLIB="$$(AR_$(1)) s" \
685 CFLAGS="$$(CFG_GCCISH_CFLAGS_$(1)) -Wno-error -fno-stack-protector" \
686 $(S)src/libbacktrace/configure --build=$(CFG_GNU_TRIPLE_$(CFG_BUILD)) --host=$(CFG_GNU_TRIPLE_$(1)))
687 $$(Q)echo '#undef HAVE_ATOMIC_FUNCTIONS' >> \
688 $$(BACKTRACE_BUILD_DIR_$(1))/config.h
689 $$(Q)echo '#undef HAVE_SYNC_FUNCTIONS' >> \
690 $$(BACKTRACE_BUILD_DIR_$(1))/config.h
692 $$(BACKTRACE_LIB_$(1)): $$(BACKTRACE_BUILD_DIR_$(1))/Makefile $$(MKFILE_DEPS)
693 @$$(call E, make: libbacktrace)
694 $$(Q)$$(MAKE) -C $$(BACKTRACE_BUILD_DIR_$(1)) \
695 INCDIR=$(S)src/libbacktrace
696 $$(Q)cp $$(BACKTRACE_BUILD_DIR_$(1))/.libs/libbacktrace.a $$@
700 ################################################################################
701 # libc/libunwind for musl
703 # When we're building a musl-like target we're going to link libc/libunwind
704 # statically into the standard library and liblibc, so we need to make sure
705 # they're in a location that we can find
706 ################################################################################
708 ifeq ($$(findstring musl,$(1)),musl)
709 $$(RT_OUTPUT_DIR_$(1))/%: $$(CFG_MUSL_ROOT)/lib/%
712 # Ask gcc where it is
713 $$(RT_OUTPUT_DIR_$(1))/%:
714 cp $$(shell $$(CC_$(1)) -print-file-name=$$(@F)) $$@
719 # Instantiate template for all stages/targets
720 $(foreach target,$(CFG_TARGET), \
721 $(eval $(call DEF_THIRD_PARTY_TARGETS,$(target))))