| #!/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. |
| |
| ### build Fuchsia |
| |
| ## usage: fx build [ninja option,...] [target,...] |
| |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"/lib/vars.sh |
| fx-config-read |
| |
| readonly LOCK_FILE="${FUCHSIA_DIR}"/.build_lock |
| |
| # Use a lock file around a command if possible. |
| # Print a message if the lock isn't immediately entered, |
| # and block until it is. |
| function try_locked { |
| if !(command -v shlock >/dev/null); then |
| # Can't lock! Fall back to unlocked operation. |
| "$@" |
| elif shlock -f "${LOCK_FILE}" -p $$; then |
| # This will cause a deadlock if any subcommand calls back to fx build, |
| # because shlock isn't reentrant by forked processes. |
| cmd_locked "$@" |
| else |
| echo "Locked by ${LOCK_FILE}..." |
| while ! shlock -f "${LOCK_FILE}" -p $$; do sleep .1; done |
| cmd_locked "$@" |
| fi |
| } |
| |
| # Exit trap to clean up lock file |
| function _rmlock () { |
| [[ -n "${LOCK_FILE}" ]] && rm -f "${LOCK_FILE}" |
| } |
| |
| function cmd_locked { |
| trap _rmlock EXIT |
| "$@" |
| exit $? |
| } |
| |
| function main { |
| # TERM is passed for the pretty ninja UI |
| # PATH is passed as some tools are referenced via $PATH due to platform differences. |
| # TMPDIR is passed for Goma on macOS. TMPDIR must be set, or unset, not |
| # empty. Some Dart build tools have been observed writing into source paths |
| # when TMPDIR="" - it is deliberately unquoted and using the ${+} expansion |
| # expression). |
| try_locked env -i TERM="${TERM}" PATH="${PATH}" ${TMPDIR+"TMPDIR=$TMPDIR"} \ |
| "${FUCHSIA_DIR}/buildtools/ninja" -j "$(fx-choose-build-concurrency)" \ |
| -l "$(fx-cpu-count)" \ |
| -C "${FUCHSIA_BUILD_DIR}" "$@" |
| } |
| |
| main "$@" |