Add vector_math and box2d as external Dart packages

- Add vector_math and box2d packages as third-party DEPS
- Minimally extend dart_pkg GN rule to support external packages
- Add //third_party/dart-pkg build target to setup external packages in gen/dart-pkg

R=abarth@chromium.org

Review URL: https://codereview.chromium.org/1157263003
diff --git a/.gitignore b/.gitignore
index d208518..27f8e12 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,6 +37,8 @@
 /third_party/brotli/src/
 /third_party/colorama/src/
 /third_party/dart-sdk/
+/third_party/dart-pkg/vector_math
+/third_party/dart-pkg/box2d
 /third_party/dejavu-fonts-ttf-2.34/ttf/*.ttf
 /third_party/freetype/
 /third_party/go/tool/
diff --git a/DEPS b/DEPS
index 0c38ed5..3e481a7 100644
--- a/DEPS
+++ b/DEPS
@@ -31,6 +31,8 @@
   'lss_revision': 'e079768b7e3a94dcbe7d338496c0c3bde7151b6e',
   'nss_revision': 'bb4e75a43d007518ae7d618665ea2f25b0c60b63',
   'nacl_revision': '87d5dd90911a0657c27574f78e86b7dfc4ad8b29',
+  'vector_math_dart_revision': '65915583f7aa606cb47ed265f853c18c60102b81',
+  'box2d_dart_revision': 'c5e65d9546275e78ad2a1d51b459e7638f6e4323',
 }
 
 # Only these hosts are allowed for dependencies in this DEPS file.
@@ -100,6 +102,12 @@
 
   'src/native_client':
     Var('chromium_git') + '/native_client/src/native_client.git' + '@' + Var('nacl_revision'),
+
+  'src/third_party/dart-pkg/vector_math':
+      Var('chromium_git') + '/external/github.com/google/vector_math.dart.git' + '@' + Var('vector_math_dart_revision'),
+
+  'src/third_party/dart-pkg/box2d':
+      Var('chromium_git') + '/external/github.com/google/box2d.dart.git' + '@' + Var('box2d_dart_revision'),
 }
 
 
diff --git a/build/ls.py b/build/ls.py
new file mode 100755
index 0000000..638c3bd
--- /dev/null
+++ b/build/ls.py
@@ -0,0 +1,31 @@
+#!/usr/bin/python
+# Copyright 2015 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Recursively list files of the target directory. Ignores dot files."""
+
+import argparse
+import os
+import sys
+
+def main(target_directory):
+  for root, dirs, files in os.walk(target_directory):
+    files = [f for f in files if not f[0] == '.']
+    dirs[:] = [d for d in dirs if not d[0] == '.']
+    for f in files:
+      path = os.path.join(root, f)
+      print path
+
+if __name__ == '__main__':
+  parser = argparse.ArgumentParser(
+      description="Recursively list files of the target directory")
+  parser.add_argument("--target-directory",
+                      dest="target_directory",
+                      metavar="<target-directory>",
+                      type=str,
+                      required=True,
+                      help="The target directory")
+
+  args = parser.parse_args()
+  sys.exit(main(args.target_directory))
diff --git a/mojo/dart/embedder/BUILD.gn b/mojo/dart/embedder/BUILD.gn
index 207020f..ac64810 100644
--- a/mojo/dart/embedder/BUILD.gn
+++ b/mojo/dart/embedder/BUILD.gn
@@ -46,6 +46,7 @@
     "//crypto",
     "//dart/runtime:libdart",
     "//dart/runtime/bin:libdart_embedder_noio",
+    "//third_party/dart-pkg",
     "//mojo/public/c/system",
     "//mojo/public/cpp/system",
   ]
diff --git a/mojo/public/dart/rules.gni b/mojo/public/dart/rules.gni
index 03b17b1..7786cd4 100644
--- a/mojo/public/dart/rules.gni
+++ b/mojo/public/dart/rules.gni
@@ -215,6 +215,9 @@
 #   sources
 #       List of sources to include in the package.
 #
+#   pkg_dir (optional)
+#       Directory containing the package sources. This overrides sources.
+#
 #   deps (optional)
 #       Note: this can only contain mojom targets.
 #
@@ -224,7 +227,11 @@
 #       Directory containing sdk-ext .dart sources.
 #
 template("dart_pkg") {
-  pubspec_yaml_path = rebase_path("pubspec.yaml")
+  if (defined(invoker.pkg_dir)) {
+    pubspec_yaml_path = rebase_path("pubspec.yaml", "", invoker.pkg_dir)
+  } else {
+    pubspec_yaml_path = rebase_path("pubspec.yaml")
+  }
   dart_package_name_script =
       rebase_path("mojo/public/tools/dart_package_name.py", ".", mojo_sdk_root)
   package_name = exec_script(dart_package_name_script,
@@ -239,7 +246,7 @@
   package_root = rebase_path("$root_gen_dir/dart-pkg/packages")
   stamp_file = "$root_gen_dir/dart-pkg/${package_name}.stamp"
 
-  assert(defined(invoker.sources))
+  assert(defined(invoker.sources) || defined(invoker.pkg_dir))
 
   action(target_name) {
     deps = []
@@ -276,10 +283,23 @@
       stamp_file,
     ]
 
+    if (defined(invoker.sources)) {
+      sources = invoker.sources
+    } else {
+      assert(defined(invoker.pkg_dir))
+      list_script = rebase_path("build/ls.py", ".", mojo_sdk_root)
+      sources = exec_script(list_script,
+                            [
+                              "--target-directory",
+                              rebase_path(invoker.pkg_dir),
+                            ],
+                            "list lines")
+    }
+
     inputs = [
                list_mojoms_script,
                script,
-             ] + rebase_path(invoker.sources)
+             ] + rebase_path(sources)
 
     args = [
              "--package-name",
@@ -293,7 +313,7 @@
              "--stamp-file",
              rebase_path(stamp_file),
              "--package-sources",
-           ] + rebase_path(invoker.sources) + [ "--mojom-sources" ] +
+           ] + rebase_path(sources) + [ "--mojom-sources" ] +
            rebase_path(mojom_sources, "", mojo_sdk_root) +
            [ "--sdk-ext-directories" ] + rebase_path(sdk_ext_directory)
   }
diff --git a/third_party/dart-pkg/BUILD.gn b/third_party/dart-pkg/BUILD.gn
new file mode 100644
index 0000000..09d8e8d
--- /dev/null
+++ b/third_party/dart-pkg/BUILD.gn
@@ -0,0 +1,20 @@
+# Copyright 2015 The Chromium 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("//mojo/public/dart/rules.gni")
+
+group("dart-pkg") {
+  deps = [
+    ":box2d",
+    ":vector_math",
+  ]
+}
+
+dart_pkg("box2d") {
+  pkg_dir = "//third_party/dart-pkg/box2d"
+}
+
+dart_pkg("vector_math") {
+  pkg_dir = "//third_party/dart-pkg/vector_math"
+}