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/Makefile b/Makefile
index aa5c136..acec159 100644
--- a/Makefile
+++ b/Makefile
@@ -27,6 +27,7 @@
 #
 # See https://sites.google.com/site/skiadocs/ for complete documentation.
 
+SKIA_OUT ?= out
 BUILDTYPE ?= Debug
 CWD := $(shell pwd)
 
@@ -72,6 +73,9 @@
 .PHONY: clean
 clean:
 	rm -rf out xcodebuild
+ifneq (out, $(SKIA_OUT))
+	rm -rf $(SKIA_OUT)
+endif
 
 # Run gyp no matter what.
 .PHONY: gyp
@@ -90,13 +94,13 @@
 .PHONY: gyp_if_needed
 gyp_if_needed:
 ifneq (,$(findstring Linux, $(uname)))
-	$(MAKE) out/Makefile
+	$(MAKE) $(SKIA_OUT)/Makefile
 endif
 ifneq (,$(findstring Darwin, $(uname)))
 	$(CWD)/gyp_skia
 endif
 
-out/Makefile:
+$(SKIA_OUT)/Makefile:
 	$(CWD)/gyp_skia
 
 # For all specific targets: run gyp if necessary, and then pass control to
@@ -106,9 +110,11 @@
 .PHONY: $(VALID_TARGETS)
 $(VALID_TARGETS):: gyp_if_needed
 ifneq (,$(findstring skia_os=android, $(GYP_DEFINES)))
-	$(MAKE) -C out $@ BUILDTYPE=$(BUILDTYPE)
+	$(MAKE) -C $(SKIA_OUT) $@ BUILDTYPE=$(BUILDTYPE)
 else ifneq (,$(findstring Linux, $(uname)))
-	$(MAKE) -C out $@ BUILDTYPE=$(BUILDTYPE)
+	$(MAKE) -C $(SKIA_OUT) $@ BUILDTYPE=$(BUILDTYPE)
+else ifneq (,$(findstring make, $(GYP_GENERATORS)))
+	$(MAKE) -C $(SKIA_OUT) $@ BUILDTYPE=$(BUILDTYPE)
 else ifneq (,$(findstring Darwin, $(uname)))
 	rm -f out/$(BUILDTYPE) || if test -d out/$(BUILDTYPE); then echo "run 'make clean' or otherwise delete out/$(BUILDTYPE)"; exit 1; fi
 	xcodebuild -project out/gyp/$@.xcodeproj -configuration $(BUILDTYPE)
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=.'])