This is an archived page. It's not actively maintained.

定制 .userconfig 文件

通过将一些 bash 代码放在 B2G 源码 .userconfig 文件中, 您能够对编译过程的一些方面进行定制。 本文将对修改能达到的效果及如何修改进行介绍。

 .userconfig 文件并没有被源代码控制,因此当您更新源码树时,您的更改不会被覆盖。它需要在   B2G 根目录下被创建; 也就是和,, 等在一个目录下。在运行您的配置和编译步骤时,需要添加该文件。

The .userconfig file, if it exists, is sourced by the script, which is in turn sourced by these scripts:, (through,, and tools/ The run-*.sh scripts use it to determine where Gecko is for your build. The script is used by every B2G related mach command.

重要: 您的 .userconfig 文件应该在 B2G 源码根目录下,而不是您的 home 目录!

Changing the Gecko source tree

By default, the build uses the gecko tree, which is cloned from a tree in github. Some people like to use mozilla-inbound, or mozilla-central. To do this create your clone whereever you like and add a line to your .userconfig which sets GECKO_PATH, for example:

export B2G_DIR=${B2G_DIR:-$(cd $(dirname $0); pwd)}
echo "B2G_DIR = ${B2G_DIR}"

export GECKO_PATH=${B2G_DIR}/mozilla-inbound

Note: if building against a custom Gecko in Mac OS X, the mozilla-central directory must be in a case sensitive file system or else GECKO_PATH will be ignored. You can check whether the file system is case sensitive by running this in a Terminal window:

echo -n This file system is case->tmp; echo -n in>>TMP; echo sensitive>>tmp; cat tmp

Getting B2G_DIR the way it is above allows your .userconfig to work without having hard-coded paths.

Changing Gaia settings

Sometimes, you'd like to be able to change gaia settings. For example, enabling adb in a user build. The gaia Makefile passes in --override build/custom-settings.json when calling build/, so we can write some bash which will write {"devtools.debugger.remote-enabled": true} into the custom-settings.json file. We try to avoid changing custom-settings.json if we don't need to, so we actually write into and if the contents differ from custom-settings.json then we'll replace it.

export GAIA_PATH=${GAIA_PATH:-$(cd gaia; pwd)}
export CUSTOM_SETTINGS="${GAIA_PATH}/build/config/custom-settings.json"
cat > "${CUSTOM_SETTINGS}.new" <<EOF
{"devtools.debugger.remote-enabled": true}
if [[ -f ${CUSTOM_SETTINGS} ]] && cmp "${CUSTOM_SETTINGS}" "${CUSTOM_SETTINGS}.new" >& /dev/null; then
  rm "${CUSTOM_SETTINGS}.new"

Another easier way is to configure a build/config/custom-prefs.js file in the Gaia working directory, so that means in gaia/build/config/custom-prefs.js if you're in the B2G directory. See Gaia Build System Primer, Customizing the preferences.

Note:  Currently the build is not smart enough to look in a different directory based on GAIA_PATH.  This is different from how GECKO_PATH behaves.  If you wish to use a separate Gaia clone you can manually run make from that directory.

Create a debug build

To build a debug build, put the following line in your .userconfig file:

export B2G_DEBUG=1

Profiling build

To enable profiling (for best results with the built-in (SPS) platform profiler), add the following to your .userconfig file then rebuild:


Do NOT pair with B2G_NOOPT. The results will be meaningless!

Disable the optimizer

To disable the optimizer (which may create builds that are easier to debug), add the following to your .userconfig file then rebuild:

export B2G_NOOPT=1

Disable First Time User experience

If you build and reflash a lot, going through the First Time User experience constantly can be annoying. You can disable this by adding the following to your .userconfig:

export NOFTU=1

Enable gaia developer mode

If you plan to develop apps or hack gaia, you can automatically set various usefull settings and preferences to ease working with the device. For example, it will automatically enable the remote debugging feature and disable the prompt when an incoming debugging connection starts.

What you need is the following export added to your .userconfig:


Enable valgrind

Valgrind is useful for debugging memory or threading issues with your application. For more information on running valgrind, see Debugging B2G using valgrind [en-US].

To use valgrind under B2G, add the following to your .userconfig:

export B2G_VALGRIND=1

Changing the default host compiler

On some recent distributions which use GCC 4.7 as the default compiler you will need to specify an older version in order to be able to build, to do so add two lines to your .userconfig file setting the CC and CXX variables to set the alternate C and C++ compilers respectively. For example to set the GCC 4.6 compiler on Ubuntu 12.10 use:

export CC=gcc-4.6
export CXX=g++-4.6

Or if you're using a version built from sources provide the full path to the exectuables:

export CC=/opt/gcc-4.6.4/bin/gcc
export CXX=/opt/gcc-4.6.4/bin/g++

Specify a custom Gecko object tree location

Once you start changing gecko source trees and other build options, you may want to also modify where your objects get stored (so, for example, all of your debug objects go into a different tree from your non-debug objects). So you might do something like:

export GECKO_OBJDIR=${GECKO_PATH}/objdir-gonk-debug

Using ${GECKO_PATH} makes it easy to switch between different gecko trees (eg: central, beta, aurora, etc).

Keeping both debug and non-debug objects

You can use your .userconfig file to switch back and forth between debug and release builds without having to rebuild everything each time!

export B2G_DIR=${B2G_DIR:-$(cd $(dirname $0); pwd)}
echo "B2G_DIR = ${B2G_DIR}"

export GECKO_PATH=${B2G_DIR}/mozilla-inbound

export B2G_DEBUG=1
echo "B2G_DEBUG = ${B2G_DEBUG}"

export GECKO_OBJDIR=${GECKO_PATH}/objdir-gonk
if [[ "${B2G_DEBUG}" != "0" ]]; then
if [[ "${GECKO_PATH/*mozilla-inbound*/mozilla-inbound}" == "mozilla-inbound" ]]; then

The echo commands help remind you what your current settings are. To switch between debug and release builds, simply change the value of B2G_DEBUG on line 7.