tools/setup-build: Into the 'active' symlink directory

Previously we'd generate the build files into the symlink target directory.
CMake gets grumpy if the cache is invalidated, and you're building in the 'active' directory, which does not match the directory path it remembered.

Also: Enable ccache if found on PATH

Change-Id: I7e0dc93516b2c59d6bf346fc943acea3d0021087
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/88311
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
diff --git a/tools/setup-build b/tools/setup-build
index 69087f7..49ecc6c 100755
--- a/tools/setup-build
+++ b/tools/setup-build
@@ -32,9 +32,10 @@
 function generate() {
   CMD=$1
   pushd "$ROOT_DIR" > /dev/null
-  ${CMD}
-  rm -fr "out/active" || true
-  ln -s "$BUILD_DIR" "out/active"
+    mkdir -p "out/$BUILD_DIR"
+    rm -fr "out/active" || true
+    ln -s "$BUILD_DIR" "out/active"
+    ${CMD}
   popd > /dev/null
 }
 
@@ -42,10 +43,10 @@
   "gn")
     case $BUILD_TYPE in
       "debug")
-        generate "gn gen out/${BUILD_DIR} --args=is_debug=true"
+        generate "gn gen out/active --args=is_debug=true"
       ;;
       "release")
-        generate "gn gen out/${BUILD_DIR}"
+        generate "gn gen out/active"
       ;;
       *)
         echo "invalid build type '${BUILD_TYPE}'"
@@ -54,12 +55,16 @@
     esac
   ;;
   "cmake")
+    CMAKE_FLAGS=""
+    if [[ -x $(command -v ccache) ]]; then
+      CMAKE_FLAGS+="-DCMAKE_CXX_COMPILER_LAUNCHER=ccache"
+    fi
     case $BUILD_TYPE in
       "debug")
-        generate "cmake -S . -B out/$BUILD_DIR -GNinja -DCMAKE_BUILD_TYPE=Debug"
+        generate "cmake -S . -B out/active -GNinja -DCMAKE_BUILD_TYPE=Debug ${CMAKE_FLAGS}"
       ;;
       "release")
-        generate "cmake -S . -B out/$BUILD_DIR -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo"
+        generate "cmake -S . -B out/active -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo ${CMAKE_FLAGS}"
       ;;
       *)
         echo "invalid build type '${BUILD_TYPE}'"