| # Copyright 2019 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. |
| |
| import("$zx/public/gn/toolchain/environment_redirect.gni") |
| import("$zx/public/gn/unification/zx_manifest.gni") |
| |
| # True iff the current toolchain builds hermetic modules. |
| is_hermetic = toolchain.tags + [ "hermetic" ] - [ "hermetic" ] != toolchain.tags |
| |
| # Define a loadable_module() for use as a hermetic compute engine. |
| # |
| # This just like loadable_module() but it builds the code in the special |
| # environment necessary to be loaded as a hermetic module. |
| # |
| # TODO(mcgrathr): Currently it's far too difficult to build code for this |
| # environment, so we're reusing some of the hackery done for userboot. With |
| # the right compiler-driver and linkable runtime support, it should be |
| # possible to make "hermetic mode" just a config() (i.e. compilation flags |
| # and/or static libraries) that can simply be added to any loadable_module(). |
| # But note that in this build, it would still need to avoid any instrumented |
| # variants, e.g. via all terminal targets passing variant_target() |
| # target.exclude_variant_tags = invoker.exclude_variant_tags. |
| # |
| # Parameters |
| # |
| # install_path |
| # - Optional: See loadable_module(). hermetic_module() gives |
| # this a different default, but it has the same meaning. |
| # - Default: "lib/hermetic/$output_name.so" |
| # |
| # See loadable_module() for other parameters and details. |
| # |
| template("hermetic_module") { |
| main_target = target_name |
| if (is_hermetic) { |
| # This toolchain builds things in hermetic mode. |
| loadable_module(main_target) { |
| forward_variables_from(invoker, "*") |
| if (defined(visibility)) { |
| visibility += [ ":$target_name" ] |
| } |
| if (!defined(output_name)) { |
| output_name = target_name |
| } |
| if (!defined(install_path)) { |
| install_path = "lib/hermetic/$output_name.so" |
| } |
| |
| # TODO(48501): This config is only in the userboot environment's list |
| # to work around fxb/48501. Since hermetic modules can contain data, |
| # they shouldn't use the linker script. |
| configs -= [ "$zx/public/gn/config:rodso" ] |
| } |
| } else { |
| # Piggy-back on the userboot toolchain to build hermetic code for us. |
| not_needed(invoker, "*") |
| module_target = "$target_name.module" |
| environment_redirect(module_target) { |
| forward_variables_from(invoker, |
| [ |
| "testonly", |
| "visibility", |
| ]) |
| environment_label = "$zx/kernel/lib/userabi/userboot" |
| direct = true |
| deps = [ ":$main_target" ] |
| } |
| group_deps = [ ":$module_target" ] |
| |
| # Declare the manifest in the non-hermetic, non-fuzzer, non-shared, |
| # non-instrumented toolchain: this ensures it only gets picked up once. |
| is_shlib = defined(toolchain.shlib) && toolchain.shlib == current_toolchain |
| is_fuzzer = string_replace(current_toolchain, "fuzzer", "") != current_toolchain |
| not_needed([ "is_fuzzer" ]) # Might go unused if is_shlib == false... |
| if (!is_shlib && !is_fuzzer && !is_kernel) { |
| manifest_target = "$target_name.manifest" |
| zx_manifest(manifest_target) { |
| forward_variables_from(invoker, [ "testonly" ]) |
| |
| target = ":$module_target" |
| |
| name = "hermetic.$main_target" |
| if (toolchain.tags + [ "instrumented" ] - [ "instrumented" ] != |
| toolchain.tags) { |
| name += toolchain.variant_suffix |
| } |
| } |
| group_deps += [ ":$manifest_target" ] |
| } |
| |
| group(main_target) { |
| forward_variables_from(invoker, [ "testonly" ]) |
| deps = group_deps |
| } |
| } |
| } |