merge in jb-release history after reset to master
diff --git a/build/build_pdk_vendor.py b/build/build_pdk_vendor.py
new file mode 100755
index 0000000..624dbef
--- /dev/null
+++ b/build/build_pdk_vendor.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2012 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the 'License');
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an 'AS IS' BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+import os, re, string, sys
+import pdk_utils as pu
+import setup_pdk_rel as setup
+
+def main(argv):
+  if len(argv) < 4:
+    print "Usage: build_pdk_vendor.py top_dir cpu_arch target_hw [lunch_target] [-jxx] [-c]"
+    print "   Runs pdk_rel build from given top_dir"
+    print "   cpu_arch: cpu arch to use like armv7-a-neon_true"
+    print "   target_hw: target H/W to build"
+    print "   lunch_target: lunch target for build. If not specified, full_target_hw-eng"
+    print "   -jxx : number of jobs for make"
+    print "   -c: clean before build"
+    sys.exit(1)
+
+  top_dir = argv[1]
+  cpu_arch = argv[2]
+  target_hw = argv[3]
+  build_j = "-j12"
+  lunch_target = "full_" + target_hw + "-eng"
+  clean_build = False
+  argv_current = 4
+  while len(argv) > argv_current:
+    arg = argv[argv_current]
+    if arg.startswith("-j"):  
+      build_j = arg
+    elif arg == "-c":
+      clean_build = True
+    else:
+      lunch_target = arg
+    argv_current += 1
+  if not os.path.isfile(top_dir + "/pdk/build/pdk_vendor.mk"):
+    print "WARNING: pdk/build/pdk_vendor.mk does not exist!!!!"
+
+  if clean_build:
+    command = "python " + top_dir + "/pdk/build/clean_pdk_rel.py " + top_dir
+    pu.execute_command(command, "cannot clean")
+
+  # setup binary and data
+  command = "python " + top_dir + "/pdk/build/setup_pdk_rel.py " + top_dir + " " \
+           + cpu_arch + " " + target_hw
+  pu.execute_command(command, "cannot copy pdk bin")
+
+  # actual build
+  command = "cd " + top_dir + " && . build/envsetup.sh && lunch " + lunch_target + " && " \
+          + "make " + build_j + " pdk_rel"
+  pu.execute_command(command, "pdk build failed")
+
+if __name__ == '__main__':
+  main(sys.argv)
diff --git a/build/clean_pdk_rel.py b/build/clean_pdk_rel.py
new file mode 100755
index 0000000..7306c04
--- /dev/null
+++ b/build/clean_pdk_rel.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2012 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the 'License');
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an 'AS IS' BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# script to clean pdk_rel source tree to allow copying new PDK bins and data
+import os, sys
+
+import pdk_utils as pu
+import setup_pdk_rel as setup
+
+def main(argv):
+  if len(argv) != 2:
+    print "Usage: clean_pdk_rel.py top_dir"
+    print "   cleans PDK data and PDK bin copied."
+    print "   Following directories are removed:"
+    print "     out"
+    print "     all files copied from pdk_data.zip, and PDK_DATA_COPIED"
+    print "     all files under " + setup.PDK_CPU_ARCH_TOP_DIR + "/pdk_bin_XXX directories" 
+    sys.exit(1)
+
+  top_dir = argv[1]
+
+  os.system("rm -rf " + top_dir + "/out")
+  pu.remove_files_listed(top_dir,
+                         pu.load_list(top_dir + "/PDK_DATA_COPIED"))
+  os.system("rm -f " + top_dir + "/PDK_DATA_COPIED")
+  os.system(" rm -rf " + top_dir + "/" + setup.PDK_CPU_ARCH_TOP_DIR + "/pdk_bin_*")
+
+if __name__ == '__main__':
+  main(sys.argv)
diff --git a/build/copy_pdk_bins.py b/build/copy_pdk_bins.py
index b19731e..0fa0bbe 100755
--- a/build/copy_pdk_bins.py
+++ b/build/copy_pdk_bins.py
@@ -17,22 +17,22 @@
 # script to copy PDK_eng build result into a dir for PDK_rel build
 
 import os, string, sys, shutil
-import copy_utils as cu
+import pdk_utils as pu
 
 
 # currently javalib.jar is not used and target jar is just copied in raw copy process
 def copy_jar(src_jar_dir, dest_dir, jar_name):
   """copy classes.jar and javalib.jar to dest_dir/jar_name dir"""
-  cu.copy_file_if_exists(src_jar_dir, dest_dir + "/" + jar_name, "classes.jar")
-  cu.copy_file_if_exists(src_jar_dir, dest_dir + "/" + jar_name, "javalib.jar")
+  pu.copy_file_if_exists(src_jar_dir, dest_dir + "/" + jar_name, "classes.jar")
+  pu.copy_file_if_exists(src_jar_dir, dest_dir + "/" + jar_name, "javalib.jar")
 
 def copy_classes_or_javalib(src_jar_dir, dest_dir, jar_name):
   """For host, either classes.jar or javalib.jar is enough.
      Use javalib only when there is no classes.jar"""
-  if cu.copy_file_new_name_if_exists(src_jar_dir + "/classes.jar", dest_dir + "/" + jar_name,
+  if pu.copy_file_new_name_if_exists(src_jar_dir + "/classes.jar", dest_dir + "/" + jar_name,
                                      jar_name + ".jar"):
     return
-  cu.copy_file_new_name_if_exists(src_jar_dir + "/javalib.jar", dest_dir + "/" + jar_name,
+  pu.copy_file_new_name_if_exists(src_jar_dir + "/javalib.jar", dest_dir + "/" + jar_name,
                                   jar_name + ".jar")
 
 class DependencyMk(object):
@@ -226,24 +226,24 @@
   os.system("rm -rf " + dest_top_dir + "/raw_copy")
   os.system("rm -rf " + dest_top_dir + "/target")
   # copy template for mk
-  cu.copy_file_if_exists(src_top_dir + "/pdk/build", dest_top_dir, "pdk_prebuilt.mk")
+  pu.copy_file_if_exists(src_top_dir + "/pdk/build", dest_top_dir, "pdk_prebuilt.mk")
   mkFile = DependencyMk(dest_top_dir + "/pdk_prebuilt.mk")
   mkFile.addString("\n\n\n")
   mkFile.addString("PDK_BIN_ORIGINAL_TARGET := " + target_name + "\n")
 
   for file_name in raw_file_list:
-    cu.copy_file_if_exists(src_out_dir, dest_top_dir + "/raw_copy", file_name)
+    pu.copy_file_if_exists(src_out_dir, dest_top_dir + "/raw_copy", file_name)
 
   for raw_dir in raw_dir_list:
-    cu.copy_dir(src_out_dir, dest_top_dir + "/raw_copy", raw_dir)
+    pu.copy_dir(src_out_dir, dest_top_dir + "/raw_copy", raw_dir)
 
   for host_a in host_a_list:
-    cu.copy_file_if_exists(src_out_dir + "/host/linux-x86/obj/STATIC_LIBRARIES/" + host_a +
+    pu.copy_file_if_exists(src_out_dir + "/host/linux-x86/obj/STATIC_LIBRARIES/" + host_a +
                            "_intermediates", dest_top_dir + "/host/lib", host_a + ".a")
     mkFile.addHostA(host_a)
 
   for host_so in host_so_list:
-    cu.copy_file_if_exists(src_out_dir + "/host/linux-x86/obj/lib/",
+    pu.copy_file_if_exists(src_out_dir + "/host/linux-x86/obj/lib/",
                            dest_top_dir + "/host/lib", host_so + ".so")
     mkFile.addHostSo(host_so)
 
@@ -254,12 +254,12 @@
     mkFile.addHostJar(host_jar)
 
   for target_a in target_a_list:
-    cu.copy_file_if_exists(src_target_top_dir + "obj/STATIC_LIBRARIES/" + target_a +
+    pu.copy_file_if_exists(src_target_top_dir + "obj/STATIC_LIBRARIES/" + target_a +
                            "_intermediates", dest_top_dir + "/target/lib", target_a + ".a")
     mkFile.addTargetA(target_a)
 
   for target_so in target_so_list:
-    cu.copy_file_if_exists(src_target_top_dir + "obj/lib/",
+    pu.copy_file_if_exists(src_target_top_dir + "obj/lib/",
                            dest_top_dir + "/target/lib", target_so + ".so")
     mkFile.addTargetSo(target_so)
 
@@ -272,5 +272,9 @@
   for file_to_remove in raw_target_files_to_remove:
     os.system("rm -rf " + dest_top_dir + "/raw_copy/" + file_to_remove)
 
+  # rename raw_copy/target/product/target_name to target/product/pdk_target
+  os.system("mv " + dest_top_dir + "/raw_copy/target/product/" + target_name \
+             + " " + dest_top_dir + "/raw_copy/target/product/pdk_target")
+
 if __name__ == '__main__':
   main(sys.argv)
diff --git a/build/copy_pdk_data.py b/build/copy_pdk_data.py
index efd2482..6a34f36 100755
--- a/build/copy_pdk_data.py
+++ b/build/copy_pdk_data.py
@@ -19,30 +19,54 @@
 # the current and the previous branch
 
 import os, string, sys, shutil
-import copy_utils as cu
+import pdk_utils as pu
 import create_source_tree as tree
 
+def clean_dest_dirs(dest_vendor_data):
+  dir_to_clean = []
+  dir_to_clean += tree.prev_copy_dir_list
+  dir_to_clean += tree.additional_dir_pdk_rel_list
+  print dir_to_clean
+  for file_name in tree.copy_files_pdk_rel_list:
+    [path, name] = file_name.rsplit("/", 1)
+    print path
+    dir_to_clean.append(path)
+
+  for dir_name in dir_to_clean:
+    dir_path = dest_vendor_data + "/" + dir_name
+    print "deleting all files under " + dir_path
+    if os.path.isfile(dir_path):
+      # this is wrong, just remove the file
+      os.system("rm " + dir_path)
+    if os.path.isdir(dir_path):
+      file_list = pu.list_files(dir_path, ".git")
+      print file_list
+      for file_name in file_list:
+        os.system("rm " + file_name)
 
 def main(argv):
-  if len(argv) != 4:
-    print "Usage: copy_pdk_data.py current previous dest_top_dir"
-    print "   ex: copy_pdk_data.py ../jb_master ../ics_master ."
+  if len(argv) < 4:
+    print "Usage: copy_pdk_data.py current previous dest_dir [-c]"
+    print "   ex: copy_pdk_data.py ../jb_master ../ics_master ./out/target/pdk_data"
+    print "   -c to clean dest_dir"
     sys.exit(1)
-  current_branch = argv[1]
-  previous_branch = argv[2]
-  dest_top = argv[3]
+  current_branch = os.path.abspath(argv[1])
+  previous_branch = os.path.abspath(argv[2])
+  dest_dir = os.path.abspath(argv[3])
+
+  cp_option = ""
+  if len(argv) == 5 and argv[4] == "-c":
+    clean_dest_dirs(dest_dir)
+    cp_option = "-n" # do not overwrite
 
   for dir_name in tree.prev_copy_dir_list:
-    cu.copy_dir(previous_branch, dest_top + "/vendor/pdk_data", dir_name)
-
-  for dir_name in tree.prev_copy_dir_pdk_eng_list:
-    cu.copy_dir(previous_branch, dest_top + "/vendor/pdk_data_internal", dir_name)
+    pu.copy_dir(previous_branch, dest_dir, dir_name, cp_option)
 
   for dir_name in tree.additional_dir_pdk_rel_list:
-    cu.copy_dir(current_branch, dest_top + "/vendor/pdk_data", dir_name)
+    pu.copy_dir(current_branch, dest_dir, dir_name, cp_option)
 
   for file_name in tree.copy_files_pdk_rel_list:
-    cu.copy_files(current_branch, dest_top + "/vendor/pdk_data", file_name)
+    pu.copy_files(current_branch, dest_dir, file_name)
 
 if __name__ == '__main__':
   main(sys.argv)
diff --git a/build/copy_utils.py b/build/copy_utils.py
deleted file mode 100755
index d9876f9..0000000
--- a/build/copy_utils.py
+++ /dev/null
@@ -1,89 +0,0 @@
-#!/usr/bin/env python
-#
-# Copyright (C) 2012 The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the 'License');
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an 'AS IS' BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-# copy relared utils for all PDK scripts
-
-import os, string, sys, shutil
-
-def copy_dir(src_top, dest_top, dir_name):
-  """copy all the files under src_top/dir_name to dest_top/dir_name."""
-  src_full_path = src_top + "/" + dir_name
-  # do not create the leaf dir as cp will create it
-  [mid_path, leaf_path] = dir_name.rsplit("/", 1)
-  dest_full_path = dest_top + "/" + mid_path
-  if not os.path.isdir(dest_full_path):
-    os.makedirs(dest_full_path)
-  print "copy dir ", src_full_path, " to ", dest_full_path
-  os.system("cp -a " + src_full_path + " " + dest_full_path)
-
-
-def copy_dir_only_file(src_top, dest_top, dir_name):
-  """copy only files directly under the given dir_name"""
-  src_full_path = src_top + "/" + dir_name
-  dest_full_path = dest_top + "/" + dir_name
-  if not os.path.isdir(dest_full_path):
-    os.makedirs(dest_full_path)
-  children = os.listdir(src_full_path)
-  for child in children:
-    child_full_name = src_full_path + "/" + child
-    if os.path.isfile(child_full_name):
-      print "copy file ", child_full_name, " to ", dest_full_path
-      os.system("cp -a " + child_full_name + " " + dest_full_path)
-
-
-def copy_files(src_top, dest_top, files_name):
-  """copy files from src_top to dest_top.
-     Note that files_name can include directories which will be created
-     under dest_top"""
-  src_full_path = src_top + "/" + files_name
-  # do not create the leaf dir as cp will create it
-  [mid_path, leaf_path] = files_name.rsplit("/", 1)
-  dest_full_path = dest_top + "/" + mid_path
-  if not os.path.isdir(dest_full_path):
-    os.makedirs(dest_full_path)
-  print "copy files ", src_full_path, " to ", dest_full_path
-  os.system("cp -a " + src_full_path + " " + dest_full_path)
-
-
-def copy_file_if_exists(src_top, dest_top, file_name):
-  """copy file src_top/file_name to dest_top/file_name
-     returns false if such file does not exist in source."""
-  src_full_name = src_top + "/" + file_name
-  if not os.path.isfile(src_full_name):
-    print "file " + src_full_name + " not found"
-    return False
-  dest_file = dest_top + "/" + file_name
-  dest_dir = os.path.dirname(dest_file)
-  if not os.path.isdir(dest_dir):
-    os.makedirs(dest_dir)
-  print "copy file ", src_full_name, " to ", dest_file
-  os.system("cp -a " + src_full_name + " " +  dest_file)
-  return True
-
-
-def copy_file_new_name_if_exists(src_full_name, dest_dir, dest_file):
-  """copy src_full_name (including dir + file name) to dest_dir/dest_file
-     will be used when renaming is necessary"""
-  if not os.path.isfile(src_full_name):
-    print "file " + src_full_name + " not found"
-    return False
-  dest_full_name = dest_dir + "/" + dest_file
-  if not os.path.isdir(dest_dir):
-    os.makedirs(dest_dir)
-  print "copy file ", src_full_name, " to ", dest_full_name
-  os.system("cp -a " + src_full_name + " " + dest_full_name)
-  return True
diff --git a/build/create_source_tree.py b/build/create_source_tree.py
index c3ab193..e4aea2a 100755
--- a/build/create_source_tree.py
+++ b/build/create_source_tree.py
@@ -20,7 +20,7 @@
 # together with additional addition in this script for build dependency
 
 import os, string, sys, shutil
-import copy_utils as cu
+import pdk_utils as pu
 
 
 def get_one_var_occurrence(f, dir_list_var):
@@ -74,8 +74,7 @@
   "prebuilt",
   "prebuilts",
   "sdk",
-  "system",
-  "frameworks/base/data"
+  "system"
 ]
 
 # the whole dir copied
@@ -84,10 +83,12 @@
   ]
 
 # these dirs will be direcly pulled as the whole git.
-# so these files will not go under vendor/pdk_data
+# so these files will not go under vendor/pdk/data
 additional_dir_pdk_rel_list_git = [
   "external/libnl-headers",
   "external/proguard",
+  "external/v8",
+  "external/safe-iop",
 ]
 
 additional_dir_pdk_rel_list = [
@@ -97,8 +98,6 @@
   "frameworks/base/include/android_runtime",
   "frameworks/base/native/include",
   "dalvik/libnativehelper/include",
-  "external/v8/include",
-  "external/safe-iop/include",
   "system/media/audio_effects/include", # should be removed after refactoring
   "frameworks/base/include/drm", # for building legacy HAL, not in PDK release?
   "frameworks/base/include/media", # for building legacy HAL, not in PDK release?
@@ -114,6 +113,7 @@
   ]
 
 copy_files_pdk_rel_list = [
+  "libcore/include/UniquePtr.h", # by h/libhardware/tests/keymaster
   "frameworks/base/media/libeffects/data/audio_effects.conf",
   "development/data/etc/apns-conf_sdk.xml",
   "development/data/etc/vold.conf"
@@ -188,19 +188,19 @@
     if dir_name in symbolic_link_list:
       create_symbolic_link(src_top_dir, dest_top_dir, dir_name)
     else:
-      cu.copy_dir(src_top_dir, dest_top_dir, "/" + dir_name)
+      pu.copy_dir(src_top_dir, dest_top_dir, "/" + dir_name)
 
   for dir_name in dir_copy_only_files_list:
-    cu.copy_dir_only_file(src_top_dir, dest_top_dir, "/" + dir_name)
+    pu.copy_dir_only_file(src_top_dir, dest_top_dir, "/" + dir_name)
 
   copy_files_list_ = copy_files_list
   if not pdk_eng:
     copy_files_list_ += copy_files_pdk_rel_list
   for file_name in copy_files_list_:
-    cu.copy_files(src_top_dir, dest_top_dir, "/" + file_name)
+    pu.copy_files(src_top_dir, dest_top_dir, "/" + file_name)
 
   # overwrite files
-  cu.copy_files(src_top_dir + "/vendor/pdk/data/google/overwrite", dest_top_dir, "/*")
+  pu.copy_files(src_top_dir + "/vendor/pdk/data/google/overwrite", dest_top_dir, "/*")
 
   for file_name in files_to_remove:
     os.system("rm -rf " + dest_top_dir + "/" + file_name)
@@ -215,7 +215,7 @@
     if dir_name in symbolic_link_list:
       create_symbolic_link(prev_src_top_dir, dest_top_dir, dir_name)
     else:
-      cu.copy_dir(prev_src_top_dir, dest_top_dir, "/" + dir_name)
+      pu.copy_dir(prev_src_top_dir, dest_top_dir, "/" + dir_name)
 
 if __name__ == '__main__':
   main(sys.argv)
diff --git a/build/pdk.mk b/build/pdk.mk
index aa7f694..914185c 100644
--- a/build/pdk.mk
+++ b/build/pdk.mk
@@ -24,8 +24,6 @@
 	bionic \
 	bootable \
 	build \
-	device \
-	hardware \
 	prebuilt \
 	prebuilts
 
@@ -44,8 +42,10 @@
 BUILD_PDK_ENG_SUBDIRS := \
 	dalvik \
 	development \
+	device \
 	external \
 	frameworks \
+	hardware \
 	libcore \
 	packages/apps/Bluetooth \
 	packages/apps/Launcher2 \
@@ -64,13 +64,13 @@
 PDK_BIN_NAME := pdk_bin_$(TARGET_ARCH_VARIANT)_false
 endif # !SMP
 
-.PHONY: pdk_bin_zip
-pdk_bin_zip: $(OUT_DIR)/target/$(PDK_BIN_NAME).zip
+.PHONY: pdk_bin_zip pdk_data_zip
+pdk_bin_zip: $(OUT_DIR)/target/$(PDK_BIN_NAME).zip pdk_data_zip
 
 
 $(OUT_DIR)/target/$(PDK_BIN_NAME).zip: $(OUT_DIR)/target/$(PDK_BIN_NAME)
 	$(info Creating $(OUT_DIR)/target/$(PDK_BIN_NAME).zip)
-	$(hide) cd $(dir $@) && rm -rf $(notdir $@) && zip -rq $(notdir $@) $(PDK_BIN_NAME)
+	$(hide) cd $(dir $@) && rm -rf $(notdir $@) && cd $(PDK_BIN_NAME) && zip -rq ../$(notdir $@) *
 
 # explicitly put dependency on two final images to avoid copying every time
 # It is too early and INSTALLED_SYSTEMIMAGE is not defined yet.
@@ -78,6 +78,16 @@
                                    $(OUT_DIR)/target/product/$(TARGET_DEVICE)/system.img
 	python $(TOPDIR)pdk/build/copy_pdk_bins.py . $(OUT_DIR)/target/$(PDK_BIN_NAME) $(TARGET_DEVICE)
 
+pdk_data_zip: $(OUT_DIR)/target/pdk_data.zip
+
+$(OUT_DIR)/target/pdk_data.zip: $(OUT_DIR)/target/pdk_data
+	$(info Creating $(OUT_DIR)/target/pdk_data.zip)
+	$(hide) cd $(dir $@) && rm -rf $(notdir $@) && cd pdk_data && zip -rq ../$(notdir $@) *
+
+$(OUT_DIR)/target/pdk_data:  $(OUT_DIR)/target/product/$(TARGET_DEVICE)/boot.img \
+                             $(OUT_DIR)/target/product/$(TARGET_DEVICE)/system.img
+	python $(TOPDIR)pdk/build/copy_pdk_data.py . . $(OUT_DIR)/target/pdk_data
+
 else # pdk_rel
 
 # overwrite the definition from conflig.mk, no package build in pdk_rel
@@ -85,6 +95,8 @@
 
 # addition for pdk_rel
 BUILD_PDK_REL_SUBDIRS := \
+	device/common \
+	device/sample \
 	external/antlr \
 	external/bluetooth \
 	external/bsdiff \
@@ -122,8 +134,12 @@
 	external/wpa_supplicant_8 \
 	external/yaffs2 \
 	external/zlib \
+	frameworks/base/data \
 	frameworks/compile \
 	frameworks/native \
+	hardware/libhardware \
+	hardware/libhardware_legacy \
+	hardware/ril \
 	system/bluetooth \
 	system/core \
 	system/extras \
@@ -173,6 +189,10 @@
 $(error PDK binaries necessary for pdk_rel build are not there! Did you run setup_pdk_rel.py? )
 endif # PDK_BIN_COPIED
 
+ifeq (,$(wildcard $(TOPDIR)PDK_DATA_COPIED))
+$(error PDK data necessary for pdk_rel build are not there! Did you run setup_pdk_rel.py? )
+endif # PDK_DATA_COPIED
+
 PRODUCT_PACKAGES += core core-junit
 
 endif # pdk_rel
diff --git a/build/pdk_google.mk b/build/pdk_google.mk
index 66b2db7..639f7c7 100644
--- a/build/pdk_google.mk
+++ b/build/pdk_google.mk
@@ -16,6 +16,12 @@
 
 # subdirs for building pdk_eng/pdk_rel for maruro/crespo
 BUILD_PDK_SUBDIRS += \
+	device/samsung/tuna \
+	device/samsung/maguro \
+	device/ti/panda \
+	hardware/broadcom \
+	hardware/invensense \
+	hardware/ti/omap4xxx \
 	vendor/broadcom \
 	vendor/invensense \
 	vendor/khronos \
@@ -24,3 +30,4 @@
 	vendor/ti
 
 
+
diff --git a/build/pdk_utils.py b/build/pdk_utils.py
new file mode 100755
index 0000000..7e518ce
--- /dev/null
+++ b/build/pdk_utils.py
@@ -0,0 +1,165 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2012 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the 'License');
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an 'AS IS' BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# copy relared utils for all PDK scripts
+
+import os, string, sys, shutil, zipfile
+
+def copy_dir(src_top, dest_top, dir_name, cp_option = ""):
+  """copy all the files under src_top/dir_name to dest_top/dir_name."""
+  src_full_path = src_top + "/" + dir_name
+  # do not create the leaf dir as cp will create it
+  [mid_path, leaf_path] = dir_name.rsplit("/", 1)
+  dest_full_path = dest_top + "/" + mid_path
+  if not os.path.isdir(dest_full_path):
+    os.makedirs(dest_full_path)
+  print "copy dir ", src_full_path, " to ", dest_full_path
+  os.system("cp -a " + " " + cp_option + " " + src_full_path + " " + dest_full_path)
+
+
+def copy_dir_only_file(src_top, dest_top, dir_name):
+  """copy only files directly under the given dir_name"""
+  src_full_path = src_top + "/" + dir_name
+  dest_full_path = dest_top + "/" + dir_name
+  if not os.path.isdir(dest_full_path):
+    os.makedirs(dest_full_path)
+  children = os.listdir(src_full_path)
+  for child in children:
+    child_full_name = src_full_path + "/" + child
+    if os.path.isfile(child_full_name):
+      print "copy file ", child_full_name, " to ", dest_full_path
+      os.system("cp -a " + child_full_name + " " + dest_full_path)
+
+
+def copy_files(src_top, dest_top, files_name):
+  """copy files from src_top to dest_top.
+     Note that files_name can include directories which will be created
+     under dest_top"""
+  src_full_path = src_top + "/" + files_name
+  # do not create the leaf dir as cp will create it
+  [mid_path, leaf_path] = files_name.rsplit("/", 1)
+  dest_full_path = dest_top + "/" + mid_path
+  if not os.path.isdir(dest_full_path):
+    os.makedirs(dest_full_path)
+  print "copy files ", src_full_path, " to ", dest_full_path
+  os.system("cp -a " + src_full_path + " " + dest_full_path)
+
+
+def copy_file_if_exists(src_top, dest_top, file_name):
+  """copy file src_top/file_name to dest_top/file_name
+     returns false if such file does not exist in source."""
+  src_full_name = src_top + "/" + file_name
+  if not os.path.isfile(src_full_name):
+    print "file " + src_full_name + " not found"
+    return False
+  dest_file = dest_top + "/" + file_name
+  dest_dir = os.path.dirname(dest_file)
+  if not os.path.isdir(dest_dir):
+    os.makedirs(dest_dir)
+  print "copy file ", src_full_name, " to ", dest_file
+  os.system("cp -a " + src_full_name + " " +  dest_file)
+  return True
+
+
+def copy_file_new_name_if_exists(src_full_name, dest_dir, dest_file):
+  """copy src_full_name (including dir + file name) to dest_dir/dest_file
+     will be used when renaming is necessary"""
+  if not os.path.isfile(src_full_name):
+    print "file " + src_full_name + " not found"
+    return False
+  dest_full_name = dest_dir + "/" + dest_file
+  if not os.path.isdir(dest_dir):
+    os.makedirs(dest_dir)
+  print "copy file ", src_full_name, " to ", dest_full_name
+  os.system("cp -a " + src_full_name + " " + dest_full_name)
+  return True
+
+def list_files(dir_name, dir_exclusion_filter = ""):
+  """recursively list all files under given dir_name directory.
+     exluding subdirs ending with dir_exlusion_filter in name
+     returns list of files which can be [] if there is no file"""
+  file_list = []
+  if dir_exclusion_filter != "" and dir_name.endswith(dir_exclusion_filter):
+    return file_list
+  for item in os.listdir(dir_name):
+    item_full_path = dir_name + "/" + item
+    # do not include symbolic link to recursion
+    if os.path.islink(item_full_path) or os.path.isfile(item_full_path):
+      file_list.append(item_full_path)
+    elif os.path.isdir(item_full_path):
+      result_list = list_files(item_full_path, dir_exclusion_filter)
+      for file_name in result_list:
+        file_list.append(file_name)
+  return file_list
+
+def src_newer_than_dest(src, dest):
+  """return True if src file/dir is newer than dest file/dir"""
+  result = True
+  src_mod_time = os.path.getmtime(src)
+  if os.path.isfile(dest) or os.path.isdir(dest):
+    dest_mod_time = os.path.getmtime(dest)
+    if dest_mod_time > src_mod_time:
+      result = False
+  return result
+
+def remove_if_exists(entry):
+  if os.path.exists(entry):
+    os.system("rm -rf " + entry)
+
+
+def list_files_in_zip(zip_file_path, no_directory = True):
+  """ list all files/directories inside the given zip_file_path.
+      Directories are not included if no_directory is True"""
+  entry_list = []
+  if not zipfile.is_zipfile(zip_file_path):
+    return entry_list
+  zip_file = zipfile.ZipFile(zip_file_path, 'r')
+  entries =  zip_file.namelist()
+  for entry in entries:
+    if not no_directory or not entry.endswith("/"):
+      entry_list.append(entry)
+
+  #print entry_list
+  return entry_list
+
+def save_list(list_to_save, file_name):
+  f = open(file_name, "w")
+  for entry in list_to_save:
+    f.write("%s\n" % entry)
+  f.close()
+
+def load_list(file_name):
+  result = []
+  if not os.path.isfile(file_name):
+    return result
+
+  for line in open(file_name, "r"):
+    result.append(line.strip())
+
+  #print result
+  return result
+
+def remove_files_listed(top_dir, files_list):
+  top_dir_ = top_dir + "/"
+  for entry in files_list:
+    path = top_dir_ + entry
+    print "remove " + path
+    os.system("rm -f " + path)
+
+def execute_command(command, error_msg):
+  if os.system(command) != 0:
+    raise RuntimeError(error_msg)
diff --git a/build/setup_pdk_rel.py b/build/setup_pdk_rel.py
index 4dcbd01..75b90f7 100755
--- a/build/setup_pdk_rel.py
+++ b/build/setup_pdk_rel.py
@@ -20,27 +20,24 @@
 # TODO : set up source code as well
 
 import os, re, string, sys
+import pdk_utils as pu
 
 PDK_BIN_PREFIX = "pdk_bin_"
-PDK_BIN_TOP_DIR = "/vendor/pdk/data/partner/"
+PDK_BIN_TOP_DIR = "/vendor/pdk/data/partner/bin"
+PDK_CPU_ARCH_TOP_DIR = "/vendor/pdk/data/partner"
+PDK_DATA_TOP_DIR = "/vendor/pdk/data/partner/data"
 
 def list_available_pdk_bins(path):
   """returns the list of pdk_bin_* dir under the given path"""
-  pdk_bin_list = list()
-  file_list = os.listdir(path)
-  for file_name in file_list:
-    if file_name.startswith(PDK_BIN_PREFIX) and os.path.isdir(path + "/" + file_name):
-        pdk_bin_list.append(file_name)
-  return pdk_bin_list
+  pdk_bins_dict = {}
 
-def get_target_name_from_pdk_bin(path):
-  """returns the original target name from the given pdk_bin_* dir"""
-  product_dir = path + "/raw_copy/target/product"
-  file_list = os.listdir(product_dir)
+  file_list = pu.list_files(os.path.abspath(path))
   for file_name in file_list:
-    if os.path.isdir(product_dir + "/" + file_name):
-        return file_name
-  assert False, "target not found from product dir"
+    m = re.search(PDK_BIN_PREFIX + "(.*)\.zip$", file_name)
+    if m != None:
+      print " pdk_bin for arch " + m.group(1) + " @ " + file_name
+      pdk_bins_dict[m.group(1)] = file_name
+  return pdk_bins_dict
 
 def main(argv):
   if len(argv) != 4:
@@ -51,29 +48,54 @@
   cpu_conf = argv[2]
   target_hw = argv[3]
 
-  pdk_bin_dirs = list_available_pdk_bins(top_dir + PDK_BIN_TOP_DIR)
-  arch_list = []
-  for pdk_bin_dir in pdk_bin_dirs:
-    arch_list.append(pdk_bin_dir[len(PDK_BIN_PREFIX): ])
+  pdk_bins_dict = list_available_pdk_bins(top_dir + PDK_BIN_TOP_DIR)
 
-  if not (cpu_conf in arch_list):
+  if not (cpu_conf in pdk_bins_dict):
     print "Specified cpu_conf", cpu_conf, "not avaialble under", top_dir + PDK_BIN_TOP_DIR
-    print "Avaiable configurations are ", arch_list
+    print "Avaiable configurations are ", pdk_bins_dict.keys()
     sys.exit(1)
 
-  print "copy pdk bins"
-  os.system("mkdir -p " + top_dir + "/out/host")
-  os.system("mkdir -p " + top_dir + "/out/target/common")
-  os.system("mkdir -p " + top_dir + "/out/target/product/" + target_hw)
-  pdk_bin_path = top_dir + PDK_BIN_TOP_DIR + PDK_BIN_PREFIX + cpu_conf
-  pdk_bin_target_name = get_target_name_from_pdk_bin(pdk_bin_path)
-  os.system("cp -a " + pdk_bin_path + "/raw_copy/host/* " + top_dir + "/out/host")
-# no target/common yet
-#  os.system("cp -a " + pdk_bin_path + "/raw_copy/target/common/* " + top_dir +
-#            "/out/target/common")
-  os.system("cp -a " + pdk_bin_path + "/raw_copy/target/product/" + pdk_bin_target_name + "/* "
-            + top_dir + "/out/target/product/" + target_hw)
-  os.system("touch " + top_dir + "/out/target/product/" + target_hw + "/PDK_BIN_COPIED")
+  pdk_bin_zip = pdk_bins_dict[cpu_conf]
+  pdk_data_zip = top_dir + PDK_DATA_TOP_DIR + "/pdk_data.zip"
+  pdk_partner_data_cpu_path = top_dir + PDK_CPU_ARCH_TOP_DIR + "/" + PDK_BIN_PREFIX + cpu_conf
+  PDK_BIN_COPIED = top_dir + "/out/target/product/" + target_hw + "/PDK_BIN_COPIED"
+  PDK_DATA_COPIED = top_dir + "/PDK_DATA_COPIED"
+
+  copy_out_dir = pu.src_newer_than_dest(pdk_bin_zip, PDK_BIN_COPIED)
+  copy_partner_data_cpu = pu.src_newer_than_dest(pdk_bin_zip, pdk_partner_data_cpu_path)
+  copy_pdk_data = pu.src_newer_than_dest(pdk_data_zip, PDK_DATA_COPIED)
+
+  if copy_out_dir:
+    print "copy pdk bins to out"
+    # clean out as binary is changed
+    pu.remove_if_exists(top_dir + "/out")
+    command = "mkdir -p " + top_dir + "/out && " \
+            + "cd " + top_dir + "/out && " \
+            + "rm -rf raw_copy && " \
+            + "unzip " + os.path.abspath(pdk_bin_zip) + " raw_copy/* && " \
+            + "mv raw_copy/target/product/pdk_target raw_copy/target/product/" + target_hw + " &&" \
+            + "mv -f raw_copy/* . && " \
+            + "touch " + os.path.abspath(PDK_BIN_COPIED)
+    os.system(command)
+
+  if copy_partner_data_cpu:
+    print "copy pdk bins to " + pdk_partner_data_cpu_path
+    pu.remove_if_exists(pdk_partner_data_cpu_path)
+    command = "mkdir -p " + pdk_partner_data_cpu_path + " && " \
+            + "cd " + pdk_partner_data_cpu_path + " && " \
+            + "unzip -o " + os.path.abspath(pdk_bin_zip) + " host/* target/* pdk_prebuilt.mk"
+    os.system(command)
+
+  if copy_pdk_data:
+    print "copy pdk data"
+    # first remove old files
+    pu.remove_files_listed(top_dir, pu.load_list(PDK_DATA_COPIED))
+    command = "cd " + top_dir + " && " \
+            + "unzip -o " + os.path.abspath(pdk_data_zip)
+    os.system(command)
+    # recorde copied files to delete correctly.
+    pu.save_list(pu.list_files_in_zip(pdk_data_zip), PDK_DATA_COPIED)
+
 
 if __name__ == '__main__':
   main(sys.argv)