]> git.proxmox.com Git - rustc.git/blame - mk/rt.mk
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / mk / rt.mk
CommitLineData
1a4d82fc
JJ
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.
223e47cc 4#
1a4d82fc
JJ
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
223e47cc 13#
1a4d82fc
JJ
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.
223e47cc 18#
1a4d82fc
JJ
19# This section should need minimal modification to add new libraries. The
20# relevant variables are:
223e47cc 21#
1a4d82fc
JJ
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################################################################################
9cc50fc6 38NATIVE_LIBS := hoedown miniz rust_test_helpers
223e47cc 39
970d7e83 40# $(1) is the target triple
1a4d82fc
JJ
41define NATIVE_LIBRARIES
42
43NATIVE_DEPS_hoedown_$(1) := hoedown/src/autolink.c \
44 hoedown/src/buffer.c \
45 hoedown/src/document.c \
46 hoedown/src/escape.c \
47 hoedown/src/html.c \
48 hoedown/src/html_blocks.c \
49 hoedown/src/html_smartypants.c \
50 hoedown/src/stack.c \
51 hoedown/src/version.c
52NATIVE_DEPS_miniz_$(1) = miniz.c
1a4d82fc 53NATIVE_DEPS_rust_test_helpers_$(1) := rust_test_helpers.c
1a4d82fc
JJ
54
55################################################################################
56# You shouldn't find it that necessary to edit anything below this line.
57################################################################################
58
59# While we're defining the native libraries for each target, we define some
60# common rules used to build files for various targets.
61
62RT_OUTPUT_DIR_$(1) := $(1)/rt
63
1a4d82fc
JJ
64$$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.c $$(MKFILE_DEPS)
65 @mkdir -p $$(@D)
66 @$$(call E, compile: $$@)
67 $$(Q)$$(call CFG_COMPILE_C_$(1), $$@, \
62682a34
SL
68 $$(call CFG_CC_INCLUDE_$(1),$$(S)src/rt/hoedown/src) \
69 $$(call CFG_CC_INCLUDE_$(1),$$(S)src/rt) \
1a4d82fc
JJ
70 $$(RUNTIME_CFLAGS_$(1))) $$<
71
72$$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.S $$(MKFILE_DEPS) \
73 $$(LLVM_CONFIG_$$(CFG_BUILD))
74 @mkdir -p $$(@D)
75 @$$(call E, compile: $$@)
76 $$(Q)$$(call CFG_ASSEMBLE_$(1),$$@,$$<)
c1a9b12d
SL
77
78# On MSVC targets the compiler's default include path (e.g. where to find system
79# headers) is specified by the INCLUDE environment variable. This may not be set
80# so the ./configure script scraped the relevant values and this is the location
81# that we put them into cl.exe's environment.
82ifeq ($$(findstring msvc,$(1)),msvc)
83$$(RT_OUTPUT_DIR_$(1))/%.o: \
84 export INCLUDE := $$(CFG_MSVC_INCLUDE_PATH_$$(HOST_$(1)))
85$(1)/rustllvm/%.o: \
86 export INCLUDE := $$(CFG_MSVC_INCLUDE_PATH_$$(HOST_$(1)))
87endif
1a4d82fc 88endef
970d7e83 89
1a4d82fc 90$(foreach target,$(CFG_TARGET),$(eval $(call NATIVE_LIBRARIES,$(target))))
223e47cc 91
1a4d82fc
JJ
92# A macro for devining how to build third party libraries listed above (based
93# on their dependencies).
94#
95# $(1) is the target
96# $(2) is the lib name
97define THIRD_PARTY_LIB
98
99OBJS_$(2)_$(1) := $$(NATIVE_DEPS_$(2)_$(1):%=$$(RT_OUTPUT_DIR_$(1))/%)
100OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.c=.o)
101OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.cpp=.o)
1a4d82fc
JJ
102OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.S=.o)
103NATIVE_$(2)_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),$(2))
104$$(RT_OUTPUT_DIR_$(1))/$$(NATIVE_$(2)_$(1)): $$(OBJS_$(2)_$(1))
105 @$$(call E, link: $$@)
62682a34 106 $$(Q)$$(call CFG_CREATE_ARCHIVE_$(1),$$@) $$^
d9579d0f 107
1a4d82fc 108endef
223e47cc 109
1a4d82fc
JJ
110$(foreach target,$(CFG_TARGET), \
111 $(eval $(call RUNTIME_RULES,$(target))))
112$(foreach lib,$(NATIVE_LIBS), \
113 $(foreach target,$(CFG_TARGET), \
114 $(eval $(call THIRD_PARTY_LIB,$(target),$(lib)))))
223e47cc 115
223e47cc 116
1a4d82fc
JJ
117################################################################################
118# Building third-party targets with external build systems
119#
120# This location is meant for dependencies which have external build systems. It
121# is still assumed that the output of each of these steps is a static library
122# in the correct location.
123################################################################################
223e47cc 124
1a4d82fc 125define DEF_THIRD_PARTY_TARGETS
223e47cc 126
1a4d82fc 127# $(1) is the target triple
223e47cc 128
9cc50fc6
SL
129ifeq ($$(CFG_WINDOWSY_$(1)),1)
130 # A bit of history here, this used to be --enable-lazy-lock added in #14006
131 # which was filed with jemalloc in jemalloc/jemalloc#83 which was also
132 # reported to MinGW: http://sourceforge.net/p/mingw-w64/bugs/395/
1a4d82fc 133 #
9cc50fc6
SL
134 # When updating jemalloc to 4.0, however, it was found that binaries would
135 # exit with the status code STATUS_RESOURCE_NOT_OWNED indicating that a thread
136 # was unlocking a mutex it never locked. Disabling this "lazy lock" option
137 # seems to fix the issue, but it was enabled by default for MinGW targets in
138 # 13473c7 for jemalloc.
139 #
140 # As a result of all that, force disabling lazy lock on Windows, and after
141 # reading some code it at least *appears* that the initialization of mutexes
142 # is otherwise ok in jemalloc, so shouldn't cause problems hopefully...
143 #
144 # tl;dr: make windows behave like other platforms by disabling lazy locking,
145 # but requires passing an option due to a historical default with
146 # jemalloc.
147 JEMALLOC_ARGS_$(1) := --disable-lazy-lock
1a4d82fc
JJ
148else ifeq ($(OSTYPE_$(1)), apple-ios)
149 JEMALLOC_ARGS_$(1) := --disable-tls
85aaf69f 150else ifeq ($(findstring android, $(OSTYPE_$(1))), android)
7453a54e
SL
151 # We force android to have prefixed symbols because apparently replacement of
152 # the libc allocator doesn't quite work. When this was tested (unprefixed
153 # symbols), it was found that the `realpath` function in libc would allocate
154 # with libc malloc (not jemalloc malloc), and then the standard library would
155 # free with jemalloc free, causing a segfault.
156 #
157 # If the test suite passes, however, without symbol prefixes then we should be
158 # good to go!
159 JEMALLOC_ARGS_$(1) := --disable-tls --with-jemalloc-prefix=je_
54a0048b
SL
160else ifeq ($(findstring dragonfly, $(OSTYPE_$(1))), dragonfly)
161 JEMALLOC_ARGS_$(1) := --with-jemalloc-prefix=je_
1a4d82fc 162endif
223e47cc 163
9346a6ac
AL
164ifdef CFG_ENABLE_DEBUG_JEMALLOC
165 JEMALLOC_ARGS_$(1) += --enable-debug --enable-fill
166endif
167
1a4d82fc
JJ
168################################################################################
169# jemalloc
170################################################################################
223e47cc
LB
171
172ifdef CFG_ENABLE_FAST_MAKE
1a4d82fc 173JEMALLOC_DEPS := $(S)/.gitmodules
223e47cc 174else
1a4d82fc
JJ
175JEMALLOC_DEPS := $(wildcard \
176 $(S)src/jemalloc/* \
177 $(S)src/jemalloc/*/* \
178 $(S)src/jemalloc/*/*/* \
179 $(S)src/jemalloc/*/*/*/*)
223e47cc
LB
180endif
181
1a4d82fc
JJ
182# See #17183 for details, this file is touched during the build process so we
183# don't want to consider it as a dependency.
184JEMALLOC_DEPS := $(filter-out $(S)src/jemalloc/VERSION,$(JEMALLOC_DEPS))
185
186JEMALLOC_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc)
187ifeq ($$(CFG_WINDOWSY_$(1)),1)
188 JEMALLOC_REAL_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc_s)
223e47cc 189else
1a4d82fc
JJ
190 JEMALLOC_REAL_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc_pic)
191endif
192JEMALLOC_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(JEMALLOC_NAME_$(1))
193JEMALLOC_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/jemalloc
194JEMALLOC_LOCAL_$(1) := $$(JEMALLOC_BUILD_DIR_$(1))/lib/$$(JEMALLOC_REAL_NAME_$(1))
195
196$$(JEMALLOC_LOCAL_$(1)): $$(JEMALLOC_DEPS) $$(MKFILE_DEPS)
197 @$$(call E, make: jemalloc)
198 cd "$$(JEMALLOC_BUILD_DIR_$(1))"; "$(S)src/jemalloc/configure" \
7453a54e 199 $$(JEMALLOC_ARGS_$(1)) $(CFG_JEMALLOC_FLAGS) \
1a4d82fc
JJ
200 --build=$$(CFG_GNU_TRIPLE_$(CFG_BUILD)) --host=$$(CFG_GNU_TRIPLE_$(1)) \
201 CC="$$(CC_$(1)) $$(CFG_JEMALLOC_CFLAGS_$(1))" \
970d7e83 202 AR="$$(AR_$(1))" \
1a4d82fc
JJ
203 RANLIB="$$(AR_$(1)) s" \
204 CPPFLAGS="-I $(S)src/rt/" \
205 EXTRA_CFLAGS="-g1 -ffunction-sections -fdata-sections"
206 $$(Q)$$(MAKE) -C "$$(JEMALLOC_BUILD_DIR_$(1))" build_lib_static
207
1a4d82fc
JJ
208ifeq ($(1),$$(CFG_BUILD))
209ifneq ($$(CFG_JEMALLOC_ROOT),)
210$$(JEMALLOC_LIB_$(1)): $$(CFG_JEMALLOC_ROOT)/libjemalloc_pic.a
211 @$$(call E, copy: jemalloc)
212 $$(Q)cp $$< $$@
213else
214$$(JEMALLOC_LIB_$(1)): $$(JEMALLOC_LOCAL_$(1))
215 $$(Q)cp $$< $$@
223e47cc 216endif
970d7e83 217else
1a4d82fc
JJ
218$$(JEMALLOC_LIB_$(1)): $$(JEMALLOC_LOCAL_$(1))
219 $$(Q)cp $$< $$@
220endif
970d7e83 221
1a4d82fc
JJ
222################################################################################
223# compiler-rt
224################################################################################
223e47cc 225
1a4d82fc
JJ
226ifdef CFG_ENABLE_FAST_MAKE
227COMPRT_DEPS := $(S)/.gitmodules
228else
229COMPRT_DEPS := $(wildcard \
230 $(S)src/compiler-rt/* \
231 $(S)src/compiler-rt/*/* \
232 $(S)src/compiler-rt/*/*/* \
233 $(S)src/compiler-rt/*/*/*/*)
234endif
223e47cc 235
62682a34 236COMPRT_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),compiler-rt)
1a4d82fc
JJ
237COMPRT_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(COMPRT_NAME_$(1))
238COMPRT_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/compiler-rt
223e47cc 239
62682a34 240ifeq ($$(findstring msvc,$(1)),msvc)
54a0048b 241$$(COMPRT_LIB_$(1)): $$(COMPRT_DEPS) $$(MKFILE_DEPS) $$(LLVM_CONFIG_$$(CFG_BUILD))
92a42be0
SL
242 @$$(call E, cmake: compiler-rt)
243 $$(Q)cd "$$(COMPRT_BUILD_DIR_$(1))"; $$(CFG_CMAKE) "$(S)src/compiler-rt" \
244 -DCMAKE_BUILD_TYPE=$$(LLVM_BUILD_CONFIG_MODE) \
54a0048b 245 -DLLVM_CONFIG_PATH=$$(LLVM_CONFIG_$$(CFG_BUILD)) \
92a42be0
SL
246 -G"$$(CFG_CMAKE_GENERATOR)"
247 $$(Q)$$(CFG_CMAKE) --build "$$(COMPRT_BUILD_DIR_$(1))" \
248 --target lib/builtins/builtins \
249 --config $$(LLVM_BUILD_CONFIG_MODE) \
250 -- //v:m //nologo
251 $$(Q)cp $$(COMPRT_BUILD_DIR_$(1))/lib/windows/$$(LLVM_BUILD_CONFIG_MODE)/clang_rt.builtins-$$(HOST_$(1)).lib $$@
c1a9b12d 252else
92a42be0
SL
253COMPRT_CC_$(1) := $$(CC_$(1))
254COMPRT_AR_$(1) := $$(AR_$(1))
255# We chomp -Werror here because GCC warns about the type signature of
256# builtins not matching its own and the build fails. It's a bit hacky,
257# but what can we do, we're building libclang-rt using GCC ......
54a0048b 258COMPRT_CFLAGS_$(1) := $$(CFG_GCCISH_CFLAGS_$(1)) -Wno-error -std=c99
92a42be0
SL
259
260# FreeBSD Clang's packaging is problematic; it doesn't copy unwind.h to
261# the standard include directory. This should really be in our changes to
262# compiler-rt, but we override the CFLAGS here so there isn't much choice
263ifeq ($$(findstring freebsd,$(1)),freebsd)
264 COMPRT_CFLAGS_$(1) += -I/usr/include/c++/v1
c1a9b12d 265endif
62682a34 266
7453a54e
SL
267ifeq ($$(findstring emscripten,$(1)),emscripten)
268
269# FIXME: emscripten doesn't use compiler-rt and can't build it without
270# further hacks
271$$(COMPRT_LIB_$(1)):
272 touch $$@
273
274else
275
1a4d82fc
JJ
276$$(COMPRT_LIB_$(1)): $$(COMPRT_DEPS) $$(MKFILE_DEPS)
277 @$$(call E, make: compiler-rt)
278 $$(Q)$$(MAKE) -C "$(S)src/compiler-rt" \
279 ProjSrcRoot="$(S)src/compiler-rt" \
280 ProjObjRoot="$$(abspath $$(COMPRT_BUILD_DIR_$(1)))" \
62682a34
SL
281 CC='$$(COMPRT_CC_$(1))' \
282 AR='$$(COMPRT_AR_$(1))' \
283 RANLIB='$$(COMPRT_AR_$(1)) s' \
284 CFLAGS="$$(COMPRT_CFLAGS_$(1))" \
1a4d82fc
JJ
285 TargetTriple=$(1) \
286 triple-builtins
62682a34 287 $$(Q)cp $$(COMPRT_BUILD_DIR_$(1))/triple/builtins/libcompiler_rt.a $$@
7453a54e
SL
288
289endif # if emscripten
92a42be0 290endif
7453a54e 291
1a4d82fc
JJ
292################################################################################
293# libbacktrace
294#
295# We use libbacktrace on linux to get symbols in backtraces, but only on linux.
296# Elsewhere we use other system utilities, so this library is only built on
297# linux.
298################################################################################
299
300BACKTRACE_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),backtrace)
301BACKTRACE_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(BACKTRACE_NAME_$(1))
302BACKTRACE_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/libbacktrace
303
e9174d1e
SL
304# We don't use this on platforms that aren't linux-based (with the exception of
305# msys2/mingw builds on windows, which use it to read the dwarf debug
306# information) so just make the file available, the compilation of libstd won't
307# actually build it.
1a4d82fc
JJ
308ifeq ($$(findstring darwin,$$(OSTYPE_$(1))),darwin)
309# See comment above
310$$(BACKTRACE_LIB_$(1)):
311 touch $$@
223e47cc 312
1a4d82fc
JJ
313else
314ifeq ($$(findstring ios,$$(OSTYPE_$(1))),ios)
315# See comment above
316$$(BACKTRACE_LIB_$(1)):
317 touch $$@
318else
223e47cc 319
e9174d1e 320ifeq ($$(findstring msvc,$(1)),msvc)
1a4d82fc
JJ
321# See comment above
322$$(BACKTRACE_LIB_$(1)):
323 touch $$@
324else
223e47cc 325
7453a54e
SL
326ifeq ($$(findstring emscripten,$(1)),emscripten)
327# FIXME: libbacktrace doesn't understand the emscripten triple
328$$(BACKTRACE_LIB_$(1)):
329 touch $$@
330else
331
1a4d82fc
JJ
332ifdef CFG_ENABLE_FAST_MAKE
333BACKTRACE_DEPS := $(S)/.gitmodules
334else
335BACKTRACE_DEPS := $(wildcard $(S)src/libbacktrace/*)
336endif
337
338# We need to export CFLAGS because otherwise it doesn't pick up cross compile
339# builds. If libbacktrace doesn't realize this, it will attempt to read 64-bit
340# elf headers when compiled for a 32-bit system, yielding blank backtraces.
341#
342# This also removes the -Werror flag specifically to prevent errors during
343# configuration.
344#
345# Down below you'll also see echos into the config.h generated by the
346# ./configure script. This is done to force libbacktrace to *not* use the
347# atomic/sync functionality because it pulls in unnecessary dependencies and we
348# never use it anyway.
e9174d1e
SL
349#
350# We also use `env PWD=` to clear the PWD environment variable, and then
351# execute the command in a new shell. This is necessary to workaround a
352# buildbot/msys2 bug: the shell is launched with PWD set to a windows-style path,
353# which results in all further uses of `pwd` also printing a windows-style path,
354# which breaks libbacktrace's configure script. Clearing PWD within the same
355# shell is not sufficient.
356
1a4d82fc
JJ
357$$(BACKTRACE_BUILD_DIR_$(1))/Makefile: $$(BACKTRACE_DEPS) $$(MKFILE_DEPS)
358 @$$(call E, configure: libbacktrace for $(1))
359 $$(Q)rm -rf $$(BACKTRACE_BUILD_DIR_$(1))
360 $$(Q)mkdir -p $$(BACKTRACE_BUILD_DIR_$(1))
e9174d1e
SL
361 $$(Q)(cd $$(BACKTRACE_BUILD_DIR_$(1)) && env \
362 PWD= \
1a4d82fc
JJ
363 CC="$$(CC_$(1))" \
364 AR="$$(AR_$(1))" \
365 RANLIB="$$(AR_$(1)) s" \
54a0048b 366 CFLAGS="$$(CFG_GCCISH_CFLAGS_$(1)) -Wno-error -fno-stack-protector" \
e9174d1e 367 $(S)src/libbacktrace/configure --build=$(CFG_GNU_TRIPLE_$(CFG_BUILD)) --host=$(CFG_GNU_TRIPLE_$(1)))
1a4d82fc
JJ
368 $$(Q)echo '#undef HAVE_ATOMIC_FUNCTIONS' >> \
369 $$(BACKTRACE_BUILD_DIR_$(1))/config.h
370 $$(Q)echo '#undef HAVE_SYNC_FUNCTIONS' >> \
371 $$(BACKTRACE_BUILD_DIR_$(1))/config.h
372
373$$(BACKTRACE_LIB_$(1)): $$(BACKTRACE_BUILD_DIR_$(1))/Makefile $$(MKFILE_DEPS)
374 @$$(call E, make: libbacktrace)
375 $$(Q)$$(MAKE) -C $$(BACKTRACE_BUILD_DIR_$(1)) \
376 INCDIR=$(S)src/libbacktrace
377 $$(Q)cp $$(BACKTRACE_BUILD_DIR_$(1))/.libs/libbacktrace.a $$@
378
7453a54e 379endif # endif for emscripten
e9174d1e 380endif # endif for msvc
1a4d82fc
JJ
381endif # endif for ios
382endif # endif for darwin
223e47cc 383
d9579d0f
AL
384################################################################################
385# libc/libunwind for musl
386#
387# When we're building a musl-like target we're going to link libc/libunwind
388# statically into the standard library and liblibc, so we need to make sure
389# they're in a location that we can find
390################################################################################
391
392ifeq ($$(findstring musl,$(1)),musl)
393$$(RT_OUTPUT_DIR_$(1))/%: $$(CFG_MUSL_ROOT)/lib/%
394 cp $$^ $$@
92a42be0
SL
395else
396# Ask gcc where it is
397$$(RT_OUTPUT_DIR_$(1))/%:
398 cp $$(shell $$(CC_$(1)) -print-file-name=$$(@F)) $$@
d9579d0f
AL
399endif
400
223e47cc
LB
401endef
402
1a4d82fc
JJ
403# Instantiate template for all stages/targets
404$(foreach target,$(CFG_TARGET), \
405 $(eval $(call DEF_THIRD_PARTY_TARGETS,$(target))))