Add tools dependencies for Rust rules.

TESTED=manual

--
MOS_MIGRATED_REVID=99161344
diff --git a/README.md b/README.md
index 4021477..369ca5e 100644
--- a/README.md
+++ b/README.md
@@ -17,9 +17,8 @@
 <a name="setup"></a>
 ## Setup
 
-Install Rust following the instructions on the [Rust website][rust]. The version
-of Rust currently supported by these build rules is Rust 1.0.0. These build
-rules also assume that Rust is installed either either `/usr` or `/usr/local`.
+To use the Rust rules, simply copy the contents of `rust.WORKSPACE` to your
+`WORKSPACE` file.
 
 <a name="basic-example"></a>
 ## Basic Example
diff --git a/rust-darwin-x86_64.BUILD b/rust-darwin-x86_64.BUILD
new file mode 100644
index 0000000..2f067e1
--- /dev/null
+++ b/rust-darwin-x86_64.BUILD
@@ -0,0 +1,23 @@
+BASE_DIR = "rust-1.1.0-x86_64-apple-darwin/"
+
+filegroup(
+    name = "rustc",
+    srcs = [BASE_DIR + "rustc/bin/rustc"],
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "rustdoc",
+    srcs = [BASE_DIR + "rustc/bin/rustdoc"],
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "rustlib",
+    srcs = glob([
+        BASE_DIR + "rustc/lib/rustlib/x86_64-apple-darwin/lib/*.rlib",
+        BASE_DIR + "rustc/lib/rustlib/x86_64-apple-darwin/lib/*.dylib",
+        BASE_DIR + "rustc/lib/rustlib/x86_64-apple-darwin/lib/*.a",
+    ]),
+    visibility = ["//visibility:public"],
+)
diff --git a/rust-linux-x86_64.BUILD b/rust-linux-x86_64.BUILD
new file mode 100644
index 0000000..2ec7728
--- /dev/null
+++ b/rust-linux-x86_64.BUILD
@@ -0,0 +1,23 @@
+BASE_DIR = "rust-1.1.0-x86_64-unknown-linux-gnu/"
+
+filegroup(
+    name = "rustc",
+    srcs = [BASE_DIR + "rustc/bin/rustc"],
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "rustdoc",
+    srcs = [BASE_DIR + "rustc/bin/rustdoc"],
+    visibility = ["//visibility:public"],
+)
+
+filegroup(
+    name = "rustlib",
+    srcs = glob([
+        BASE_DIR + "rustc/lib/rustlib/x86_64-unknown-linux-gnu/lib/*.rlib",
+        BASE_DIR + "rustc/lib/rustlib/x86_64-unknown-linux-gnu/lib/*.so",
+        BASE_DIR + "rustc/lib/rustlib/x86_64-unknown-linux-gnu/lib/*.a",
+    ]),
+    visibility = ["//visibility:public"],
+)
diff --git a/rust.WORKSPACE b/rust.WORKSPACE
new file mode 100644
index 0000000..3b307d2
--- /dev/null
+++ b/rust.WORKSPACE
@@ -0,0 +1,13 @@
+new_http_archive(
+    name = "rust-linux-x86_64",
+    url = "https://static.rust-lang.org/dist/rust-1.1.0-x86_64-unknown-linux-gnu.tar.gz",
+    sha256 = "5a8b1c4bb254a698a69cd05734909a3933567be6996422ff53f947fd115372e6",
+    build_file = "tools/build_rules/rust/rust-linux-x86_64.BUILD",
+)
+
+new_http_archive(
+    name = "rust-darwin-x86_64",
+    url = "https://static.rust-lang.org/dist/rust-1.1.0-x86_64-apple-darwin.tar.gz",
+    sha256 = "ac802916da3f9c431377c00b864a517bc356859495b7a8a123ce2c532ee8fa83",
+    build_file = "tools/build_rules/rust/rust-darwin-x86_64.BUILD",
+)
diff --git a/rust.bzl b/rust.bzl
index 5e84cc8..c5c5057 100644
--- a/rust.bzl
+++ b/rust.bzl
@@ -112,6 +112,50 @@
     features_flags += [" --cfg feature=\\\"" + feature + "\\\""]
   return features_flags
 
+def _build_rustc_command(ctx, crate_type, src, output_dir, depinfo,
+                         extra_flags=[]):
+  """
+  Builds the rustc command
+  """
+
+  # Paths to the Rust compiler and standard libraries.
+  rustc_path = ctx.file._rustc.path
+  rustlib_path = ctx.files._rustlib[0].dirname
+
+  # Paths to cc (for linker) and ar
+  cpp_fragment = ctx.configuration.fragment(cpp)
+  cc = cpp_fragment.compiler_executable
+  ar = cpp_fragment.ar_executable
+  # Currently, the CROSSTOOL config for darwin sets ar to "libtool". Because
+  # rust uses ar-specific flags, use /usr/bin/ar in this case.
+  # TODO(dzc): This is not ideal. Remove this workaround once ar_executable
+  # always points to an ar binary.
+  ar_str = "%s" % ar
+  if ar_str.find("libtool", 0) != -1:
+    ar = "/usr/bin/ar"
+
+  # Construct features flags
+  features_flags = _get_features_flags(ctx.attr.features)
+
+  return " ".join([
+      "set -e;",
+      " ".join(depinfo.setup_cmd),
+      rustc_path + " " + src,
+      "--crate-name " + ctx.label.name,
+      "--crate-type " + crate_type,
+      "-g",
+      "--codegen ar=%s" % ar,
+      "--codegen linker=%s" % cc,
+      "-L all=" + rustlib_path,
+      " ".join(extra_flags),
+      " ".join(features_flags),
+      "--out-dir " + output_dir,
+      "--emit=dep-info,link",
+      " ".join(depinfo.search_flags),
+      " ".join(depinfo.link_flags),
+      " ".join(ctx.attr.rustc_flags),
+  ])
+
 def _rust_library_impl(ctx):
   """
   Implementation for rust_library Skylark rule.
@@ -134,24 +178,14 @@
 
   # Dependencies
   depinfo = _setup_deps(ctx.attr.deps, ctx.label.name, output_dir)
-  features_flags = _get_features_flags(ctx.attr.features)
 
   # Build rustc command
-  # TODO(dzc): There is a tools dependency on rustc. Use a remote repository
-  # mechanism to fetch rustc.
-  cmd = (
-      "set -e;export PATH=/usr/bin:/usr/local/bin:$PATH;" +
-      " " + " ".join(depinfo.setup_cmd) +
-      "rustc " + lib_rs +
-      " --crate-name " + ctx.label.name +
-      " --crate-type lib -g" +
-      " " + " ".join(features_flags) +
-      " --out-dir " + output_dir +
-      " --emit=dep-info,link" +
-      " " + " ".join(depinfo.search_flags) +
-      " " + " ".join(depinfo.link_flags) +
-      " " + " ".join(ctx.attr.rustc_flags)
-  )
+  cmd = _build_rustc_command(
+      ctx = ctx,
+      crate_type = "lib",
+      src = lib_rs,
+      output_dir = output_dir,
+      depinfo = depinfo)
 
   # Compile action.
   ctx.action(
@@ -191,25 +225,15 @@
 
   # Dependencies
   depinfo = _setup_deps(ctx.attr.deps, ctx.label.name, output_dir)
-  features_flags = _get_features_flags(ctx.attr.features)
 
   # Build rustc command.
-  # TODO(dzc): There is a tools dependency on rustc. Use a remote repository
-  # mechanism to fetch rustc.
-  cmd = (
-      "set -e;export PATH=/usr/bin:/usr/local/bin:$PATH;" +
-      " " + " ".join(depinfo.setup_cmd) +
-      "rustc " + main_rs +
-      " --crate-name " + ctx.label.name +
-      " --crate-type bin -g" +
-      " " + " ".join(extra_flags) +
-      " " + " ".join(features_flags) +
-      " --out-dir " + output_dir +
-      " --emit=dep-info,link" +
-      " " + " ".join(depinfo.search_flags) +
-      " " + " ".join(depinfo.link_flags) +
-      " " + " ".join(ctx.attr.rustc_flags)
-  )
+  cmd = _build_rustc_command(
+      ctx = ctx,
+      crate_type = "bin",
+      src = main_rs,
+      output_dir = output_dir,
+      depinfo = depinfo,
+      extra_flags = extra_flags)
 
   # Compile action.
   ctx.action(
@@ -239,6 +263,11 @@
     "deps": attr.label_list(),
     "features": attr.string_list(),
     "rustc_flags": attr.string_list(),
+    "_rustc": attr.label(
+        default = Label("//tools/rust:rustc"),
+        executable = True,
+        single_file = True),
+    "_rustlib": attr.label(default = Label("//tools/rust:rustlib")),
 }
 
 rust_library = rule(