| #!/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. |
| |
| ### set build configuration |
| |
| ## usage: fx set TARGET |
| ## [--release] [--packages P1,P2,...] |
| ## [--netboot] [--bootfs] |
| ## [--build-dir DIR] |
| ## [--args ARG] [--help-args [ARG]] [--variant VARIANT] |
| ## [--goma|--no-goma] [--no-ensure-goma] |
| ## [--goma-dir DIR] |
| ## [--ccache|--no-ccache] |
| ## |
| ## where TARGET is x64, arm64 |
| ## |
| ## Sets build options. |
| ## |
| ## optional arguments: |
| ## --release Build the release variant (e.g., more compiler |
| ## optimizations, less debugging information). |
| ## --packages Include the listed packages (separated by commas) in the |
| ## system build. Defaults to the default set of packages |
| ## for the current layer (e.g., "garnet/packages/default" |
| ## for the Garnet layer). If the --packages argument is |
| ## given multiple times, all the specified packages are |
| ## included in the build. |
| ## --variant Pass --variant=VARIANT to gen.py, which itself is |
| ## a shorthand for setting the `select_variant` GN |
| ## build argument using a standard set of values. |
| ## Multiple --variant arguments can be used. |
| ## --args Additional argument to pass to gn. If the --args |
| ## argument is given multiple times, all the specified |
| ## arguments are passed to gn. |
| ## N.B. Arguments must be expressed using GN's syntax. |
| ## In particular this means that for strings they must |
| ## be quoted with double-quotes, and the quoting must |
| ## survive, for example, the shell. Thus when passing |
| ## an argument that takes a string, pass it with |
| ## something like --args=foo='"bar"'. E.g., |
| ## bash$ fx set x64 --args=foo='"bar"' |
| ## More complicated arguments, e.g., lists, require |
| ## their own special syntax. See GN documentation |
| ## for the syntax of each kind of argument. |
| ## --help-args Display GN arguments documentation. If --help-args |
| ## is followed by a GN build argument identifier, just |
| ## that argument's documentation is displayed. |
| ## If --help-args is used alone, all GN build arguments |
| ## are displayed (lots of output). |
| ## This option requires an existing build directory. |
| ## --build-dir The directory (relative to FUCHSIA_DIR) into which to |
| ## generate the build system. Defaults to |
| ## "out/<variant>-<arch>". |
| ## --goma|--no-goma Whether to use the goma service during the build. Goma |
| ## attempts to make builds faster using remote build |
| ## servers. Defaults to detecting whether goma is installed |
| ## on your machine. |
| ## --no-ensure-goma Skip ensuring that goma is started when using goma. |
| ## --goma-dir The directory where goma is installed. Defaults to |
| ## ~/goma. |
| ## --ccache|--no-ccache Whether to use ccache during the build. Ccache attempts |
| ## to make builds faster by caching build artifacts. |
| ## Defaults to detecting whether the CCACHE_DIR environment |
| ## variable is set to a directory. |
| ## --ide Pass --ide=VALUE to gn when generating to create project |
| ## files usable with that IDE. Useful values include "vs" |
| ## for Visual Studio or "xcode" for Xcode. |
| |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"/lib/vars.sh |
| |
| function main { |
| if [[ $# -lt 1 ]]; then |
| fx-command-help |
| return 1 |
| fi |
| |
| local arch= |
| case $1 in |
| x64 | x86 | x64-64) |
| arch=x64 |
| ;; |
| arm64 | aarch64) |
| arch=arm64 |
| ;; |
| *) |
| echo Unknown target \"$1\" |
| fx-command-help |
| return 1 |
| ;; |
| esac |
| |
| local gen_args="${gen_args} --target_cpu ${arch}" |
| shift |
| |
| local variant=debug |
| local packages= |
| local extra_packages= |
| local build_dir= |
| local use_goma |
| local goma_dir |
| local ensure_goma=1 |
| local ccache |
| while [[ $# -ne 0 ]]; do |
| case $1 in |
| --release) |
| gen_args="${gen_args} --release" |
| variant=release |
| ;; |
| --packages) |
| if [[ $# -lt 2 ]]; then |
| fx-command-help |
| return 1 |
| fi |
| if [[ -z "${packages}" ]]; then |
| packages="$2" |
| else |
| packages="${packages},$2" |
| fi |
| shift |
| ;; |
| --netboot) |
| extra_packages="${extra_packages},build/packages/netboot" |
| ;; |
| --bootfs) |
| extra_packages="${extra_packages},build/packages/bootfs" |
| gen_args="${gen_args} --args bootfs_packages=true" |
| ;; |
| --build-dir) |
| if [[ $# -lt 2 ]]; then |
| fx-command-help |
| return 1 |
| fi |
| build_dir="$2" |
| shift |
| ;; |
| --goma) |
| use_goma=1 |
| ;; |
| --no-goma) |
| use_goma=0 |
| ;; |
| --no-ensure-goma) |
| ensure_goma=0 |
| ;; |
| --goma-dir) |
| if [[ $# -lt 2 ]]; then |
| fx-command-help |
| return 1 |
| fi |
| goma_dir=$2 |
| if [[ ! -d "${goma_dir}" ]]; then |
| echo -e "error: GOMA directory does not exist: "${goma_dir}"" |
| return 1 |
| fi |
| shift |
| ;; |
| --variant) |
| if [[ $# -lt 2 ]]; then |
| fx-command-help |
| return 1 |
| fi |
| gen_args="${gen_args} --variant $2" |
| shift |
| ;; |
| --args) |
| if [[ $# -lt 2 ]]; then |
| fx-command-help |
| return 1 |
| fi |
| gen_args="${gen_args} --args $2" |
| shift |
| ;; |
| --help-args) |
| gen_args="${gen_args} --help-args" |
| if [[ $# -ge 2 ]] && [[ "$2" != '--*' ]]; then |
| gen_args="${gen_args} $2" |
| shift |
| fi |
| ;; |
| --ccache) |
| ccache=1 |
| ;; |
| --no-ccache) |
| ccache=0 |
| ;; |
| --ide) |
| if [[ $# -lt 2 ]]; then |
| fx-command-help |
| return 1 |
| fi |
| gen_args="${gen_args} --ide=$2" |
| shift |
| ;; |
| *) |
| fx-command-help |
| return 1 |
| esac |
| shift |
| done |
| |
| if [[ -n "${packages}" ]]; then |
| gen_args="${gen_args} --packages ${packages}${extra_packages}" |
| else |
| local -r jiri_manifest="${FUCHSIA_DIR}/.jiri_manifest" |
| |
| # TODO(pylaligand): remove this check once the Fuchsia population has moved |
| # away from that specific manifest. |
| if grep -q "\"fuchsia\"" "${jiri_manifest}"; then |
| echo -e "error: You are using the obsolete 'fuchsia' manifest." |
| echo -e "error: Please switch to the Topaz manifest instead: fx set-layer topaz" |
| return 1 |
| fi |
| |
| # TODO(abarth): We might want to make this logic more sophisticated once we |
| # have a mechanism for switching between layers or for selecting which |
| # layers to build from source. |
| |
| # If multiple cases below trigger, gen.py will error out because gen.py |
| # doesn't support multiple --packages arguments. However, we shouldn't get |
| # into this situation because jiri would have already errored out due to the |
| # conflicting version requirements of importing 2+ layers at the same time. |
| |
| if grep -q "manifest/garnet" "${jiri_manifest}"; then |
| gen_args="${gen_args} --packages garnet/packages/default${extra_packages}" |
| fi |
| |
| if grep -q "manifest/peridot" "${jiri_manifest}"; then |
| gen_args="${gen_args} --packages peridot/packages/default${extra_packages}" |
| fi |
| |
| if grep -q "manifest/topaz" "${jiri_manifest}"; then |
| gen_args="${gen_args} --packages topaz/packages/default${extra_packages}" |
| fi |
| |
| if [[ `cat ${jiri_manifest}` =~ vendor/([a-zA-Z0-9_-]+) ]]; then |
| local -r name="${BASH_REMATCH[1]}" |
| gen_args="${gen_args} --packages vendor/${name}/packages/default${extra_packages}" |
| fi |
| fi |
| |
| if [[ -n "${build_dir}" ]]; then |
| gen_args="${gen_args} --build-dir ${build_dir}" |
| else |
| # gen.py defaults to determining the build directory from the variant and |
| # the arch. We replicate that logic here rather than passing a redundant |
| # argument to gen.py. |
| build_dir="out/${variant}-${arch}" |
| fi |
| |
| # If a goma directory wasn't specified explicitly then default to "~/goma". |
| if [[ -z "${goma_dir}" ]]; then |
| goma_dir="$HOME/goma" |
| fi |
| |
| # Automatically detect goma and ccache if not specified explicitly. |
| if [[ -z "${use_goma}" ]] && [[ -z "${ccache}" ]]; then |
| if [[ -d "${goma_dir}" ]]; then |
| use_goma=1 |
| elif [[ -n "${CCACHE_DIR}" ]] && [[ -d "${CCACHE_DIR}" ]]; then |
| ccache=1 |
| fi |
| fi |
| |
| # Add --goma or --ccache as appropriate. |
| if [[ "${use_goma}" -eq 1 ]]; then |
| gen_args="${gen_args} --goma ${goma_dir}" |
| elif [[ "${ccache}" -eq 1 ]]; then |
| gen_args="${gen_args} --ccache" |
| fi |
| |
| # Update print-vars if you add or remove anything from this list. |
| cat >"${FUCHSIA_CONFIG}" <<END |
| # Generated by "fx set". |
| FUCHSIA_BUILD_DIR="${build_dir}" |
| FUCHSIA_VARIANT="${variant}" |
| FUCHSIA_ARCH="${arch}" |
| END |
| |
| "${FUCHSIA_DIR}/build/gn/gen.py" ${gen_args} "$@" || return $? |
| |
| if [[ "${use_goma}" -eq 1 ]] && [[ "${ensure_goma}" -eq 1 ]]; then |
| if ! [[ $("${goma_dir}/gomacc" port) =~ ^[0-9]+$ ]]; then |
| "${goma_dir}/goma_ctl.py" ensure_start || return $? |
| fi |
| fi |
| } |
| |
| main "$@" |