| #!/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]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $? |
| fx-config-read |
| |
| function main { |
| local args=() |
| local have_load=false |
| local have_jobs=false |
| while [[ $# -gt 0 ]]; do |
| case "$1" in |
| -l) have_load=true ;; |
| -j) have_jobs=true ;; |
| esac |
| args+=("$1") |
| shift |
| done |
| |
| if ! $have_load; then |
| if [[ "$(uname -s)" == "Darwin" ]]; then |
| # Load level on Darwin is quite different from that of Linux, wherein a |
| # load level of 1 per CPU is not necessarily a prohibitive load level. An |
| # unscientific study of build side effects suggests that cpus*20 is a |
| # reaosnable value to prevent catastrophic load (i.e. user can not kill |
| # the build, can not lock the screen, etc). |
| local cpus="$(fx-cpu-count)" |
| args=("-l" $(($cpus * 20)) "${args[@]}") |
| fi |
| fi |
| |
| if ! $have_jobs; then |
| local concurrency="$(fx-choose-build-concurrency)" |
| # macOS in particular has a low default for number of open file descriptors |
| # per process, which is prohibitive for higher job counts. Here we raise |
| # the number of allowed file descriptors per process if it appears to be |
| # low in order to avoid failures due to the limit. See `getrlimit(2)` for |
| # more information. |
| local min_limit=$((${concurrency} * 2)) |
| if [[ $(ulimit -n) -lt "${min_limit}" ]]; then |
| ulimit -n "${min_limit}" |
| fi |
| args=("-j" "${concurrency}" "${args[@]}") |
| fi |
| |
| |
| # 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). |
| fx-try-locked env -i TERM="${TERM}" PATH="${PATH}" \ |
| ${NINJA_STATUS+"NINJA_STATUS=${NINJA_STATUS}"} \ |
| ${TMPDIR+"TMPDIR=$TMPDIR"} \ |
| "${FUCHSIA_DIR}/buildtools/ninja" -C "${FUCHSIA_BUILD_DIR}" "${args[@]}" |
| } |
| |
| main "$@" |