Support cc_object modules in mixed builds

Test: With a handwritten conversion of crtbegin_so1, USE_BAZEL_ANALYSIS=1 m crtbegin_so1
Change-Id: I7c777d7f46b37aa1827cc04205e2014f9293bf35
diff --git a/android/bazel_handler.go b/android/bazel_handler.go
index a5c4bed..415f00e 100644
--- a/android/bazel_handler.go
+++ b/android/bazel_handler.go
@@ -36,12 +36,14 @@
 
 const (
 	getAllFiles CqueryRequestType = iota
+	getCcObjectFiles
 )
 
 // Map key to describe bazel cquery requests.
 type cqueryKey struct {
 	label       string
 	requestType CqueryRequestType
+	archType    ArchType
 }
 
 type BazelContext interface {
@@ -50,7 +52,11 @@
 	// has been queued to be run later.
 
 	// Returns result files built by building the given bazel target label.
-	GetAllFiles(label string) ([]string, bool)
+	GetAllFiles(label string, archType ArchType) ([]string, bool)
+
+	// Returns object files produced by compiling the given cc-related target.
+	// Retrieves these files from Bazel's CcInfo provider.
+	GetCcObjectFiles(label string, archType ArchType) ([]string, bool)
 
 	// TODO(cparsons): Other cquery-related methods should be added here.
 	// ** End cquery methods
@@ -100,7 +106,12 @@
 	AllFiles map[string][]string
 }
 
-func (m MockBazelContext) GetAllFiles(label string) ([]string, bool) {
+func (m MockBazelContext) GetAllFiles(label string, archType ArchType) ([]string, bool) {
+	result, ok := m.AllFiles[label]
+	return result, ok
+}
+
+func (m MockBazelContext) GetCcObjectFiles(label string, archType ArchType) ([]string, bool) {
 	result, ok := m.AllFiles[label]
 	return result, ok
 }
@@ -123,8 +134,8 @@
 
 var _ BazelContext = MockBazelContext{}
 
-func (bazelCtx *bazelContext) GetAllFiles(label string) ([]string, bool) {
-	result, ok := bazelCtx.cquery(label, getAllFiles)
+func (bazelCtx *bazelContext) GetAllFiles(label string, archType ArchType) ([]string, bool) {
+	result, ok := bazelCtx.cquery(label, getAllFiles, archType)
 	if ok {
 		bazelOutput := strings.TrimSpace(result)
 		return strings.Split(bazelOutput, ", "), true
@@ -133,7 +144,21 @@
 	}
 }
 
-func (n noopBazelContext) GetAllFiles(label string) ([]string, bool) {
+func (bazelCtx *bazelContext) GetCcObjectFiles(label string, archType ArchType) ([]string, bool) {
+	result, ok := bazelCtx.cquery(label, getCcObjectFiles, archType)
+	if ok {
+		bazelOutput := strings.TrimSpace(result)
+		return strings.Split(bazelOutput, ", "), true
+	} else {
+		return nil, false
+	}
+}
+
+func (n noopBazelContext) GetAllFiles(label string, archType ArchType) ([]string, bool) {
+	panic("unimplemented")
+}
+
+func (n noopBazelContext) GetCcObjectFiles(label string, archType ArchType) ([]string, bool) {
 	panic("unimplemented")
 }
 
@@ -207,8 +232,9 @@
 // If the given request was already made (and the results are available), then
 // returns (result, true). If the request is queued but no results are available,
 // then returns ("", false).
-func (context *bazelContext) cquery(label string, requestType CqueryRequestType) (string, bool) {
-	key := cqueryKey{label, requestType}
+func (context *bazelContext) cquery(label string, requestType CqueryRequestType,
+	archType ArchType) (string, bool) {
+	key := cqueryKey{label, requestType, archType}
 	if result, ok := context.results[key]; ok {
 		return result, true
 	} else {
@@ -241,17 +267,21 @@
 		fmt.Sprintf("--platforms=%s", canonicalizeLabel("//build/bazel/platforms:generic_x86_64")))
 	cmdFlags = append(cmdFlags,
 		fmt.Sprintf("--extra_toolchains=%s", canonicalizeLabel("//prebuilts/clang/host/linux-x86:all")))
+	// Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
+	cmdFlags = append(cmdFlags, "--experimental_repository_disable_download")
 	cmdFlags = append(cmdFlags, extraFlags...)
 
 	bazelCmd := exec.Command(context.bazelPath, cmdFlags...)
 	bazelCmd.Dir = context.workspaceDir
-	bazelCmd.Env = append(os.Environ(), "HOME="+context.homeDir, pwdPrefix())
-
+	bazelCmd.Env = append(os.Environ(), "HOME="+context.homeDir, pwdPrefix(),
+		// Disables local host detection of gcc; toolchain information is defined
+		// explicitly in BUILD files.
+		"BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1")
 	stderr := &bytes.Buffer{}
 	bazelCmd.Stderr = stderr
 
 	if output, err := bazelCmd.Output(); err != nil {
-		return "", fmt.Errorf("bazel command failed. command: [%s], error [%s]", bazelCmd, stderr)
+		return "", fmt.Errorf("bazel command failed. command: [%s], env: [%s], error [%s]", bazelCmd, bazelCmd.Env, stderr)
 	} else {
 		return string(output), nil
 	}
@@ -273,20 +303,81 @@
 }
 
 func (context *bazelContext) mainBzlFileContents() []byte {
+	// TODO(cparsons): Define configuration transitions programmatically based
+	// on available archs.
 	contents := `
 #####################################################
 # This file is generated by soong_build. Do not edit.
 #####################################################
 
+def _x86_64_transition_impl(settings, attr):
+    return {
+        "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_x86_64",
+    }
+
+def _x86_transition_impl(settings, attr):
+    return {
+        "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_x86",
+    }
+
+def _arm64_transition_impl(settings, attr):
+    return {
+        "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_arm64",
+    }
+
+def _arm_transition_impl(settings, attr):
+    return {
+        "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_arm",
+    }
+
+x86_64_transition = transition(
+    implementation = _x86_64_transition_impl,
+    inputs = [],
+    outputs = [
+        "//command_line_option:platforms",
+    ],
+)
+
+x86_transition = transition(
+    implementation = _x86_transition_impl,
+    inputs = [],
+    outputs = [
+        "//command_line_option:platforms",
+    ],
+)
+
+arm64_transition = transition(
+    implementation = _arm64_transition_impl,
+    inputs = [],
+    outputs = [
+        "//command_line_option:platforms",
+    ],
+)
+
+arm_transition = transition(
+    implementation = _arm_transition_impl,
+    inputs = [],
+    outputs = [
+        "//command_line_option:platforms",
+    ],
+)
+
 def _mixed_build_root_impl(ctx):
-    return [DefaultInfo(files = depset(ctx.files.deps))]
+    all_files = ctx.files.deps_x86_64 + ctx.files.deps_x86 + ctx.files.deps_arm64 + ctx.files.deps_arm
+    return [DefaultInfo(files = depset(all_files))]
 
 # Rule representing the root of the build, to depend on all Bazel targets that
 # are required for the build. Building this target will build the entire Bazel
 # build tree.
 mixed_build_root = rule(
     implementation = _mixed_build_root_impl,
-    attrs = {"deps" : attr.label_list()},
+    attrs = {
+        "deps_x86_64" : attr.label_list(cfg = x86_64_transition),
+        "deps_x86" : attr.label_list(cfg = x86_transition),
+        "deps_arm64" : attr.label_list(cfg = arm64_transition),
+        "deps_arm" : attr.label_list(cfg = arm_transition),
+        "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
+    },
 )
 
 def _phony_root_impl(ctx):
@@ -317,25 +408,48 @@
 }
 
 func (context *bazelContext) mainBuildFileContents() []byte {
+	// TODO(cparsons): Map label to attribute programmatically; don't use hard-coded
+	// architecture mapping.
 	formatString := `
 # This file is generated by soong_build. Do not edit.
 load(":main.bzl", "mixed_build_root", "phony_root")
 
 mixed_build_root(name = "buildroot",
-    deps = [%s],
+    deps_x86_64 = [%s],
+    deps_x86 = [%s],
+    deps_arm64 = [%s],
+    deps_arm = [%s],
 )
 
 phony_root(name = "phonyroot",
     deps = [":buildroot"],
 )
 `
-	var buildRootDeps []string = nil
+	var deps_x86_64 []string = nil
+	var deps_x86 []string = nil
+	var deps_arm64 []string = nil
+	var deps_arm []string = nil
 	for val, _ := range context.requests {
-		buildRootDeps = append(buildRootDeps, fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label)))
+		labelString := fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label))
+		switch getArchString(val) {
+		case "x86_64":
+			deps_x86_64 = append(deps_x86_64, labelString)
+		case "x86":
+			deps_x86 = append(deps_x86, labelString)
+		case "arm64":
+			deps_arm64 = append(deps_arm64, labelString)
+		case "arm":
+			deps_arm = append(deps_arm, labelString)
+		default:
+			panic(fmt.Sprintf("unhandled architecture %s for %s", getArchString(val), val))
+		}
 	}
-	buildRootDepsString := strings.Join(buildRootDeps, ",\n            ")
 
-	return []byte(fmt.Sprintf(formatString, buildRootDepsString))
+	return []byte(fmt.Sprintf(formatString,
+		strings.Join(deps_x86_64, ",\n            "),
+		strings.Join(deps_x86, ",\n            "),
+		strings.Join(deps_arm64, ",\n            "),
+		strings.Join(deps_arm, ",\n            ")))
 }
 
 func (context *bazelContext) cqueryStarlarkFileContents() []byte {
@@ -345,23 +459,64 @@
   %s
 }
 
+getCcObjectFilesLabels = {
+  %s
+}
+
+def get_cc_object_files(target):
+  result = []
+  linker_inputs = providers(target)["CcInfo"].linking_context.linker_inputs.to_list()
+
+  for linker_input in linker_inputs:
+    for library in linker_input.libraries:
+      for object in library.objects:
+        result += [object.path]
+  return result
+
+def get_arch(target):
+  buildoptions = build_options(target)
+  platforms = build_options(target)["//command_line_option:platforms"]
+  if len(platforms) != 1:
+    # An individual configured target should have only one platform architecture.
+    # Note that it's fine for there to be multiple architectures for the same label,
+    # but each is its own configured target.
+    fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms))
+  platform_name = build_options(target)["//command_line_option:platforms"][0].name
+  if platform_name == "host":
+    return "HOST"
+  elif not platform_name.startswith("generic_"):
+    fail("expected platform name of the form 'generic_<arch>', but was " + str(platforms))
+    return "UNKNOWN"
+  return platform_name[len("generic_"):]
+
 def format(target):
-  if str(target.label) in getAllFilesLabels:
-    return str(target.label) + ">>" + ', '.join([f.path for f in target.files.to_list()])
+  id_string = str(target.label) + "|" + get_arch(target)
+  if id_string in getAllFilesLabels:
+    return id_string + ">>" + ', '.join([f.path for f in target.files.to_list()])
+  elif id_string in getCcObjectFilesLabels:
+    return id_string + ">>" + ', '.join(get_cc_object_files(target))
   else:
     # This target was not requested via cquery, and thus must be a dependency
     # of a requested target.
-    return ""
+    return id_string + ">>NONE"
 `
-	var buildRootDeps []string = nil
-	// TODO(cparsons): Sort by request type instead of assuming all requests
-	// are of GetAllFiles type.
-	for val, _ := range context.requests {
-		buildRootDeps = append(buildRootDeps, fmt.Sprintf("\"%s\" : True", canonicalizeLabel(val.label)))
-	}
-	buildRootDepsString := strings.Join(buildRootDeps, ",\n  ")
+	var getAllFilesDeps []string = nil
+	var getCcObjectFilesDeps []string = nil
 
-	return []byte(fmt.Sprintf(formatString, buildRootDepsString))
+	for val, _ := range context.requests {
+		labelWithArch := getCqueryId(val)
+		mapEntryString := fmt.Sprintf("%q : True", labelWithArch)
+		switch val.requestType {
+		case getAllFiles:
+			getAllFilesDeps = append(getAllFilesDeps, mapEntryString)
+		case getCcObjectFiles:
+			getCcObjectFilesDeps = append(getCcObjectFilesDeps, mapEntryString)
+		}
+	}
+	getAllFilesDepsString := strings.Join(getAllFilesDeps, ",\n  ")
+	getCcObjectFilesDepsString := strings.Join(getCcObjectFilesDeps, ",\n  ")
+
+	return []byte(fmt.Sprintf(formatString, getAllFilesDepsString, getCcObjectFilesDepsString))
 }
 
 // Returns a workspace-relative path containing build-related metadata required
@@ -414,9 +569,15 @@
 	}
 	buildrootLabel := "//:buildroot"
 	cqueryOutput, err = context.issueBazelCommand(bazel.CqueryBuildRootRunName, "cquery",
-		[]string{fmt.Sprintf("deps(%s)", buildrootLabel)},
+		[]string{fmt.Sprintf("kind(rule, deps(%s))", buildrootLabel)},
 		"--output=starlark",
 		"--starlark:file="+cqueryFileRelpath)
+	err = ioutil.WriteFile(
+		absolutePath(filepath.Join(context.intermediatesDir(), "cquery.out")),
+		[]byte(cqueryOutput), 0666)
+	if err != nil {
+		return err
+	}
 
 	if err != nil {
 		return err
@@ -431,10 +592,10 @@
 	}
 
 	for val, _ := range context.requests {
-		if cqueryResult, ok := cqueryResults[canonicalizeLabel(val.label)]; ok {
+		if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
 			context.results[val] = string(cqueryResult)
 		} else {
-			return fmt.Errorf("missing result for bazel target %s", val.label)
+			return fmt.Errorf("missing result for bazel target %s. query output: [%s]", getCqueryId(val), cqueryOutput)
 		}
 	}
 
@@ -510,6 +671,9 @@
 
 	// Register bazel-owned build statements (obtained from the aquery invocation).
 	for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
+		if len(buildStatement.Command) < 1 {
+			panic(fmt.Sprintf("unhandled build statement: %s", buildStatement))
+		}
 		rule := NewRuleBuilder(pctx, ctx)
 		cmd := rule.Command()
 		cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ && %s",
@@ -531,3 +695,16 @@
 		rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic)
 	}
 }
+
+func getCqueryId(key cqueryKey) string {
+	return canonicalizeLabel(key.label) + "|" + getArchString(key)
+}
+
+func getArchString(key cqueryKey) string {
+	arch := key.archType.Name
+	if len(arch) > 0 {
+		return arch
+	} else {
+		return "x86_64"
+	}
+}