]> git.lizzy.rs Git - rust.git/blob - mk/prepare.mk
mk: Simplify how prepare.mk, install.mk, and dist.mk deal with stages
[rust.git] / mk / prepare.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 # Basic support for producing installation images.
12 #
13 # The 'prepare' build target copies all release artifacts from the build
14 # directory to some other location, placing all binaries, libraries, and
15 # docs in their final locations relative to each other.
16 #
17 # It requires the following variables to be set:
18 #
19 #   PREPARE_HOST - the host triple
20 #   PREPARE_TARGETS - the target triples, space separated
21 #   PREPARE_DEST_DIR - the directory to put the image
22
23
24 # On windows we install from stage3, but on unix only stage2
25 ifdef CFG_WINDOWSY_$(CFG_BUILD)
26 PREPARE_STAGE=3
27 else
28 PREPARE_STAGE=2
29 endif
30
31 prepare: PREPARE_DIR_CMD=$(DEFAULT_PREPARE_DIR_CMD)
32 prepare: PREPARE_BIN_CMD=$(DEFAULT_PREPARE_BIN_CMD)
33 prepare: PREPARE_LIB_CMD=$(DEFAULT_PREPARE_LIB_CMD)
34 prepare: PREPARE_MAN_CMD=$(DEFAULT_PREPARE_MAN_CMD)
35 prepare: prepare-base
36
37 prepare-base: PREPARE_SOURCE_DIR=$(PREPARE_HOST)/stage$(PREPARE_STAGE)
38 prepare-base: PREPARE_SOURCE_BIN_DIR=$(PREPARE_SOURCE_DIR)/bin
39 prepare-base: PREPARE_SOURCE_LIB_DIR=$(PREPARE_SOURCE_DIR)/$(CFG_LIBDIR_RELATIVE)
40 prepare-base: PREPARE_SOURCE_MAN_DIR=$(S)/man
41 prepare-base: PREPARE_DEST_BIN_DIR=$(PREPARE_DEST_DIR)/bin
42 prepare-base: PREPARE_DEST_LIB_DIR=$(PREPARE_DEST_DIR)/$(CFG_LIBDIR_RELATIVE)
43 prepare-base: PREPARE_DEST_MAN_DIR=$(PREPARE_DEST_DIR)/man/man1
44 prepare-base: prepare-host prepare-targets
45
46 prepare-everything: prepare-host prepare-targets
47
48 DEFAULT_PREPARE_DIR_CMD = umask 022 && mkdir -p
49 DEFAULT_PREPARE_BIN_CMD = install -m755
50 DEFAULT_PREPARE_LIB_CMD = install -m644
51 DEFAULT_PREPARE_MAN_CMD = install -m644
52
53 # Create a directory
54 # $(1) is the directory
55 define PREPARE_DIR
56         @$(Q)$(call E, prepare: $(1))
57         $(Q)$(PREPARE_DIR_CMD) $(1)
58 endef
59
60 # Copy an executable
61 # $(1) is the filename/libname-glob
62 define PREPARE_BIN
63         @$(call E, prepare: $(PREPARE_DEST_BIN_DIR)/$(1))
64         $(Q)$(PREPARE_BIN_CMD) $(PREPARE_SOURCE_BIN_DIR)/$(1) $(PREPARE_DEST_BIN_DIR)/$(1)
65 endef
66
67 # Copy a dylib or rlib
68 # $(1) is the filename/libname-glob
69 #
70 # XXX: Don't remove the $(nop) command below!
71 # Yeah, that's right, it's voodoo. Something in the way this macro is being expanded
72 # causes it to parse incorrectly. Throwing in that empty command seems to fix the
73 # problem. I'm sorry, just don't remove the $(nop), alright?
74 define PREPARE_LIB
75         $(nop)
76         @$(call E, prepare: $(PREPARE_WORKING_DEST_LIB_DIR)/$(1))
77         $(Q)LIB_NAME="$(notdir $(lastword $(wildcard $(PREPARE_WORKING_SOURCE_LIB_DIR)/$(1))))"; \
78         MATCHES="$(filter-out %$(notdir $(lastword $(wildcard $(PREPARE_WORKING_SOURCE_LIB_DIR)/$(1)))),\
79                         $(wildcard $(PREPARE_WORKING_DEST_LIB_DIR)/$(1)))"; \
80         if [ -n "$$MATCHES" ]; then                                              \
81           echo "warning: one or libraries matching Rust library '$(1)'" &&       \
82           echo "  (other than '$$LIB_NAME' itself) already present"     &&       \
83           echo "  at destination $(PREPARE_WORKING_DEST_LIB_DIR):"                    &&       \
84           echo $$MATCHES ;                                                       \
85         fi
86         $(Q)$(PREPARE_LIB_CMD) `ls -drt1 $(PREPARE_WORKING_SOURCE_LIB_DIR)/$(1) | tail -1` $(PREPARE_WORKING_DEST_LIB_DIR)/
87 endef
88
89 # Copy a man page
90 # $(1) - source dir
91 define PREPARE_MAN
92         @$(call E, prepare: $(PREPARE_DEST_MAN_DIR)/$(1))
93         $(Q)$(PREPARE_MAN_CMD) $(PREPARE_SOURCE_MAN_DIR)/$(1) $(PREPARE_DEST_MAN_DIR)/$(1)
94 endef
95
96
97 PREPARE_TOOLS = $(filter-out compiletest, $(TOOLS))
98
99 prepare-host: prepare-host-tools
100
101 prepare-host-tools: \
102         $(foreach tool, $(PREPARE_TOOLS),\
103           $(foreach host,$(CFG_HOST),\
104             prepare-host-tool-$(tool)-$(PREPARE_STAGE)-$(host)))
105
106 prepare-host-dirs: prepare-maybe-clean
107         $(call PREPARE_DIR,$(PREPARE_DEST_BIN_DIR))
108         $(call PREPARE_DIR,$(PREPARE_DEST_LIB_DIR))
109         $(call PREPARE_DIR,$(PREPARE_DEST_MAN_DIR))
110
111 # $(1) is tool
112 # $(2) is stage
113 # $(3) is host
114 define DEF_PREPARE_HOST_TOOL
115 prepare-host-tool-$(1)-$(2)-$(3): prepare-maybe-clean \
116                                   $$(foreach dep,$$(TOOL_DEPS_$(1)),prepare-host-lib-$$(dep)-$(2)-$(3)) \
117                                   $$(HBIN$(2)_H_$(3))/$(1)$$(X_$(3)) \
118                                   prepare-host-dirs
119         $$(if $$(findstring $(2), $$(PREPARE_STAGE)),\
120       $$(if $$(findstring $(3), $$(PREPARE_HOST)),\
121         $$(call PREPARE_BIN,$(1)$$(X_$$(PREPARE_HOST))),),)
122         $$(if $$(findstring $(2), $$(PREPARE_STAGE)),\
123       $$(if $$(findstring $(3), $$(PREPARE_HOST)),\
124         $$(call PREPARE_MAN,$(1).1),),)
125 endef
126
127 $(foreach tool,$(PREPARE_TOOLS),\
128   $(foreach host,$(CFG_HOST),\
129       $(eval $(call DEF_PREPARE_HOST_TOOL,$(tool),$(PREPARE_STAGE),$(host)))))
130
131 # For host libraries only install dylibs, not rlibs since the host libs are only
132 # used to support rustc and rustc uses dynamic linking
133 #
134 # $(1) is tool
135 # $(2) is stage
136 # $(3) is host
137 define DEF_PREPARE_HOST_LIB
138 prepare-host-lib-$(1)-$(2)-$(3): PREPARE_WORKING_SOURCE_LIB_DIR=$$(PREPARE_SOURCE_LIB_DIR)
139 prepare-host-lib-$(1)-$(2)-$(3): PREPARE_WORKING_DEST_LIB_DIR=$$(PREPARE_DEST_LIB_DIR)
140 prepare-host-lib-$(1)-$(2)-$(3): prepare-maybe-clean \
141                                  $$(foreach dep,$$(RUST_DEPS_$(1)),prepare-host-lib-$$(dep)-$(2)-$(3))\
142                                  $$(HLIB$(2)_H_$(3))/stamp.$(1) \
143                                  prepare-host-dirs
144         $$(if $$(findstring $(2), $$(PREPARE_STAGE)),\
145       $$(if $$(findstring $(3), $$(PREPARE_HOST)),\
146         $$(call PREPARE_LIB,$$(call CFG_LIB_GLOB_$$(PREPARE_HOST),$(1))),),)
147 endef
148
149 $(foreach lib,$(CRATES),\
150   $(foreach host,$(CFG_HOST),\
151     $(eval $(call DEF_PREPARE_HOST_LIB,$(lib),$(PREPARE_STAGE),$(host)))))
152
153 prepare-targets:\
154         $(foreach host,$(CFG_HOST),\
155            $(foreach target,$(CFG_TARGET),\
156              prepare-target-$(target)-host-$(host)-$(PREPARE_STAGE)))
157
158 # $(1) is stage
159 # $(2) is target
160 # $(3) is host
161 define DEF_PREPARE_TARGET_N
162 # Rebind PREPARE_*_LIB_DIR to point to rustlib, then install the libs for the targets
163 prepare-target-$(2)-host-$(3)-$(1): PREPARE_WORKING_SOURCE_LIB_DIR=$$(PREPARE_SOURCE_LIB_DIR)/$$(CFG_RUSTLIBDIR)/$(2)/lib
164 prepare-target-$(2)-host-$(3)-$(1): PREPARE_WORKING_DEST_LIB_DIR=$$(PREPARE_DEST_LIB_DIR)/$$(CFG_RUSTLIBDIR)/$(2)/lib
165 prepare-target-$(2)-host-$(3)-$(1): prepare-maybe-clean \
166         $$(foreach crate,$$(TARGET_CRATES), \
167           $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(crate)) \
168         $$(if $$(findstring $(2),$$(CFG_HOST)), \
169           $$(foreach crate,$$(HOST_CRATES), \
170             $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(crate)),)
171 # Only install if this host and target combo is being prepared. Also be sure to
172 # *not* install the rlibs for host crates because there's no need to statically
173 # link against most of them. They just produce a large amount of extra size
174 # bloat.
175         $$(if $$(findstring $(1), $$(PREPARE_STAGE)),\
176       $$(if $$(findstring $(2), $$(PREPARE_TARGETS)),\
177         $$(if $$(findstring $(3), $$(PREPARE_HOST)),\
178           $$(call PREPARE_DIR,$$(PREPARE_WORKING_DEST_LIB_DIR))\
179           $$(foreach crate,$$(TARGET_CRATES),\
180             $$(call PREPARE_LIB,$$(call CFG_LIB_GLOB_$(2),$$(crate)))\
181             $$(call PREPARE_LIB,$$(call CFG_RLIB_GLOB,$$(crate))))\
182           $$(if $$(findstring $(2),$$(CFG_HOST)),\
183             $$(foreach crate,$$(HOST_CRATES),\
184               $$(call PREPARE_LIB,$$(call CFG_LIB_GLOB_$(2),$$(crate)))),)\
185           $$(call PREPARE_LIB,libmorestack.a) \
186           $$(call PREPARE_LIB,libcompiler-rt.a),),),)
187 endef
188
189 $(foreach host,$(CFG_HOST),\
190   $(foreach target,$(CFG_TARGET), \
191     $(eval $(call DEF_PREPARE_TARGET_N,$(PREPARE_STAGE),$(target),$(host)))))
192
193 prepare-maybe-clean:
194         $(if $(findstring true,$(PREPARE_CLEAN)),\
195       @$(call E, cleaning destination $@),)
196         $(if $(findstring true,$(PREPARE_CLEAN)),\
197       $(Q)rm -rf $(PREPARE_DEST_DIR),)