envsetup: add a helper to fix "out/target/common" not being really common

* out/target/common isn't really common, and if you do
  back to back builds for multiple devices, that is noticable.
* Use out/target/common-$device instead, and link the appropriate
  dir to out/target/common every time lunch() is run, if
  ANDROID_FIXUP_COMMON_OUT is set.
* Refer https://groups.google.com/forum/#!topic/android-building/ispbOgzoyg8
  for more info.

Change-Id: I11e7df0e68e2a60ce32576f06397d60fc9465b60
diff --git a/build/envsetup.sh b/build/envsetup.sh
index ba713b3..3060c8d 100644
--- a/build/envsetup.sh
+++ b/build/envsetup.sh
@@ -244,6 +244,24 @@
 
 alias bib=breakfast
 
+function fixup_common_out_dir() {
+    common_out_dir=$(get_build_var OUT_DIR)/target/common
+    target_device=$(get_build_var TARGET_DEVICE)
+    if [ ! -z $ANDROID_FIXUP_COMMON_OUT ]; then
+        if [ -d ${common_out_dir} ] && [ ! -L ${common_out_dir} ]; then
+            mv ${common_out_dir} ${common_out_dir}-${target_device}
+            ln -s ${common_out_dir}-${target_device} ${common_out_dir}
+        else
+            [ -L ${common_out_dir} ] && rm ${common_out_dir}
+            mkdir -p ${common_out_dir}-${target_device}
+            ln -s ${common_out_dir}-${target_device} ${common_out_dir}
+        fi
+    else
+        [ -L ${common_out_dir} ] && rm ${common_out_dir}
+        mkdir -p ${common_out_dir}
+    fi
+}
+
 # Make using all available CPUs
 function mka() {
     m -j "$@"