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