diff --git a/runtime/BUILD.gn b/runtime/BUILD.gn index 8bba43823ec..ecf8a5d55c0 100644 --- a/runtime/BUILD.gn +++ b/runtime/BUILD.gn @@ -165,13 +165,20 @@ config("dart_config") { "-fno-exceptions", ] - if (dart_debug) { + ldflags = [] + if (is_clang && dart_vm_code_coverage) { + cflags += [ + "-O0", + "-fprofile-arcs", + "-ftest-coverage", + ] + ldflags += [ "--coverage" ] + } else if (dart_debug) { cflags += [ "-O${dart_debug_optimization_level}" ] } else { cflags += [ "-O3" ] } - ldflags = [] if (defined(is_asan) && is_asan) { ldflags += [ "-fsanitize=address" ] } diff --git a/runtime/runtime_args.gni b/runtime/runtime_args.gni index e986bbcd2f9..9009ed43d57 100644 --- a/runtime/runtime_args.gni +++ b/runtime/runtime_args.gni @@ -31,9 +31,13 @@ declare_args() { # Available options are: arm, arm64, x64, ia32, and dbc. dart_target_arch = target_cpu - # The optimization level to use for debug builds. + # The optimization level to use for debug builds. Defaults to 0 for builds with + # code coverage enabled. dart_debug_optimization_level = "2" + # Whether to enable code coverage for the standalone VM. + dart_vm_code_coverage = false + # Whether to fall back to built-in root certificates when they cannot be # verified at the operating system level. dart_use_fallback_root_certificates = false diff --git a/tools/gn.py b/tools/gn.py index 877e73547ca..c2546a1714d 100755 --- a/tools/gn.py +++ b/tools/gn.py @@ -230,6 +230,9 @@ def ToGnArgs(args, mode, arch, target_os): gn_args['target_cpu']) gn_args['is_clang'] = args.clang and not dont_use_clang + enable_code_coverage = args.code_coverage and gn_args['is_clang'] + gn_args['dart_vm_code_coverage'] = enable_code_coverage + gn_args['is_asan'] = args.asan and gn_args['is_clang'] gn_args['is_msan'] = args.msan and gn_args['is_clang'] gn_args['is_tsan'] = args.tsan and gn_args['is_clang'] @@ -262,7 +265,11 @@ def ToGnArgs(args, mode, arch, target_os): gn_args['use_goma'] = False gn_args['goma_dir'] = None - if args.debug_opt_level: + # Code coverage requires -O0 to be set. + if enable_code_coverage: + gn_args['dart_debug_optimization_level'] = 0 + gn_args['debug_optimization_level'] = 0 + elif args.debug_opt_level: gn_args['dart_debug_optimization_level'] = args.debug_opt_level gn_args['debug_optimization_level'] = args.debug_opt_level @@ -384,6 +391,11 @@ def parse_args(args): help='Disable Clang', dest='clang', action='store_false') + other_group.add_argument('--code-coverage', + help='Enable code coverage for the standalone VM', + default=False, + dest="code_coverage", + action='store_true') other_group.add_argument('--debug-opt-level', '-d', help='The optimization level to use for debug builds',