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