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