]> git.lizzy.rs Git - rust.git/blob - mk/target.mk
rollup merge of #27618: dotdash/drop_fixes
[rust.git] / mk / target.mk
1 # Copyright 2012 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 # This is the compile-time target-triple for the compiler. For the compiler at
12 # runtime, this should be considered the host-triple. More explanation for why
13 # this exists can be found on issue #2400
14 export CFG_COMPILER_HOST_TRIPLE
15
16 # The standard libraries should be held up to a higher standard than any old
17 # code, make sure that these common warnings are denied by default. These can
18 # be overridden during development temporarily. For stage0, we allow warnings
19 # which may be bugs in stage0 (should be fixed in stage1+)
20 RUST_LIB_FLAGS_ST0 += -W warnings
21 RUST_LIB_FLAGS_ST1 += -D warnings
22 RUST_LIB_FLAGS_ST2 += -D warnings
23
24 # Macro that generates the full list of dependencies for a crate at a particular
25 # stage/target/host tuple.
26 #
27 # $(1) - stage
28 # $(2) - target
29 # $(3) - host
30 # $(4) crate
31 define RUST_CRATE_FULLDEPS
32 CRATE_FULLDEPS_$(1)_T_$(2)_H_$(3)_$(4) := \
33                 $$(CRATEFILE_$(4)) \
34                 $$(RSINPUTS_$(4)) \
35                 $$(foreach dep,$$(RUST_DEPS_$(4)), \
36                   $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep)) \
37                 $$(foreach dep,$$(NATIVE_DEPS_$(4)), \
38                   $$(RT_OUTPUT_DIR_$(2))/$$(call CFG_STATIC_LIB_NAME_$(2),$$(dep))) \
39                 $$(foreach dep,$$(NATIVE_DEPS_$(4)_T_$(2)), \
40                   $$(RT_OUTPUT_DIR_$(2))/$$(dep)) \
41                 $$(foreach dep,$$(NATIVE_TOOL_DEPS_$(4)_T_$(2)), \
42                   $$(TBIN$(1)_T_$(3)_H_$(3))/$$(dep)) \
43                 $$(CUSTOM_DEPS$(1)_$(4)_T_$(2))
44 endef
45
46 $(foreach host,$(CFG_HOST), \
47  $(foreach target,$(CFG_TARGET), \
48   $(foreach stage,$(STAGES), \
49    $(foreach crate,$(CRATES), \
50     $(eval $(call RUST_CRATE_FULLDEPS,$(stage),$(target),$(host),$(crate)))))))
51
52 # RUST_TARGET_STAGE_N template: This defines how target artifacts are built
53 # for all stage/target architecture combinations. This is one giant rule which
54 # works as follows:
55 #
56 #   1. The immediate dependencies are the rust source files
57 #   2. Each rust crate dependency is listed (based on their stamp files),
58 #      as well as all native dependencies (listed in RT_OUTPUT_DIR)
59 #   3. The stage (n-1) compiler is required through the TSREQ dependency
60 #   4. When actually executing the rule, the first thing we do is to clean out
61 #      old libs and rlibs via the REMOVE_ALL_OLD_GLOB_MATCHES macro
62 #   5. Finally, we get around to building the actual crate. It's just one
63 #      "small" invocation of the previous stage rustc. We use -L to
64 #      RT_OUTPUT_DIR so all the native dependencies are picked up.
65 #      Additionally, we pass in the llvm dir so rustc can link against it.
66 #   6. Some cleanup is done (listing what was just built) if verbose is turned
67 #      on.
68 #
69 # $(1) is the stage
70 # $(2) is the target triple
71 # $(3) is the host triple
72 # $(4) is the crate name
73 define RUST_TARGET_STAGE_N
74
75 $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$(4): CFG_COMPILER_HOST_TRIPLE = $(2)
76 $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$(4): \
77                 $$(CRATEFILE_$(4)) \
78                 $$(CRATE_FULLDEPS_$(1)_T_$(2)_H_$(3)_$(4)) \
79                 $$(LLVM_CONFIG_$(2)) \
80                 $$(TSREQ$(1)_T_$(2)_H_$(3)) \
81                 | $$(TLIB$(1)_T_$(2)_H_$(3))/
82         @$$(call E, rustc: $$(@D)/lib$(4))
83         @touch $$@.start_time
84         $$(call REMOVE_ALL_OLD_GLOB_MATCHES, \
85             $$(dir $$@)$$(call CFG_LIB_GLOB_$(2),$(4)))
86         $$(call REMOVE_ALL_OLD_GLOB_MATCHES, \
87             $$(dir $$@)$$(call CFG_RLIB_GLOB,$(4)))
88         $(Q)CFG_LLVM_LINKAGE_FILE=$$(LLVM_LINKAGE_PATH_$(2)) \
89             $$(subst @,,$$(STAGE$(1)_T_$(2)_H_$(3))) \
90                 $$(RUST_LIB_FLAGS_ST$(1)) \
91                 -L "$$(RT_OUTPUT_DIR_$(2))" \
92                 $$(LLVM_LIBDIR_RUSTFLAGS_$(2)) \
93                 $$(LLVM_STDCPP_RUSTFLAGS_$(2)) \
94                 $$(RUSTFLAGS_$(4)) \
95                 $$(RUSTFLAGS$(1)_$(4)_T_$(2)) \
96                 --out-dir $$(@D) \
97                 -C extra-filename=-$$(CFG_FILENAME_EXTRA) \
98                 $$<
99         @touch -r $$@.start_time $$@ && rm $$@.start_time
100         $$(call LIST_ALL_OLD_GLOB_MATCHES, \
101             $$(dir $$@)$$(call CFG_LIB_GLOB_$(2),$(4)))
102         $$(call LIST_ALL_OLD_GLOB_MATCHES, \
103             $$(dir $$@)$$(call CFG_RLIB_GLOB,$(4)))
104
105 endef
106
107 # Macro for building any tool as part of the rust compilation process. Each
108 # tool is defined in crates.mk with a list of library dependencies as well as
109 # the source file for the tool. Building each tool will also be passed '--cfg
110 # <tool>' for usage in driver.rs
111 #
112 # This build rule is similar to the one found above, just tweaked for
113 # locations and things.
114 #
115 # $(1) - stage
116 # $(2) - target triple
117 # $(3) - host triple
118 # $(4) - name of the tool being built
119 define TARGET_TOOL
120
121 $$(TBIN$(1)_T_$(2)_H_$(3))/$(4)$$(X_$(2)): \
122                 $$(TOOL_SOURCE_$(4)) \
123                 $$(TOOL_INPUTS_$(4)) \
124                 $$(foreach dep,$$(TOOL_DEPS_$(4)), \
125                     $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep)) \
126                 $$(TSREQ$(1)_T_$(2)_H_$(3)) \
127                 | $$(TBIN$(1)_T_$(2)_H_$(3))/
128         @$$(call E, rustc: $$@)
129         $$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$< --cfg $(4)
130
131 endef
132
133 # Every recipe in RUST_TARGET_STAGE_N outputs to $$(TLIB$(1)_T_$(2)_H_$(3),
134 # a directory that can be cleaned out during the middle of a run of
135 # the get-snapshot.py script.  Therefore, every recipe needs to have
136 # an order-only dependency either on $(SNAPSHOT_RUSTC_POST_CLEANUP) or
137 # on $$(TSREQ$(1)_T_$(2)_H_$(3)), to ensure that no products will be
138 # put into the target area until after the get-snapshot.py script has
139 # had its chance to clean it out; otherwise the other products will be
140 # inadvertently included in the clean out.
141 SNAPSHOT_RUSTC_POST_CLEANUP=$(HBIN0_H_$(CFG_BUILD))/rustc$(X_$(CFG_BUILD))
142
143 define TARGET_HOST_RULES
144
145 $$(TBIN$(1)_T_$(2)_H_$(3))/:
146         mkdir -p $$@
147
148 $$(TLIB$(1)_T_$(2)_H_$(3))/:
149         mkdir -p $$@
150
151 $$(TLIB$(1)_T_$(2)_H_$(3))/%: $$(RT_OUTPUT_DIR_$(2))/% \
152             | $$(TLIB$(1)_T_$(2)_H_$(3))/ $$(SNAPSHOT_RUSTC_POST_CLEANUP)
153         @$$(call E, cp: $$@)
154         $$(Q)cp $$< $$@
155
156 $$(TBIN$(1)_T_$(2)_H_$(3))/%: $$(CFG_LLVM_INST_DIR_$(2))/bin/% \
157             | $$(TBIN$(1)_T_$(2)_H_$(3))/ $$(SNAPSHOT_RUSTC_POST_CLEANUP)
158         @$$(call E, cp: $$@)
159         $$(Q)cp $$< $$@
160 endef
161
162 $(foreach source,$(CFG_HOST), \
163  $(foreach target,$(CFG_TARGET), \
164   $(eval $(call TARGET_HOST_RULES,0,$(target),$(source))) \
165   $(eval $(call TARGET_HOST_RULES,1,$(target),$(source))) \
166   $(eval $(call TARGET_HOST_RULES,2,$(target),$(source))) \
167   $(eval $(call TARGET_HOST_RULES,3,$(target),$(source)))))
168
169 # In principle, each host can build each target for both libs and tools
170 $(foreach crate,$(CRATES), \
171  $(foreach source,$(CFG_HOST), \
172   $(foreach target,$(CFG_TARGET), \
173    $(eval $(call RUST_TARGET_STAGE_N,0,$(target),$(source),$(crate))) \
174    $(eval $(call RUST_TARGET_STAGE_N,1,$(target),$(source),$(crate))) \
175    $(eval $(call RUST_TARGET_STAGE_N,2,$(target),$(source),$(crate))) \
176    $(eval $(call RUST_TARGET_STAGE_N,3,$(target),$(source),$(crate))))))
177
178 $(foreach host,$(CFG_HOST), \
179  $(foreach target,$(CFG_TARGET), \
180   $(foreach stage,$(STAGES), \
181    $(foreach tool,$(TOOLS), \
182     $(eval $(call TARGET_TOOL,$(stage),$(target),$(host),$(tool)))))))
183
184 # We have some triples which are bootstrapped from other triples, and this means
185 # that we need to fixup some of the native tools that a triple depends on.
186 #
187 # For example, MSVC requires the llvm-ar.exe executable to manage archives, but
188 # it bootstraps from the GNU Windows triple. This means that the compiler will
189 # add this directory to PATH when executing new processes:
190 #
191 #       $SYSROOT/rustlib/x86_64-pc-windows-gnu/bin
192 #
193 # Unfortunately, however, the GNU triple is not known about in stage0, so the
194 # tools are actually located in:
195 #
196 #       $SYSROOT/rustlib/x86_64-pc-windows-msvc/bin
197 #
198 # To remedy this problem, the rules below copy all native tool dependencies into
199 # the bootstrap triple's location in stage 0 so the bootstrap compiler can find
200 # the right sets of tools. Later stages (1+) will have the right host triple for
201 # the compiler, so there's no need to worry there.
202 #
203 # $(1) - stage
204 # $(2) - triple that's being used as host/target
205 # $(3) - triple snapshot is built for
206 # $(4) - crate
207 # $(5) - tool
208 #
209 # FIXME(stage0): remove this and all other relevant support in the makefiles
210 #                after a snapshot is made
211 define MOVE_TOOLS_TO_SNAPSHOT_HOST_DIR
212 ifneq (,$(3))
213 $$(TLIB$(1)_T_$(2)_H_$(2))/stamp.$(4): $$(HLIB$(1)_H_$(2))/rustlib/$(3)/bin/$(5)
214
215 $$(HLIB$(1)_H_$(2))/rustlib/$(3)/bin/$(5): $$(TBIN$(1)_T_$(2)_H_$(2))/$(5)
216         mkdir -p $$(@D)
217         cp $$< $$@
218 endif
219 endef
220
221 $(foreach target,$(CFG_TARGET), \
222  $(foreach crate,$(CRATES), \
223   $(foreach tool,$(NATIVE_TOOL_DEPS_$(crate)_T_$(target)), \
224    $(eval $(call MOVE_TOOLS_TO_SNAPSHOT_HOST_DIR,0,$(target),$(BOOTSTRAP_FROM_$(target)),$(crate),$(tool))))))
225
226 # For MSVC targets we need to set up some environment variables for the linker
227 # to work correctly when building Rust crates. These two variables are:
228 #
229 # - LIB tells the linker the default search path for finding system libraries,
230 #   for example kernel32.dll
231 # - PATH needs to be modified to ensure that MSVC's link.exe is first in the
232 #   path instead of MinGW's /usr/bin/link.exe (entirely unrelated)
233 #
234 # The values for these variables are detected by the configure script.
235 #
236 # FIXME(stage0): remove this and all other relevant support in the makefiles
237 #                after a snapshot is made
238 define SETUP_LIB_MSVC_ENV_VARS
239 ifeq ($$(findstring msvc,$(2)),msvc)
240 $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$(4): \
241         export LIB := $$(CFG_MSVC_LIB_PATH_$$(HOST_$(2)))
242 $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$(4): \
243         export PATH := $$(CFG_MSVC_BINDIR_$$(HOST_$(2))):$$(PATH)
244 endif
245 endef
246 define SETUP_TOOL_MSVC_ENV_VARS
247 ifeq ($$(findstring msvc,$(2)),msvc)
248 $$(TBIN$(1)_T_$(2)_H_$(3))/$(4)$$(X_$(2)): \
249         export LIB := $$(CFG_MSVC_LIB_PATH_$$(HOST_$(2)))
250 $$(TBIN$(1)_T_$(2)_H_$(3))/$(4)$$(X_$(2)): \
251         export PATH := $$(CFG_MSVC_BINDIR_$$(HOST_$(2))):$$(PATH)
252 endif
253 endef
254
255 $(foreach host,$(CFG_HOST), \
256  $(foreach target,$(CFG_TARGET), \
257   $(foreach crate,$(CRATES), \
258    $(eval $(call SETUP_LIB_MSVC_ENV_VARS,0,$(target),$(host),$(crate))))))
259 $(foreach host,$(CFG_HOST), \
260  $(foreach target,$(CFG_TARGET), \
261   $(foreach tool,$(TOOLS), \
262    $(eval $(call SETUP_TOOL_MSVC_ENV_VARS,0,$(target),$(host),$(tool))))))