]> git.lizzy.rs Git - rust.git/blob - mk/target.mk
Auto merge of #33786 - birkenfeld:make-fix, r=jseyfried
[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 # Used as defaults for the runtime ar and cc tools
17 export CFG_DEFAULT_LINKER
18 export CFG_DEFAULT_AR
19
20 # Macro that generates the full list of dependencies for a crate at a particular
21 # stage/target/host tuple.
22 #
23 # $(1) - stage
24 # $(2) - target
25 # $(3) - host
26 # $(4) crate
27 define RUST_CRATE_FULLDEPS
28 CRATE_FULLDEPS_$(1)_T_$(2)_H_$(3)_$(4) := \
29                 $$(CRATEFILE_$(4)) \
30                 $$(RSINPUTS_$(4)) \
31                 $$(foreach dep,$$(RUST_DEPS_$(4)_T_$(2)), \
32                   $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep)) \
33                 $$(foreach dep,$$(NATIVE_DEPS_$(4)), \
34                   $$(RT_OUTPUT_DIR_$(2))/$$(call CFG_STATIC_LIB_NAME_$(2),$$(dep))) \
35                 $$(foreach dep,$$(NATIVE_DEPS_$(4)_T_$(2)), \
36                   $$(RT_OUTPUT_DIR_$(2))/$$(dep))
37 endef
38
39 $(foreach host,$(CFG_HOST), \
40  $(foreach target,$(CFG_TARGET), \
41   $(foreach stage,$(STAGES), \
42    $(foreach crate,$(CRATES), \
43     $(eval $(call RUST_CRATE_FULLDEPS,$(stage),$(target),$(host),$(crate)))))))
44
45 # $(1) stage
46 # $(2) target
47 # $(3) host
48 define DEFINE_BOOTSTRAP_KEY
49 BOOTSTRAP_KEY$(1)_T_$(2)_H_$(3) := $$(CFG_BOOTSTRAP_KEY)
50 ifeq ($(1),0)
51 ifeq ($(3),$$(CFG_BUILD))
52 BOOTSTRAP_KEY$(1)_T_$(2)_H_$(3) := $$(CFG_BOOTSTRAP_KEY_STAGE0)
53 endif
54 endif
55 endef
56
57 $(foreach host,$(CFG_TARGET), \
58  $(foreach target,$(CFG_TARGET), \
59   $(foreach stage,$(STAGES), \
60    $(eval $(call DEFINE_BOOTSTRAP_KEY,$(stage),$(target),$(host))))))
61
62 # RUST_TARGET_STAGE_N template: This defines how target artifacts are built
63 # for all stage/target architecture combinations. This is one giant rule which
64 # works as follows:
65 #
66 #   1. The immediate dependencies are the rust source files
67 #   2. Each rust crate dependency is listed (based on their stamp files),
68 #      as well as all native dependencies (listed in RT_OUTPUT_DIR)
69 #   3. The stage (n-1) compiler is required through the TSREQ dependency
70 #   4. When actually executing the rule, the first thing we do is to clean out
71 #      old libs and rlibs via the REMOVE_ALL_OLD_GLOB_MATCHES macro
72 #   5. Finally, we get around to building the actual crate. It's just one
73 #      "small" invocation of the previous stage rustc. We use -L to
74 #      RT_OUTPUT_DIR so all the native dependencies are picked up.
75 #      Additionally, we pass in the llvm dir so rustc can link against it.
76 #   6. Some cleanup is done (listing what was just built) if verbose is turned
77 #      on.
78 #
79 # $(1) is the stage
80 # $(2) is the target triple
81 # $(3) is the host triple
82 # $(4) is the crate name
83 define RUST_TARGET_STAGE_N
84
85 $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$(4): CFG_COMPILER_HOST_TRIPLE = $(2)
86 $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$(4): \
87         export RUSTC_BOOTSTRAP_KEY := $$(BOOTSTRAP_KEY$(1)_T_$(2)_H_$(3))
88 $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$(4): \
89                 $$(CRATEFILE_$(4)) \
90                 $$(CRATE_FULLDEPS_$(1)_T_$(2)_H_$(3)_$(4)) \
91                 $$(LLVM_CONFIG_$(2)) \
92                 $$(TSREQ$(1)_T_$(2)_H_$(3)) \
93                 | $$(TLIB$(1)_T_$(2)_H_$(3))/
94         @$$(call E, rustc: $$(@D)/lib$(4))
95         @touch $$@.start_time
96         $$(call REMOVE_ALL_OLD_GLOB_MATCHES, \
97             $$(dir $$@)$$(call CFG_LIB_GLOB_$(2),$(4)))
98         $$(call REMOVE_ALL_OLD_GLOB_MATCHES, \
99             $$(dir $$@)$$(call CFG_RLIB_GLOB,$(4)))
100         $(Q)CFG_LLVM_LINKAGE_FILE=$$(LLVM_LINKAGE_PATH_$(2)) \
101             $$(subst @,,$$(STAGE$(1)_T_$(2)_H_$(3))) \
102                 $$(RUST_LIB_FLAGS_ST$(1)) \
103                 -L "$$(RT_OUTPUT_DIR_$(2))" \
104                 $$(LLVM_LIBDIR_RUSTFLAGS_$(2)) \
105                 $$(LLVM_STDCPP_RUSTFLAGS_$(2)) \
106                 $$(RUSTFLAGS_$(4)) \
107                 $$(RUSTFLAGS$(1)_$(4)) \
108                 $$(RUSTFLAGS$(1)_$(4)_T_$(2)) \
109                 --out-dir $$(@D) \
110                 -C extra-filename=-$$(CFG_FILENAME_EXTRA) \
111                 -C metadata=$$(CFG_FILENAME_EXTRA) \
112                 $$<
113         @touch -r $$@.start_time $$@ && rm $$@.start_time
114         $$(call LIST_ALL_OLD_GLOB_MATCHES, \
115             $$(dir $$@)$$(call CFG_LIB_GLOB_$(2),$(4)))
116         $$(call LIST_ALL_OLD_GLOB_MATCHES, \
117             $$(dir $$@)$$(call CFG_RLIB_GLOB,$(4)))
118
119 endef
120
121 # Macro for building any tool as part of the rust compilation process. Each
122 # tool is defined in crates.mk with a list of library dependencies as well as
123 # the source file for the tool. Building each tool will also be passed '--cfg
124 # <tool>' for usage in driver.rs
125 #
126 # This build rule is similar to the one found above, just tweaked for
127 # locations and things.
128 #
129 # $(1) - stage
130 # $(2) - target triple
131 # $(3) - host triple
132 # $(4) - name of the tool being built
133 define TARGET_TOOL
134
135 $$(TBIN$(1)_T_$(2)_H_$(3))/$(4)$$(X_$(2)): \
136         export RUSTC_BOOTSTRAP_KEY := $$(BOOTSTRAP_KEY$(1)_T_$(2)_H_$(3))
137 $$(TBIN$(1)_T_$(2)_H_$(3))/$(4)$$(X_$(2)): \
138                 $$(TOOL_SOURCE_$(4)) \
139                 $$(TOOL_INPUTS_$(4)) \
140                 $$(foreach dep,$$(TOOL_DEPS_$(4)), \
141                     $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep)) \
142                 $$(TSREQ$(1)_T_$(2)_H_$(3)) \
143                 | $$(TBIN$(1)_T_$(2)_H_$(3))/
144         @$$(call E, rustc: $$@)
145         $$(STAGE$(1)_T_$(2)_H_$(3)) \
146                 $$(LLVM_LIBDIR_RUSTFLAGS_$(2)) \
147                 -o $$@ $$< --cfg $(4)
148
149 endef
150
151 # Macro for building runtime startup/shutdown object files;
152 # these are Rust's equivalent of crti.o, crtn.o
153 #
154 # $(1) - stage
155 # $(2) - target triple
156 # $(3) - host triple
157 # $(4) - object basename
158 define TARGET_RUSTRT_STARTUP_OBJ
159
160 $$(TLIB$(1)_T_$(2)_H_$(3))/$(4).o: \
161                 $(S)src/rtstartup/$(4).rs \
162                 $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.core \
163                 $$(HSREQ$(1)_T_$(2)_H_$(3)) \
164                 | $$(TBIN$(1)_T_$(2)_H_$(3))/
165         @$$(call E, rustc: $$@)
166         $$(STAGE$(1)_T_$(2)_H_$(3)) --emit=obj -o $$@ $$<
167
168 ifeq ($$(CFG_RUSTRT_HAS_STARTUP_OBJS_$(2)), 1)
169 # Add dependencies on Rust startup objects to all crates that depend on core.
170 # This ensures that they are built after core (since they depend on it),
171 # but before everything else (since they are needed for linking dylib crates).
172 $$(foreach crate, $$(TARGET_CRATES_$(2)), \
173         $$(if $$(findstring core,$$(DEPS_$$(crate))), \
174                 $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(crate))) : $$(TLIB$(1)_T_$(2)_H_$(3))/$(4).o
175 endif
176
177 endef
178
179 # Every recipe in RUST_TARGET_STAGE_N outputs to $$(TLIB$(1)_T_$(2)_H_$(3),
180 # a directory that can be cleaned out during the middle of a run of
181 # the get-snapshot.py script.  Therefore, every recipe needs to have
182 # an order-only dependency either on $(SNAPSHOT_RUSTC_POST_CLEANUP) or
183 # on $$(TSREQ$(1)_T_$(2)_H_$(3)), to ensure that no products will be
184 # put into the target area until after the get-snapshot.py script has
185 # had its chance to clean it out; otherwise the other products will be
186 # inadvertently included in the clean out.
187 SNAPSHOT_RUSTC_POST_CLEANUP=$(HBIN0_H_$(CFG_BUILD))/rustc$(X_$(CFG_BUILD))
188
189 define TARGET_HOST_RULES
190
191 $$(TLIB$(1)_T_$(2)_H_$(3))/: $$(SNAPSHOT_RUSTC_POST_CLEANUP)
192         mkdir -p $$@
193
194 $$(TBIN$(1)_T_$(2)_H_$(3))/: $$(SNAPSHOT_RUSTC_POST_CLEANUP)
195         mkdir -p $$@
196
197 $$(TLIB$(1)_T_$(2)_H_$(3))/%: $$(RT_OUTPUT_DIR_$(2))/% \
198             $$(SNAPSHOT_RUSTC_POST_CLEANUP) \
199             | $$(TLIB$(1)_T_$(2)_H_$(3))/
200         @$$(call E, cp: $$@)
201         $$(Q)cp $$< $$@
202 endef
203
204 $(foreach source,$(CFG_HOST), \
205  $(foreach target,$(CFG_TARGET), \
206   $(eval $(call TARGET_HOST_RULES,0,$(target),$(source))) \
207   $(eval $(call TARGET_HOST_RULES,1,$(target),$(source))) \
208   $(eval $(call TARGET_HOST_RULES,2,$(target),$(source))) \
209   $(eval $(call TARGET_HOST_RULES,3,$(target),$(source)))))
210
211 # In principle, each host can build each target for both libs and tools
212 $(foreach crate,$(CRATES), \
213  $(foreach source,$(CFG_HOST), \
214   $(foreach target,$(CFG_TARGET), \
215    $(eval $(call RUST_TARGET_STAGE_N,0,$(target),$(source),$(crate))) \
216    $(eval $(call RUST_TARGET_STAGE_N,1,$(target),$(source),$(crate))) \
217    $(eval $(call RUST_TARGET_STAGE_N,2,$(target),$(source),$(crate))) \
218    $(eval $(call RUST_TARGET_STAGE_N,3,$(target),$(source),$(crate))))))
219
220 $(foreach host,$(CFG_HOST), \
221  $(foreach target,$(CFG_TARGET), \
222   $(foreach stage,$(STAGES), \
223    $(foreach tool,$(TOOLS), \
224     $(eval $(call TARGET_TOOL,$(stage),$(target),$(host),$(tool)))))))
225
226 $(foreach host,$(CFG_HOST), \
227  $(foreach target,$(CFG_TARGET), \
228   $(foreach stage,$(STAGES), \
229    $(foreach obj,rsbegin rsend, \
230     $(eval $(call TARGET_RUSTRT_STARTUP_OBJ,$(stage),$(target),$(host),$(obj)))))))