1# Copyright 2012 the V8 project authors. All rights reserved.
2# Redistribution and use in source and binary forms, with or without
3# modification, are permitted provided that the following conditions are
4# met:
5#
6#     * Redistributions of source code must retain the above copyright
7#       notice, this list of conditions and the following disclaimer.
8#     * Redistributions in binary form must reproduce the above
9#       copyright notice, this list of conditions and the following
10#       disclaimer in the documentation and/or other materials provided
11#       with the distribution.
12#     * Neither the name of Google Inc. nor the names of its
13#       contributors may be used to endorse or promote products derived
14#       from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28
29# Variable default definitions. Override them by exporting them in your shell.
30CXX ?= g++
31LINK ?= g++
32OUTDIR ?= out
33TESTJOBS ?= -j16
34GYPFLAGS ?=
35TESTFLAGS ?=
36ANDROID_NDK_ROOT ?=
37ANDROID_TOOL_PREFIX = $(ANDROID_NDK_ROOT)/toolchain/bin/arm-linux-androideabi
38
39# Special build flags. Use them like this: "make library=shared"
40
41# library=shared || component=shared_library
42ifeq ($(library), shared)
43  GYPFLAGS += -Dcomponent=shared_library
44endif
45ifdef component
46  GYPFLAGS += -Dcomponent=$(component)
47endif
48# console=readline
49ifdef console
50  GYPFLAGS += -Dconsole=$(console)
51endif
52# disassembler=on
53ifeq ($(disassembler), on)
54  GYPFLAGS += -Dv8_enable_disassembler=1
55endif
56# objectprint=on
57ifeq ($(objectprint), on)
58  GYPFLAGS += -Dv8_object_print=1
59endif
60# snapshot=off
61ifeq ($(snapshot), off)
62  GYPFLAGS += -Dv8_use_snapshot='false'
63endif
64# gdbjit=on
65ifeq ($(gdbjit), on)
66  GYPFLAGS += -Dv8_enable_gdbjit=1
67endif
68# liveobjectlist=on
69ifeq ($(liveobjectlist), on)
70  GYPFLAGS += -Dv8_use_liveobjectlist=true
71endif
72# vfp3=off
73ifeq ($(vfp3), off)
74  GYPFLAGS += -Dv8_can_use_vfp_instructions=false
75else
76  GYPFLAGS += -Dv8_can_use_vfp_instructions=true
77endif
78# debuggersupport=off
79ifeq ($(debuggersupport), off)
80  GYPFLAGS += -Dv8_enable_debugger_support=0
81endif
82# soname_version=1.2.3
83ifdef soname_version
84  GYPFLAGS += -Dsoname_version=$(soname_version)
85endif
86# werror=no
87ifeq ($(werror), no)
88  GYPFLAGS += -Dwerror=''
89endif
90# presubmit=no
91ifeq ($(presubmit), no)
92  TESTFLAGS += --no-presubmit
93endif
94# strictaliasing=off (workaround for GCC-4.5)
95ifeq ($(strictaliasing), off)
96  GYPFLAGS += -Dv8_no_strict_aliasing=1
97endif
98
99# ----------------- available targets: --------------------
100# - "dependencies": pulls in external dependencies (currently: GYP)
101# - any arch listed in ARCHES (see below)
102# - any mode listed in MODES
103# - every combination <arch>.<mode>, e.g. "ia32.release"
104# - "native": current host's architecture, release mode
105# - any of the above with .check appended, e.g. "ia32.release.check"
106# - "android": cross-compile for Android/ARM (release mode)
107# - default (no target specified): build all DEFAULT_ARCHES and MODES
108# - "check": build all targets and run all tests
109# - "<arch>.clean" for any <arch> in ARCHES
110# - "clean": clean all ARCHES
111
112# ----------------- internal stuff ------------------------
113
114# Architectures and modes to be compiled. Consider these to be internal
115# variables, don't override them (use the targets instead).
116ARCHES = ia32 x64 arm mips
117DEFAULT_ARCHES = ia32 x64 arm
118MODES = release debug
119
120# List of files that trigger Makefile regeneration:
121GYPFILES = build/all.gyp build/common.gypi build/standalone.gypi \
122           preparser/preparser.gyp samples/samples.gyp src/d8.gyp \
123           test/cctest/cctest.gyp tools/gyp/v8.gyp
124
125# Generates all combinations of ARCHES and MODES, e.g. "ia32.release".
126BUILDS = $(foreach mode,$(MODES),$(addsuffix .$(mode),$(ARCHES)))
127# Generates corresponding test targets, e.g. "ia32.release.check".
128CHECKS = $(addsuffix .check,$(BUILDS))
129# File where previously used GYPFLAGS are stored.
130ENVFILE = $(OUTDIR)/environment
131
132.PHONY: all check clean dependencies $(ENVFILE).new native \
133        $(ARCHES) $(MODES) $(BUILDS) $(CHECKS) $(addsuffix .clean,$(ARCHES)) \
134        $(addsuffix .check,$(MODES)) $(addsuffix .check,$(ARCHES)) \
135        must-set-ANDROID_NDK_ROOT
136
137# Target definitions. "all" is the default.
138all: $(MODES)
139
140# Compile targets. MODES and ARCHES are convenience targets.
141.SECONDEXPANSION:
142$(MODES): $(addsuffix .$$@,$(DEFAULT_ARCHES))
143
144$(ARCHES): $(addprefix $$@.,$(MODES))
145
146# Defines how to build a particular target (e.g. ia32.release).
147$(BUILDS): $(OUTDIR)/Makefile-$$(basename $$@)
148	@$(MAKE) -C "$(OUTDIR)" -f Makefile-$(basename $@) \
149	         CXX="$(CXX)" LINK="$(LINK)" \
150	         BUILDTYPE=$(shell echo $(subst .,,$(suffix $@)) | \
151	                     python -c "print raw_input().capitalize()") \
152	         builddir="$(shell pwd)/$(OUTDIR)/$@"
153
154native: $(OUTDIR)/Makefile-native
155	@$(MAKE) -C "$(OUTDIR)" -f Makefile-native \
156	         CXX="$(CXX)" LINK="$(LINK)" BUILDTYPE=Release \
157	         builddir="$(shell pwd)/$(OUTDIR)/$@"
158
159# TODO(jkummerow): add "android.debug" when we need it.
160android android.release: $(OUTDIR)/Makefile-android
161	@$(MAKE) -C "$(OUTDIR)" -f Makefile-android \
162	        CXX="$(ANDROID_TOOL_PREFIX)-g++" \
163	        AR="$(ANDROID_TOOL_PREFIX)-ar" \
164	        RANLIB="$(ANDROID_TOOL_PREFIX)-ranlib" \
165	        CC="$(ANDROID_TOOL_PREFIX)-gcc" \
166	        LD="$(ANDROID_TOOL_PREFIX)-ld" \
167	        LINK="$(ANDROID_TOOL_PREFIX)-g++" \
168	        BUILDTYPE=Release \
169	        builddir="$(shell pwd)/$(OUTDIR)/android.release"
170
171# Test targets.
172check: all
173	@tools/test-wrapper-gypbuild.py $(TESTJOBS) --outdir=$(OUTDIR) \
174	    --arch=$(shell echo $(DEFAULT_ARCHES) | sed -e 's/ /,/g') \
175	    $(TESTFLAGS)
176
177$(addsuffix .check,$(MODES)): $$(basename $$@)
178	@tools/test-wrapper-gypbuild.py $(TESTJOBS) --outdir=$(OUTDIR) \
179	    --mode=$(basename $@) $(TESTFLAGS)
180
181$(addsuffix .check,$(ARCHES)): $$(basename $$@)
182	@tools/test-wrapper-gypbuild.py $(TESTJOBS) --outdir=$(OUTDIR) \
183	    --arch=$(basename $@) $(TESTFLAGS)
184
185$(CHECKS): $$(basename $$@)
186	@tools/test-wrapper-gypbuild.py $(TESTJOBS) --outdir=$(OUTDIR) \
187	    --arch-and-mode=$(basename $@) $(TESTFLAGS)
188
189native.check: native
190	@tools/test-wrapper-gypbuild.py $(TESTJOBS) --outdir=$(OUTDIR)/native \
191	    --arch-and-mode=. $(TESTFLAGS)
192
193# Clean targets. You can clean each architecture individually, or everything.
194$(addsuffix .clean,$(ARCHES)):
195	rm -f $(OUTDIR)/Makefile-$(basename $@)
196	rm -rf $(OUTDIR)/$(basename $@).release
197	rm -rf $(OUTDIR)/$(basename $@).debug
198	find $(OUTDIR) -regex '.*\(host\|target\)-$(basename $@)\.mk' -delete
199
200native.clean:
201	rm -f $(OUTDIR)/Makefile-native
202	rm -rf $(OUTDIR)/native
203	find $(OUTDIR) -regex '.*\(host\|target\)-native\.mk' -delete
204
205android.clean:
206	rm -f $(OUTDIR)/Makefile-android
207	rm -rf $(OUTDIR)/android.release
208	find $(OUTDIR) -regex '.*\(host\|target\)-android\.mk' -delete
209
210clean: $(addsuffix .clean,$(ARCHES)) native.clean
211
212# GYP file generation targets.
213$(OUTDIR)/Makefile-ia32: $(GYPFILES) $(ENVFILE)
214	build/gyp/gyp --generator-output="$(OUTDIR)" build/all.gyp \
215	              -Ibuild/standalone.gypi --depth=. -Dtarget_arch=ia32 \
216	              -S-ia32 $(GYPFLAGS)
217
218$(OUTDIR)/Makefile-x64: $(GYPFILES) $(ENVFILE)
219	build/gyp/gyp --generator-output="$(OUTDIR)" build/all.gyp \
220	              -Ibuild/standalone.gypi --depth=. -Dtarget_arch=x64 \
221	              -S-x64 $(GYPFLAGS)
222
223$(OUTDIR)/Makefile-arm: $(GYPFILES) $(ENVFILE) build/armu.gypi
224	build/gyp/gyp --generator-output="$(OUTDIR)" build/all.gyp \
225	              -Ibuild/standalone.gypi --depth=. -Ibuild/armu.gypi \
226	              -S-arm $(GYPFLAGS)
227
228$(OUTDIR)/Makefile-mips: $(GYPFILES) $(ENVFILE) build/mipsu.gypi
229	build/gyp/gyp --generator-output="$(OUTDIR)" build/all.gyp \
230	              -Ibuild/standalone.gypi --depth=. -Ibuild/mipsu.gypi \
231	              -S-mips $(GYPFLAGS)
232
233$(OUTDIR)/Makefile-native: $(GYPFILES) $(ENVFILE)
234	build/gyp/gyp --generator-output="$(OUTDIR)" build/all.gyp \
235	              -Ibuild/standalone.gypi --depth=. -S-native $(GYPFLAGS)
236
237$(OUTDIR)/Makefile-android: $(GYPFILES) $(ENVFILE) build/android.gypi \
238                            must-set-ANDROID_NDK_ROOT
239	CC="${ANDROID_TOOL_PREFIX}-gcc" \
240	build/gyp/gyp --generator-output="$(OUTDIR)" build/all.gyp \
241	              -Ibuild/standalone.gypi --depth=. -Ibuild/android.gypi \
242	              -S-android $(GYPFLAGS)
243
244must-set-ANDROID_NDK_ROOT:
245ifndef ANDROID_NDK_ROOT
246	  $(error ANDROID_NDK_ROOT is not set)
247endif
248
249# Replaces the old with the new environment file if they're different, which
250# will trigger GYP to regenerate Makefiles.
251$(ENVFILE): $(ENVFILE).new
252	@if test -r $(ENVFILE) && cmp $(ENVFILE).new $(ENVFILE) >/dev/null; \
253	    then rm $(ENVFILE).new; \
254	    else mv $(ENVFILE).new $(ENVFILE); fi
255
256# Stores current GYPFLAGS in a file.
257$(ENVFILE).new:
258	@mkdir -p $(OUTDIR); echo "GYPFLAGS=$(GYPFLAGS)" > $(ENVFILE).new;
259
260# Dependencies.
261dependencies:
262	svn checkout --force http://gyp.googlecode.com/svn/trunk build/gyp \
263	    --revision 1026
264