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