Shorten paths in DEPS environment variable

Summary:
Shorten paths in DEPS environment variable (take 2)

We were running into character limits because of the number of
DEPS and directory names being long. This takes the basedir and
sticks it into another environment variable which is then reused
in DEPS. This means anyone relying on $DEPS will still get the
proper paths
diff --git a/src/com/facebook/buck/shell/Genrule.java b/src/com/facebook/buck/shell/Genrule.java
index e9d3994..8559b34 100644
--- a/src/com/facebook/buck/shell/Genrule.java
+++ b/src/com/facebook/buck/shell/Genrule.java
@@ -196,6 +196,8 @@
     for (BuildRule dep : getDeps()) {
       transformNames(processedBuildRules, depFiles, dep);
     }
+
+    environmentVariablesBuilder.put("GEN_DIR", relativeToAbsolutePathFunction.apply(BuckConstant.GEN_DIR));
     environmentVariablesBuilder.put("DEPS", Joiner.on(' ').skipNulls().join(depFiles));
     environmentVariablesBuilder.put("SRCDIR", absolutePathToSrcDirectory);
     environmentVariablesBuilder.put("TMP", absolutePathToTmpDirectory);
@@ -218,7 +220,22 @@
 
     String output = rule.getPathToOutputFile();
     if (output != null) {
-      appendTo.add(relativeToAbsolutePathFunction.apply(output));
+      // TODO(mbolin): This is a giant hack and we should do away with $DEPS altogether.
+      // There can be a lot of paths here and the filesystem location can be arbitrarily long.
+      // We can easily hit the shell command character limit. What this does is find
+      // BuckConstant.GEN_DIR (which should be the same for every path) and replaces
+      // it with a shell variable. This way the character count is much lower when run
+      // from the shell but anyone reading the environment variable will get the
+      // full paths due to variable interpolation
+      if (output.startsWith(BuckConstant.GEN_DIR)) {
+        String relativePath = output.substring(BuckConstant.GEN_DIR.length());
+        if (relativePath.charAt(0) != '/') {
+          relativePath = "/" + relativePath;
+        }
+        appendTo.add("$GEN_DIR" + relativePath);
+      } else {
+        appendTo.add(relativeToAbsolutePathFunction.apply(output));
+      }
     }
 
     for (BuildRule dep : rule.getDeps()) {
diff --git a/test/com/facebook/buck/android/ApkGenruleTest.java b/test/com/facebook/buck/android/ApkGenruleTest.java
index 033ba74..4c0c44a 100644
--- a/test/com/facebook/buck/android/ApkGenruleTest.java
+++ b/test/com/facebook/buck/android/ApkGenruleTest.java
@@ -213,6 +213,7 @@
         .put("SRCS", "/opt/local/fbandroid/src/com/facebook/signer.py " +
             "/opt/local/fbandroid/src/com/facebook/key.properties")
         .put("APK", GEN_DIR + "/fb4a.apk")
+        .put("GEN_DIR", "/opt/local/fbandroid/" + GEN_DIR)
         .put("DEPS", "")
         .put("TMP", "/opt/local/fbandroid/" + relativePathToTmpDir)
         .put("SRCDIR", "/opt/local/fbandroid/" + relativePathToSrcDir)
diff --git a/test/com/facebook/buck/shell/GenruleTest.java b/test/com/facebook/buck/shell/GenruleTest.java
index 66b5d4a..5610210 100644
--- a/test/com/facebook/buck/shell/GenruleTest.java
+++ b/test/com/facebook/buck/shell/GenruleTest.java
@@ -214,8 +214,9 @@
         .put("SRCS", "/opt/local/fbandroid/src/com/facebook/katana/convert_to_katana.py " +
             "/opt/local/fbandroid/src/com/facebook/katana/AndroidManifest.xml")
         .put("OUT", "/opt/local/fbandroid/" + GEN_DIR + "/src/com/facebook/katana/AndroidManifest.xml")
+        .put("GEN_DIR", "/opt/local/fbandroid/" + GEN_DIR)
         .put("DEPS",
-            "/opt/local/fbandroid/" + GEN_DIR + "/java/com/facebook/util/lib__util__output/util.jar")
+            "$GEN_DIR/java/com/facebook/util/lib__util__output/util.jar")
         .put("TMP", "/opt/local/fbandroid/" + pathToTmpDir)
         .put("SRCDIR", "/opt/local/fbandroid/" + pathToSrcDir)
         .build(),