autohooks.sh is a Bash script that generates a MOZ_BUILD_HOOK
file automatically for Mozilla builds. It detects which directories have changed between the current HEAD
/tip
and a specified git
merge base (defaulting to bookmarks/central
) or hg revision, and then disables compiler optimizations (COMPILE_FLAGS["OPTIMIZE"] = []
) in those directories.
By selectively disabling optimizations where you’re actively working, you gain faster compile times and easier debugging, without needing to manually edit moz.build
in each changed directory or adjust a MOZ_BUILD_HOOK
file manually.
-
Make the script executable:
chmod +x /path/to/autohooks.sh
-
Add to your
.mozconfig
:ac_add_options MOZ_BUILD_HOOK=$(/path/to/autohooks.sh)
This uses
bookmarks/central
as the merge base by default.
autohooks.sh [-h] [-b <MERGE_BASE>] [-t <TEMPLATE>]
Options:
-h Show help message
-b <MERGE_BASE> Specify a custom merge base (default: bookmarks/central)
-t <TEMPLATE> Include the contents of <TEMPLATE> at the start of the hooks file
-
Default:
ac_add_options MOZ_BUILD_HOOK=$(/path/to/autohooks.sh)
Uses
bookmarks/central
as the merge base. -
Custom Merge Base:
ac_add_options MOZ_BUILD_HOOK=$(/path/to/autohooks.sh -b mybranch)
Disables optimization in directories changed between
HEAD
andmybranch
. -
Adding a Template:
ac_add_options MOZ_BUILD_HOOK=$(/path/to/autohooks.sh -t /path/to/template_hooks.txt)
Prepends custom flags or logic from
template_hooks.txt
to the generated hooks file. -
Combine Both:
ac_add_options MOZ_BUILD_HOOK=$(/path/to/autohooks.sh -t /path/to/template_hooks.txt -b mybranch)
-
Detect Changed Directories
The script uses either:git diff --name-only <merge-base> HEAD
or
hg status -n --rev <merge-base>:"last(outgoing())"
to find modified files, then checks if their directories contain a
moz.build
file. -
Disable Optimization
For each directory with changes, the script appends:if RELATIVEDIR.startswith("<directory>"): COMPILE_FLAGS["OPTIMIZE"] = []
to a temporary hooks file.
-
Reuse or Create Hooks File
If an existing hooks file in/tmp
is identical to the new one, the script reuses it; otherwise, it saves the new file under a name based on the merge base. Finally, it prints the file path forMOZ_BUILD_HOOK
to pick up.
- You frequently switch between branches and need to disable optimizations in new or modified directories on each checkout.
- You want to centralize all your “disable optimization” logic without editing individual
moz.build
files, or manually updating aMOZ_BUILD_HOOK
file. - You have a template of flags or commands you always want included (e.g., special debug flags).
This script simplifies the process of selectively disabling optimization, letting you focus on development rather than manual build configuration.