Jesse Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 1 | package cc |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "sort" |
| 6 | "strings" |
| 7 | |
Jesse Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 8 | "android/soong/android" |
| 9 | ) |
| 10 | |
| 11 | func init() { |
| 12 | android.RegisterSingletonType("cflag_artifacts_text", cflagArtifactsTextFactory) |
| 13 | } |
| 14 | |
Cole Faust | baa24a2 | 2023-01-13 15:05:44 -0800 | [diff] [blame] | 15 | func cflagArtifactsTextFactory() android.Singleton { |
| 16 | return &cflagArtifactsText{} |
| 17 | } |
| 18 | |
Jesse Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 19 | var ( |
| 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 Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 34 | // Stores output files. |
| 35 | type cflagArtifactsText struct { |
Cole Faust | baa24a2 | 2023-01-13 15:05:44 -0800 | [diff] [blame] | 36 | outputs android.WritablePaths |
Jesse Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // allowedDir verifies if the directory/project is part of the TrackedCFlagsDir |
| 40 | // filter. |
| 41 | func allowedDir(subdir string) bool { |
| 42 | subdir += "/" |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 43 | return android.HasAnyPrefix(subdir, TrackedCFlagsDir) |
Jesse Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Cole Faust | baa24a2 | 2023-01-13 15:05:44 -0800 | [diff] [blame] | 46 | // 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 |
| 48 | func (s *cflagArtifactsText) GenCFlagArtifact(ctx android.SingletonContext, |
| 49 | flag string, modulesUsing, modulesNotUsing []string) { |
Jesse Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 50 | |
Cole Faust | baa24a2 | 2023-01-13 15:05:44 -0800 | [diff] [blame] | 51 | filename := "module_cflags" + flag + ".txt" |
Jesse Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 52 | filepath := android.PathForOutput(ctx, "cflags", filename) |
Jesse Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 53 | |
Cole Faust | baa24a2 | 2023-01-13 15:05:44 -0800 | [diff] [blame] | 54 | 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 Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 59 | |
Cole Faust | baa24a2 | 2023-01-13 15:05:44 -0800 | [diff] [blame] | 60 | android.WriteFileRule(ctx, filepath, strings.Join(lines, "\n")) |
| 61 | s.outputs = append(s.outputs, filepath) |
Jesse Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | func (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 Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 73 | cflags := ccModule.flags.Local.CFlags |
| 74 | cppflags := ccModule.flags.Local.CppFlags |
Jesse Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 75 | 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 Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 94 | sort.Strings(modulesWithCFlag["!"+flag]) |
Cole Faust | baa24a2 | 2023-01-13 15:05:44 -0800 | [diff] [blame] | 95 | s.GenCFlagArtifact(ctx, flag, modulesWithCFlag[flag], modulesWithCFlag["!"+flag]) |
Jesse Pai | 4d33666 | 2019-09-13 17:08:52 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | func (s *cflagArtifactsText) MakeVars(ctx android.MakeVarsContext) { |
| 100 | ctx.Strict("SOONG_MODULES_CFLAG_ARTIFACTS", strings.Join(s.outputs.Strings(), " ")) |
| 101 | } |