testing.gni 7.7 KB
Newer Older
M
Michael Goderbauer 已提交
1
# Copyright 2013 The Flutter Authors. All rights reserved.
2 3 4
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

5
import("//build/compiled_action.gni")
6
import("//flutter/common/config.gni")
7 8
import("//third_party/dart/build/dart/dart_action.gni")

9 10 11
is_aot_test =
    flutter_runtime_mode == "profile" || flutter_runtime_mode == "release"

12 13 14 15 16
# Unit tests targets are only enabled for host machines and Fuchsia right now
declare_args() {
  enable_unittests = current_toolchain == host_toolchain || is_fuchsia
}

17 18
# Creates a translation unit that defines the flutter::testing::GetFixturesPath
# method that tests can use to locate their fixtures.
19
#
20 21 22
# Arguments
#     assets_dir (required): The assets directory
template("fixtures_location") {
23 24
  testonly = true

25
  assert(defined(invoker.assets_dir), "The assets directory.")
26

27
  location_path = rebase_path(invoker.assets_dir)
28 29 30 31

  # Array of source lines. We use a list to ensure a trailing newline is
  # emitted by write_file() to comply with -Wnewline-eof.
  location_source = [ "namespace flutter {namespace testing {const char* GetFixturesPath() {return \"$location_path\";}}}" ]
32
  location_source_path = "$target_gen_dir/fixtures_location$target_name.cc"
33

34
  write_file(location_source_path, location_source)
35

36 37 38 39
  source_set(target_name) {
    public = []
    sources = [
      location_source_path,
40 41
    ]
  }
42
}
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
# Generates the Dart kernel snapshot.
#
# Arguments
#     dart_main (required): The Main Dart file.
#
#     dart_kernel (required): The path to the ouput kernel snapshot in the out
#                             directory.
template("dart_snapshot_kernel") {
  testonly = true

  assert(defined(invoker.dart_main), "The Dart Main file must be specified")
  assert(defined(invoker.dart_kernel),
         "The Dart Kernel file location must be specified")

  dart_action(target_name) {
59
    testonly = true
60 61

    deps = [
62 63 64
      # This generates the Frontend server snapshot used in this Dart action as
      # well as the patched SDK.
      "//third_party/dart/utils/kernel-service:frontend_server",
65 66
    ]

67
    if (is_aot_test) {
68
      deps += [ "//flutter/lib/snapshot:strong_platform" ]
69 70
    }

71 72
    script = "$root_out_dir/frontend_server.dart.snapshot"

73 74 75 76
    inputs = [
      invoker.dart_main,
    ]

77
    outputs = [
78
      invoker.dart_kernel,
79
    ]
80

81 82 83 84 85 86 87 88 89 90 91 92
    snapshot_depfile = "$target_gen_dir/snapshot_$target_name.depfile.d"
    depfile = snapshot_depfile

    args = [
      "--sdk-root",
      rebase_path("$root_out_dir/flutter_patched_sdk"),
      "--target",
      "flutter",
      "--output-dill",
      rebase_path(invoker.dart_kernel, root_out_dir),
      "--depfile",
      rebase_path(snapshot_depfile),
93
    ]
94

95 96
    if (flutter_runtime_mode == "release" ||
        flutter_runtime_mode == "jit_release") {
97 98 99 100 101 102 103 104 105 106
      args += [ "-Ddart.vm.product=true" ]
    }

    if (is_aot_test) {
      args += [
        "--aot",

        # type flow analysis
        "--tfa",
      ]
107
    }
108 109

    args += [ rebase_path(invoker.dart_main) ]
110
  }
111
}
112

113 114 115 116 117 118 119 120 121 122 123 124
# Generates an AOT snapshot from a kernel snapshot.
#
# Arguments:
#
#     dart_kernel (required): The path to the kernel snapshot.
template("dart_snapshot_aot") {
  testonly = true

  assert(defined(invoker.dart_kernel),
         "The Dart Kernel file location must be specified")

  compiled_action(target_name) {
125
    testonly = true
126

127 128 129
    tool = "//third_party/dart/runtime/bin:gen_snapshot"

    inputs = [
130
      invoker.dart_kernel,
131 132
    ]

133 134
    # Custom ELF loader is used for Mac and Windows.
    elf_object = "$target_gen_dir/assets/app_elf_snapshot.so"
135

136 137 138
    outputs = [
      elf_object,
    ]
139 140 141 142

    args = [
      "--causal_async_stacks",
      "--deterministic",
143 144
      "--snapshot_kind=app-aot-elf",
      "--elf=" + rebase_path(elf_object),
145
      rebase_path(invoker.dart_kernel),
146 147
    ]

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    forward_variables_from(invoker, [ "deps" ])
  }
}

# Generates a kernel or AOT snapshot as necessary from the main Dart file.
# Other Dart dependencies referenced by that main Dart file will be tracked.
#
# Arguments:
#
#     dart_main (required): The path to the main Dart file.
template("dart_snapshot") {
  assert(defined(invoker.dart_main), "The main Dart file must be specified.")

  testonly = true

  dart_snapshot_kernel_target_name = "dart_snapshot_kernel_$target_name"
  dart_snapshot_kernel_path = "$target_gen_dir/assets/kernel_blob.bin"
  dart_snapshot_kernel(dart_snapshot_kernel_target_name) {
    dart_main = invoker.dart_main
    dart_kernel = dart_snapshot_kernel_path
  }

170 171
  snapshot_deps = []
  snapshot_public_deps = [ ":$dart_snapshot_kernel_target_name" ]
172 173 174 175 176 177 178 179 180 181

  if (is_aot_test) {
    dart_snapshot_aot_target_name = "dart_snapshot_aot_$target_name"
    dart_snapshot_aot(dart_snapshot_aot_target_name) {
      dart_kernel = dart_snapshot_kernel_path
      deps = [
        ":$dart_snapshot_kernel_target_name",
      ]
    }
    snapshot_deps += [ ":$dart_snapshot_aot_target_name" ]
182 183 184 185
  }

  group(target_name) {
    testonly = true
186
    deps = snapshot_deps
187
    public_deps = snapshot_public_deps
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  }
}

# Copies a (potentially empty) list of fixtures to the fixtures directory for
# the unit test.
#
# Arguments:
#
#     fixtures (required): The list of fixtures to copy.
template("copy_fixtures") {
  testonly = true

  assert(defined(invoker.fixtures), "The test fixtures must be specified.")

  has_fixtures = false
  foreach(fixture, invoker.fixtures) {
    has_fixtures = true
  }

  if (has_fixtures) {
    copy(target_name) {
      sources = invoker.fixtures
      outputs = [
        "$target_gen_dir/assets/{{source_file_part}}",
      ]
    }
  } else {
    group(target_name) {
      # The copy target cannot accept an empty list.
    }
  }
}

# Specifies the fixtures to copy to a location known by the specific unit test.
# Test executable can only depend on one such target. You can use either one of
# both arguments to expand this template. If you have none, then you'll see the
# unused invoker scope error. In such cases specify the fixtures using an empty
# array.
#
227 228 229 230 231 232
# The targets which generate the outputs from these test fixtures (e.g. the
# Dart kernel snapshot) are exposed as public dependencies of the test fixture
# target. This is so that users can depend on the test fixture target directly
# and be able to access the generated outputs without needing to know about the
# internal dependency structure generated by this template.
#
233 234 235 236 237 238 239 240 241 242 243 244
# Arguments:
#
#     fixtures (optional): The list of test fixtures. An empty list may be
#                          specified.
#
#     dart_main (optional): The path to the main Dart file. If specified, it is
#                           snapshotted.
template("test_fixtures") {
  # Even if no fixtures are present, the location of the fixtures directory
  # must always be known to tests.
  fixtures_location_target_name = "fixtures_location_$target_name"
  fixtures_location(fixtures_location_target_name) {
245 246 247 248 249
    if (is_fuchsia) {
      assets_dir = "/pkg/data/assets"
    } else {
      assets_dir = "$target_gen_dir/assets"
    }
250 251
  }
  test_deps = [ ":$fixtures_location_target_name" ]
252
  test_public_deps = []
253 254 255 256 257 258

  # If the fixtures are specified, copy them to the assets directory.
  if (defined(invoker.fixtures)) {
    copy_fixtures_target_name = "copy_fixtures_$target_name"
    copy_fixtures(copy_fixtures_target_name) {
      fixtures = invoker.fixtures
259
    }
260
    test_public_deps += [ ":$copy_fixtures_target_name" ]
261 262 263 264 265 266 267 268 269
  }

  # If a Dart file is specified, snapshot it and place it in the generated
  # assets directory.
  if (defined(invoker.dart_main)) {
    dart_snapshot_target_name = "dart_snapshot_$target_name"
    dart_snapshot(dart_snapshot_target_name) {
      dart_main = invoker.dart_main
    }
270
    test_public_deps += [ ":$dart_snapshot_target_name" ]
271 272 273 274 275
  }

  group(target_name) {
    testonly = true
    deps = test_deps
276
    public_deps = test_public_deps
277 278
  }
}