| # Copyright 2018 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("//build/config/profile/config.gni") |
| import("//build/toolchain/variant.gni") |
| |
| if (profile_source_files != []) { |
| # This can't use a generated_file() target because there's no way to |
| # express the deps to make GN accept the file in the inputs list if it's |
| # the output of some target. |
| _profile_source_files_file = "$root_out_dir/profile-source-files.list" |
| _profile_source_files_lines = [] |
| foreach(file, profile_source_files) { |
| _profile_source_files_lines += |
| [ "src:" + rebase_path(file, root_build_dir) ] |
| } |
| write_file(_profile_source_files_file, |
| _profile_source_files_lines, |
| "list lines") |
| profile_source_files_list_files += [ _profile_source_files_file ] |
| } |
| |
| # Common flags for all coverage-based variants defined below. |
| # IMPORTANT: This should not include any rust or linker flags! |
| _coverage_common_flags = [ |
| "-fprofile-instr-generate", |
| "-fcoverage-mapping", |
| ] |
| |
| _coverage_inputs = profile_source_files_list_files |
| foreach(file, profile_source_files_list_files) { |
| _coverage_common_flags += |
| [ "-fprofile-list=" + rebase_path(file, root_build_dir) ] |
| } |
| |
| variant("coverage") { |
| common_flags = _coverage_common_flags |
| inputs = _coverage_inputs |
| |
| if (!is_kernel && is_fuchsia) { |
| # The statically-linked profiling runtime depends on libzircon. |
| # TODO(fxbug.dev/61522): Ensure this works with shared_library() instances too! |
| deps = [ "//src/zircon/lib/zircon" ] |
| |
| dynamic_linker_flags = "-dynamic-linker=coverage/ld.so.1" |
| ldflags = [ "-Wl,$dynamic_linker_flags" ] |
| rustflags = [ "-Clink-arg=$dynamic_linker_flags" ] |
| } |
| } |
| |
| # This variant is similar to `coverage` but is used to only compile sdk_source_set() |
| # targets with coverage instrumentation. This works as follows: |
| # |
| # - Unlike `coverage`, this variant does not add assembler and compiler flags |
| # to enable profiling instrumentation. |
| # |
| # - The sdk_source_set() template adds the "coverage-sdk-source-config" config |
| # to its inner target when they are built in the "coverage-sdk" variant. This |
| # adds the necessary cflags/asmflags when building the target. |
| # |
| # - All final binaries (executables and shared libraries) are linked to the |
| # profiling runtime. Even if none of their code is actually instrumented. |
| # |
| # - Since this is an instrumented build variant, the libdir is 'lib/coverage-sdk'. |
| # |
| variant("coverage-sdk") { |
| inputs = _coverage_inputs |
| |
| if (!is_kernel && is_fuchsia) { |
| # The statically-linked profiling runtime depends on libzircon. |
| # TODO(fxbug.dev/61522): Ensure this works with shared_library() instances too! |
| deps = [ "//src/zircon/lib/zircon" ] |
| } |
| |
| # All instrumented build variants place their dynamic loader and dependency |
| # libraries under lib/<variant>/ so ensure generated executables will find |
| # it here. See toolchain_variant.libprefix definition. |
| dynamic_linker_flags = "-dynamic-linker=coverage-sdk/ld.so.1" |
| ldflags = [ "-Wl,$dynamic_linker_flags" ] |
| rustflags = [ "-Clink-arg=$dynamic_linker_flags" ] |
| |
| ldflags += [ "-fprofile-instr-generate" ] |
| } |
| |
| # This config should only be added to sdk_source_set() targets. |
| config("coverage-sdk-source-config") { |
| cflags = _coverage_common_flags |
| asmflags = _coverage_common_flags |
| } |
| |
| variant("profile") { |
| common_flags = _coverage_common_flags |
| |
| inputs = profile_source_files_list_files |
| foreach(file, profile_source_files_list_files) { |
| common_flags += [ "-fprofile-list=" + rebase_path(file, root_build_dir) ] |
| } |
| |
| if (!is_kernel && is_fuchsia) { |
| # The statically-linked profiling runtime depends on libzircon. |
| # TODO(fxbug.dev/61522): Ensure this works with shared_library() instances too! |
| deps = [ "//src/zircon/lib/zircon" ] |
| |
| dynamic_linker_flags = "-dynamic-linker=profile/ld.so.1" |
| ldflags = [ "-Wl,$dynamic_linker_flags" ] |
| rustflags = [ "-Clink-arg=$dynamic_linker_flags" ] |
| } |
| } |
| |
| # Only enable either "coverage-rust" or clang coverage (variant("profile") or variant("coverage")), |
| # not both. |
| # |
| # -Zinstrument-coverage is known to work with -Copt-level=1 (or 2 or 3, possibly others) |
| # in tested cases. If helpful, we may want to include it. |
| # |
| # DO NOT NAME THIS WITH PREFIX `rust-`. The prefix will be stripped in some cases, and fail to |
| # work with other cases that do not expect the stripped prefix. |
| variant("coverage-rust") { |
| rustflags = [ "-Zinstrument-coverage" ] |
| |
| # TODO: Update this once rust updates its LLVM to version 13. |
| if (is_fuchsia) { |
| rustflags += [ "-Clink-arg=-znostart-stop-gc" ] |
| } else { |
| rustflags += [ "-Clink-arg=-Wl,-z,nostart-stop-gc" ] |
| } |
| |
| if (!is_kernel && is_fuchsia) { |
| # The statically-linked profiling runtime depends on libzircon. |
| # TODO(fxbug.dev/61522): Ensure this works with shared_library() instances too! |
| deps = [ "//src/zircon/lib/zircon" ] |
| |
| dynamic_linker_flags = "-dynamic-linker=coverage-rust/ld.so.1" |
| ldflags = [ "-Wl,$dynamic_linker_flags" ] |
| rustflags += [ "-Clink-arg=$dynamic_linker_flags" ] |
| } |
| } |