Add clang-tidy support
For every file which we can run clang-tidy (C/C++ clang-built), we add a
new build node that depends on the object file (since clang-tidy does
not export a depfile), and is depended on by the link step. This is
better than how we're doing it in make, since calling tidy can be turned
on or off without needing to rebuild the object files.
This does not attempt to port WITH_TIDY_ONLY from Make, since the way
that it works is broken (due to the lack of a depfile).
Bug: 32244182
Test: WITH_TIDY=true mmma -j bionic/libc
Test: ./soong (Setting ClangTidy: true)
Change-Id: I40bbb5bb00d292d72bf1c293b93080b5f9f6d8ea
diff --git a/cc/builder.go b/cc/builder.go
index 0006ed3..faa39d1 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -150,6 +150,14 @@
Restat: true,
},
"crossCompile")
+
+ clangTidy = pctx.AndroidStaticRule("clangTidy",
+ blueprint.RuleParams{
+ Command: "rm -f $out && ${config.ClangBin}/clang-tidy $tidyFlags $in -- $cFlags && touch $out",
+ CommandDeps: []string{"${config.ClangBin}/clang-tidy"},
+ Description: "tidy $out",
+ },
+ "cFlags", "tidyFlags")
)
func init() {
@@ -174,8 +182,10 @@
libFlags string
yaccFlags string
protoFlags string
+ tidyFlags string
toolchain config.Toolchain
clang bool
+ tidy bool
stripKeepSymbols bool
stripKeepMiniDebugInfo bool
@@ -183,18 +193,21 @@
}
type Objects struct {
- objFiles android.Paths
+ objFiles android.Paths
+ tidyFiles android.Paths
}
func (a Objects) Copy() Objects {
return Objects{
- objFiles: append(android.Paths{}, a.objFiles...),
+ objFiles: append(android.Paths{}, a.objFiles...),
+ tidyFiles: append(android.Paths{}, a.tidyFiles...),
}
}
func (a Objects) Append(b Objects) Objects {
return Objects{
- objFiles: append(a.objFiles, b.objFiles...),
+ objFiles: append(a.objFiles, b.objFiles...),
+ tidyFiles: append(a.tidyFiles, b.tidyFiles...),
}
}
@@ -203,6 +216,10 @@
flags builderFlags, deps android.Paths) Objects {
objFiles := make(android.Paths, len(srcFiles))
+ var tidyFiles android.Paths
+ if flags.tidy && flags.clang {
+ tidyFiles = make(android.Paths, 0, len(srcFiles))
+ }
cflags := flags.globalFlags + " " + flags.cFlags + " " + flags.conlyFlags
cppflags := flags.globalFlags + " " + flags.cFlags + " " + flags.cppFlags
@@ -223,11 +240,13 @@
var moduleCflags string
var ccCmd string
+ tidy := flags.tidy && flags.clang
switch srcFile.Ext() {
case ".S", ".s":
ccCmd = "gcc"
moduleCflags = asflags
+ tidy = false
case ".c":
ccCmd = "gcc"
moduleCflags = cflags
@@ -264,24 +283,45 @@
"ccCmd": ccCmd,
},
})
+
+ if tidy {
+ tidyFile := android.ObjPathWithExt(ctx, srcFile, subdir, "tidy")
+ tidyFiles = append(tidyFiles, tidyFile)
+
+ ctx.ModuleBuild(pctx, android.ModuleBuildParams{
+ Rule: clangTidy,
+ Output: tidyFile,
+ Input: srcFile,
+ // We must depend on objFile, since clang-tidy doesn't
+ // support exporting dependencies.
+ Implicit: objFile,
+ Args: map[string]string{
+ "cFlags": moduleCflags,
+ "tidyFlags": flags.tidyFlags,
+ },
+ })
+ }
+
}
return Objects{
- objFiles: objFiles,
+ objFiles: objFiles,
+ tidyFiles: tidyFiles,
}
}
// Generate a rule for compiling multiple .o files to a static library (.a)
func TransformObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
- flags builderFlags, outputFile android.ModuleOutPath) {
+ flags builderFlags, outputFile android.ModuleOutPath, deps android.Paths) {
arCmd := gccCmd(flags.toolchain, "ar")
arFlags := "crsPD"
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
- Rule: ar,
- Output: outputFile,
- Inputs: objFiles,
+ Rule: ar,
+ Output: outputFile,
+ Inputs: objFiles,
+ Implicits: deps,
Args: map[string]string{
"arFlags": arFlags,
"arCmd": arCmd,
@@ -294,7 +334,7 @@
// very small command line length limit, so we have to split the ar into multiple
// steps, each appending to the previous one.
func TransformDarwinObjToStaticLib(ctx android.ModuleContext, objFiles android.Paths,
- flags builderFlags, outputPath android.ModuleOutPath) {
+ flags builderFlags, outputPath android.ModuleOutPath, deps android.Paths) {
arFlags := "cqs"
@@ -303,8 +343,9 @@
dummyAr := android.PathForModuleOut(ctx, "dummy"+staticLibraryExtension)
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
- Rule: emptyFile,
- Output: dummy,
+ Rule: emptyFile,
+ Output: dummy,
+ Implicits: deps,
})
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
@@ -347,9 +388,10 @@
if in == "" {
ctx.Build(pctx, blueprint.BuildParams{
- Rule: darwinAr,
- Outputs: []string{out},
- Inputs: l,
+ Rule: darwinAr,
+ Outputs: []string{out},
+ Inputs: l,
+ Implicits: deps.Strings(),
Args: map[string]string{
"arFlags": arFlags,
},