Merge m-c to mozilla-inbound
[gecko.git] / client.mk
blobc494cc64c022afa4e5cbfbdb31423fe564c3294a
1 # -*- makefile -*-
2 # vim:set ts=8 sw=8 sts=8 noet:
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 # Build a mozilla application.
9 # To build a tree,
10 # 1. hg clone ssh://hg.mozilla.org/mozilla-central mozilla
11 # 2. cd mozilla
12 # 3. create your .mozconfig file with
13 # ac_add_options --enable-application=browser
14 # 4. gmake -f client.mk
16 # Other targets (gmake -f client.mk [targets...]),
17 # build
18 # clean (realclean is now the same as clean)
19 # distclean
21 # See http://developer.mozilla.org/en/docs/Build_Documentation for
22 # more information.
24 # Options:
25 # MOZ_BUILD_PROJECTS - Build multiple projects in subdirectories
26 # of MOZ_OBJDIR
27 # MOZ_OBJDIR - Destination object directory
28 # MOZ_MAKE_FLAGS - Flags to pass to $(MAKE)
29 # MOZ_PREFLIGHT_ALL } - Makefiles to run before any project in
30 # MOZ_PREFLIGHT } MOZ_BUILD_PROJECTS, before each project, after
31 # MOZ_POSTFLIGHT } each project, and after all projects; these
32 # MOZ_POSTFLIGHT_ALL } variables contain space-separated lists
33 # MOZ_UNIFY_BDATE - Set to use the same bdate for each project in
34 # MOZ_BUILD_PROJECTS
36 #######################################################################
37 # Defines
39 comma := ,
41 CWD := $(CURDIR)
42 ifneq (1,$(words $(CWD)))
43 $(error The mozilla directory cannot be located in a path with spaces.)
44 endif
46 ifeq "$(CWD)" "/"
47 CWD := /.
48 endif
50 ifndef TOPSRCDIR
51 ifeq (,$(wildcard client.mk))
52 TOPSRCDIR := $(patsubst %/,%,$(dir $(MAKEFILE_LIST)))
53 else
54 TOPSRCDIR := $(CWD)
55 endif
56 endif
58 # try to find autoconf 2.13 - discard errors from 'which'
59 # MacOS X 10.4 sends "no autoconf*" errors to stdout, discard those via grep
60 AUTOCONF ?= $(shell which autoconf-2.13 autoconf2.13 autoconf213 2>/dev/null | grep -v '^no autoconf' | head -1)
62 # See if the autoconf package was installed through fink
63 ifeq (,$(strip $(AUTOCONF)))
64 AUTOCONF = $(shell which fink >/dev/null 2>&1 && echo `which fink`/../../lib/autoconf2.13/bin/autoconf)
65 endif
67 ifeq (,$(strip $(AUTOCONF)))
68 AUTOCONF=$(error Could not find autoconf 2.13)
69 endif
71 SH := /bin/sh
72 PERL ?= perl
73 PYTHON ?= $(shell which python2.7 > /dev/null 2>&1 && echo python2.7 || echo python)
75 CONFIG_GUESS_SCRIPT := $(wildcard $(TOPSRCDIR)/build/autoconf/config.guess)
76 ifdef CONFIG_GUESS_SCRIPT
77 CONFIG_GUESS := $(shell $(CONFIG_GUESS_SCRIPT))
78 endif
81 ####################################
82 # Sanity checks
84 # Windows checks.
85 ifneq (,$(findstring mingw,$(CONFIG_GUESS)))
87 # check for CRLF line endings
88 ifneq (0,$(shell $(PERL) -e 'binmode(STDIN); while (<STDIN>) { if (/\r/) { print "1"; exit } } print "0"' < $(TOPSRCDIR)/client.mk))
89 $(error This source tree appears to have Windows-style line endings. To \
90 convert it to Unix-style line endings, check \
91 "https://developer.mozilla.org/en-US/docs/Developer_Guide/Mozilla_build_FAQ\#Win32-specific_questions" \
92 for a workaround of this issue.)
93 endif
95 # Set this for baseconfig.mk
96 HOST_OS_ARCH=WINNT
97 endif
99 ####################################
100 # Load mozconfig Options
102 # See build pages, http://www.mozilla.org/build/ for how to set up mozconfig.
104 define CR
107 endef
109 # As $(shell) doesn't preserve newlines, use sed to replace them with an
110 # unlikely sequence (||), which is then replaced back to newlines by make
111 # before evaluation. $(shell) replacing newlines with spaces, || is always
112 # followed by a space (since sed doesn't remove newlines), except on the
113 # last line, so replace both '|| ' and '||'.
114 # Also, make MOZ_PGO available to mozconfig when passed on make command line.
115 # Likewise for MOZ_CURRENT_PROJECT.
116 MOZCONFIG_CONTENT := $(subst ||,$(CR),$(subst || ,$(CR),$(shell $(addprefix MOZ_CURRENT_PROJECT=,$(MOZ_CURRENT_PROJECT)) MOZ_PGO=$(MOZ_PGO) $(TOPSRCDIR)/mach environment --format=client.mk | sed 's/$$/||/')))
117 $(eval $(MOZCONFIG_CONTENT))
119 export FOUND_MOZCONFIG
121 # As '||' was used as a newline separator, it means it's not occurring in
122 # lines themselves. It can thus safely be used to replaces normal spaces,
123 # to then replace newlines with normal spaces. This allows to get a list
124 # of mozconfig output lines.
125 MOZCONFIG_OUT_LINES := $(subst $(CR), ,$(subst $(NULL) $(NULL),||,$(MOZCONFIG_CONTENT)))
126 # Filter-out comments from those lines.
127 START_COMMENT = \#
128 MOZCONFIG_OUT_FILTERED := $(filter-out $(START_COMMENT)%,$(MOZCONFIG_OUT_LINES))
130 ifdef AUTOCLOBBER
131 export AUTOCLOBBER=1
132 endif
133 export MOZ_PGO
135 ifdef MOZ_PARALLEL_BUILD
136 MOZ_MAKE_FLAGS := $(filter-out -j%,$(MOZ_MAKE_FLAGS))
137 MOZ_MAKE_FLAGS += -j$(MOZ_PARALLEL_BUILD)
138 endif
140 # Automatically add -jN to make flags if not defined. N defaults to number of cores.
141 ifeq (,$(findstring -j,$(MOZ_MAKE_FLAGS)))
142 cores=$(shell $(PYTHON) -c 'import multiprocessing; print(multiprocessing.cpu_count())')
143 MOZ_MAKE_FLAGS += -j$(cores)
144 endif
147 ifdef MOZ_BUILD_PROJECTS
149 ifdef MOZ_CURRENT_PROJECT
150 BUILD_PROJECT_ARG = MOZ_BUILD_APP=$(MOZ_CURRENT_PROJECT)
151 export MOZ_CURRENT_PROJECT
152 else
153 MOZ_MAKE = $(error Cannot build in the OBJDIR when MOZ_CURRENT_PROJECT is not set.)
154 endif
155 endif # MOZ_BUILD_PROJECTS
157 MOZ_MAKE = $(MAKE) $(MOZ_MAKE_FLAGS) -C $(OBJDIR)
159 # 'configure' scripts generated by autoconf.
160 CONFIGURES := $(TOPSRCDIR)/configure
161 CONFIGURES += $(TOPSRCDIR)/js/src/configure
163 # Make targets that are going to be passed to the real build system
164 OBJDIR_TARGETS = install export libs clean realclean distclean maybe_clobber_profiledbuild upload sdk installer package package-compare stage-package source-package l10n-check automation/build
166 #######################################################################
167 # Rules
169 # The default rule is build
170 build::
171 $(MAKE) -f $(TOPSRCDIR)/client.mk $(if $(MOZ_PGO),profiledbuild,realbuild) CREATE_MOZCONFIG_JSON=
173 # Include baseconfig.mk for its $(MAKE) validation.
174 include $(TOPSRCDIR)/config/baseconfig.mk
176 # Define mkdir
177 include $(TOPSRCDIR)/config/makefiles/makeutils.mk
178 include $(TOPSRCDIR)/config/makefiles/autotargets.mk
180 # Create a makefile containing the mk_add_options values from mozconfig,
181 # but only do so when OBJDIR is defined (see further above).
182 ifdef MOZ_BUILD_PROJECTS
183 ifdef MOZ_CURRENT_PROJECT
184 WANT_MOZCONFIG_MK = 1
185 else
186 WANT_MOZCONFIG_MK =
187 endif
188 else
189 WANT_MOZCONFIG_MK = 1
190 endif
192 ifdef WANT_MOZCONFIG_MK
193 # For now, only output "export" lines and lines containing UPLOAD_EXTRA_FILES
194 # from mach environment --format=client.mk output.
195 MOZCONFIG_MK_LINES := $(filter export||% UPLOAD_EXTRA_FILES% %UPLOAD_EXTRA_FILES%,$(MOZCONFIG_OUT_LINES))
196 $(OBJDIR)/.mozconfig.mk: $(TOPSRCDIR)/client.mk $(FOUND_MOZCONFIG) $(call mkdir_deps,$(OBJDIR)) $(OBJDIR)/CLOBBER
197 $(if $(MOZCONFIG_MK_LINES),( $(foreach line,$(MOZCONFIG_MK_LINES), echo '$(subst ||, ,$(line))';) )) > $@
199 # Include that makefile so that it is created. This should not actually change
200 # the environment since MOZCONFIG_CONTENT, which MOZCONFIG_OUT_LINES derives
201 # from, has already been eval'ed.
202 include $(OBJDIR)/.mozconfig.mk
203 endif
205 # Print out any options loaded from mozconfig.
206 all realbuild clean distclean export libs install realclean::
207 ifneq (,$(strip $(MOZCONFIG_OUT_FILTERED)))
208 $(info Adding client.mk options from $(FOUND_MOZCONFIG):)
209 $(foreach line,$(MOZCONFIG_OUT_FILTERED),$(info $(NULL) $(NULL) $(NULL) $(NULL) $(subst ||, ,$(line))))
210 endif
212 # Windows equivalents
213 build_all: build
214 clobber clobber_all: clean
216 # helper target for mobile
217 build_and_deploy: build package install
219 # Do everything from scratch
220 everything: clean build
222 ####################################
223 # Profile-Guided Optimization
224 # This is up here, outside of the MOZ_CURRENT_PROJECT logic so that this
225 # is usable in multi-pass builds, where you might not have a runnable
226 # application until all the build passes and postflight scripts have run.
227 profiledbuild::
228 $(MAKE) -f $(TOPSRCDIR)/client.mk realbuild MOZ_PROFILE_GENERATE=1 MOZ_PGO_INSTRUMENTED=1 CREATE_MOZCONFIG_JSON=
229 $(MAKE) -C $(OBJDIR) package MOZ_PGO_INSTRUMENTED=1 MOZ_INTERNAL_SIGNING_FORMAT= MOZ_EXTERNAL_SIGNING_FORMAT=
230 rm -f $(OBJDIR)/jarlog/en-US.log
231 MOZ_PGO_INSTRUMENTED=1 JARLOG_FILE=jarlog/en-US.log EXTRA_TEST_ARGS=10 $(MAKE) -C $(OBJDIR) pgo-profile-run
232 $(MAKE) -f $(TOPSRCDIR)/client.mk maybe_clobber_profiledbuild CREATE_MOZCONFIG_JSON=
233 $(MAKE) -f $(TOPSRCDIR)/client.mk realbuild MOZ_PROFILE_USE=1 CREATE_MOZCONFIG_JSON=
235 #####################################################
236 # Build date unification
238 ifdef MOZ_UNIFY_BDATE
239 ifndef MOZ_BUILD_DATE
240 ifdef MOZ_BUILD_PROJECTS
241 MOZ_BUILD_DATE = $(shell $(PYTHON) $(TOPSRCDIR)/toolkit/xre/make-platformini.py --print-buildid)
242 export MOZ_BUILD_DATE
243 endif
244 endif
245 endif
247 #####################################################
248 # Preflight, before building any project
250 realbuild preflight_all::
251 ifeq (,$(MOZ_CURRENT_PROJECT)$(if $(MOZ_PREFLIGHT_ALL),,1))
252 # Don't run preflight_all for individual projects in multi-project builds
253 # (when MOZ_CURRENT_PROJECT is set.)
254 ifndef MOZ_BUILD_PROJECTS
255 # Building a single project, OBJDIR is usable.
256 set -e; \
257 for mkfile in $(MOZ_PREFLIGHT_ALL); do \
258 $(MAKE) -f $(TOPSRCDIR)/$$mkfile preflight_all TOPSRCDIR=$(TOPSRCDIR) OBJDIR=$(OBJDIR) MOZ_OBJDIR=$(MOZ_OBJDIR); \
259 done
260 else
261 # OBJDIR refers to the project-specific OBJDIR, which is not available at
262 # this point when building multiple projects. Only MOZ_OBJDIR is available.
263 set -e; \
264 for mkfile in $(MOZ_PREFLIGHT_ALL); do \
265 $(MAKE) -f $(TOPSRCDIR)/$$mkfile preflight_all TOPSRCDIR=$(TOPSRCDIR) MOZ_OBJDIR=$(MOZ_OBJDIR) MOZ_BUILD_PROJECTS='$(MOZ_BUILD_PROJECTS)'; \
266 done
267 endif
268 endif
270 # If we're building multiple projects, but haven't specified which project,
271 # loop through them.
273 ifeq (,$(MOZ_CURRENT_PROJECT)$(if $(MOZ_BUILD_PROJECTS),,1))
274 configure realbuild preflight postflight $(OBJDIR_TARGETS)::
275 set -e; \
276 for app in $(MOZ_BUILD_PROJECTS); do \
277 $(MAKE) -f $(TOPSRCDIR)/client.mk $@ MOZ_CURRENT_PROJECT=$$app; \
278 done
280 else
282 # MOZ_CURRENT_PROJECT: either doing a single-project build, or building an
283 # individual project in a multi-project build.
285 ####################################
286 # Configure
288 MAKEFILE = $(wildcard $(OBJDIR)/Makefile)
289 CONFIG_STATUS = $(wildcard $(OBJDIR)/config.status)
290 CONFIG_CACHE = $(wildcard $(OBJDIR)/config.cache)
292 EXTRA_CONFIG_DEPS := \
293 $(TOPSRCDIR)/aclocal.m4 \
294 $(wildcard $(TOPSRCDIR)/build/autoconf/*.m4) \
295 $(TOPSRCDIR)/js/src/aclocal.m4 \
296 $(NULL)
298 $(CONFIGURES): %: %.in $(EXTRA_CONFIG_DEPS)
299 @echo Generating $@ using autoconf
300 cd $(@D); $(AUTOCONF)
302 CONFIG_STATUS_DEPS := \
303 $(wildcard $(TOPSRCDIR)/*/confvars.sh) \
304 $(CONFIGURES) \
305 $(TOPSRCDIR)/CLOBBER \
306 $(TOPSRCDIR)/nsprpub/configure \
307 $(TOPSRCDIR)/config/milestone.txt \
308 $(TOPSRCDIR)/browser/config/version.txt \
309 $(TOPSRCDIR)/browser/config/version_display.txt \
310 $(TOPSRCDIR)/build/virtualenv_packages.txt \
311 $(TOPSRCDIR)/python/mozbuild/mozbuild/virtualenv.py \
312 $(TOPSRCDIR)/testing/mozbase/packages.txt \
313 $(OBJDIR)/.mozconfig.json \
314 $(NULL)
316 CONFIGURE_ENV_ARGS += \
317 MAKE='$(MAKE)' \
318 $(NULL)
320 # configure uses the program name to determine @srcdir@. Calling it without
321 # $(TOPSRCDIR) will set @srcdir@ to "."; otherwise, it is set to the full
322 # path of $(TOPSRCDIR).
323 ifeq ($(TOPSRCDIR),$(OBJDIR))
324 CONFIGURE = ./configure
325 else
326 CONFIGURE = $(TOPSRCDIR)/configure
327 endif
329 $(OBJDIR)/CLOBBER: $(TOPSRCDIR)/CLOBBER
330 $(PYTHON) $(TOPSRCDIR)/config/pythonpath.py -I $(TOPSRCDIR)/testing/mozbase/mozfile \
331 $(TOPSRCDIR)/python/mozbuild/mozbuild/controller/clobber.py $(TOPSRCDIR) $(OBJDIR)
333 configure-files: $(CONFIGURES)
335 configure-preqs = \
336 $(OBJDIR)/CLOBBER \
337 configure-files \
338 $(call mkdir_deps,$(OBJDIR)) \
339 $(if $(MOZ_BUILD_PROJECTS),$(call mkdir_deps,$(MOZ_OBJDIR))) \
340 save-mozconfig \
341 $(OBJDIR)/.mozconfig.json \
342 $(NULL)
344 CREATE_MOZCONFIG_JSON = $(shell $(TOPSRCDIR)/mach environment --format=json -o $(OBJDIR)/.mozconfig.json)
345 # Force CREATE_MOZCONFIG_JSON above to be resolved, without side effects in
346 # case the result is non empty, and allowing an override on the make command
347 # line not running the command (using := $(shell) still runs the shell command).
348 ifneq (,$(CREATE_MOZCONFIG_JSON))
349 endif
351 $(OBJDIR)/.mozconfig.json: $(call mkdir_deps,$(OBJDIR)) ;
353 save-mozconfig: $(FOUND_MOZCONFIG)
354 ifdef FOUND_MOZCONFIG
355 -cp $(FOUND_MOZCONFIG) $(OBJDIR)/.mozconfig
356 endif
358 configure:: $(configure-preqs)
359 @echo cd $(OBJDIR);
360 @echo $(CONFIGURE) $(CONFIGURE_ARGS)
361 @cd $(OBJDIR) && $(BUILD_PROJECT_ARG) $(CONFIGURE_ENV_ARGS) $(CONFIGURE) $(CONFIGURE_ARGS) \
362 || ( echo '*** Fix above errors and then restart with\
363 "$(MAKE) -f client.mk build"' && exit 1 )
364 @touch $(OBJDIR)/Makefile
366 ifneq (,$(MAKEFILE))
367 $(OBJDIR)/Makefile: $(OBJDIR)/config.status
369 $(OBJDIR)/config.status: $(CONFIG_STATUS_DEPS)
370 else
371 $(OBJDIR)/Makefile: $(CONFIG_STATUS_DEPS)
372 endif
373 @$(MAKE) -f $(TOPSRCDIR)/client.mk configure CREATE_MOZCONFIG_JSON=
375 ifneq (,$(CONFIG_STATUS))
376 $(OBJDIR)/config/autoconf.mk: $(TOPSRCDIR)/config/autoconf.mk.in
377 $(PYTHON) $(OBJDIR)/config.status -n --file=$(OBJDIR)/config/autoconf.mk
378 endif
381 ####################################
382 # Preflight
384 realbuild preflight::
385 ifdef MOZ_PREFLIGHT
386 set -e; \
387 for mkfile in $(MOZ_PREFLIGHT); do \
388 $(MAKE) -f $(TOPSRCDIR)/$$mkfile preflight TOPSRCDIR=$(TOPSRCDIR) OBJDIR=$(OBJDIR) MOZ_OBJDIR=$(MOZ_OBJDIR); \
389 done
390 endif
392 ####################################
393 # Build it
395 realbuild:: $(OBJDIR)/Makefile $(OBJDIR)/config.status
396 +$(MOZ_MAKE)
398 ####################################
399 # Other targets
401 # Pass these target onto the real build system
402 $(OBJDIR_TARGETS):: $(OBJDIR)/Makefile $(OBJDIR)/config.status
403 +$(MOZ_MAKE) $@
405 ####################################
406 # Postflight
408 realbuild postflight::
409 ifdef MOZ_POSTFLIGHT
410 set -e; \
411 for mkfile in $(MOZ_POSTFLIGHT); do \
412 $(MAKE) -f $(TOPSRCDIR)/$$mkfile postflight TOPSRCDIR=$(TOPSRCDIR) OBJDIR=$(OBJDIR) MOZ_OBJDIR=$(MOZ_OBJDIR); \
413 done
414 endif
416 endif # MOZ_CURRENT_PROJECT
418 ####################################
419 # Postflight, after building all projects
421 realbuild postflight_all::
422 ifeq (,$(MOZ_CURRENT_PROJECT)$(if $(MOZ_POSTFLIGHT_ALL),,1))
423 # Don't run postflight_all for individual projects in multi-project builds
424 # (when MOZ_CURRENT_PROJECT is set.)
425 ifndef MOZ_BUILD_PROJECTS
426 # Building a single project, OBJDIR is usable.
427 set -e; \
428 for mkfile in $(MOZ_POSTFLIGHT_ALL); do \
429 $(MAKE) -f $(TOPSRCDIR)/$$mkfile postflight_all TOPSRCDIR=$(TOPSRCDIR) OBJDIR=$(OBJDIR) MOZ_OBJDIR=$(MOZ_OBJDIR); \
430 done
431 else
432 # OBJDIR refers to the project-specific OBJDIR, which is not available at
433 # this point when building multiple projects. Only MOZ_OBJDIR is available.
434 set -e; \
435 for mkfile in $(MOZ_POSTFLIGHT_ALL); do \
436 $(MAKE) -f $(TOPSRCDIR)/$$mkfile postflight_all TOPSRCDIR=$(TOPSRCDIR) MOZ_OBJDIR=$(MOZ_OBJDIR) MOZ_BUILD_PROJECTS='$(MOZ_BUILD_PROJECTS)'; \
437 done
438 endif
439 endif
441 cleansrcdir:
442 @cd $(TOPSRCDIR); \
443 if [ -f Makefile ]; then \
444 $(MAKE) distclean ; \
445 else \
446 echo 'Removing object files from srcdir...'; \
447 rm -fr `find . -type d \( -name .deps -print -o -name CVS \
448 -o -exec test ! -d {}/CVS \; \) -prune \
449 -o \( -name '*.[ao]' -o -name '*.so' \) -type f -print`; \
450 build/autoconf/clean-config.sh; \
453 echo-variable-%:
454 @echo $($*)
456 # This makefile doesn't support parallel execution. It does pass
457 # MOZ_MAKE_FLAGS to sub-make processes, so they will correctly execute
458 # in parallel.
459 .NOTPARALLEL:
461 .PHONY: checkout \
462 real_checkout \
463 realbuild \
464 build \
465 profiledbuild \
466 cleansrcdir \
467 pull_all \
468 build_all \
469 clobber \
470 clobber_all \
471 pull_and_build_all \
472 everything \
473 configure \
474 preflight_all \
475 preflight \
476 postflight \
477 postflight_all \
478 $(OBJDIR_TARGETS)