blob: 0ac946564d6bd67c8f809a00e093bd1bced1a0fe [file] [log] [blame]
#!/bin/bash
# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
### Run Ninja to build Fuchsia
## usage: fx build [-SWITCH...] [zircon/TARGET | TARGET ...]
##
## This runs Ninja, usually twice: once for Zircon and once for Fuchsia.
## With no explicit targets, Zircon Ninja builds its default targets and
## then Fuchsia Ninja builds its default targets.
##
## For each `zircon/TARGET` argument, `TARGET` is passed to Zircon Ninja.
## For each other `TARGET` argument, `TARGET` is passed to Fuchsia Ninja.
## If all the target arguments are `zircon/TARGET`, Fuchsia Ninja is not run.
##
## Switch arguments are passed through to Ninja (same switches for both).
## Run `fx build -h` to see Ninja argument details.
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $?
fx-config-read
function run-ninja {
local continue_on_error=false
for (( i=1; i<=$#; i++)); do
j=$((i+1))
if [ "${!i}" = "-k" ] && [ "${!j}" = "0" ]; then
continue_on_error=true
fi
done
# Use a subshell because fx-cmd-locked locks until exit, not function return.
if [ "$continue_on_error" = false ]; then
(fx-run-ninja "${FUCHSIA_DIR}/buildtools/ninja" "$@") || exit
else
local status
(fx-run-ninja "${FUCHSIA_DIR}/buildtools/ninja" "$@")
status=$?
if [ "${status}" -ne 0 ]; then
fx-warn "Build outputs may be inconsistent and may remain so; a clean build is recommended."
fi
return "${status}"
fi
}
function main {
local switches=() zircon_targets=() fuchsia_targets=()
while [ $# -gt 0 ]; do
case "$1" in
# These Ninja switches take an argument.
-[Cfjkldt])
switches+=("$1" "$2")
shift
;;
-*)
switches+=("$1")
;;
zircon/*)
zircon_targets+=("${1#zircon/}")
;;
*)
fuchsia_targets+=("$1")
;;
esac
shift
done
if [ ! -d "$ZIRCON_BUILDROOT" ]; then
echo >&2 "Re-running gn gen first (missing $ZIRCON_BUILDROOT)"
fx-gen || return
fi
# Build requested Zircon targets.
# Add default if building any Fuchsia targets.
if [ ${#fuchsia_targets[@]} -ne 0 ]; then
zircon_targets+=(default)
fi
local status
run-ninja -C "${ZIRCON_BUILDROOT}" "${switches[@]}" "${zircon_targets[@]}"
status=$?
# If there were explicit Zircon targets and no other explicit targets,
# the Zircon run was enough by itself. Otherwise Zircon default is
# a prerequisite for any Fuchsia target (including implicit default).
if [ ${#fuchsia_targets[@]} -ne 0 -o ${#zircon_targets[@]} -eq 0 ]; then
run-ninja -C "${FUCHSIA_BUILD_DIR}" "${switches[@]}" "${fuchsia_targets[@]}"
fuchsia_status=$?
if [ "${status}" -eq 0 ]; then
status="${fuchsia_status}"
fi
fi
return "${status}"
}
main "$@"