Update gyp to allow alternative out directories

If the enviroment variable SKIA_OUT is set the output of both
gyp and make will be redirected to that directory.

Review URL: https://codereview.appspot.com/6782095

git-svn-id: http://skia.googlecode.com/svn/trunk@6578 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gyp_skia b/gyp_skia
index af08f07..8032589 100755
--- a/gyp_skia
+++ b/gyp_skia
@@ -11,6 +11,7 @@
 
 import glob
 import os
+import platform
 import shlex
 import sys
 
@@ -22,10 +23,6 @@
 # Directory within which we can find most of Skia's gyp configuration files.
 gyp_config_dir = os.path.join(script_dir, 'gyp')
 
-# Directory within which we want all generated files (including Makefiles)
-# to be written.
-output_dir = os.path.join(os.path.abspath(script_dir), 'out')
-
 # Ensure we import our current gyp source's module, not any version
 # pre-installed in your PYTHONPATH.
 sys.path.insert(0, os.path.join(gyp_source_dir, 'pylib'))
@@ -54,6 +51,31 @@
 
   return result
 
+# Return the directory where all generated files (including Makefiles) are to
+# be written.
+def get_output_dir():
+
+  # SKIA_OUT can be any directory either as a child of the standard out/
+  # directory or any given location on the file system (e.g. /tmp/skia)
+  output_dir = os.getenv('SKIA_OUT')
+
+  if not output_dir:
+    return os.path.join(os.path.abspath(script_dir), 'out')
+
+  if (os.name != 'posix' or
+     (sys.platform.startswith('darwin') and 
+      (not os.getenv('GYP_GENERATORS') or 
+       'make' not in os.getenv('GYP_GENERATORS')))):
+    print 'ERROR: variable SKIA_OUT is not valid on Mac (using xcodebuild)' \
+          ' or Windows'
+    sys.exit(-1);
+
+  if os.path.isabs(output_dir):
+    return output_dir
+  else:
+    return os.path.join(os.path.abspath(script_dir), output_dir)
+
+
 if __name__ == '__main__':
   args = sys.argv[1:]
 
@@ -83,7 +105,7 @@
   args.extend(['--depth', '.'])
 
   # Tell gyp to write the Makefiles into output_dir
-  args.extend(['--generator-output', os.path.abspath(output_dir)])
+  args.extend(['--generator-output', os.path.abspath(get_output_dir())])
 
   # Tell make to write its output into the same dir
   args.extend(['-Goutput_dir=.'])