]> git.lizzy.rs Git - rust.git/blob - mk/platform.mk
Auto merge of #27630 - sylvestre:master, r=dotdash
[rust.git] / mk / platform.mk
1 # Copyright 2012-2015 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 # Create variables HOST_<triple> containing the host part
13 # of each target triple.  For example, the triple i686-darwin-macos
14 # would create a variable HOST_i686-darwin-macos with the value
15 # i386.
16 define DEF_HOST_VAR
17   HOST_$(1) = $(subst i686,i386,$(word 1,$(subst -, ,$(1))))
18 endef
19 $(foreach t,$(CFG_TARGET),$(eval $(call DEF_HOST_VAR,$(t))))
20 $(foreach t,$(CFG_TARGET),$(info cfg: host for $(t) is $(HOST_$(t))))
21
22 # Ditto for OSTYPE
23 define DEF_OSTYPE_VAR
24   OSTYPE_$(1) = $(subst $(firstword $(subst -, ,$(1)))-,,$(1))
25 endef
26 $(foreach t,$(CFG_TARGET),$(eval $(call DEF_OSTYPE_VAR,$(t))))
27 $(foreach t,$(CFG_TARGET),$(info cfg: os for $(t) is $(OSTYPE_$(t))))
28
29 # On Darwin, we need to run dsymutil so the debugging information ends
30 # up in the right place.  On other platforms, it automatically gets
31 # embedded into the executable, so use a no-op command.
32 CFG_DSYMUTIL := true
33
34 # Hack: not sure how to test if a file exists in make other than this
35 OS_SUPP = $(patsubst %,--suppressions=%, \
36       $(wildcard $(CFG_SRC_DIR)src/etc/$(CFG_OSTYPE).supp*))
37
38 ifdef CFG_DISABLE_OPTIMIZE_CXX
39   $(info cfg: disabling C++ optimization (CFG_DISABLE_OPTIMIZE_CXX))
40   CFG_GCCISH_CFLAGS += -O0
41 else
42   CFG_GCCISH_CFLAGS += -O2
43 endif
44
45 # The soname thing is for supporting a statically linked jemalloc.
46 # see https://blog.mozilla.org/jseward/2012/06/05/valgrind-now-supports-jemalloc-builds-directly/
47 ifdef CFG_VALGRIND
48   CFG_VALGRIND += --error-exitcode=100 \
49                   --fair-sched=try \
50                   --quiet \
51                   --soname-synonyms=somalloc=NONE \
52                   --suppressions=$(CFG_SRC_DIR)src/etc/x86.supp \
53                   $(OS_SUPP)
54   ifdef CFG_ENABLE_HELGRIND
55     CFG_VALGRIND += --tool=helgrind
56   else
57     CFG_VALGRIND += --tool=memcheck \
58                     --leak-check=full
59   endif
60 endif
61
62 # If we actually want to run Valgrind on a given platform, set this variable
63 define DEF_GOOD_VALGRIND
64   ifeq ($(OSTYPE_$(1)),unknown-linux-gnu)
65     GOOD_VALGRIND_$(1) = 1
66   endif
67   ifneq (,$(filter $(OSTYPE_$(1)),darwin freebsd))
68     ifeq (HOST_$(1),x86_64)
69       GOOD_VALGRIND_$(1) = 1
70     endif
71   endif
72 endef
73 $(foreach t,$(CFG_TARGET),$(eval $(call DEF_GOOD_VALGRIND,$(t))))
74 $(foreach t,$(CFG_TARGET),$(info cfg: good valgrind for $(t) is $(GOOD_VALGRIND_$(t))))
75
76 ifneq ($(findstring linux,$(CFG_OSTYPE)),)
77   ifdef CFG_PERF
78     ifneq ($(CFG_PERF_WITH_LOGFD),)
79         CFG_PERF_TOOL := $(CFG_PERF) stat -r 3 --log-fd 2
80     else
81         CFG_PERF_TOOL := $(CFG_PERF) stat -r 3
82     endif
83   else
84     ifdef CFG_VALGRIND
85       CFG_PERF_TOOL := \
86         $(CFG_VALGRIND) --tool=cachegrind --cache-sim=yes --branch-sim=yes
87     else
88       CFG_PERF_TOOL := /usr/bin/time --verbose
89     endif
90   endif
91 endif
92
93 AR := ar
94
95 define SET_FROM_CFG
96   ifdef CFG_$(1)
97     ifeq ($(origin $(1)),undefined)
98       $$(info cfg: using $(1)=$(CFG_$(1)) (CFG_$(1)))
99       $(1)=$(CFG_$(1))
100     endif
101     ifeq ($(origin $(1)),default)
102       $$(info cfg: using $(1)=$(CFG_$(1)) (CFG_$(1)))
103       $(1)=$(CFG_$(1))
104     endif
105   endif
106 endef
107
108 $(foreach cvar,CC CXX CPP CFLAGS CXXFLAGS CPPFLAGS, \
109   $(eval $(call SET_FROM_CFG,$(cvar))))
110
111 CFG_RLIB_GLOB=lib$(1)-*.rlib
112
113 include $(wildcard $(CFG_SRC_DIR)mk/cfg/*.mk)
114
115 define ADD_INSTALLED_OBJECTS
116   INSTALLED_OBJECTS_$(1) += $$(call CFG_STATIC_LIB_NAME_$(1),compiler-rt)
117 endef
118
119 $(foreach target,$(CFG_TARGET), \
120   $(eval $(call ADD_INSTALLED_OBJECTS,$(target))))
121
122 define DEFINE_LINKER
123   ifndef LINK_$(1)
124     LINK_$(1) := $$(CC_$(1))
125   endif
126 endef
127
128 $(foreach target,$(CFG_TARGET), \
129   $(eval $(call DEFINE_LINKER,$(target))))
130
131 # The -Qunused-arguments sidesteps spurious warnings from clang
132 define FILTER_FLAGS
133   ifeq ($$(CFG_USING_CLANG),1)
134     ifneq ($(findstring clang,$$(shell $(CC_$(1)) -v)),)
135       CFG_GCCISH_CFLAGS_$(1) += -Qunused-arguments
136       CFG_GCCISH_CXXFLAGS_$(1) += -Qunused-arguments
137     endif
138   endif
139 endef
140
141 $(foreach target,$(CFG_TARGET), \
142   $(eval $(call FILTER_FLAGS,$(target))))
143
144 # Configure various macros to pass gcc or cl.exe style arguments
145 define CC_MACROS
146   CFG_CC_INCLUDE_$(1)=-I $$(1)
147   ifeq ($$(findstring msvc,$(1)),msvc)
148     CFG_CC_OUTPUT_$(1)=-Fo:$$(1)
149     CFG_CREATE_ARCHIVE_$(1)=$$(AR_$(1)) -OUT:$$(1)
150   else
151     CFG_CC_OUTPUT_$(1)=-o $$(1)
152     CFG_CREATE_ARCHIVE_$(1)=$$(AR_$(1)) crus $$(1)
153   endif
154 endef
155
156 $(foreach target,$(CFG_TARGET), \
157   $(eval $(call CC_MACROS,$(target))))
158
159
160 ifeq ($(CFG_CCACHE_CPP2),1)
161   CCACHE_CPP2=1
162   export CCACHE_CPP
163 endif
164
165 ifdef CFG_CCACHE_BASEDIR
166   CCACHE_BASEDIR=$(CFG_CCACHE_BASEDIR)
167   export CCACHE_BASEDIR
168 endif
169
170 FIND_COMPILER = $(word 1,$(1:ccache=))
171
172 define CFG_MAKE_TOOLCHAIN
173   # Prepend the tools with their prefix if cross compiling
174   ifneq ($(CFG_BUILD),$(1))
175     ifneq ($$(findstring msvc,$(1)),msvc)
176        CC_$(1)=$(CROSS_PREFIX_$(1))$(CC_$(1))
177        CXX_$(1)=$(CROSS_PREFIX_$(1))$(CXX_$(1))
178        CPP_$(1)=$(CROSS_PREFIX_$(1))$(CPP_$(1))
179        AR_$(1)=$(CROSS_PREFIX_$(1))$(AR_$(1))
180        LINK_$(1)=$(CROSS_PREFIX_$(1))$(LINK_$(1))
181        RUSTC_CROSS_FLAGS_$(1)=-C linker=$$(call FIND_COMPILER,$$(LINK_$(1))) \
182            -C ar=$$(call FIND_COMPILER,$$(AR_$(1))) $(RUSTC_CROSS_FLAGS_$(1))
183
184        RUSTC_FLAGS_$(1)=$$(RUSTC_CROSS_FLAGS_$(1)) $(RUSTC_FLAGS_$(1))
185     endif
186   endif
187
188   CFG_COMPILE_C_$(1) = $$(CC_$(1)) \
189         $$(CFG_GCCISH_CFLAGS) \
190         $$(CFG_GCCISH_CFLAGS_$(1)) \
191         -c $$(call CFG_CC_OUTPUT_$(1),$$(1)) $$(2)
192   CFG_LINK_C_$(1) = $$(CC_$(1)) \
193         $$(CFG_GCCISH_LINK_FLAGS) -o $$(1) \
194         $$(CFG_GCCISH_LINK_FLAGS_$(1)) \
195         $$(CFG_GCCISH_DEF_FLAG_$(1))$$(3) $$(2) \
196         $$(call CFG_INSTALL_NAME_$(1),$$(4))
197   CFG_COMPILE_CXX_$(1) = $$(CXX_$(1)) \
198         $$(CFG_GCCISH_CFLAGS) \
199         $$(CFG_GCCISH_CXXFLAGS) \
200         $$(CFG_GCCISH_CFLAGS_$(1)) \
201         $$(CFG_GCCISH_CXXFLAGS_$(1)) \
202         -c $$(call CFG_CC_OUTPUT_$(1),$$(1)) $$(2)
203   CFG_LINK_CXX_$(1) = $$(CXX_$(1)) \
204         $$(CFG_GCCISH_LINK_FLAGS) -o $$(1) \
205         $$(CFG_GCCISH_LINK_FLAGS_$(1)) \
206         $$(CFG_GCCISH_DEF_FLAG_$(1))$$(3) $$(2) \
207         $$(call CFG_INSTALL_NAME_$(1),$$(4))
208
209   ifeq ($$(findstring $(HOST_$(1)),arm aarch64 mips mipsel powerpc),)
210
211   # On Bitrig, we need the relocation model to be PIC for everything
212   ifeq (,$(filter $(OSTYPE_$(1)),bitrig))
213     LLVM_MC_RELOCATION_MODEL="pic"
214   else
215     LLVM_MC_RELOCATION_MODEL="default"
216   endif
217
218   # We're using llvm-mc as our assembler because it supports
219   # .cfi pseudo-ops on mac
220   CFG_ASSEMBLE_$(1)=$$(CPP_$(1)) -E $$(2) | \
221                     $$(LLVM_MC_$$(CFG_BUILD)) \
222                     -assemble \
223                     -relocation-model=$$(LLVM_MC_RELOCATION_MODEL) \
224                     -filetype=obj \
225                     -triple=$(1) \
226                     -o=$$(1)
227   else
228
229   # For the ARM, AARCH64, MIPS and POWER crosses, use the toolchain assembler
230   # FIXME: We should be able to use the LLVM assembler
231   CFG_ASSEMBLE_$(1)=$$(CC_$(1)) $$(CFG_GCCISH_CFLAGS_$(1)) \
232                    $$(2) -c -o $$(1)
233
234   endif
235
236 endef
237
238 $(foreach target,$(CFG_TARGET), \
239   $(eval $(call CFG_MAKE_TOOLCHAIN,$(target))))
240
241 # There are more comments about this available in the target specification for
242 # Windows MSVC in the compiler, but the gist of it is that we use `llvm-ar.exe`
243 # instead of `lib.exe` for assembling archives, so we need to inject this custom
244 # dependency here.
245 #
246 # FIXME(stage0): remove this and all other relevant support in the makefiles
247 #                after a snapshot is made
248 define ADD_LLVM_AR_TO_MSVC_DEPS
249 ifeq ($$(findstring msvc,$(1)),msvc)
250 NATIVE_TOOL_DEPS_core_T_$(1) += llvm-ar.exe
251 INSTALLED_BINS_$(1) += llvm-ar.exe
252 endif
253 endef
254
255 $(foreach target,$(CFG_TARGET), \
256   $(eval $(call ADD_LLVM_AR_TO_MSVC_DEPS,$(target))))
257
258 # When working with MSVC on windows, each DLL needs to explicitly declare its
259 # interface to the outside world through some means. The options for doing so
260 # include:
261 #
262 # 1. A custom attribute on each function itself
263 # 2. A linker argument saying what to export
264 # 3. A file which lists all symbols that need to be exported
265 #
266 # The Rust compiler takes care (1) for us for all Rust code by annotating all
267 # public-facing functions with dllexport, but we have a few native dependencies
268 # which need to cross the DLL boundary. The most important of these dependencies
269 # is LLVM which is linked into `rustc_llvm.dll` but primarily used from
270 # `rustc_trans.dll`. This means that many of LLVM's C API functions need to be
271 # exposed from `rustc_llvm.dll` to be forwarded over the boundary.
272 #
273 # Unfortunately, at this time, LLVM does not handle this sort of exportation on
274 # Windows for us, so we're forced to do it ourselves if we want it (which seems
275 # like the path of least resistance right now). To do this we generate a `.DEF`
276 # file [1] which we then custom-pass to the linker when building the rustc_llvm
277 # crate. This DEF file list all symbols that are exported from
278 # `src/librustc_llvm/lib.rs` and is generated by a small python script.
279 #
280 # Fun times!
281 #
282 # [1]: https://msdn.microsoft.com/en-us/library/28d6s79h.aspx
283 #
284 # FIXME(stage0): remove this macro and the usage below (and the commments above)
285 #                when a new snapshot is available. Also remove the
286 #                RUSTFLAGS$(1)_.._T_ variable in mk/target.mk along with
287 #                CUSTOM_DEPS (as they were only added for this)
288 define ADD_RUSTC_LLVM_DEF_TO_MSVC
289 ifeq ($$(findstring msvc,$(1)),msvc)
290 RUSTFLAGS0_rustc_llvm_T_$(1) += -C link-args="-DEF:$(1)/rt/rustc_llvm.def"
291 CUSTOM_DEPS0_rustc_llvm_T_$(1) += $(1)/rt/rustc_llvm.def
292
293 $(1)/rt/rustc_llvm.def: $$(S)src/etc/mklldef.py $$(S)src/librustc_llvm/lib.rs
294         $$(CFG_PYTHON) $$^ $$@ rustc_llvm-$$(CFG_FILENAME_EXTRA)
295 endif
296 endef
297
298 $(foreach target,$(CFG_TARGET), \
299   $(eval $(call ADD_RUSTC_LLVM_DEF_TO_MSVC,$(target))))
300