blob: 25a7a43bec02d9616bda535bfd6633e5d3e5e81c [file] [log] [blame]
Jesse Pai4d336662019-09-13 17:08:52 -07001package cc
2
3import (
4 "fmt"
5 "sort"
6 "strings"
7
Jesse Pai4d336662019-09-13 17:08:52 -07008 "android/soong/android"
9)
10
11func init() {
12 android.RegisterSingletonType("cflag_artifacts_text", cflagArtifactsTextFactory)
13}
14
Cole Faustbaa24a22023-01-13 15:05:44 -080015func cflagArtifactsTextFactory() android.Singleton {
16 return &cflagArtifactsText{}
17}
18
Jesse Pai4d336662019-09-13 17:08:52 -070019var (
20 TrackedCFlags = []string{
21 "-Wall",
22 "-Werror",
23 "-Wextra",
24 "-Wthread-safety",
25 "-O3",
26 }
27
28 TrackedCFlagsDir = []string{
29 "device/google/",
30 "vendor/google/",
31 }
32)
33
Jesse Pai4d336662019-09-13 17:08:52 -070034// Stores output files.
35type cflagArtifactsText struct {
Cole Faustbaa24a22023-01-13 15:05:44 -080036 outputs android.WritablePaths
Jesse Pai4d336662019-09-13 17:08:52 -070037}
38
39// allowedDir verifies if the directory/project is part of the TrackedCFlagsDir
40// filter.
41func allowedDir(subdir string) bool {
42 subdir += "/"
Jaewoong Jung3aff5782020-02-11 07:54:35 -080043 return android.HasAnyPrefix(subdir, TrackedCFlagsDir)
Jesse Pai4d336662019-09-13 17:08:52 -070044}
45
Cole Faustbaa24a22023-01-13 15:05:44 -080046// GenCFlagArtifact is used to generate the build rules which produce a file
47// that contains a list of all modules using/not using a particular cflag
48func (s *cflagArtifactsText) GenCFlagArtifact(ctx android.SingletonContext,
49 flag string, modulesUsing, modulesNotUsing []string) {
Jesse Pai4d336662019-09-13 17:08:52 -070050
Cole Faustbaa24a22023-01-13 15:05:44 -080051 filename := "module_cflags" + flag + ".txt"
Jesse Pai4d336662019-09-13 17:08:52 -070052 filepath := android.PathForOutput(ctx, "cflags", filename)
Jesse Pai4d336662019-09-13 17:08:52 -070053
Cole Faustbaa24a22023-01-13 15:05:44 -080054 lines := make([]string, 0, 2+len(modulesUsing)+len(modulesNotUsing))
55 lines = append(lines, "# Modules using "+flag)
56 lines = append(lines, modulesUsing...)
57 lines = append(lines, "# Modules not using "+flag)
58 lines = append(lines, modulesNotUsing...)
Jesse Pai4d336662019-09-13 17:08:52 -070059
Cole Faustbaa24a22023-01-13 15:05:44 -080060 android.WriteFileRule(ctx, filepath, strings.Join(lines, "\n"))
61 s.outputs = append(s.outputs, filepath)
Jesse Pai4d336662019-09-13 17:08:52 -070062}
63
64func (s *cflagArtifactsText) GenerateBuildActions(ctx android.SingletonContext) {
65 modulesWithCFlag := make(map[string][]string)
66
67 // Scan through all modules, selecting the ones that are part of the filter,
68 // and then storing into a map which tracks whether or not tracked C flag is
69 // used or not.
70 ctx.VisitAllModules(func(module android.Module) {
71 if ccModule, ok := module.(*Module); ok {
72 if allowedDir(ctx.ModuleDir(ccModule)) {
Colin Cross4af21ed2019-11-04 09:37:55 -080073 cflags := ccModule.flags.Local.CFlags
74 cppflags := ccModule.flags.Local.CppFlags
Jesse Pai4d336662019-09-13 17:08:52 -070075 module := fmt.Sprintf("%s:%s (%s)",
76 ctx.BlueprintFile(ccModule),
77 ctx.ModuleName(ccModule),
78 ctx.ModuleSubDir(ccModule))
79 for _, flag := range TrackedCFlags {
80 if inList(flag, cflags) || inList(flag, cppflags) {
81 modulesWithCFlag[flag] = append(modulesWithCFlag[flag], module)
82 } else {
83 modulesWithCFlag["!"+flag] = append(modulesWithCFlag["!"+flag], module)
84 }
85 }
86 }
87 }
88 })
89
90 // Traversing map and setting up rules to produce intermediary files which
91 // contain parts of each expected C Flag artifact.
92 for _, flag := range TrackedCFlags {
93 sort.Strings(modulesWithCFlag[flag])
Jesse Pai4d336662019-09-13 17:08:52 -070094 sort.Strings(modulesWithCFlag["!"+flag])
Cole Faustbaa24a22023-01-13 15:05:44 -080095 s.GenCFlagArtifact(ctx, flag, modulesWithCFlag[flag], modulesWithCFlag["!"+flag])
Jesse Pai4d336662019-09-13 17:08:52 -070096 }
97}
98
99func (s *cflagArtifactsText) MakeVars(ctx android.MakeVarsContext) {
100 ctx.Strict("SOONG_MODULES_CFLAG_ARTIFACTS", strings.Join(s.outputs.Strings(), " "))
101}