]> git.lizzy.rs Git - rust.git/blob - mk/rt.mk
db1119c81e7344ad74b5714ed2001c6a0b9dcfd7
[rust.git] / mk / rt.mk
1 # This is a procedure to define the targets for building
2 # the runtime.
3 #
4 # Argument 1 is the target triple.
5 #
6 # This is not really the right place to explain this, but
7 # for those of you who are not Makefile gurus, let me briefly
8 # cover the $ expansion system in use here, because it
9 # confused me for a while!  The variable DEF_RUNTIME_TARGETS
10 # will be defined once and then expanded with different
11 # values substituted for $(1) each time it is called.
12 # That resulting text is then eval'd.
13 #
14 # For most variables, you could use a single $ sign.  The result
15 # is that the substitution would occur when the CALL occurs,
16 # I believe.  The problem is that the automatic variables $< and $@
17 # need to be expanded-per-rule.  Therefore, for those variables at
18 # least, you need $$< and $$@ in the variable text.  This way, after
19 # the CALL substitution occurs, you will have $< and $@.  This text
20 # will then be evaluated, and all will work as you like.
21 #
22 # Reader beware, this explanantion could be wrong, but it seems to
23 # fit the experimental data (i.e., I was able to get the system
24 # working under these assumptions).
25
26 # Hack for passing flags into LIBUV, see below.
27 define DEF_LIBUV_FLAGS
28 ifeq ($(HOST_$(1)), i386)
29         ifneq (,$(findstring mingw32,$(OSTYPE_$(1))))
30                 # Don't add -fPIC since it's ignored for this target
31                 # and it will just cause the build to fail due to -Werror
32                 LIBUV_FLAGS_$(1) = -m32 -I$(S)src/etc/mingw-fix-include
33         else
34                 LIBUV_FLAGS_$(1) = -m32 -fPIC -I$(S)src/etc/mingw-fix-include
35         endif
36 else ifeq ($(HOST_$(1)), x86_64)
37         ifneq (,$(findstring mingw32,$(OSTYPE_$(1))))
38                 # Don't add -fPIC since it's ignored for this target
39                 # and it will just cause the build to fail due to -Werror
40                 LIBUV_FLAGS_$(1) = -m64
41         else
42                 LIBUV_FLAGS_$(1) = -m64 -fPIC
43         endif
44 else ifeq ($(HOST_$(1)), arm)
45         ifeq ($(OSTYPE_$(1)), apple-darwin)
46                 IOS_SDK := $(shell xcrun --show-sdk-path -sdk iphoneos 2>/dev/null)
47                 LIBUV_FLAGS_$(1) = -fPIC -std=gnu99 -I$(IOS_SDK)/usr/include -I$(IOS_SDK)/usr/include/c++/4.2.1
48         else
49                 LIBUV_FLAGS_$(1) = -fPIC -std=gnu99
50         endif
51 else ifeq ($(HOST_$(1)), mips)
52         LIBUV_FLAGS_$(1) = -fPIC -mips32r2 -msoft-float -mabi=32
53 else
54         LIBUV_FLAGS_$(1) = -fPIC -std=gnu99
55 endif
56 endef
57 $(foreach t,$(CFG_TARGET),$(eval $(call DEF_LIBUV_FLAGS,$(t))))
58
59 # when we're doing a snapshot build, we intentionally degrade as many
60 # features in libuv and the runtime as possible, to ease portability.
61
62 SNAP_DEFINES:=
63 ifneq ($(strip $(findstring snap,$(MAKECMDGOALS))),)
64         SNAP_DEFINES=-DRUST_SNAPSHOT
65 endif
66
67 define DEF_LIBUV_ARCH_VAR
68   LIBUV_ARCH_$(1) = $$(subst i386,ia32,$$(subst x86_64,x64,$$(HOST_$(1))))
69 endef
70 $(foreach t,$(CFG_TARGET),$(eval $(call DEF_LIBUV_ARCH_VAR,$(t))))
71
72 ifdef CFG_ENABLE_FAST_MAKE
73 LIBUV_DEPS := $(S)/.gitmodules
74 else
75 LIBUV_DEPS := $(wildcard \
76               $(S)src/libuv/* \
77               $(S)src/libuv/*/* \
78               $(S)src/libuv/*/*/* \
79               $(S)src/libuv/*/*/*/*)
80 endif
81
82 LIBUV_NO_LOAD = run-benchmarks.target.mk run-tests.target.mk \
83                 uv_dtrace_header.target.mk uv_dtrace_provider.target.mk
84
85 export PYTHONPATH := $(PYTHONPATH):$(S)src/gyp/pylib
86
87 define DEF_RUNTIME_TARGETS
88
89 ######################################################################
90 # Runtime (C++) library variables
91 ######################################################################
92
93 # $(1) is the target triple
94 # $(2) is the stage number
95
96 RUNTIME_CFLAGS_$(1)_$(2) = -D_RUST_STAGE$(2)
97 RUNTIME_CXXFLAGS_$(1)_$(2) = -D_RUST_STAGE$(2)
98
99 # XXX: Like with --cfg stage0, pass the defines for stage1 to the stage0
100 # build of non-build-triple host compilers
101 ifeq ($(2),0)
102 ifneq ($(strip $(CFG_BUILD)),$(strip $(1)))
103 RUNTIME_CFLAGS_$(1)_$(2) = -D_RUST_STAGE1
104 RUNTIME_CXXFLAGS_$(1)_$(2) = -D_RUST_STAGE1
105 endif
106 endif
107
108 RUNTIME_CXXS_$(1)_$(2) := \
109               rt/rust_cxx_glue.cpp
110
111 RUNTIME_CS_$(1)_$(2) := \
112               rt/rust_builtin.c \
113               rt/rust_upcall.c \
114               rt/miniz.c \
115               rt/rust_android_dummy.c \
116               rt/rust_test_helpers.c
117
118 # stage0 remove this after the next snapshot
119 %.cpp:
120         @touch tmp/foo.o
121
122 RUNTIME_S_$(1)_$(2) := rt/arch/$$(HOST_$(1))/_context.S \
123                         rt/arch/$$(HOST_$(1))/record_sp.S
124
125 RT_BUILD_DIR_$(1)_$(2) := $$(RT_OUTPUT_DIR_$(1))/stage$(2)
126
127 RUNTIME_DEF_$(1)_$(2) := $$(RT_OUTPUT_DIR_$(1))/rustrt$$(CFG_DEF_SUFFIX_$(1))
128 RUNTIME_INCS_$(1)_$(2) := -I $$(S)src/rt -I $$(S)src/rt/isaac -I $$(S)src/rt/uthash \
129                      -I $$(S)src/rt/arch/$$(HOST_$(1))
130 RUNTIME_OBJS_$(1)_$(2) := $$(RUNTIME_CXXS_$(1)_$(2):rt/%.cpp=$$(RT_BUILD_DIR_$(1)_$(2))/%.o) \
131                      $$(RUNTIME_CS_$(1)_$(2):rt/%.c=$$(RT_BUILD_DIR_$(1)_$(2))/%.o) \
132                      $$(RUNTIME_S_$(1)_$(2):rt/%.S=$$(RT_BUILD_DIR_$(1)_$(2))/%.o)
133 ALL_OBJ_FILES += $$(RUNTIME_OBJS_$(1)_$(2))
134
135 MORESTACK_OBJS_$(1)_$(2) := $$(RT_BUILD_DIR_$(1)_$(2))/arch/$$(HOST_$(1))/morestack.o
136 ALL_OBJ_FILES += $$(MORESTACK_OBJS_$(1)_$(2))
137
138 $$(RT_BUILD_DIR_$(1)_$(2))/rust_cxx_glue.o: rt/rust_cxx_glue.cpp $$(MKFILE_DEPS)
139         @$$(call E, compile: $$@)
140         $$(Q)$$(call CFG_COMPILE_CXX_$(1), $$@, $$(RUNTIME_INCS_$(1)_$(2)) \
141                  $$(SNAP_DEFINES) $$(RUNTIME_CXXFLAGS_$(1)_$(2))) $$<
142
143 $$(RT_BUILD_DIR_$(1)_$(2))/%.o: rt/%.c $$(MKFILE_DEPS)
144         @$$(call E, compile: $$@)
145         $$(Q)$$(call CFG_COMPILE_C_$(1), $$@, $$(RUNTIME_INCS_$(1)_$(2)) \
146                  $$(SNAP_DEFINES) $$(RUNTIME_CFLAGS_$(1)_$(2))) $$<
147
148 $$(RT_BUILD_DIR_$(1)_$(2))/%.o: rt/%.S  $$(MKFILE_DEPS) \
149                      $$(LLVM_CONFIG_$$(CFG_BUILD))
150         @$$(call E, compile: $$@)
151         $$(Q)$$(call CFG_ASSEMBLE_$(1),$$@,$$<)
152
153 $$(RT_BUILD_DIR_$(1)_$(2))/arch/$$(HOST_$(1))/libmorestack.a: $$(MORESTACK_OBJS_$(1)_$(2))
154         @$$(call E, link: $$@)
155         $$(Q)$(AR_$(1)) rcs $$@ $$^
156
157 $$(RT_BUILD_DIR_$(1)_$(2))/$(CFG_RUNTIME_$(1)): $$(RUNTIME_OBJS_$(1)_$(2)) $$(MKFILE_DEPS) \
158                         $$(RUNTIME_DEF_$(1)_$(2))
159         @$$(call E, link: $$@)
160         $$(Q)$$(call CFG_LINK_CXX_$(1),$$@, $$(RUNTIME_OBJS_$(1)_$(2)) \
161             $$(CFG_LIBUV_LINK_FLAGS_$(1)),$$(RUNTIME_DEF_$(1)_$(2)),$$(CFG_RUNTIME_$(1)))
162
163 # These could go in rt.mk or rustllvm.mk, they're needed for both.
164
165 # This regexp has a single $$ escaped twice
166 $(1)/%.bsd.def:    %.def.in $$(MKFILE_DEPS)
167         @$$(call E, def: $$@)
168         $$(Q)echo "{" > $$@
169         $$(Q)sed 's/.$$$$/&;/' $$< >> $$@
170         $$(Q)echo "};" >> $$@
171
172 $(1)/%.linux.def:    %.def.in $$(MKFILE_DEPS)
173         @$$(call E, def: $$@)
174         $$(Q)echo "{" > $$@
175         $$(Q)sed 's/.$$$$/&;/' $$< >> $$@
176         $$(Q)echo "};" >> $$@
177
178 $(1)/%.darwin.def:      %.def.in $$(MKFILE_DEPS)
179         @$$(call E, def: $$@)
180         $$(Q)sed 's/^./_&/' $$< > $$@
181
182 $(1)/%.android.def:  %.def.in $$(MKFILE_DEPS)
183         @$$(call E, def: $$@)
184         $$(Q)echo "{" > $$@
185         $$(Q)sed 's/.$$$$/&;/' $$< >> $$@
186         $$(Q)echo "};" >> $$@
187
188 $(1)/%.mingw32.def:     %.def.in $$(MKFILE_DEPS)
189         @$$(call E, def: $$@)
190         $$(Q)echo LIBRARY $$* > $$@
191         $$(Q)echo EXPORTS >> $$@
192         $$(Q)sed 's/^./    &/' $$< >> $$@
193
194 endef
195
196
197 ######################################################################
198 # Runtime third party targets (libuv, jemalloc, etc.)
199 #
200 # These targets do not need to be built once per stage, so these
201 # rules just build them once and then we're done with them.
202 ######################################################################
203
204 define DEF_THIRD_PARTY_TARGETS
205
206 # $(1) is the target triple
207
208 RT_OUTPUT_DIR_$(1) := $(1)/rt
209
210 ifeq ($$(CFG_WINDOWSY_$(1)), 1)
211   LIBUV_OSTYPE_$(1) := win
212 else ifeq ($(OSTYPE_$(1)), apple-darwin)
213   LIBUV_OSTYPE_$(1) := mac
214 else ifeq ($(OSTYPE_$(1)), unknown-freebsd)
215   LIBUV_OSTYPE_$(1) := freebsd
216 else ifeq ($(OSTYPE_$(1)), linux-androideabi)
217   LIBUV_OSTYPE_$(1) := android
218   LIBUV_ARGS_$(1) := PLATFORM=android host=android OS=linux
219 else
220   LIBUV_OSTYPE_$(1) := linux
221 endif
222
223 LIBUV_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),uv)
224 LIBUV_LIB_$(1) := $$(RT_OUTPUT_DIR_$(1))/libuv/$$(LIBUV_NAME_$(1))
225
226 LIBUV_MAKEFILE_$(1) := $$(CFG_BUILD_DIR)$$(RT_OUTPUT_DIR_$(1))/libuv/Makefile
227
228 $$(LIBUV_MAKEFILE_$(1)): $$(LIBUV_DEPS)
229         (cd $(S)src/libuv/ && \
230          $$(CFG_PYTHON) ./gyp_uv.py -f make -Dtarget_arch=$$(LIBUV_ARCH_$(1)) \
231            -D ninja \
232            -DOS=$$(LIBUV_OSTYPE_$(1)) \
233            -Goutput_dir=$$(@D) --generator-output $$(@D))
234
235 # Windows has a completely different build system for libuv because of mingw. In
236 # theory when we support msvc then we should be using gyp's msvc output instead
237 # of mingw's makefile for windows
238 ifdef CFG_WINDOWSY_$(1)
239 $$(LIBUV_LIB_$(1)): $$(LIBUV_DEPS)
240         $$(Q)$$(MAKE) -C $$(S)src/libuv -f Makefile.mingw \
241                 CC="$$(CC_$(1)) $$(CFG_GCCISH_CFLAGS_$(1)) $$(LIBUV_FLAGS_$(1)) $$(SNAP_DEFINES)" \
242                 AR="$$(AR_$(1))" \
243                 V=$$(VERBOSE)
244         $$(Q)cp $$(S)src/libuv/libuv.a $$@
245 else
246 $$(LIBUV_LIB_$(1)): $$(LIBUV_DEPS) $$(LIBUV_MAKEFILE_$(1))
247         $$(Q)$$(MAKE) -C $$(@D) \
248                 CFLAGS="$$(CFG_GCCISH_CFLAGS_$(1)) $$(LIBUV_FLAGS_$(1)) $$(SNAP_DEFINES)" \
249                 LDFLAGS="$$(CFG_GCCISH_LINK_FLAGS_$(1)) $$(LIBUV_FLAGS_$(1))" \
250                 CC="$$(CC_$(1))" \
251                 CXX="$$(CXX_$(1))" \
252                 AR="$$(AR_$(1))" \
253                 $$(LIBUV_ARGS_$(1)) \
254                 builddir="." \
255                 BUILDTYPE=Release \
256                 NO_LOAD="$$(LIBUV_NO_LOAD)" \
257                 V=$$(VERBOSE)
258 endif
259
260 # libuv support functionality (extra C/C++ that we need to use libuv)
261
262 UV_SUPPORT_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),uv_support)
263 UV_SUPPORT_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/uv_support
264 UV_SUPPORT_LIB_$(1) := $$(UV_SUPPORT_DIR_$(1))/$$(UV_SUPPORT_NAME_$(1))
265 UV_SUPPORT_CS_$(1) := rt/rust_uv.c
266 UV_SUPPORT_OBJS_$(1) := $$(UV_SUPPORT_CS_$(1):rt/%.c=$$(UV_SUPPORT_DIR_$(1))/%.o)
267
268 $$(UV_SUPPORT_DIR_$(1))/%.o: rt/%.c
269         @$$(call E, compile: $$@)
270         @mkdir -p $$(@D)
271         $$(Q)$$(call CFG_COMPILE_C_$(1), $$@, \
272                 -I $$(S)src/libuv/include \
273                  $$(RUNTIME_CFLAGS_$(1))) $$<
274
275 $$(UV_SUPPORT_LIB_$(1)): $$(UV_SUPPORT_OBJS_$(1))
276         @$$(call E, link: $$@)
277         $$(Q)$$(AR_$(1)) rcs $$@ $$^
278
279 # sundown markdown library (used by librustdoc)
280
281 SUNDOWN_NAME_$(1) := $$(call CFG_STATIC_LIB_NAME_$(1),sundown)
282 SUNDOWN_DIR_$(1) := $$(RT_OUTPUT_DIR_$(1))/sundown
283 SUNDOWN_LIB_$(1) := $$(SUNDOWN_DIR_$(1))/$$(SUNDOWN_NAME_$(1))
284
285 SUNDOWN_CS_$(1) := rt/sundown/src/autolink.c \
286                         rt/sundown/src/buffer.c \
287                         rt/sundown/src/stack.c \
288                         rt/sundown/src/markdown.c \
289                         rt/sundown/html/houdini_href_e.c \
290                         rt/sundown/html/houdini_html_e.c \
291                         rt/sundown/html/html_smartypants.c \
292                         rt/sundown/html/html.c
293
294 SUNDOWN_OBJS_$(1) := $$(SUNDOWN_CS_$(1):rt/%.c=$$(SUNDOWN_DIR_$(1))/%.o)
295
296 $$(SUNDOWN_DIR_$(1))/%.o: rt/%.c
297         @$$(call E, compile: $$@)
298         @mkdir -p $$(@D)
299         $$(Q)$$(call CFG_COMPILE_C_$(1), $$@, \
300                 -I $$(S)src/rt/sundown/src -I $$(S)src/rt/sundown/html \
301                  $$(RUNTIME_CFLAGS_$(1))) $$<
302
303 $$(SUNDOWN_LIB_$(1)): $$(SUNDOWN_OBJS_$(1))
304         @$$(call E, link: $$@)
305         $$(Q)$$(AR_$(1)) rcs $$@ $$^
306
307 endef
308
309 # Instantiate template for all stages/targets
310 $(foreach target,$(CFG_TARGET), \
311      $(eval $(call DEF_THIRD_PARTY_TARGETS,$(target))))
312 $(foreach stage,$(STAGES), \
313     $(foreach target,$(CFG_TARGET), \
314          $(eval $(call DEF_RUNTIME_TARGETS,$(target),$(stage)))))