]> git.lizzy.rs Git - rust.git/blob - mk/rt.mk
auto merge of #14102 : moonglum/rust/slice-clarification-in-readme, r=kballard
[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 := rustrt hoedown uv_support morestack miniz context_switch
39
40 # $(1) is the target triple
41 define NATIVE_LIBRARIES
42
43 NATIVE_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
52 NATIVE_DEPS_uv_support_$(1) := rust_uv.c
53 NATIVE_DEPS_miniz_$(1) = miniz.c
54 NATIVE_DEPS_rustrt_$(1) := rust_builtin.c \
55                         rust_android_dummy.c \
56                         rust_test_helpers.c \
57                         rust_try.ll \
58                         arch/$$(HOST_$(1))/record_sp.S
59 NATIVE_DEPS_morestack_$(1) := arch/$$(HOST_$(1))/morestack.S
60 NATIVE_DEPS_context_switch_$(1) := \
61                         arch/$$(HOST_$(1))/_context.S
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=$(1) -relocation-model=pic -o $$@ $$<
78
79 $$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.c $$(MKFILE_DEPS)
80         @mkdir -p $$(@D)
81         @$$(call E, compile: $$@)
82         $$(Q)$$(call CFG_COMPILE_C_$(1), $$@, \
83                 -I $$(S)src/rt/hoedown/src \
84                 -I $$(S)src/libuv/include -I $$(S)src/rt \
85                  $$(RUNTIME_CFLAGS_$(1))) $$<
86
87 $$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.S $$(MKFILE_DEPS) \
88             $$(LLVM_CONFIG_$$(CFG_BUILD))
89         @mkdir -p $$(@D)
90         @$$(call E, compile: $$@)
91         $$(Q)$$(call CFG_ASSEMBLE_$(1),$$@,$$<)
92 endef
93
94 $(foreach target,$(CFG_TARGET),$(eval $(call NATIVE_LIBRARIES,$(target))))
95
96 # A macro for devining how to build third party libraries listed above (based
97 # on their dependencies).
98 #
99 # $(1) is the target
100 # $(2) is the lib name
101 define THIRD_PARTY_LIB
102
103 OBJS_$(2)_$(1) := $$(NATIVE_DEPS_$(2)_$(1):%=$$(RT_OUTPUT_DIR_$(1))/%)
104 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.c=.o)
105 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.cpp=.o)
106 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.ll=.o)
107 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.S=.o)
108 NATIVE_$(2)_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),$(2))
109 $$(RT_OUTPUT_DIR_$(1))/$$(NATIVE_$(2)_$(1)): $$(OBJS_$(2)_$(1))
110         @$$(call E, link: $$@)
111         $$(Q)$$(AR_$(1)) rcs $$@ $$^
112
113 endef
114
115 $(foreach target,$(CFG_TARGET),                                     \
116  $(eval $(call RUNTIME_RULES,$(target))))
117 $(foreach lib,$(NATIVE_LIBS),                                       \
118  $(foreach target,$(CFG_TARGET),                                    \
119   $(eval $(call THIRD_PARTY_LIB,$(target),$(lib)))))
120
121
122 ################################################################################
123 # Building third-party targets with external build systems
124 #
125 # This location is meant for dependencies which have external build systems. It
126 # is still assumed that the output of each of these steps is a static library
127 # in the correct location.
128 ################################################################################
129
130 ################################################################################
131 # libuv
132 ################################################################################
133
134 define DEF_LIBUV_ARCH_VAR
135   LIBUV_ARCH_$(1) = $$(subst i386,ia32,$$(subst x86_64,x64,$$(HOST_$(1))))
136 endef
137 $(foreach t,$(CFG_TARGET),$(eval $(call DEF_LIBUV_ARCH_VAR,$(t))))
138
139 ifdef CFG_ENABLE_FAST_MAKE
140 LIBUV_DEPS := $(S)/.gitmodules
141 else
142 LIBUV_DEPS := $(wildcard \
143               $(S)src/libuv/* \
144               $(S)src/libuv/*/* \
145               $(S)src/libuv/*/*/* \
146               $(S)src/libuv/*/*/*/*)
147 endif
148
149 LIBUV_NO_LOAD = run-benchmarks.target.mk run-tests.target.mk \
150                 uv_dtrace_header.target.mk uv_dtrace_provider.target.mk
151
152 export PYTHONPATH := $(PYTHONPATH):$(S)src/gyp/pylib
153
154 define DEF_THIRD_PARTY_TARGETS
155
156 # $(1) is the target triple
157
158 ifeq ($$(CFG_WINDOWSY_$(1)), 1)
159   LIBUV_OSTYPE_$(1) := win
160   # This isn't necessarily a desired option, but it's harmless and works around
161   # what appears to be a mingw-w64 bug.
162   #
163   # https://sourceforge.net/p/mingw-w64/bugs/395/
164   JEMALLOC_ARGS_$(1) := --enable-lazy-lock
165 else ifeq ($(OSTYPE_$(1)), apple-darwin)
166   LIBUV_OSTYPE_$(1) := mac
167 else ifeq ($(OSTYPE_$(1)), unknown-freebsd)
168   LIBUV_OSTYPE_$(1) := freebsd
169 else ifeq ($(OSTYPE_$(1)), linux-androideabi)
170   LIBUV_OSTYPE_$(1) := android
171   LIBUV_ARGS_$(1) := PLATFORM=android host=android OS=linux
172   JEMALLOC_ARGS_$(1) := --disable-tls
173 else
174   LIBUV_OSTYPE_$(1) := linux
175 endif
176
177 LIBUV_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),uv)
178 LIBUV_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/libuv
179 LIBUV_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(LIBUV_NAME_$(1))
180
181 LIBUV_MAKEFILE_$(1) := $$(CFG_BUILD_DIR)$$(RT_OUTPUT_DIR_$(1))/libuv/Makefile
182
183 LIBUV_STAMP_$(1) = $$(LIBUV_DIR_$(1))/libuv-auto-clean-stamp
184
185 $$(LIBUV_STAMP_$(1)): $(S)src/rt/libuv-auto-clean-trigger
186         $$(Q)rm -rf $$(LIBUV_DIR_$(1))
187         $$(Q)mkdir -p $$(@D)
188         touch $$@
189
190 # libuv triggers a few warnings on some platforms
191 LIBUV_CFLAGS_$(1) := $(subst -Werror,,$(CFG_GCCISH_CFLAGS_$(1)))
192
193 $$(LIBUV_MAKEFILE_$(1)): $$(LIBUV_DEPS) $$(MKFILE_DEPS) $$(LIBUV_STAMP_$(1))
194         (cd $(S)src/libuv/ && \
195          $$(CFG_PYTHON) ./gyp_uv.py -f make -Dtarget_arch=$$(LIBUV_ARCH_$(1)) \
196            -D ninja \
197            -DOS=$$(LIBUV_OSTYPE_$(1)) \
198            -Goutput_dir=$$(@D) --generator-output $$(@D))
199         touch $$@
200
201 # Windows has a completely different build system for libuv because of mingw. In
202 # theory when we support msvc then we should be using gyp's msvc output instead
203 # of mingw's makefile for windows
204 ifdef CFG_WINDOWSY_$(1)
205 $$(LIBUV_LIB_$(1)): $$(LIBUV_DEPS) $$(MKFILE_DEPS)
206         $$(Q)$$(MAKE) -C $$(S)src/libuv -f Makefile.mingw \
207                 LDFLAGS="$$(CFG_GCCISH_LINK_FLAGS_$(1))" \
208                 CC="$$(CC_$(1)) $$(LIBUV_CFLAGS_$(1)) $$(SNAP_DEFINES)" \
209                 CXX="$$(CXX_$(1))" \
210                 AR="$$(AR_$(1))" \
211                 V=$$(VERBOSE)
212         $$(Q)cp $$(S)src/libuv/libuv.a $$@
213 else
214 $$(LIBUV_LIB_$(1)): $$(LIBUV_DIR_$(1))/Release/libuv.a $$(MKFILE_DEPS)
215         $$(Q)cp $$< $$@
216 $$(LIBUV_DIR_$(1))/Release/libuv.a: $$(LIBUV_DEPS) $$(LIBUV_MAKEFILE_$(1)) \
217                                     $$(MKFILE_DEPS)
218         $$(Q)$$(MAKE) -C $$(LIBUV_DIR_$(1)) \
219                 CFLAGS="$$(LIBUV_CFLAGS_$(1)) $$(SNAP_DEFINES)" \
220                 LDFLAGS="$$(CFG_GCCISH_LINK_FLAGS_$(1))" \
221                 CC="$$(CC_$(1))" \
222                 CXX="$$(CXX_$(1))" \
223                 AR="$$(AR_$(1))" \
224                 $$(LIBUV_ARGS_$(1)) \
225                 BUILDTYPE=Release \
226                 NO_LOAD="$$(LIBUV_NO_LOAD)" \
227                 V=$$(VERBOSE)
228         $$(Q)touch $$@
229
230 endif
231
232 ################################################################################
233 # jemalloc
234 ################################################################################
235
236 ifdef CFG_ENABLE_FAST_MAKE
237 JEMALLOC_DEPS := $(S)/.gitmodules
238 else
239 JEMALLOC_DEPS := $(wildcard \
240                    $(S)src/jemalloc/* \
241                    $(S)src/jemalloc/*/* \
242                    $(S)src/jemalloc/*/*/* \
243                    $(S)src/jemalloc/*/*/*/*)
244 endif
245
246 JEMALLOC_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc)
247 ifeq ($$(CFG_WINDOWSY_$(1)),1)
248   JEMALLOC_REAL_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc_s)
249 else
250   JEMALLOC_REAL_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc_pic)
251 endif
252 JEMALLOC_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(JEMALLOC_NAME_$(1))
253 JEMALLOC_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/jemalloc
254
255 $$(JEMALLOC_LIB_$(1)): $$(JEMALLOC_DEPS) $$(MKFILE_DEPS)
256         @$$(call E, make: jemalloc)
257         cd "$$(JEMALLOC_BUILD_DIR_$(1))"; "$(S)src/jemalloc/configure" \
258                 $$(JEMALLOC_ARGS_$(1)) --enable-cc-silence --with-jemalloc-prefix=je_ \
259                 --disable-experimental --build=$(CFG_BUILD) --host=$(1) \
260                 CC="$$(CC_$(1))" \
261                 AR="$$(AR_$(1))" \
262                 RANLIB="$$(AR_$(1)) s" \
263                 EXTRA_CFLAGS="$$(CFG_CFLAGS_$(1))"
264         $$(Q)$$(MAKE) -C "$$(JEMALLOC_BUILD_DIR_$(1))" build_lib_static
265         $$(Q)cp $$(JEMALLOC_BUILD_DIR_$(1))/lib/$$(JEMALLOC_REAL_NAME_$(1)) $$(JEMALLOC_LIB_$(1))
266
267 ################################################################################
268 # compiler-rt
269 ################################################################################
270
271 ifdef CFG_ENABLE_FAST_MAKE
272 COMPRT_DEPS := $(S)/.gitmodules
273 else
274 COMPRT_DEPS := $(wildcard \
275               $(S)src/compiler-rt/* \
276               $(S)src/compiler-rt/*/* \
277               $(S)src/compiler-rt/*/*/* \
278               $(S)src/compiler-rt/*/*/*/*)
279 endif
280
281 COMPRT_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),compiler-rt)
282 COMPRT_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(COMPRT_NAME_$(1))
283 COMPRT_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/compiler-rt
284
285 $$(COMPRT_LIB_$(1)): $$(COMPRT_DEPS) $$(MKFILE_DEPS)
286         @$$(call E, make: compiler-rt)
287         $$(Q)$$(MAKE) -C "$(S)src/compiler-rt" \
288                 ProjSrcRoot="$(S)src/compiler-rt" \
289                 ProjObjRoot="$$(abspath $$(COMPRT_BUILD_DIR_$(1)))" \
290                 CC="$$(CC_$(1))" \
291                 AR="$$(AR_$(1))" \
292                 RANLIB="$$(AR_$(1)) s" \
293                 CFLAGS="$$(CFG_GCCISH_CFLAGS_$(1))" \
294                 TargetTriple=$(1) \
295                 triple-builtins
296         $$(Q)cp $$(COMPRT_BUILD_DIR_$(1))/triple/builtins/libcompiler_rt.a $$(COMPRT_LIB_$(1))
297
298 ################################################################################
299 # libbacktrace
300 #
301 # We use libbacktrace on linux to get symbols in backtraces, but only on linux.
302 # Elsewhere we use other system utilities, so this library is only built on
303 # linux.
304 ################################################################################
305
306 BACKTRACE_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),backtrace)
307 BACKTRACE_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(BACKTRACE_NAME_$(1))
308 BACKTRACE_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/libbacktrace
309
310 ifeq ($$(findstring darwin,$$(OSTYPE_$(1))),darwin)
311
312 # We don't use this on platforms that aren't linux-based, so just make the file
313 # available, the compilation of libstd won't actually build it.
314 $$(BACKTRACE_LIB_$(1)):
315         touch $$@
316
317 else
318 ifeq ($$(CFG_WINDOWSY_$(1)),1)
319 $$(BACKTRACE_LIB_$(1)):
320         touch $$@
321 else
322
323 ifdef CFG_ENABLE_FAST_MAKE
324 BACKTRACE_DEPS := $(S)/.gitmodules
325 else
326 BACKTRACE_DEPS := $(wildcard $(S)src/libbacktrace/*)
327 endif
328
329 # We need to export CFLAGS because otherwise it doesn't pick up cross compile
330 # builds. If libbacktrace doesn't realize this, it will attempt to read 64-bit
331 # elf headers when compiled for a 32-bit system, yielding blank backtraces.
332 #
333 # This also removes the -Werror flag specifically to prevent errors during
334 # configuration.
335 #
336 # Down below you'll also see echos into the config.h generated by the
337 # ./configure script. This is done to force libbacktrace to *not* use the
338 # atomic/sync functionality because it pulls in unnecessary dependencies and we
339 # never use it anyway.
340 $$(BACKTRACE_BUILD_DIR_$(1))/Makefile: \
341                 export CFLAGS:=$$(CFG_GCCISH_CFLAGS_$(1):-Werror=) \
342                                 -fno-stack-protector
343 $$(BACKTRACE_BUILD_DIR_$(1))/Makefile: export CC:=$$(CC_$(1))
344 $$(BACKTRACE_BUILD_DIR_$(1))/Makefile: export AR:=$$(AR_$(1))
345 $$(BACKTRACE_BUILD_DIR_$(1))/Makefile: export RANLIB:=$$(AR_$(1)) s
346 $$(BACKTRACE_BUILD_DIR_$(1))/Makefile: $$(BACKTRACE_DEPS) $$(MKFILE_DEPS)
347         $$(Q)rm -rf $$(BACKTRACE_BUILD_DIR_$(1))
348         $$(Q)mkdir -p $$(BACKTRACE_BUILD_DIR_$(1))
349         $$(Q)(cd $$(BACKTRACE_BUILD_DIR_$(1)) && \
350               $(S)src/libbacktrace/configure --target=$(1) --host=$(CFG_BUILD))
351         $$(Q)echo '#undef HAVE_ATOMIC_FUNCTIONS' >> \
352               $$(BACKTRACE_BUILD_DIR_$(1))/config.h
353         $$(Q)echo '#undef HAVE_SYNC_FUNCTIONS' >> \
354               $$(BACKTRACE_BUILD_DIR_$(1))/config.h
355
356 $$(BACKTRACE_LIB_$(1)): $$(BACKTRACE_BUILD_DIR_$(1))/Makefile $$(MKFILE_DEPS)
357         @$$(call E, make: libbacktrace)
358         $$(Q)$$(MAKE) -C $$(BACKTRACE_BUILD_DIR_$(1)) \
359                 INCDIR=$(S)src/libbacktrace
360         $$(Q)cp $$(BACKTRACE_BUILD_DIR_$(1))/.libs/libbacktrace.a $$@
361
362 endif # endif for windowsy
363 endif # endif for darwin
364
365 endef
366
367 # Instantiate template for all stages/targets
368 $(foreach target,$(CFG_TARGET), \
369      $(eval $(call DEF_THIRD_PARTY_TARGETS,$(target))))