Generate Compdb file in the output directory

This change modifies the location of generated compdb file from
$ANDROID_BUILD_TOP to where compdbOtuputProjectsDirectory points to,
under $OUT_DIR.  This is especially required for the build environment
that maps $ANDROID_BUILD_TOP as read-only, such as Gitc.

Test: m SOONG_GEN_COMPDB=1 nothing
      examine generated compile_commands.json
Signed-off-by: Changyeon Jo <changyeon@google.com>

Change-Id: I86700f89b613a443a33982ecd5501b0f68a62be7
diff --git a/cc/compdb.go b/cc/compdb.go
index 519380f..dff14db 100644
--- a/cc/compdb.go
+++ b/cc/compdb.go
@@ -27,7 +27,7 @@
 // This singleton generates a compile_commands.json file. It does so for each
 // blueprint Android.bp resulting in a cc.Module when either make, mm, mma, mmm
 // or mmma is called. It will only create a single compile_commands.json file
-// at out/development/ide/compdb/compile_commands.json. It will also symlink it
+// at ${OUT_DIR}/soong/development/ide/compdb/compile_commands.json. It will also symlink it
 // to ${SOONG_LINK_COMPDB_TO} if set. In general this should be created by running
 // make SOONG_GEN_COMPDB=1 nothing to get all targets.
 
@@ -43,7 +43,7 @@
 
 const (
 	compdbFilename                = "compile_commands.json"
-	compdbOutputProjectsDirectory = "out/development/ide/compdb"
+	compdbOutputProjectsDirectory = "development/ide/compdb"
 
 	// Environment variables used to modify behavior of this singleton.
 	envVariableGenerateCompdb          = "SOONG_GEN_COMPDB"
@@ -78,12 +78,12 @@
 	})
 
 	// Create the output file.
-	dir := filepath.Join(getCompdbAndroidSrcRootDirectory(ctx), compdbOutputProjectsDirectory)
-	os.MkdirAll(dir, 0777)
-	compDBFile := filepath.Join(dir, compdbFilename)
-	f, err := os.Create(compdbFilename)
+	dir := android.PathForOutput(ctx, compdbOutputProjectsDirectory)
+	os.MkdirAll(dir.String(), 0777)
+	compDBFile := dir.Join(ctx, compdbFilename)
+	f, err := os.Create(compDBFile.String())
 	if err != nil {
-		log.Fatalf("Could not create file %s: %s", filepath.Join(dir, compdbFilename), err)
+		log.Fatalf("Could not create file %s: %s", compDBFile, err)
 	}
 	defer f.Close()
 
@@ -106,7 +106,7 @@
 	finalLinkPath := filepath.Join(ctx.Config().Getenv(envVariableCompdbLink), compdbFilename)
 	if finalLinkPath != "" {
 		os.Remove(finalLinkPath)
-		if err := os.Symlink(compDBFile, finalLinkPath); err != nil {
+		if err := os.Symlink(compDBFile.String(), finalLinkPath); err != nil {
 			log.Fatalf("Unable to symlink %s to %s: %s", compDBFile, finalLinkPath, err)
 		}
 	}