Use handcrafted build targets in bp2build

If both bp2build_available and label are specified, label will be
preferred.

Initially, we copy the entire BUILD.bazel file. Eventually we may move
this to use bazel query for a more accurate result.

Test: go test *
Test: build/bazel/scripts/milestone-2/demo.sh full
Test: GENERATE_BAZEL_FILES=true m nothing
      edit bionic/libc/tools/BUILD.bazel
      GENERATE_BAZEL_FILES=true m nothing and verify changes picked up
Bug: 180516554
Change-Id: I43025583300e6b10d2c18032cd4a76237b578d59
diff --git a/bp2build/conversion.go b/bp2build/conversion.go
index 1225f2b..7877bb8 100644
--- a/bp2build/conversion.go
+++ b/bp2build/conversion.go
@@ -24,9 +24,9 @@
 	// Write top level files: WORKSPACE and BUILD. These files are empty.
 	files = append(files, newFile("", "WORKSPACE", ""))
 	// Used to denote that the top level directory is a package.
-	files = append(files, newFile("", "BUILD", ""))
+	files = append(files, newFile("", GeneratedBuildFileName, ""))
 
-	files = append(files, newFile(bazelRulesSubDir, "BUILD", ""))
+	files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
 
 	if mode == QueryView {
 		// These files are only used for queryview.
@@ -47,7 +47,14 @@
 	files := make([]BazelFile, 0, len(buildToTargets))
 	for _, dir := range android.SortedStringKeys(buildToTargets) {
 		targets := buildToTargets[dir]
-		sort.Slice(targets, func(i, j int) bool { return targets[i].name < targets[j].name })
+		sort.Slice(targets, func(i, j int) bool {
+			// this will cover all bp2build generated targets
+			if targets[i].name < targets[j].name {
+				return true
+			}
+			// give a strict ordering to content from hand-crafted targets
+			return targets[i].content < targets[j].content
+		})
 		content := soongModuleLoad
 		if mode == Bp2Build {
 			content = `# This file was automatically generated by bp2build for the Bazel migration project.
@@ -62,7 +69,7 @@
 			content += "\n\n"
 		}
 		content += targets.String()
-		files = append(files, newFile(dir, "BUILD", content))
+		files = append(files, newFile(dir, GeneratedBuildFileName, content))
 	}
 	return files
 }