]> git.proxmox.com Git - rustc.git/blob - mk/rt.mk
Imported Upstream version 1.1.0+dfsg1
[rustc.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 := rust_builtin hoedown morestack miniz \
39 rustrt_native rust_test_helpers
40
41 # $(1) is the target triple
42 define NATIVE_LIBRARIES
43
44 NATIVE_DEPS_hoedown_$(1) := hoedown/src/autolink.c \
45 hoedown/src/buffer.c \
46 hoedown/src/document.c \
47 hoedown/src/escape.c \
48 hoedown/src/html.c \
49 hoedown/src/html_blocks.c \
50 hoedown/src/html_smartypants.c \
51 hoedown/src/stack.c \
52 hoedown/src/version.c
53 NATIVE_DEPS_miniz_$(1) = miniz.c
54 NATIVE_DEPS_rust_builtin_$(1) := rust_builtin.c \
55 rust_android_dummy.c
56 NATIVE_DEPS_rustrt_native_$(1) := \
57 rust_try.ll \
58 arch/$$(HOST_$(1))/record_sp.S
59 NATIVE_DEPS_rust_test_helpers_$(1) := rust_test_helpers.c
60 NATIVE_DEPS_morestack_$(1) := arch/$$(HOST_$(1))/morestack.S
61
62
63 ################################################################################
64 # You shouldn't find it that necessary to edit anything below this line.
65 ################################################################################
66
67 # While we're defining the native libraries for each target, we define some
68 # common rules used to build files for various targets.
69
70 RT_OUTPUT_DIR_$(1) := $(1)/rt
71
72 $$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.ll $$(MKFILE_DEPS) \
73 $$(LLVM_CONFIG_$$(CFG_BUILD))
74 @mkdir -p $$(@D)
75 @$$(call E, compile: $$@)
76 $$(Q)$$(LLC_$$(CFG_BUILD)) $$(CFG_LLC_FLAGS_$(1)) \
77 -filetype=obj -mtriple=$$(CFG_LLVM_TARGET_$(1)) \
78 -relocation-model=pic -o $$@ $$<
79
80 $$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.c $$(MKFILE_DEPS)
81 @mkdir -p $$(@D)
82 @$$(call E, compile: $$@)
83 $$(Q)$$(call CFG_COMPILE_C_$(1), $$@, \
84 -I $$(S)src/rt/hoedown/src \
85 -I $$(S)src/rt \
86 $$(RUNTIME_CFLAGS_$(1))) $$<
87
88 $$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.S $$(MKFILE_DEPS) \
89 $$(LLVM_CONFIG_$$(CFG_BUILD))
90 @mkdir -p $$(@D)
91 @$$(call E, compile: $$@)
92 $$(Q)$$(call CFG_ASSEMBLE_$(1),$$@,$$<)
93 endef
94
95 $(foreach target,$(CFG_TARGET),$(eval $(call NATIVE_LIBRARIES,$(target))))
96
97 # A macro for devining how to build third party libraries listed above (based
98 # on their dependencies).
99 #
100 # $(1) is the target
101 # $(2) is the lib name
102 define THIRD_PARTY_LIB
103
104 OBJS_$(2)_$(1) := $$(NATIVE_DEPS_$(2)_$(1):%=$$(RT_OUTPUT_DIR_$(1))/%)
105 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.c=.o)
106 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.cpp=.o)
107 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.ll=.o)
108 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.S=.o)
109 NATIVE_$(2)_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),$(2))
110 $$(RT_OUTPUT_DIR_$(1))/$$(NATIVE_$(2)_$(1)): $$(OBJS_$(2)_$(1))
111 @$$(call E, link: $$@)
112 $$(Q)$$(AR_$(1)) rcs $$@ $$^
113
114 ifeq ($$(findstring windows,$(1)),windows)
115 $$(RT_OUTPUT_DIR_$(1))/lib$(2).a: $$(RT_OUTPUT_DIR_$(1))/$$(NATIVE_$(2)_$(1))
116 $$(Q)cp $$^ $$@
117 endif
118
119 endef
120
121 $(foreach target,$(CFG_TARGET), \
122 $(eval $(call RUNTIME_RULES,$(target))))
123 $(foreach lib,$(NATIVE_LIBS), \
124 $(foreach target,$(CFG_TARGET), \
125 $(eval $(call THIRD_PARTY_LIB,$(target),$(lib)))))
126
127
128 ################################################################################
129 # Building third-party targets with external build systems
130 #
131 # This location is meant for dependencies which have external build systems. It
132 # is still assumed that the output of each of these steps is a static library
133 # in the correct location.
134 ################################################################################
135
136 define DEF_THIRD_PARTY_TARGETS
137
138 # $(1) is the target triple
139
140 ifeq ($$(CFG_WINDOWSY_$(1)), 1)
141 # This isn't necessarily a desired option, but it's harmless and works around
142 # what appears to be a mingw-w64 bug.
143 #
144 # https://sourceforge.net/p/mingw-w64/bugs/395/
145 JEMALLOC_ARGS_$(1) := --enable-lazy-lock
146 else ifeq ($(OSTYPE_$(1)), apple-ios)
147 JEMALLOC_ARGS_$(1) := --disable-tls
148 else ifeq ($(findstring android, $(OSTYPE_$(1))), android)
149 JEMALLOC_ARGS_$(1) := --disable-tls
150 endif
151
152 ifdef CFG_ENABLE_DEBUG_JEMALLOC
153 JEMALLOC_ARGS_$(1) += --enable-debug --enable-fill
154 endif
155
156 ################################################################################
157 # jemalloc
158 ################################################################################
159
160 ifdef CFG_ENABLE_FAST_MAKE
161 JEMALLOC_DEPS := $(S)/.gitmodules
162 else
163 JEMALLOC_DEPS := $(wildcard \
164 $(S)src/jemalloc/* \
165 $(S)src/jemalloc/*/* \
166 $(S)src/jemalloc/*/*/* \
167 $(S)src/jemalloc/*/*/*/*)
168 endif
169
170 # See #17183 for details, this file is touched during the build process so we
171 # don't want to consider it as a dependency.
172 JEMALLOC_DEPS := $(filter-out $(S)src/jemalloc/VERSION,$(JEMALLOC_DEPS))
173
174 JEMALLOC_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc)
175 ifeq ($$(CFG_WINDOWSY_$(1)),1)
176 JEMALLOC_REAL_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc_s)
177 else
178 JEMALLOC_REAL_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc_pic)
179 endif
180 JEMALLOC_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(JEMALLOC_NAME_$(1))
181 JEMALLOC_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/jemalloc
182 JEMALLOC_LOCAL_$(1) := $$(JEMALLOC_BUILD_DIR_$(1))/lib/$$(JEMALLOC_REAL_NAME_$(1))
183
184 $$(JEMALLOC_LOCAL_$(1)): $$(JEMALLOC_DEPS) $$(MKFILE_DEPS)
185 @$$(call E, make: jemalloc)
186 cd "$$(JEMALLOC_BUILD_DIR_$(1))"; "$(S)src/jemalloc/configure" \
187 $$(JEMALLOC_ARGS_$(1)) --with-jemalloc-prefix=je_ $(CFG_JEMALLOC_FLAGS) \
188 --build=$$(CFG_GNU_TRIPLE_$(CFG_BUILD)) --host=$$(CFG_GNU_TRIPLE_$(1)) \
189 CC="$$(CC_$(1)) $$(CFG_JEMALLOC_CFLAGS_$(1))" \
190 AR="$$(AR_$(1))" \
191 RANLIB="$$(AR_$(1)) s" \
192 CPPFLAGS="-I $(S)src/rt/" \
193 EXTRA_CFLAGS="-g1 -ffunction-sections -fdata-sections"
194 $$(Q)$$(MAKE) -C "$$(JEMALLOC_BUILD_DIR_$(1))" build_lib_static
195
196 ifeq ($$(CFG_DISABLE_JEMALLOC),)
197 RUSTFLAGS_alloc := --cfg jemalloc
198 ifeq ($(1),$$(CFG_BUILD))
199 ifneq ($$(CFG_JEMALLOC_ROOT),)
200 $$(JEMALLOC_LIB_$(1)): $$(CFG_JEMALLOC_ROOT)/libjemalloc_pic.a
201 @$$(call E, copy: jemalloc)
202 $$(Q)cp $$< $$@
203 else
204 $$(JEMALLOC_LIB_$(1)): $$(JEMALLOC_LOCAL_$(1))
205 $$(Q)cp $$< $$@
206 endif
207 else
208 $$(JEMALLOC_LIB_$(1)): $$(JEMALLOC_LOCAL_$(1))
209 $$(Q)cp $$< $$@
210 endif
211 else
212 $$(JEMALLOC_LIB_$(1)): $$(MKFILE_DEPS)
213 $$(Q)touch $$@
214 endif
215
216 ################################################################################
217 # compiler-rt
218 ################################################################################
219
220 ifdef CFG_ENABLE_FAST_MAKE
221 COMPRT_DEPS := $(S)/.gitmodules
222 else
223 COMPRT_DEPS := $(wildcard \
224 $(S)src/compiler-rt/* \
225 $(S)src/compiler-rt/*/* \
226 $(S)src/compiler-rt/*/*/* \
227 $(S)src/compiler-rt/*/*/*/*)
228 endif
229
230 COMPRT_NAME_$(1) := libcompiler-rt.a
231 COMPRT_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(COMPRT_NAME_$(1))
232 COMPRT_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/compiler-rt
233
234 $$(COMPRT_LIB_$(1)): $$(COMPRT_DEPS) $$(MKFILE_DEPS)
235 @$$(call E, make: compiler-rt)
236 $$(Q)$$(MAKE) -C "$(S)src/compiler-rt" \
237 ProjSrcRoot="$(S)src/compiler-rt" \
238 ProjObjRoot="$$(abspath $$(COMPRT_BUILD_DIR_$(1)))" \
239 CC="$$(CC_$(1))" \
240 AR="$$(AR_$(1))" \
241 RANLIB="$$(AR_$(1)) s" \
242 CFLAGS="$$(CFG_GCCISH_CFLAGS_$(1))" \
243 TargetTriple=$(1) \
244 triple-builtins
245 $$(Q)cp $$(COMPRT_BUILD_DIR_$(1))/triple/builtins/libcompiler_rt.a $$(COMPRT_LIB_$(1))
246
247 ################################################################################
248 # libbacktrace
249 #
250 # We use libbacktrace on linux to get symbols in backtraces, but only on linux.
251 # Elsewhere we use other system utilities, so this library is only built on
252 # linux.
253 ################################################################################
254
255 BACKTRACE_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),backtrace)
256 BACKTRACE_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(BACKTRACE_NAME_$(1))
257 BACKTRACE_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/libbacktrace
258
259 # We don't use this on platforms that aren't linux-based, so just make the file
260 # available, the compilation of libstd won't actually build it.
261 ifeq ($$(findstring darwin,$$(OSTYPE_$(1))),darwin)
262 # See comment above
263 $$(BACKTRACE_LIB_$(1)):
264 touch $$@
265
266 else
267 ifeq ($$(findstring ios,$$(OSTYPE_$(1))),ios)
268 # See comment above
269 $$(BACKTRACE_LIB_$(1)):
270 touch $$@
271 else
272
273 ifeq ($$(CFG_WINDOWSY_$(1)),1)
274 # See comment above
275 $$(BACKTRACE_LIB_$(1)):
276 touch $$@
277 else
278
279 ifdef CFG_ENABLE_FAST_MAKE
280 BACKTRACE_DEPS := $(S)/.gitmodules
281 else
282 BACKTRACE_DEPS := $(wildcard $(S)src/libbacktrace/*)
283 endif
284
285 # We need to export CFLAGS because otherwise it doesn't pick up cross compile
286 # builds. If libbacktrace doesn't realize this, it will attempt to read 64-bit
287 # elf headers when compiled for a 32-bit system, yielding blank backtraces.
288 #
289 # This also removes the -Werror flag specifically to prevent errors during
290 # configuration.
291 #
292 # Down below you'll also see echos into the config.h generated by the
293 # ./configure script. This is done to force libbacktrace to *not* use the
294 # atomic/sync functionality because it pulls in unnecessary dependencies and we
295 # never use it anyway.
296 $$(BACKTRACE_BUILD_DIR_$(1))/Makefile: $$(BACKTRACE_DEPS) $$(MKFILE_DEPS)
297 @$$(call E, configure: libbacktrace for $(1))
298 $$(Q)rm -rf $$(BACKTRACE_BUILD_DIR_$(1))
299 $$(Q)mkdir -p $$(BACKTRACE_BUILD_DIR_$(1))
300 $$(Q)(cd $$(BACKTRACE_BUILD_DIR_$(1)) && \
301 CC="$$(CC_$(1))" \
302 AR="$$(AR_$(1))" \
303 RANLIB="$$(AR_$(1)) s" \
304 CFLAGS="$$(CFG_GCCISH_CFLAGS_$(1):-Werror=) -fno-stack-protector" \
305 $(S)src/libbacktrace/configure --target=$(1) --host=$(CFG_BUILD))
306 $$(Q)echo '#undef HAVE_ATOMIC_FUNCTIONS' >> \
307 $$(BACKTRACE_BUILD_DIR_$(1))/config.h
308 $$(Q)echo '#undef HAVE_SYNC_FUNCTIONS' >> \
309 $$(BACKTRACE_BUILD_DIR_$(1))/config.h
310
311 $$(BACKTRACE_LIB_$(1)): $$(BACKTRACE_BUILD_DIR_$(1))/Makefile $$(MKFILE_DEPS)
312 @$$(call E, make: libbacktrace)
313 $$(Q)$$(MAKE) -C $$(BACKTRACE_BUILD_DIR_$(1)) \
314 INCDIR=$(S)src/libbacktrace
315 $$(Q)cp $$(BACKTRACE_BUILD_DIR_$(1))/.libs/libbacktrace.a $$@
316
317 endif # endif for windowsy
318 endif # endif for ios
319 endif # endif for darwin
320
321 ################################################################################
322 # libc/libunwind for musl
323 #
324 # When we're building a musl-like target we're going to link libc/libunwind
325 # statically into the standard library and liblibc, so we need to make sure
326 # they're in a location that we can find
327 ################################################################################
328
329 ifeq ($$(findstring musl,$(1)),musl)
330 $$(RT_OUTPUT_DIR_$(1))/%: $$(CFG_MUSL_ROOT)/lib/%
331 cp $$^ $$@
332 endif
333
334 endef
335
336 # Instantiate template for all stages/targets
337 $(foreach target,$(CFG_TARGET), \
338 $(eval $(call DEF_THIRD_PARTY_TARGETS,$(target))))