Change to the way the build directory is extracted

Use TMPDIR bitbake config variable to move the build directory into the
obmcdir "/tmp/openbmc" inside the container. When the copy out occurs it
will do it to the extraction directory which will default to the obmcext
git repository under the WORKSPACE directory. The link has been updated
to reflect this change. Before doing the run the extraction directory
will be created and chowned to the user UID and GID.

Change-Id: Ic99dbf0f0d1cdc613440a2c21b3cdc3385e9a386
Signed-off-by: Alanny Lopez <alanny.lopez@ibm.com>
diff --git a/build-setup.sh b/build-setup.sh
index 7587f0e..4e9eb2e 100755
--- a/build-setup.sh
+++ b/build-setup.sh
@@ -14,14 +14,17 @@
 #  obmcext      = Path of the OpenBMC repo directory used in creating a copy
 #                 inside the container that is not mounted to external storage
 #                 default directory location "${WORKSPACE}/openbmc"
-#  obmcdir      = Path of the OpenBMC directory, where the build occurs inside
-#                 the container cannot be placed on external storage default
+#  builddir     = Path of the OpenBMC directory where the build occurs inside
+#                 the container, cannot be placed on external storage default
 #                 directory location "/tmp/openbmc"
 #  sscdir       = Path of the BitBake shared-state cache directoy, will default
 #                 to directory "/home/${USER}", used to speed up builds.
 #  WORKSPACE    = Path of the workspace directory where some intermediate files
 #                 and the images will be saved to.
 #
+#  extraction   = Path where the ombcdir contents will be copied out to when
+#                 the build completes. Defaults to ${obmcext}/build/tmp.
+#
 # Optional Variables:
 #  launch       = job|pod
 #                 Can be left blank to launch via Docker if not using
@@ -46,10 +49,11 @@
 target=${target:-qemu}
 distro=${distro:-ubuntu}
 imgtag=${imgtag:-latest}
-obmcdir=${obmcdir:-/tmp/openbmc}
+builddir=${builddir:-/tmp/openbmc}
 sscdir=${sscdir:-${HOME}}
 WORKSPACE=${WORKSPACE:-${HOME}/${RANDOM}${RANDOM}}
 obmcext=${obmcext:-${WORKSPACE}/openbmc}
+extraction=${extraction:-${obmcext}/build/tmp}
 launch=${launch:-}
 http_proxy=${http_proxy:-}
 PROXY=""
@@ -79,6 +83,12 @@
   git clone https://github.com/openbmc/openbmc ${obmcext}
 fi
 
+# Make and chown the extraction directory to avoid permission errors
+if [ ! -d ${extraction} ]; then
+  mkdir -p ${extraction}
+fi
+chown ${UID}:${GROUPS} ${extraction}
+
 # Work out what build target we should be running and set BitBake command
 case ${target} in
   barreleye)
@@ -224,11 +234,8 @@
 
 set -xeo pipefail
 
-# Use the mounted repo cache to make an internal repo not mounted externally
-cp -R ${obmcext} ${obmcdir}
-
-# Go into the OpenBMC directory (the openbmc script will put us in a build subdir)
-cd ${obmcdir}
+# Go into the OpenBMC directory, the build will handle changing directories
+cd ${obmcext}
 
 # Set up proxies
 export ftp_proxy=${http_proxy}
@@ -274,14 +281,14 @@
 SSTATE_DIR="${sscdir}/bitbake_sharedstatecache"
 USER_CLASSES += "buildstats"
 INHERIT_remove = "uninative"
+TMPDIR="${builddir}"
 EOF_CONF
 
 # Kick off a build
 bitbake ${BITBAKE_OPTS} obmc-phosphor-image
 
 # Copy build directory of internal obmcdir into workspace directory
-cp -a ${obmcdir}/build/. ${WORKSPACE}/build/
-
+cp -r ${builddir}/* ${extraction}
 EOF_SCRIPT
 
 chmod a+x ${WORKSPACE}/build.sh
@@ -328,7 +335,7 @@
 fi
 
 # To maintain function of resources that used an older path, add a link
-ln -sf ${WORKSPACE}/build/tmp/deploy ${WORKSPACE}/deploy
+ln -sf ${extraction}/deploy ${WORKSPACE}/deploy
 
 # Timestamp for build
 echo "Build completed, $(date)"