Add --use-sdk support for dart2js and ddk modular tests

This allows the test runners to use snapshots instead of using the compilers
directly from source.

Change-Id: I70664a740bed8de647adb658bd521cd574aa685e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/104385
Commit-Queue: Sigmund Cherem <sigmund@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
diff --git a/pkg/compiler/tool/modular_test_suite.dart b/pkg/compiler/tool/modular_test_suite.dart
index a5749c9..67c5bf0 100644
--- a/pkg/compiler/tool/modular_test_suite.dart
+++ b/pkg/compiler/tool/modular_test_suite.dart
@@ -17,8 +17,11 @@
 
 Uri sdkRoot = Platform.script.resolve("../../../");
 Options _options;
+String _dart2jsScript;
+String _kernelWorkerScript;
 main(List<String> args) async {
   _options = Options.parse(args);
+  await _resolveScripts();
   await runSuite(
       sdkRoot.resolve('tests/modular/'),
       'tests/modular',
@@ -135,7 +138,7 @@
     }
 
     List<String> args = [
-      sdkRoot.resolve("utils/bazel/kernel_worker.dart").toFilePath(),
+      _kernelWorkerScript,
       '--no-summary-only',
       '--target',
       'dart2js',
@@ -190,7 +193,10 @@
         transitiveDependencies.map((m) => '${toUri(m, dillId)}');
     List<String> args = [
       '--packages=${sdkRoot.toFilePath()}/.packages',
-      'package:compiler/src/dart2js.dart',
+      _dart2jsScript,
+      // TODO(sigmund): remove this dependency on libraries.json
+      if (_options.useSdk)
+        '--libraries-spec=$_librarySpecForSnapshot',
       '${toUri(module, dillId)}',
       for (String flag in flags) '--enable-experiment=$flag',
       '${Flags.dillDependencies}=${dillDependencies.join(',')}',
@@ -239,7 +245,8 @@
     if (_options.verbose) print("\nstep: dart2js backend on $module");
     List<String> args = [
       '--packages=${sdkRoot.toFilePath()}/.packages',
-      'package:compiler/src/dart2js.dart',
+      _dart2jsScript,
+      if (_options.useSdk) '--libraries-spec=$_librarySpecForSnapshot',
       '${toUri(module, updatedDillId)}',
       for (String flag in flags) '--enable-experiment=$flag',
       '${Flags.readData}=${toUri(module, globalDataId)}',
@@ -282,10 +289,10 @@
   Future<void> execute(Module module, Uri root, ModuleDataToRelativeUri toUri,
       List<String> flags) async {
     if (_options.verbose) print("step: dart2js backend on $module");
-    var sdkRoot = Platform.script.resolve("../../../../");
     List<String> args = [
       '--packages=${sdkRoot.toFilePath()}/.packages',
-      'package:compiler/src/dart2js.dart',
+      _dart2jsScript,
+      if (_options.useSdk) '--libraries-spec=$_librarySpecForSnapshot',
       '${toUri(module, updatedDillId)}',
       for (String flag in flags) '${Flags.enableLanguageExperiments}=$flag',
       '${Flags.readData}=${toUri(module, globalDataId)}',
@@ -406,3 +413,30 @@
   @override
   String toString() => name;
 }
+
+Future<void> _resolveScripts() async {
+  Future<String> resolve(
+      String sourceUriOrPath, String relativeSnapshotPath) async {
+    Uri sourceUri = sdkRoot.resolve(sourceUriOrPath);
+    String result =
+        sourceUri.scheme == 'file' ? sourceUri.toFilePath() : sourceUriOrPath;
+    if (_options.useSdk) {
+      String snapshot = Uri.file(Platform.resolvedExecutable)
+          .resolve(relativeSnapshotPath)
+          .toFilePath();
+      if (await File(snapshot).exists()) {
+        return snapshot;
+      }
+    }
+    return result;
+  }
+
+  _dart2jsScript = await resolve(
+      'package:compiler/src/dart2js.dart', 'snapshots/dart2js.dart.snapshot');
+  _kernelWorkerScript = await resolve('utils/bazel/kernel_worker.dart',
+      'snapshots/kernel_worker.dart.snapshot');
+}
+
+String _librarySpecForSnapshot = Uri.file(Platform.resolvedExecutable)
+    .resolve('../lib/libraries.json')
+    .toFilePath();
diff --git a/pkg/dev_compiler/test/modular_suite.dart b/pkg/dev_compiler/test/modular_suite.dart
index 2b3e170..78c22cb 100644
--- a/pkg/dev_compiler/test/modular_suite.dart
+++ b/pkg/dev_compiler/test/modular_suite.dart
@@ -14,8 +14,11 @@
 
 Uri sdkRoot = Platform.script.resolve("../../../");
 Options _options;
+String _dartdevcScript;
+String _kernelWorkerScript;
 main(List<String> args) async {
   _options = Options.parse(args);
+  await _resolveScripts();
   await runSuite(
       sdkRoot.resolve('tests/modular/'),
       'tests/modular',
@@ -79,7 +82,7 @@
     }
 
     List<String> args = [
-      sdkRoot.resolve("utils/bazel/kernel_worker.dart").toFilePath(),
+      _kernelWorkerScript,
       '--summary-only',
       '--target',
       'ddc',
@@ -156,7 +159,7 @@
 
     List<String> args = [
       '--packages=${sdkRoot.toFilePath()}/.packages',
-      sdkRoot.resolve('pkg/dev_compiler/bin/dartdevc.dart').toFilePath(),
+      _dartdevcScript,
       '--kernel',
       '--modules=es6',
       '--no-summarize',
@@ -309,3 +312,24 @@
     return '$rootScheme:/$relativeUri';
   }
 }
+
+Future<void> _resolveScripts() async {
+  Future<String> resolve(
+      String sdkSourcePath, String relativeSnapshotPath) async {
+    String result = sdkRoot.resolve(sdkSourcePath).toFilePath();
+    if (_options.useSdk) {
+      String snapshot = Uri.file(Platform.resolvedExecutable)
+          .resolve(relativeSnapshotPath)
+          .toFilePath();
+      if (await File(snapshot).exists()) {
+        return snapshot;
+      }
+    }
+    return result;
+  }
+
+  _dartdevcScript = await resolve(
+      'pkg/dev_compiler/bin/dartdevc.dart', 'snapshots/dartdevc.dart.snapshot');
+  _kernelWorkerScript = await resolve('utils/bazel/kernel_worker.dart',
+      'snapshots/kernel_worker.dart.snapshot');
+}
diff --git a/pkg/modular_test/lib/src/runner.dart b/pkg/modular_test/lib/src/runner.dart
index 9c3b36f..d7e1dbd 100644
--- a/pkg/modular_test/lib/src/runner.dart
+++ b/pkg/modular_test/lib/src/runner.dart
@@ -64,6 +64,7 @@
   int shard = 1;
   String configurationName;
   Uri outputDirectory;
+  bool useSdk = false;
 
   static Options parse(List<String> args) {
     var parser = new ArgParser()
@@ -74,6 +75,8 @@
       ..addFlag('show-skipped',
           defaultsTo: false,
           help: 'print the name of the tests skipped by the filtering option')
+      ..addFlag('use-sdk',
+          defaultsTo: false, help: 'whether to use snapshots from a built sdk')
       ..addOption('filter',
           help: 'only run tests containing this filter as a substring')
       ..addOption('shards',
@@ -101,6 +104,7 @@
     return Options()
       ..showSkipped = argResults['show-skipped']
       ..verbose = argResults['verbose']
+      ..useSdk = argResults['use-sdk']
       ..filter = argResults['filter']
       ..shards = shards
       ..shard = shard
diff --git a/tools/bots/test_matrix.json b/tools/bots/test_matrix.json
index 8f7a188..9eb3373 100644
--- a/tools/bots/test_matrix.json
+++ b/tools/bots/test_matrix.json
@@ -890,12 +890,14 @@
         },
         {
           "name": "ddc kernel modular tests",
-          "script": "out/ReleaseX64/dart",
+          "script": "out/ReleaseX64/dart-sdk/bin/dart",
           "testRunner": true,
           "arguments": [
             "pkg/dev_compiler/test/modular_suite.dart",
             "--configuration-name",
-            "dartdevk-${system}-release"
+            "dartdevk-${system}-release",
+            "--verbose",
+            "--use-sdk"
           ]
         },
         {
@@ -996,12 +998,14 @@
         },
         {
           "name": "ddc kernel modular tests",
-          "script": "xcodebuild/ReleaseX64/dart",
+          "script": "xcodebuild/ReleaseX64/dart-sdk/bin/dart",
           "testRunner": true,
           "arguments": [
             "pkg/dev_compiler/test/modular_suite.dart",
             "--configuration-name",
-            "dartdevk-${system}-release"
+            "dartdevk-${system}-release",
+            "--verbose",
+            "--use-sdk"
           ]
         },
         {
@@ -1154,22 +1158,24 @@
           "arguments": ["create_sdk"]
         },
         {
+          "name": "dart2js modular tests",
+          "script": "out/ReleaseX64/dart-sdk/bin/dart",
+          "testRunner": true,
+          "arguments": [
+            "pkg/compiler/tool/modular_test_suite.dart",
+            "--configuration-name",
+            "dart2js-${system}-release-d8",
+            "--verbose",
+            "--use-sdk"
+          ]
+        },
+        {
           "name": "dart2js unit tests",
           "arguments": [
             "-nunittest-asserts-no-sdk-linux",
             "dart2js",
             "pkg//compiler/"
           ]
-        },
-        {
-          "name": "dart2js modular tests",
-          "script": "out/ReleaseX64/dart",
-          "testRunner": true,
-          "arguments": [
-            "pkg/compiler/tool/modular_test_suite.dart",
-            "--configuration-name",
-            "dart2js-${system}-release-d8"
-          ]
         }
       ]
     },