Allow to specify the output Android.mk file path.

- Allow to specify the output Android.mk file path.
  We need this to generate Android.mk into the out diretory.
- Makefile strings don't need quotes.
- Return non-zero exit code if it fails.
- Other trivial format changes.

Change-Id: I460083f6e0a2707cd4a6fe0bef3f8ae7562e9edb
diff --git a/androidbp/cmd/androidbp.go b/androidbp/cmd/androidbp.go
index a68b11c..6e0bc7f 100644
--- a/androidbp/cmd/androidbp.go
+++ b/androidbp/cmd/androidbp.go
@@ -30,9 +30,9 @@
 	} else {
 		switch value.Type {
 		case bpparser.Bool:
-			return fmt.Sprintf(`"%t"`, value.BoolValue)
+			return fmt.Sprintf("%t", value.BoolValue)
 		case bpparser.String:
-			return fmt.Sprintf(`"%s"`, processWildcards(value.StringValue))
+			return fmt.Sprintf("%s", processWildcards(value.StringValue))
 		case bpparser.List:
 			return fmt.Sprintf("\\\n%s\n", listToMkString(value.ListValue))
 		case bpparser.Map:
@@ -80,7 +80,7 @@
 	lines := make([]string, 0, len(list))
 	for _, tok := range list {
 		if tok.Type == bpparser.String {
-			lines = append(lines, fmt.Sprintf("\t\"%s\"", processWildcards(tok.StringValue)))
+			lines = append(lines, fmt.Sprintf("    %s", processWildcards(tok.StringValue)))
 		} else {
 			lines = append(lines, fmt.Sprintf("# ERROR: unsupported type %s in list",
 				tok.Type.String()))
@@ -222,7 +222,9 @@
 	for _, tok := range value.ListValue {
 		subdirs = append(subdirs, tok.StringValue)
 	}
-	fmt.Fprintf(w, "include $(wildcard $(addsuffix %s, Android.mk))\n", strings.Join(subdirs, " "))
+	// The current makefile may be generated to outside the source tree (such as the out directory), with a different structure.
+	fmt.Fprintf(w, "# Uncomment the following line if you really want to include subdir Android.mks.\n")
+	fmt.Fprintf(w, "# include $(wildcard $(addsuffix $(LOCAL_PATH)/%s/, Android.mk))\n", strings.Join(subdirs, " "))
 }
 
 func (w *androidMkWriter) handleAssignment(assignment *bpparser.Assignment) {
@@ -284,17 +286,17 @@
 }
 
 func (w *androidMkWriter) handleLocalPath() error {
-	androidMkDir, err := filepath.Abs(w.path)
+	localPath, err := filepath.Abs(w.path)
 	if err != nil {
 		return err
 	}
 
-	top, err := getTopOfAndroidTree(androidMkDir)
+	top, err := getTopOfAndroidTree(localPath)
 	if err != nil {
 		return err
 	}
 
-	rel, err := filepath.Rel(top, androidMkDir)
+	rel, err := filepath.Rel(top, localPath)
 	if err != nil {
 		return err
 	}
@@ -303,11 +305,10 @@
 	return nil
 }
 
-func (w *androidMkWriter) write() {
-	outFilePath := fmt.Sprintf("%s/Androidbp.mk", w.path)
-	fmt.Printf("Writing %s\n", outFilePath)
+func (w *androidMkWriter) write(androidMk string) error {
+	fmt.Printf("Writing %s\n", androidMk)
 
-	f, err := os.Create(outFilePath)
+	f, err := os.Create(androidMk)
 	if err != nil {
 		panic(err)
 	}
@@ -317,8 +318,7 @@
 	w.Writer = bufio.NewWriter(f)
 
 	if err := w.handleLocalPath(); err != nil {
-		fmt.Println(err.Error())
-		return
+		return err
 	}
 
 	for block := range w.iter() {
@@ -335,33 +335,46 @@
 	if err = w.Flush(); err != nil {
 		panic(err)
 	}
+	return nil
 }
 
 func main() {
 	if len(os.Args) < 2 {
 		fmt.Println("No filename supplied")
-		return
+		os.Exit(1)
 	}
 
-	reader, err := os.Open(os.Args[1])
+	androidBp := os.Args[1]
+	var androidMk string
+	if len(os.Args) >= 3 {
+		androidMk = os.Args[2]
+	} else {
+		androidMk = androidBp + ".mk"
+	}
+
+	reader, err := os.Open(androidBp)
 	if err != nil {
 		fmt.Println(err.Error())
-		return
+		os.Exit(1)
 	}
 
 	scope := bpparser.NewScope(nil)
-	blueprint, errs := bpparser.Parse(os.Args[1], reader, scope)
+	blueprint, errs := bpparser.Parse(androidBp, reader, scope)
 	if len(errs) > 0 {
-		fmt.Println("%d errors parsing %s", len(errs), os.Args[1])
+		fmt.Println("%d errors parsing %s", len(errs), androidBp)
 		fmt.Println(errs)
-		return
+		os.Exit(1)
 	}
 
 	writer := &androidMkWriter{
 		blueprint: blueprint,
-		path:      path.Dir(os.Args[1]),
+		path:      path.Dir(androidBp),
 		mapScope:  make(map[string][]*bpparser.Property),
 	}
 
-	writer.write()
+	err = writer.write(androidMk)
+	if err != nil {
+		fmt.Println(err.Error())
+		os.Exit(1)
+	}
 }
diff --git a/androidbp/cmd/soong.go b/androidbp/cmd/soong.go
index b8d7e0e..9b5a688 100644
--- a/androidbp/cmd/soong.go
+++ b/androidbp/cmd/soong.go
@@ -99,10 +99,10 @@
 
 // TODO: host target?
 var targetScopedPropertyConditionals = map[string]string{
-	"android32":     "ifeq($(TARGET_IS_64_BIT), false)",
+	"android32":     "ifneq($(TARGET_IS_64_BIT), true)",
 	"not_android32": "ifeq($(TARGET_IS_64_BIT), true)",
 	"android64":     "ifeq($(TARGET_IS_64_BIT), true)",
-	"not_android64": "ifeq($(TARGET_IS_64_BIT), false)",
+	"not_android64": "ifneq($(TARGET_IS_64_BIT), true)",
 }
 
 var disabledHostConditionals = map[string]string{