Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fa049385 authored by Joel Galenson's avatar Joel Galenson
Browse files

Migrate Rust to LLVM coverage.

Bug: 177675913
Test: Manually compile, run, and see output with llvm-cov.
Change-Id: I66729cff87a848782e9fa1b95cbbc06318c5761a
parent 66f7fdd1
Loading
Loading
Loading
Loading
+0 −7
Original line number Diff line number Diff line
@@ -82,9 +82,6 @@ func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.Andr
	}

	ret.Class = "EXECUTABLES"
	ret.ExtraEntries = append(ret.ExtraEntries, func(entries *android.AndroidMkEntries) {
		entries.SetOptionalPath("LOCAL_PREBUILT_COVERAGE_ARCHIVE", binary.coverageOutputZipFile)
	})
}

func (test *testDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
@@ -117,10 +114,6 @@ func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.An
	if library.distFile.Valid() {
		ret.DistFiles = android.MakeDefaultDistFiles(library.distFile.Path())
	}

	ret.ExtraEntries = append(ret.ExtraEntries, func(entries *android.AndroidMkEntries) {
		entries.SetOptionalPath("LOCAL_PREBUILT_COVERAGE_ARCHIVE", library.coverageOutputZipFile)
	})
}

func (procMacro *procMacroDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkEntries) {
+1 −16
Original line number Diff line number Diff line
@@ -121,7 +121,7 @@ func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps Path
	flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
	flags.LinkFlags = append(flags.LinkFlags, deps.linkObjects...)

	outputs := TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)
	TransformSrcToBinary(ctx, srcPath, deps, flags, outputFile, deps.linkDirs)

	if binary.stripper.NeedsStrip(ctx) {
		strippedOutputFile := android.PathForModuleOut(ctx, "stripped", fileName)
@@ -129,24 +129,9 @@ func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps Path
		binary.strippedOutputFile = android.OptionalPathForPath(strippedOutputFile)
	}

	binary.coverageFile = outputs.coverageFile

	var coverageFiles android.Paths
	if outputs.coverageFile != nil {
		coverageFiles = append(coverageFiles, binary.coverageFile)
	}
	if len(deps.coverageFiles) > 0 {
		coverageFiles = append(coverageFiles, deps.coverageFiles...)
	}
	binary.coverageOutputZipFile = TransformCoverageFilesToZip(ctx, coverageFiles, binary.getStem(ctx))

	return outputFile
}

func (binary *binaryDecorator) coverageOutputZipPath() android.OptionalPath {
	return binary.coverageOutputZipFile
}

func (binary *binaryDecorator) autoDep(ctx BaseModuleContext) autoDep {
	// Binaries default to dylib dependencies for device, rlib for host.
	if binary.preferRlib() {
+1 −43
Original line number Diff line number Diff line
@@ -19,10 +19,8 @@ import (
	"strings"

	"github.com/google/blueprint"
	"github.com/google/blueprint/pathtools"

	"android/soong/android"
	"android/soong/cc"
	"android/soong/rust/config"
)

@@ -77,7 +75,6 @@ var (

type buildOutput struct {
	outputFile android.Path
	coverageFile android.Path
}

func init() {
@@ -195,27 +192,6 @@ func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, fl
		implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
	}

	if flags.Coverage {
		var gcnoFile android.WritablePath
		// Provide consistency with cc gcda output, see cc/builder.go init()
		profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/"

		if outputFile.Ext() != "" {
			// rustc seems to split the output filename at the first '.' when determining the gcno filename
			// so we need to do the same here.
			gcnoFile = android.PathForModuleOut(ctx, strings.Split(outputFile.Base(), ".")[0]+".gcno")
			rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
				ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcda")).String())
		} else {
			gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno")
			rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
				ctx, outputFile.Base()+".gcda").String())
		}

		implicitOutputs = append(implicitOutputs, gcnoFile)
		output.coverageFile = gcnoFile
	}

	if len(deps.SrcDeps) > 0 {
		genSubDir := "out/"
		moduleGenDir := android.PathForModuleOut(ctx, genSubDir)
@@ -292,21 +268,3 @@ func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, fl

	return output
}

func TransformCoverageFilesToZip(ctx ModuleContext,
	covFiles android.Paths, baseName string) android.OptionalPath {
	if len(covFiles) > 0 {

		outputFile := android.PathForModuleOut(ctx, baseName+".zip")

		ctx.Build(pctx, android.BuildParams{
			Rule:        zip,
			Description: "zip " + outputFile.Base(),
			Inputs:      covFiles,
			Output:      outputFile,
		})

		return android.OptionalPathForPath(outputFile)
	}
	return android.OptionalPath{}
}
+2 −4
Original line number Diff line number Diff line
@@ -137,7 +137,6 @@ type BaseCompilerProperties struct {

type baseCompiler struct {
	Properties BaseCompilerProperties
	coverageFile android.Path //rustc generates a single gcno file

	// Install related
	dir      string
@@ -147,7 +146,6 @@ type baseCompiler struct {
	path     android.InstallPath
	location installLocation

	coverageOutputZipFile android.OptionalPath
	distFile android.OptionalPath
	// Stripped output file. If Valid(), this file will be installed instead of outputFile.
	strippedOutputFile android.OptionalPath
+5 −3
Original line number Diff line number Diff line
@@ -20,7 +20,9 @@ import (
	"android/soong/cc"
)

var CovLibraryName = "libprofile-extras"
var CovLibraryName = "libprofile-clang-extras"

const profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw"

type coverage struct {
	Properties cc.CoverageProperties
@@ -53,9 +55,9 @@ func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags
		flags.Coverage = true
		coverage := ctx.GetDirectDepWithTag(CovLibraryName, cc.CoverageDepTag).(cc.LinkableInterface)
		flags.RustFlags = append(flags.RustFlags,
			"-Z profile", "-g", "-C opt-level=0", "-C link-dead-code")
			"-Z instrument-coverage", "-g", "-C link-dead-code")
		flags.LinkFlags = append(flags.LinkFlags,
			"--coverage", "-g", coverage.OutputFile().Path().String(), "-Wl,--wrap,getenv")
			profileInstrFlag, "-g", coverage.OutputFile().Path().String(), "-Wl,--wrap,open")
		deps.StaticLibs = append(deps.StaticLibs, coverage.OutputFile().Path())
	}

Loading