]> git.lizzy.rs Git - rust.git/blob - mk/main.mk
mk: Move most of Makefile.in to .mk files
[rust.git] / mk / main.mk
1 # Copyright 2014 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 # We track all of the object files we might build so that we can find
12 # and include all of the .d files in one fell swoop.
13 ALL_OBJ_FILES :=
14
15 MKFILE_DEPS := config.stamp $(call rwildcard,$(CFG_SRC_DIR)mk/,*)
16 NON_BUILD_HOST = $(filter-out $(CFG_BUILD),$(CFG_HOST))
17 NON_BUILD_TARGET = $(filter-out $(CFG_BUILD),$(CFG_TARGET))
18
19 ifneq ($(MAKE_RESTARTS),)
20 CFG_INFO := $(info cfg: make restarts: $(MAKE_RESTARTS))
21 endif
22
23 CFG_INFO := $(info cfg: build triple $(CFG_BUILD))
24 CFG_INFO := $(info cfg: host triples $(CFG_HOST))
25 CFG_INFO := $(info cfg: target triples $(CFG_TARGET))
26
27 ifneq ($(wildcard $(NON_BUILD_HOST)),)
28 CFG_INFO := $(info cfg: non-build host triples $(NON_BUILD_HOST))
29 endif
30 ifneq ($(wildcard $(NON_BUILD_TARGET)),)
31 CFG_INFO := $(info cfg: non-build target triples $(NON_BUILD_TARGET))
32 endif
33
34 CFG_RUSTC_FLAGS := $(RUSTFLAGS)
35 CFG_GCCISH_CFLAGS :=
36 CFG_GCCISH_LINK_FLAGS :=
37
38 ifdef CFG_DISABLE_OPTIMIZE
39   $(info cfg: disabling rustc optimization (CFG_DISABLE_OPTIMIZE))
40   CFG_RUSTC_FLAGS +=
41 else
42   # The rtopt cfg turns off runtime sanity checks
43   CFG_RUSTC_FLAGS += -O --cfg rtopt
44 endif
45
46 ifdef CFG_DISABLE_DEBUG
47   CFG_RUSTC_FLAGS += --cfg ndebug
48   CFG_GCCISH_CFLAGS += -DRUST_NDEBUG
49 else
50   $(info cfg: enabling more debugging (CFG_ENABLE_DEBUG))
51   CFG_RUSTC_FLAGS += --cfg debug
52   CFG_GCCISH_CFLAGS += -DRUST_DEBUG
53 endif
54
55 ifdef SAVE_TEMPS
56   CFG_RUSTC_FLAGS += --save-temps
57 endif
58 ifdef ASM_COMMENTS
59   CFG_RUSTC_FLAGS += -Z asm-comments
60 endif
61 ifdef TIME_PASSES
62   CFG_RUSTC_FLAGS += -Z time-passes
63 endif
64 ifdef TIME_LLVM_PASSES
65   CFG_RUSTC_FLAGS += -Z time-llvm-passes
66 endif
67 ifdef TRACE
68   CFG_RUSTC_FLAGS += -Z trace
69 endif
70 ifdef CFG_DISABLE_RPATH
71 CFG_RUSTC_FLAGS += -C no-rpath
72 endif
73
74 # The executables crated during this compilation process have no need to include
75 # static copies of libstd and libextra. We also generate dynamic versions of all
76 # libraries, so in the interest of space, prefer dynamic linking throughout the
77 # compilation process.
78 #
79 # Note though that these flags are omitted for stage2+. This means that the
80 # snapshot will be generated with a statically linked rustc so we only have to
81 # worry about the distribution of one file (with its native dynamic
82 # dependencies)
83 RUSTFLAGS_STAGE0 += -C prefer-dynamic
84 RUSTFLAGS_STAGE1 += -C prefer-dynamic
85
86 # platform-specific auto-configuration
87 include $(CFG_SRC_DIR)mk/platform.mk
88
89 # Run the stage1/2 compilers under valgrind
90 ifdef VALGRIND_COMPILE
91   CFG_VALGRIND_COMPILE :=$(CFG_VALGRIND)
92 else
93   CFG_VALGRIND_COMPILE :=
94 endif
95
96 # version-string calculation
97 CFG_GIT_DIR := $(CFG_SRC_DIR).git
98 CFG_RELEASE = 0.10-pre
99 CFG_VERSION = $(CFG_RELEASE)
100 # windows exe's need numeric versions - don't use anything but
101 # numbers and dots here
102 CFG_VERSION_WIN = 0.10
103
104 # since $(CFG_GIT) may contain spaces (especially on Windows),
105 # we need to escape them. (" " to r"\ ")
106 # Note that $(subst ...) ignores space after `subst`,
107 # so we use a hack: define $(SPACE) which contains space character.
108 SPACE :=
109 SPACE +=
110 ifneq ($(wildcard $(subst $(SPACE),\$(SPACE),$(CFG_GIT))),)
111 ifneq ($(wildcard $(subst $(SPACE),\$(SPACE),$(CFG_GIT_DIR))),)
112     CFG_VERSION += $(shell git --git-dir='$(CFG_GIT_DIR)' log -1 \
113                      --pretty=format:'(%h %ci)')
114     CFG_VER_HASH = $(shell git --git-dir='$(CFG_GIT_DIR)' rev-parse HEAD)
115 endif
116 endif
117
118 ifdef CFG_ENABLE_VALGRIND
119   $(info cfg: enabling valgrind (CFG_ENABLE_VALGRIND))
120 else
121   CFG_VALGRIND :=
122 endif
123 ifdef CFG_BAD_VALGRIND
124   $(info cfg: disabling valgrind due to its unreliability on this platform)
125   CFG_VALGRIND :=
126 endif
127
128
129 ######################################################################
130 # Target-and-rule "utility variables"
131 ######################################################################
132
133 define DEF_X
134 X_$(1) := $(CFG_EXE_SUFFIX_$(1))
135 endef
136 $(foreach target,$(CFG_TARGET),\
137   $(eval $(call DEF_X,$(target))))
138
139 # "Source" files we generate in builddir along the way.
140 GENERATED :=
141
142 # Delete the built-in rules.
143 .SUFFIXES:
144 %:: %,v
145 %:: RCS/%,v
146 %:: RCS/%
147 %:: s.%
148 %:: SCCS/s.%
149
150
151 ######################################################################
152 # Cleaning out old crates
153 ######################################################################
154
155 # $(1) is the path for directory to match against
156 # $(2) is the glob to use in the match
157 #
158 # Note that a common bug is to accidentally construct the glob denoted
159 # by $(2) with a space character prefix, which invalidates the
160 # construction $(1)$(2).
161 define CHECK_FOR_OLD_GLOB_MATCHES
162   $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then echo "warning: there are previous" \'$(notdir $(2))\' "libraries:" $$MATCHES; fi
163 endef
164
165 # Same interface as above, but deletes rather than just listing the files.
166 ifdef VERBOSE
167 define REMOVE_ALL_OLD_GLOB_MATCHES
168   $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then echo "warning: removing previous" \'$(notdir $(1))\' "libraries:" $$MATCHES; rm $$MATCHES ; fi
169 endef
170 else
171 define REMOVE_ALL_OLD_GLOB_MATCHES
172   $(Q)MATCHES="$(wildcard $(1))"; if [ -n "$$MATCHES" ] ; then rm $$MATCHES ; fi
173 endef
174 endif
175
176 # We use a different strategy for LIST_ALL_OLD_GLOB_MATCHES_EXCEPT
177 # than in the macros above because it needs the result of running the
178 # `ls` command after other rules in the command list have run; the
179 # macro-expander for $(wildcard ...) would deliver its results too
180 # soon. (This is in contrast to the macros above, which are meant to
181 # be run at the outset of a command list in a rule.)
182 ifdef VERBOSE
183 define LIST_ALL_OLD_GLOB_MATCHES
184   @echo "info: now are following matches for" '$(notdir $(1))' "libraries:"
185   @( ls $(1) 2>/dev/null || true )
186 endef
187 else
188 define LIST_ALL_OLD_GLOB_MATCHES
189 endef
190 endif
191
192 ######################################################################
193 # LLVM macros
194 ######################################################################
195
196 # FIXME: x86-ism
197 LLVM_COMPONENTS=x86 arm mips ipo bitreader bitwriter linker asmparser jit mcjit \
198                 interpreter instrumentation
199
200 # Only build these LLVM tools
201 LLVM_TOOLS=bugpoint llc llvm-ar llvm-as llvm-dis llvm-mc opt llvm-extract
202
203 define DEF_LLVM_VARS
204 # The configure script defines these variables with the target triples
205 # separated by Z. This defines new ones with the expected format.
206 CFG_LLVM_BUILD_DIR_$(1):=$$(CFG_LLVM_BUILD_DIR_$(subst -,_,$(1)))
207 CFG_LLVM_INST_DIR_$(1):=$$(CFG_LLVM_INST_DIR_$(subst -,_,$(1)))
208
209 # Any rules that depend on LLVM should depend on LLVM_CONFIG
210 LLVM_CONFIG_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-config$$(X_$(1))
211 LLVM_MC_$(1):=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-mc$$(X_$(1))
212 LLVM_VERSION_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --version)
213 LLVM_BINDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --bindir)
214 LLVM_INCDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --includedir)
215 LLVM_LIBDIR_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libdir)
216 LLVM_LIBS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --libs $$(LLVM_COMPONENTS))
217 LLVM_LDFLAGS_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --ldflags)
218 # On FreeBSD, it may search wrong headers (that are for pre-installed LLVM),
219 # so we replace -I with -iquote to ensure that it searches bundled LLVM first.
220 LLVM_CXXFLAGS_$(1)=$$(subst -I, -iquote , $$(shell "$$(LLVM_CONFIG_$(1))" --cxxflags))
221 LLVM_HOST_TRIPLE_$(1)=$$(shell "$$(LLVM_CONFIG_$(1))" --host-target)
222
223 LLVM_AS_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llvm-as$$(X_$(1))
224 LLC_$(1)=$$(CFG_LLVM_INST_DIR_$(1))/bin/llc$$(X_$(1))
225
226 endef
227
228 $(foreach host,$(CFG_HOST), \
229  $(eval $(call DEF_LLVM_VARS,$(host))))
230
231 ######################################################################
232 # Exports for sub-utilities
233 ######################################################################
234
235 # Note that any variable that re-configure should pick up needs to be
236 # exported
237
238 export CFG_SRC_DIR
239 export CFG_BUILD_DIR
240 export CFG_VERSION
241 export CFG_VERSION_WIN
242 export CFG_RELEASE
243 export CFG_BUILD
244 export CFG_LLVM_ROOT
245 export CFG_ENABLE_MINGW_CROSS
246 export CFG_PREFIX
247 export CFG_LIBDIR
248 export CFG_RUSTLIBDIR
249 export CFG_LIBDIR_RELATIVE
250 export CFG_DISABLE_INJECT_STD_VERSION
251
252 ######################################################################
253 # Per-stage targets and runner
254 ######################################################################
255
256 STAGES = 0 1 2 3
257
258 define SREQ
259 # $(1) is the stage number
260 # $(2) is the target triple
261 # $(3) is the host triple
262
263 # Destinations of artifacts for the host compiler
264 HROOT$(1)_H_$(3) = $(3)/stage$(1)
265 HBIN$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/bin
266 HLIB$(1)_H_$(3) = $$(HROOT$(1)_H_$(3))/$$(CFG_LIBDIR_RELATIVE)
267
268 # Destinations of artifacts for target architectures
269 TROOT$(1)_T_$(2)_H_$(3) = $$(HLIB$(1)_H_$(3))/$$(CFG_RUSTLIBDIR)/$(2)
270 TBIN$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/bin
271 TLIB$(1)_T_$(2)_H_$(3) = $$(TROOT$(1)_T_$(2)_H_$(3))/lib
272
273 # Preqrequisites for using the stageN compiler
274 ifeq ($(1),0)
275 HSREQ$(1)_H_$(3) = $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))
276 else
277 HSREQ$(1)_H_$(3) = \
278         $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3)) \
279         $$(HLIB$(1)_H_$(3))/stamp.rustc \
280         $$(foreach dep,$$(RUST_DEPS_rustc),$$(HLIB$(1)_H_$(3))/stamp.$$(dep)) \
281         $$(MKFILE_DEPS)
282 endif
283
284 # Prerequisites for using the stageN compiler to build target artifacts
285 TSREQ$(1)_T_$(2)_H_$(3) = \
286         $$(HSREQ$(1)_H_$(3)) \
287         $$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a \
288         $$(TLIB$(1)_T_$(2)_H_$(3))/libcompiler-rt.a
289
290 # Prerequisites for a working stageN compiler and libraries, for a specific
291 # target
292 SREQ$(1)_T_$(2)_H_$(3) = \
293         $$(TSREQ$(1)_T_$(2)_H_$(3)) \
294         $$(foreach dep,$$(TARGET_CRATES),\
295             $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep))
296
297 # Prerequisites for a working stageN compiler and complete set of target
298 # libraries
299 CSREQ$(1)_T_$(2)_H_$(3) = \
300         $$(TSREQ$(1)_T_$(2)_H_$(3)) \
301         $$(HBIN$(1)_H_$(3))/rustdoc$$(X_$(3)) \
302         $$(foreach dep,$$(CRATES),$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep)) \
303         $$(foreach dep,$$(HOST_CRATES),$$(HLIB$(1)_H_$(3))/stamp.$$(dep))
304
305 ifeq ($(1),0)
306 # Don't run the the stage0 compiler under valgrind - that ship has sailed
307 CFG_VALGRIND_COMPILE$(1) =
308 else
309 CFG_VALGRIND_COMPILE$(1) = $$(CFG_VALGRIND_COMPILE)
310 endif
311
312 # Add RUSTFLAGS_STAGEN values to the build command
313 EXTRAFLAGS_STAGE$(1) = $$(RUSTFLAGS_STAGE$(1))
314
315 CFGFLAG$(1)_T_$(2)_H_$(3) = stage$(1)
316
317 # Pass --cfg stage0 only for the build->host part of stage0;
318 # if you're building a cross config, the host->* parts are
319 # effectively stage1, since it uses the just-built stage0.
320 ifeq ($(1),0)
321 ifneq ($(strip $(CFG_BUILD)),$(strip $(3)))
322 CFGFLAG$(1)_T_$(2)_H_$(3) = stage1
323 endif
324 endif
325
326 ifdef CFG_DISABLE_RPATH
327 ifeq ($$(OSTYPE_$(3)),apple-darwin)
328   RPATH_VAR$(1)_T_$(2)_H_$(3) := \
329       DYLD_LIBRARY_PATH="$$$$DYLD_LIBRARY_PATH:$$(HLIB$(1)_H_$(3))"
330 else
331   RPATH_VAR$(1)_T_$(2)_H_$(3) := \
332       LD_LIBRARY_PATH="$$$$LD_LIBRARY_PATH:$$(HLIB$(1)_H_$(3))"
333 endif
334 else
335     RPATH_VAR$(1)_T_$(2)_H_$(3) :=
336 endif
337
338 STAGE$(1)_T_$(2)_H_$(3) :=                                              \
339         $$(Q)$$(RPATH_VAR$(1)_T_$(2)_H_$(3))                            \
340                 $$(call CFG_RUN_TARG_$(3),$(1),                         \
341                 $$(CFG_VALGRIND_COMPILE$(1))                            \
342                 $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))                     \
343                 --cfg $$(CFGFLAG$(1)_T_$(2)_H_$(3))                     \
344                 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2)) \
345                 $$(RUSTC_FLAGS_$(2))
346
347 PERF_STAGE$(1)_T_$(2)_H_$(3) :=                                         \
348         $$(Q)$$(call CFG_RUN_TARG_$(3),$(1),                            \
349                 $$(CFG_PERF_TOOL)                                       \
350                 $$(HBIN$(1)_H_$(3))/rustc$$(X_$(3))                     \
351                 --cfg $$(CFGFLAG$(1)_T_$(2)_H_$(3))                     \
352                 $$(CFG_RUSTC_FLAGS) $$(EXTRAFLAGS_STAGE$(1)) --target=$(2)) \
353                 $$(RUSTC_FLAGS_$(2))
354
355 endef
356
357 $(foreach build,$(CFG_HOST), \
358  $(eval $(foreach target,$(CFG_TARGET), \
359   $(eval $(foreach stage,$(STAGES), \
360    $(eval $(call SREQ,$(stage),$(target),$(build))))))))
361
362 ######################################################################
363 # rustc-H-targets
364 #
365 # Builds a functional Rustc for the given host.
366 ######################################################################
367
368 define DEF_RUSTC_STAGE_TARGET
369 # $(1) == architecture
370 # $(2) == stage
371
372 rustc-stage$(2)-H-$(1):                                                 \
373         $$(foreach target,$$(CFG_TARGET),$$(SREQ$(2)_T_$$(target)_H_$(1)))
374
375 endef
376
377 $(foreach host,$(CFG_HOST),                                             \
378  $(eval $(foreach stage,1 2 3,                                          \
379   $(eval $(call DEF_RUSTC_STAGE_TARGET,$(host),$(stage))))))
380
381 rustc-stage1: rustc-stage1-H-$(CFG_BUILD)
382 rustc-stage2: rustc-stage2-H-$(CFG_BUILD)
383 rustc-stage3: rustc-stage3-H-$(CFG_BUILD)
384
385 define DEF_RUSTC_TARGET
386 # $(1) == architecture
387
388 rustc-H-$(1): rustc-stage2-H-$(1)
389 endef
390
391 $(foreach host,$(CFG_TARGET),                   \
392  $(eval $(call DEF_RUSTC_TARGET,$(host))))
393
394 rustc-stage1: rustc-stage1-H-$(CFG_BUILD)
395 rustc-stage2: rustc-stage2-H-$(CFG_BUILD)
396 rustc-stage3: rustc-stage3-H-$(CFG_BUILD)
397 rustc: rustc-H-$(CFG_BUILD)
398
399 rustc-H-all: $(foreach host,$(CFG_HOST),rustc-H-$(host))
400
401 ######################################################################
402 # Entrypoint rule
403 ######################################################################
404
405 .DEFAULT_GOAL := all
406
407 define ALL_TARGET_N
408 ifneq ($$(findstring $(1),$$(CFG_HOST)),)
409 # This is a host
410 all-target-$(1)-host-$(2): $$(CSREQ2_T_$(1)_H_$(2))
411 else
412 # This is a target only
413 all-target-$(1)-host-$(2): $$(SREQ2_T_$(1)_H_$(2))
414 endif
415 endef
416
417 $(foreach target,$(CFG_TARGET), \
418  $(foreach host,$(CFG_HOST), \
419  $(eval $(call ALL_TARGET_N,$(target),$(host)))))
420
421 ALL_TARGET_RULES = $(foreach target,$(CFG_TARGET), \
422         $(foreach host,$(CFG_HOST), \
423  all-target-$(target)-host-$(host)))
424
425 all: $(ALL_TARGET_RULES) $(GENERATED) docs
426
427 help:
428 # Show the comments from Makefile.in as "help"
429 # pick everything between tags | remove first line | remove last line
430 # | remove extra (?) line | strip leading `#` from lines
431         $(Q)awk '/<help>/,/<\/help>/' $(S)/Makefile.in | sed '1d' | sed '$$d' | sed 's/^# \?//'