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 # $(1) is the target triple
41 define NATIVE_LIBRARIES
43 NATIVE_DEPS_hoedown_$(1) := hoedown/src/autolink.c \
44 hoedown/src/buffer.c \
45 hoedown/src/document.c \
46 hoedown/src/escape.c \
48 hoedown/src/html_blocks.c \
49 hoedown/src/html_smartypants.c \
52 NATIVE_DEPS_miniz_$(1) = miniz.c
53 NATIVE_DEPS_rust_test_helpers_$(1) := rust_test_helpers.c
55 ################################################################################
56 # You shouldn't find it that necessary to edit anything below this line.
57 ################################################################################
59 # While we're defining the native libraries for each target, we define some
60 # common rules used to build files for various targets.
62 RT_OUTPUT_DIR_$(1) := $(1)/rt
64 $$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.c $$(MKFILE_DEPS)
66 @$$(call E, compile: $$@)
67 $$(Q)$$(call CFG_COMPILE_C_$(1), $$@, \
68 $$(call CFG_CC_INCLUDE_$(1),$$(S)src/rt/hoedown/src) \
69 $$(call CFG_CC_INCLUDE_$(1),$$(S)src/rt) \
70 $$(RUNTIME_CFLAGS_$(1))) $$<
72 $$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.S $$(MKFILE_DEPS) \
73 $$(LLVM_CONFIG_$$(CFG_BUILD))
75 @$$(call E, compile: $$@)
76 $$(Q)$$(call CFG_ASSEMBLE_$(1),$$@,$$<)
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.
82 ifeq ($$(findstring msvc,$(1)),msvc)
83 $$(RT_OUTPUT_DIR_$(1))/%.o: \
84 export INCLUDE := $$(CFG_MSVC_INCLUDE_PATH_$$(HOST_$(1)))
86 export INCLUDE := $$(CFG_MSVC_INCLUDE_PATH_$$(HOST_$(1)))
90 $(foreach target,$(CFG_TARGET),$(eval $(call NATIVE_LIBRARIES,$(target))))
92 # A macro for devining how to build third party libraries listed above (based
93 # on their dependencies).
96 # $(2) is the lib name
97 define THIRD_PARTY_LIB
99 OBJS_$(2)_$(1) := $$(NATIVE_DEPS_$(2)_$(1):%=$$(RT_OUTPUT_DIR_$(1))/%)
100 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.c=.o)
101 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.cpp=.o)
102 OBJS_$(2)_$(1) := $$(OBJS_$(2)_$(1):.S=.o)
103 NATIVE_$(2)_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),$(2))
104 $$(RT_OUTPUT_DIR_$(1))/$$(NATIVE_$(2)_$(1)): $$(OBJS_$(2)_$(1))
105 @$$(call E, link: $$@)
106 $$(Q)$$(call CFG_CREATE_ARCHIVE_$(1),$$@) $$^
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)))))
117 ################################################################################
118 # Building third-party targets with external build systems
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 ################################################################################
125 define DEF_THIRD_PARTY_TARGETS
127 # $(1) is the target triple
129 ifeq ($$(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/
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.
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...
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
147 JEMALLOC_ARGS_$(1) := --disable-lazy-lock
148 else ifeq ($(OSTYPE_$(1)), apple-ios)
149 JEMALLOC_ARGS_$(1) := --disable-tls
150 else ifeq ($(findstring android, $(OSTYPE_$(1))), android)
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.
157 # If the test suite passes, however, without symbol prefixes then we should be
159 JEMALLOC_ARGS_$(1) := --disable-tls --with-jemalloc-prefix=je_
160 else ifeq ($(findstring dragonfly, $(OSTYPE_$(1))), dragonfly)
161 JEMALLOC_ARGS_$(1) := --with-jemalloc-prefix=je_
164 ifdef CFG_ENABLE_DEBUG_JEMALLOC
165 JEMALLOC_ARGS_$(1) += --enable-debug --enable-fill
168 ################################################################################
170 ################################################################################
172 ifdef CFG_ENABLE_FAST_MAKE
173 JEMALLOC_DEPS := $(S)/.gitmodules
175 JEMALLOC_DEPS := $(wildcard \
177 $(S)src/jemalloc/*/* \
178 $(S)src/jemalloc/*/*/* \
179 $(S)src/jemalloc/*/*/*/*)
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.
184 JEMALLOC_DEPS := $(filter-out $(S)src/jemalloc/VERSION,$(JEMALLOC_DEPS))
186 JEMALLOC_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc)
187 ifeq ($$(CFG_WINDOWSY_$(1)),1)
188 JEMALLOC_REAL_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc_s)
190 JEMALLOC_REAL_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),jemalloc_pic)
192 JEMALLOC_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(JEMALLOC_NAME_$(1))
193 JEMALLOC_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/jemalloc
194 JEMALLOC_LOCAL_$(1) := $$(JEMALLOC_BUILD_DIR_$(1))/lib/$$(JEMALLOC_REAL_NAME_$(1))
196 $$(JEMALLOC_LOCAL_$(1)): $$(JEMALLOC_DEPS) $$(MKFILE_DEPS)
197 @$$(call E, make: jemalloc)
198 cd "$$(JEMALLOC_BUILD_DIR_$(1))"; "$(S)src/jemalloc/configure" \
199 $$(JEMALLOC_ARGS_$(1)) $(CFG_JEMALLOC_FLAGS) \
200 --build=$$(CFG_GNU_TRIPLE_$(CFG_BUILD)) --host=$$(CFG_GNU_TRIPLE_$(1)) \
201 CC="$$(CC_$(1)) $$(CFG_JEMALLOC_CFLAGS_$(1))" \
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
208 ifeq ($(1),$$(CFG_BUILD))
209 ifneq ($$(CFG_JEMALLOC_ROOT),)
210 $$(JEMALLOC_LIB_$(1)): $$(CFG_JEMALLOC_ROOT)/libjemalloc_pic.a
211 @$$(call E, copy: jemalloc)
214 $$(JEMALLOC_LIB_$(1)): $$(JEMALLOC_LOCAL_$(1))
218 $$(JEMALLOC_LIB_$(1)): $$(JEMALLOC_LOCAL_$(1))
222 ################################################################################
224 ################################################################################
226 ifdef CFG_ENABLE_FAST_MAKE
227 COMPRT_DEPS := $(S)/.gitmodules
229 COMPRT_DEPS := $(wildcard \
230 $(S)src/compiler-rt/* \
231 $(S)src/compiler-rt/*/* \
232 $(S)src/compiler-rt/*/*/* \
233 $(S)src/compiler-rt/*/*/*/*)
236 # compiler-rt's build system is a godawful mess. Here we figure out
237 # the ridiculous platform-specific values and paths necessary to get
238 # useful artifacts out of it.
240 COMPRT_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),compiler-rt)
241 COMPRT_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(COMPRT_NAME_$(1))
242 COMPRT_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/compiler-rt
244 COMPRT_ARCH_$(1) := $$(word 1,$$(subst -, ,$(1)))
246 # All this is to figure out the path to the compiler-rt bin
247 ifeq ($$(findstring windows-msvc,$(1)),windows-msvc)
248 COMPRT_DIR_$(1) := windows/Release
249 COMPRT_LIB_NAME_$(1) := clang_rt.builtins-$$(patsubst i%86,i386,$$(COMPRT_ARCH_$(1)))
252 ifeq ($$(findstring windows-gnu,$(1)),windows-gnu)
253 COMPRT_DIR_$(1) := windows
254 COMPRT_LIB_NAME_$(1) := clang_rt.builtins-$$(COMPRT_ARCH_$(1))
257 ifeq ($$(findstring darwin,$(1)),darwin)
258 COMPRT_DIR_$(1) := builtins
259 COMPRT_LIB_NAME_$(1) := clang_rt.builtins_$$(patsubst i686,i386,$$(COMPRT_ARCH_$(1)))_osx
262 ifeq ($$(findstring ios,$(1)),ios)
263 COMPRT_DIR_$(1) := builtins
264 COMPRT_ARCH_$(1) := $$(patsubst armv7s,armv7em,$$(COMPRT_ARCH_$(1)))
265 COMPRT_LIB_NAME_$(1) := clang_rt.hard_pic_$$(COMPRT_ARCH_$(1))_macho_embedded
266 ifeq ($$(COMPRT_ARCH_$(1)),aarch64)
267 COMPRT_LIB_NAME_$(1) := clang_rt.builtins_arm64_ios
269 COMPRT_DEFINES_$(1) := -DCOMPILER_RT_ENABLE_IOS=ON
272 ifndef COMPRT_DIR_$(1)
273 # NB: FreeBSD and NetBSD output to "linux"...
274 COMPRT_DIR_$(1) := linux
275 COMPRT_ARCH_$(1) := $$(patsubst i586,i386,$$(COMPRT_ARCH_$(1)))
277 ifeq ($$(findstring android,$(1)),android)
278 ifeq ($$(findstring arm,$$(COMPRT_ARCH_$(1))),arm)
279 COMPRT_ARCH_$(1) := armhf
283 ifeq ($$(findstring eabihf,$(1)),eabihf)
284 ifeq ($$(findstring armv7,$(1)),)
285 COMPRT_LIB_NAME_$(1) := clang_rt.builtins-armhf
289 ifndef COMPRT_LIB_NAME_$(1)
290 COMPRT_LIB_NAME_$(1) := clang_rt.builtins-$$(COMPRT_ARCH_$(1))
295 ifeq ($$(findstring windows-gnu,$(1)),windows-gnu)
296 COMPRT_LIB_FILE_$(1) := lib$$(COMPRT_LIB_NAME_$(1)).a
299 ifeq ($$(findstring android,$(1)),android)
300 ifeq ($$(findstring arm,$(1)),arm)
301 COMPRT_LIB_FILE_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),$$(COMPRT_LIB_NAME_$(1))-android)
305 ifndef COMPRT_LIB_FILE_$(1)
306 COMPRT_LIB_FILE_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),$$(COMPRT_LIB_NAME_$(1)))
309 COMPRT_OUTPUT_$(1) := $$(COMPRT_BUILD_DIR_$(1))/lib/$$(COMPRT_DIR_$(1))/$$(COMPRT_LIB_FILE_$(1))
311 ifeq ($$(findstring windows-msvc,$(1)),windows-msvc)
312 COMPRT_BUILD_ARGS_$(1) := //v:m //nologo
313 COMPRT_BUILD_TARGET_$(1) := lib/builtins/builtins
314 COMPRT_BUILD_CC_$(1) :=
316 COMPRT_BUILD_ARGS_$(1) :=
317 ifndef COMPRT_BUILD_TARGET_$(1)
318 COMPRT_BUILD_TARGET_$(1) := $$(COMPRT_LIB_NAME_$(1))
320 COMPRT_BUILD_CC_$(1) := -DCMAKE_C_COMPILER=$$(call FIND_COMPILER,$$(CC_$(1))) \
321 -DCMAKE_CXX_COMPILER=$$(call FIND_COMPILER,$$(CXX_$(1)))
323 ifeq ($$(findstring ios,$(1)),)
324 COMPRT_BUILD_CC_$(1) := $$(COMPRT_BUILD_CC_$(1)) \
325 -DCMAKE_C_FLAGS="$$(CFG_GCCISH_CFLAGS_$(1)) -Wno-error"
330 ifeq ($$(findstring emscripten,$(1)),emscripten)
332 # FIXME: emscripten doesn't use compiler-rt and can't build it without
339 $$(COMPRT_LIB_$(1)): $$(COMPRT_DEPS) $$(MKFILE_DEPS) $$(LLVM_CONFIG_$$(CFG_BUILD))
340 @$$(call E, cmake: compiler-rt)
341 $$(Q)rm -rf $$(COMPRT_BUILD_DIR_$(1))
342 $$(Q)mkdir $$(COMPRT_BUILD_DIR_$(1))
343 $$(Q)cd "$$(COMPRT_BUILD_DIR_$(1))"; \
344 $$(CFG_CMAKE) "$(S)src/compiler-rt" \
345 -DCMAKE_BUILD_TYPE=$$(LLVM_BUILD_CONFIG_MODE) \
346 -DLLVM_CONFIG_PATH=$$(LLVM_CONFIG_$$(CFG_BUILD)) \
347 -DCOMPILER_RT_DEFAULT_TARGET_TRIPLE=$(1) \
348 -DCOMPILER_RT_BUILD_SANITIZERS=OFF \
349 -DCOMPILER_RT_BUILD_EMUTLS=OFF \
350 $$(COMPRT_DEFINES_$(1)) \
351 $$(COMPRT_BUILD_CC_$(1)) \
352 -G"$$(CFG_CMAKE_GENERATOR)"
353 $$(Q)$$(CFG_CMAKE) --build "$$(COMPRT_BUILD_DIR_$(1))" \
354 --target $$(COMPRT_BUILD_TARGET_$(1)) \
355 --config $$(LLVM_BUILD_CONFIG_MODE) \
356 -- $$(COMPRT_BUILD_ARGS_$(1)) $$(MFLAGS)
357 $$(Q)cp "$$(COMPRT_OUTPUT_$(1))" $$@
361 ################################################################################
364 # We use libbacktrace on linux to get symbols in backtraces, but only on linux.
365 # Elsewhere we use other system utilities, so this library is only built on
367 ################################################################################
369 BACKTRACE_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),backtrace)
370 BACKTRACE_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/$$(BACKTRACE_NAME_$(1))
371 BACKTRACE_BUILD_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/libbacktrace
373 # We don't use this on platforms that aren't linux-based (with the exception of
374 # msys2/mingw builds on windows, which use it to read the dwarf debug
375 # information) so just make the file available, the compilation of libstd won't
377 ifeq ($$(findstring darwin,$$(OSTYPE_$(1))),darwin)
379 $$(BACKTRACE_LIB_$(1)):
382 else ifeq ($$(findstring ios,$$(OSTYPE_$(1))),ios)
384 $$(BACKTRACE_LIB_$(1)):
386 else ifeq ($$(findstring msvc,$(1)),msvc)
388 $$(BACKTRACE_LIB_$(1)):
390 else ifeq ($$(findstring emscripten,$(1)),emscripten)
391 # FIXME: libbacktrace doesn't understand the emscripten triple
392 $$(BACKTRACE_LIB_$(1)):
396 ifdef CFG_ENABLE_FAST_MAKE
397 BACKTRACE_DEPS := $(S)/.gitmodules
399 BACKTRACE_DEPS := $(wildcard $(S)src/libbacktrace/*)
402 # We need to export CFLAGS because otherwise it doesn't pick up cross compile
403 # builds. If libbacktrace doesn't realize this, it will attempt to read 64-bit
404 # elf headers when compiled for a 32-bit system, yielding blank backtraces.
406 # This also removes the -Werror flag specifically to prevent errors during
409 # Down below you'll also see echos into the config.h generated by the
410 # ./configure script. This is done to force libbacktrace to *not* use the
411 # atomic/sync functionality because it pulls in unnecessary dependencies and we
412 # never use it anyway.
414 # We also use `env PWD=` to clear the PWD environment variable, and then
415 # execute the command in a new shell. This is necessary to workaround a
416 # buildbot/msys2 bug: the shell is launched with PWD set to a windows-style path,
417 # which results in all further uses of `pwd` also printing a windows-style path,
418 # which breaks libbacktrace's configure script. Clearing PWD within the same
419 # shell is not sufficient.
421 $$(BACKTRACE_BUILD_DIR_$(1))/Makefile: $$(BACKTRACE_DEPS) $$(MKFILE_DEPS)
422 @$$(call E, configure: libbacktrace for $(1))
423 $$(Q)rm -rf $$(BACKTRACE_BUILD_DIR_$(1))
424 $$(Q)mkdir -p $$(BACKTRACE_BUILD_DIR_$(1))
425 $$(Q)(cd $$(BACKTRACE_BUILD_DIR_$(1)) && env \
429 RANLIB="$$(AR_$(1)) s" \
430 CFLAGS="$$(CFG_GCCISH_CFLAGS_$(1)) -Wno-error -fno-stack-protector" \
431 $(S)src/libbacktrace/configure --build=$(CFG_GNU_TRIPLE_$(CFG_BUILD)) --host=$(CFG_GNU_TRIPLE_$(1)))
432 $$(Q)echo '#undef HAVE_ATOMIC_FUNCTIONS' >> \
433 $$(BACKTRACE_BUILD_DIR_$(1))/config.h
434 $$(Q)echo '#undef HAVE_SYNC_FUNCTIONS' >> \
435 $$(BACKTRACE_BUILD_DIR_$(1))/config.h
437 $$(BACKTRACE_LIB_$(1)): $$(BACKTRACE_BUILD_DIR_$(1))/Makefile $$(MKFILE_DEPS)
438 @$$(call E, make: libbacktrace)
439 $$(Q)$$(MAKE) -C $$(BACKTRACE_BUILD_DIR_$(1)) \
440 INCDIR=$(S)src/libbacktrace
441 $$(Q)cp $$(BACKTRACE_BUILD_DIR_$(1))/.libs/libbacktrace.a $$@
445 ################################################################################
446 # libc/libunwind for musl
448 # When we're building a musl-like target we're going to link libc/libunwind
449 # statically into the standard library and liblibc, so we need to make sure
450 # they're in a location that we can find
451 ################################################################################
453 ifeq ($$(findstring musl,$(1)),musl)
454 $$(RT_OUTPUT_DIR_$(1))/%: $$(CFG_MUSL_ROOT)/lib/%
457 # Ask gcc where it is
458 $$(RT_OUTPUT_DIR_$(1))/%:
459 cp $$(shell $$(CC_$(1)) -print-file-name=$$(@F)) $$@
464 # Instantiate template for all stages/targets
465 $(foreach target,$(CFG_TARGET), \
466 $(eval $(call DEF_THIRD_PARTY_TARGETS,$(target))))