| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 15 | package android |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 16 | |
| Colin Cross | 4498afc | 2016-10-13 14:18:27 -0700 | [diff] [blame] | 17 | import ( |
| Chris Parsons | 5011e61 | 2023-09-13 23:33:20 +0000 | [diff] [blame] | 18 | "bufio" |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 19 | "fmt" |
| Chris Parsons | 5011e61 | 2023-09-13 23:33:20 +0000 | [diff] [blame] | 20 | "path/filepath" |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 21 | "reflect" |
| Chris Parsons | 5011e61 | 2023-09-13 23:33:20 +0000 | [diff] [blame] | 22 | "regexp" |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 23 | |
| Chris Parsons | 5011e61 | 2023-09-13 23:33:20 +0000 | [diff] [blame] | 24 | "android/soong/shared" |
| Cole Faust | ae6cda6 | 2023-11-01 15:32:40 -0700 | [diff] [blame^] | 25 | |
| Colin Cross | 4498afc | 2016-10-13 14:18:27 -0700 | [diff] [blame] | 26 | "github.com/google/blueprint" |
| 27 | ) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 28 | |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 29 | // A sortable component is one whose registration order affects the order in which it is executed |
| 30 | // and so affects the behavior of the build system. As a result it is important for the order in |
| 31 | // which they are registered during tests to match the order used at runtime and so the test |
| 32 | // infrastructure will sort them to match. |
| 33 | // |
| 34 | // The sortable components are mutators, singletons and pre-singletons. Module types are not |
| 35 | // sortable because their order of registration does not affect the runtime behavior. |
| 36 | type sortableComponent interface { |
| 37 | // componentName returns the name of the component. |
| 38 | // |
| Usta Shrestha | c725f47 | 2022-01-11 02:44:21 -0500 | [diff] [blame] | 39 | // Uniquely identifies the components within the set of components used at runtime and during |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 40 | // tests. |
| 41 | componentName() string |
| 42 | |
| Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 43 | // registers this component in the supplied context. |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 44 | register(ctx *Context) |
| 45 | } |
| 46 | |
| 47 | type sortableComponents []sortableComponent |
| 48 | |
| 49 | // registerAll registers all components in this slice with the supplied context. |
| 50 | func (r sortableComponents) registerAll(ctx *Context) { |
| 51 | for _, c := range r { |
| 52 | c.register(ctx) |
| 53 | } |
| 54 | } |
| 55 | |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 56 | type moduleType struct { |
| 57 | name string |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 58 | factory ModuleFactory |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 61 | func (t moduleType) register(ctx *Context) { |
| 62 | ctx.RegisterModuleType(t.name, ModuleFactoryAdaptor(t.factory)) |
| 63 | } |
| 64 | |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 65 | var moduleTypes []moduleType |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 66 | var moduleTypesForDocs = map[string]reflect.Value{} |
| Sam Delmerico | 286bf26 | 2022-03-09 20:46:37 +0000 | [diff] [blame] | 67 | var moduleTypeByFactory = map[reflect.Value]string{} |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 68 | |
| 69 | type singleton struct { |
| LaMont Jones | e59c0db | 2023-05-15 21:50:29 +0000 | [diff] [blame] | 70 | // True if this should be registered as a parallel singleton. |
| 71 | parallel bool |
| 72 | |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 73 | name string |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 74 | factory SingletonFactory |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| LaMont Jones | e59c0db | 2023-05-15 21:50:29 +0000 | [diff] [blame] | 77 | func newSingleton(name string, factory SingletonFactory, parallel bool) singleton { |
| Cole Faust | ae6cda6 | 2023-11-01 15:32:40 -0700 | [diff] [blame^] | 78 | return singleton{parallel: parallel, name: name, factory: factory} |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | func (s singleton) componentName() string { |
| 82 | return s.name |
| 83 | } |
| 84 | |
| 85 | func (s singleton) register(ctx *Context) { |
| 86 | adaptor := SingletonFactoryAdaptor(ctx, s.factory) |
| Cole Faust | ae6cda6 | 2023-11-01 15:32:40 -0700 | [diff] [blame^] | 87 | ctx.RegisterSingletonType(s.name, adaptor, s.parallel) |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | var _ sortableComponent = singleton{} |
| 91 | |
| 92 | var singletons sortableComponents |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 93 | |
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 94 | type mutator struct { |
| Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 95 | name string |
| 96 | bottomUpMutator blueprint.BottomUpMutator |
| 97 | topDownMutator blueprint.TopDownMutator |
| 98 | transitionMutator blueprint.TransitionMutator |
| 99 | parallel bool |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 102 | var _ sortableComponent = &mutator{} |
| 103 | |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 104 | type ModuleFactory func() Module |
| 105 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 106 | // ModuleFactoryAdaptor wraps a ModuleFactory into a blueprint.ModuleFactory by converting a Module |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 107 | // into a blueprint.Module and a list of property structs |
| 108 | func ModuleFactoryAdaptor(factory ModuleFactory) blueprint.ModuleFactory { |
| 109 | return func() (blueprint.Module, []interface{}) { |
| 110 | module := factory() |
| 111 | return module, module.GetProperties() |
| 112 | } |
| 113 | } |
| 114 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 115 | type SingletonFactory func() Singleton |
| 116 | |
| 117 | // SingletonFactoryAdaptor wraps a SingletonFactory into a blueprint.SingletonFactory by converting |
| 118 | // a Singleton into a blueprint.Singleton |
| Colin Cross | 06fa588 | 2020-10-29 18:21:38 -0700 | [diff] [blame] | 119 | func SingletonFactoryAdaptor(ctx *Context, factory SingletonFactory) blueprint.SingletonFactory { |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 120 | return func() blueprint.Singleton { |
| 121 | singleton := factory() |
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 122 | if makevars, ok := singleton.(SingletonMakeVarsProvider); ok { |
| Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 123 | ctx.registerSingletonMakeVarsProvider(makevars) |
| Colin Cross | ed023ec | 2019-02-19 12:38:45 -0800 | [diff] [blame] | 124 | } |
| Colin Cross | 4c83e5c | 2019-02-25 14:54:28 -0800 | [diff] [blame] | 125 | return &singletonAdaptor{Singleton: singleton} |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 129 | func RegisterModuleType(name string, factory ModuleFactory) { |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 130 | moduleTypes = append(moduleTypes, moduleType{name, factory}) |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 131 | RegisterModuleTypeForDocs(name, reflect.ValueOf(factory)) |
| 132 | } |
| 133 | |
| 134 | // RegisterModuleTypeForDocs associates a module type name with a reflect.Value of the factory |
| 135 | // function that has documentation for the module type. It is normally called automatically |
| 136 | // by RegisterModuleType, but can be called manually after RegisterModuleType in order to |
| 137 | // override the factory method used for documentation, for example if the method passed to |
| 138 | // RegisterModuleType was a lambda. |
| 139 | func RegisterModuleTypeForDocs(name string, factory reflect.Value) { |
| 140 | moduleTypesForDocs[name] = factory |
| Sam Delmerico | 286bf26 | 2022-03-09 20:46:37 +0000 | [diff] [blame] | 141 | moduleTypeByFactory[factory] = name |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| LaMont Jones | e59c0db | 2023-05-15 21:50:29 +0000 | [diff] [blame] | 144 | func registerSingletonType(name string, factory SingletonFactory, parallel bool) { |
| 145 | singletons = append(singletons, newSingleton(name, factory, parallel)) |
| 146 | } |
| 147 | |
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 148 | func RegisterSingletonType(name string, factory SingletonFactory) { |
| LaMont Jones | e59c0db | 2023-05-15 21:50:29 +0000 | [diff] [blame] | 149 | registerSingletonType(name, factory, false) |
| 150 | } |
| 151 | |
| 152 | func RegisterParallelSingletonType(name string, factory SingletonFactory) { |
| 153 | registerSingletonType(name, factory, true) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 156 | type Context struct { |
| 157 | *blueprint.Context |
| Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 158 | config Config |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 159 | } |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 160 | |
| Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 161 | func NewContext(config Config) *Context { |
| 162 | ctx := &Context{blueprint.NewContext(), config} |
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 163 | ctx.SetSrcDir(absSrcDir) |
| Spandan Das | c576383 | 2022-11-08 18:42:16 +0000 | [diff] [blame] | 164 | ctx.AddIncludeTags(config.IncludeTags()...) |
| Sam Delmerico | 98a7329 | 2023-02-21 11:50:29 -0500 | [diff] [blame] | 165 | ctx.AddSourceRootDirs(config.SourceRootDirs()...) |
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 166 | return ctx |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| Jingwen Chen | 863e334 | 2023-10-05 10:23:58 +0000 | [diff] [blame] | 169 | // Helper function to register the module types used in bp2build. |
| Spandan Das | 75e139b | 2022-11-28 20:12:03 +0000 | [diff] [blame] | 170 | func registerModuleTypes(ctx *Context) { |
| Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 171 | for _, t := range moduleTypes { |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 172 | t.register(ctx) |
| Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 173 | } |
| Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 174 | // Required for SingletonModule types, even though we are not using them. |
| 175 | for _, t := range singletons { |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 176 | t.register(ctx) |
| Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 177 | } |
| Spandan Das | 75e139b | 2022-11-28 20:12:03 +0000 | [diff] [blame] | 178 | } |
| Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 179 | |
| Spandan Das | 75e139b | 2022-11-28 20:12:03 +0000 | [diff] [blame] | 180 | // RegisterForBazelConversion registers an alternate shadow pipeline of |
| 181 | // singletons, module types and mutators to register for converting Blueprint |
| 182 | // files to semantically equivalent BUILD files. |
| 183 | func (ctx *Context) RegisterForBazelConversion() { |
| 184 | registerModuleTypes(ctx) |
| Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 185 | RegisterMutatorsForBazelConversion(ctx, bp2buildPreArchMutators) |
| Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 186 | } |
| 187 | |
| Chris Parsons | 5011e61 | 2023-09-13 23:33:20 +0000 | [diff] [blame] | 188 | // RegisterExistingBazelTargets reads Bazel BUILD.bazel and BUILD files under |
| 189 | // the workspace, and returns a map containing names of Bazel targets defined in |
| 190 | // these BUILD files. |
| 191 | // For example, maps "//foo/bar" to ["baz", "qux"] if `//foo/bar:{baz,qux}` exist. |
| 192 | func (c *Context) RegisterExistingBazelTargets(topDir string, existingBazelFiles []string) error { |
| 193 | result := map[string][]string{} |
| 194 | |
| 195 | // Search for instances of `name = "$NAME"` (with arbitrary spacing). |
| 196 | targetNameRegex := regexp.MustCompile(`(?m)^\s*name\s*=\s*\"([^\"]+)\"`) |
| 197 | |
| 198 | parseBuildFile := func(path string) error { |
| 199 | fullPath := shared.JoinPath(topDir, path) |
| 200 | sourceDir := filepath.Dir(path) |
| 201 | |
| 202 | fileInfo, err := c.Config().fs.Stat(fullPath) |
| 203 | if err != nil { |
| 204 | return fmt.Errorf("Error accessing Bazel file '%s': %s", path, err) |
| 205 | } |
| 206 | if !fileInfo.IsDir() && |
| 207 | (fileInfo.Name() == "BUILD" || fileInfo.Name() == "BUILD.bazel") { |
| 208 | f, err := c.Config().fs.Open(fullPath) |
| 209 | if err != nil { |
| 210 | return fmt.Errorf("Error reading Bazel file '%s': %s", path, err) |
| 211 | } |
| 212 | defer f.Close() |
| 213 | scanner := bufio.NewScanner(f) |
| 214 | for scanner.Scan() { |
| 215 | line := scanner.Text() |
| 216 | matches := targetNameRegex.FindAllStringSubmatch(line, -1) |
| 217 | for _, match := range matches { |
| 218 | result[sourceDir] = append(result[sourceDir], match[1]) |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | return nil |
| 223 | } |
| 224 | |
| 225 | for _, path := range existingBazelFiles { |
| 226 | if !c.Config().Bp2buildPackageConfig.ShouldKeepExistingBuildFileForDir(filepath.Dir(path)) { |
| 227 | continue |
| 228 | } |
| 229 | err := parseBuildFile(path) |
| 230 | if err != nil { |
| 231 | return err |
| 232 | } |
| 233 | } |
| 234 | c.Config().SetBazelBuildFileTargets(result) |
| 235 | return nil |
| 236 | } |
| 237 | |
| Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 238 | // Register the pipeline of singletons, module types, and mutators for |
| 239 | // generating build.ninja and other files for Kati, from Android.bp files. |
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 240 | func (ctx *Context) Register() { |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 241 | for _, t := range moduleTypes { |
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 242 | t.register(ctx) |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| Paul Duffin | c05b034 | 2021-03-06 13:28:13 +0000 | [diff] [blame] | 245 | mutators := collateGloballyRegisteredMutators() |
| 246 | mutators.registerAll(ctx) |
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 247 | |
| Paul Duffin | 42d0b93 | 2021-03-06 20:24:50 +0000 | [diff] [blame] | 248 | singletons := collateGloballyRegisteredSingletons() |
| 249 | singletons.registerAll(ctx) |
| 250 | } |
| Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 251 | |
| Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 252 | func (ctx *Context) Config() Config { |
| 253 | return ctx.config |
| 254 | } |
| 255 | |
| 256 | func (ctx *Context) registerSingletonMakeVarsProvider(makevars SingletonMakeVarsProvider) { |
| 257 | registerSingletonMakeVarsProvider(ctx.config, makevars) |
| 258 | } |
| 259 | |
| Paul Duffin | 42d0b93 | 2021-03-06 20:24:50 +0000 | [diff] [blame] | 260 | func collateGloballyRegisteredSingletons() sortableComponents { |
| 261 | allSingletons := append(sortableComponents(nil), singletons...) |
| 262 | allSingletons = append(allSingletons, |
| Cole Faust | ae6cda6 | 2023-11-01 15:32:40 -0700 | [diff] [blame^] | 263 | singleton{parallel: true, name: "bazeldeps", factory: BazelSingleton}, |
| Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 264 | |
| Paul Duffin | 42d0b93 | 2021-03-06 20:24:50 +0000 | [diff] [blame] | 265 | // Register phony just before makevars so it can write out its phony rules as Make rules |
| Cole Faust | ae6cda6 | 2023-11-01 15:32:40 -0700 | [diff] [blame^] | 266 | singleton{parallel: false, name: "phony", factory: phonySingletonFactory}, |
| Colin Cross | 580d2ce | 2019-02-09 22:59:32 -0800 | [diff] [blame] | 267 | |
| Paul Duffin | 42d0b93 | 2021-03-06 20:24:50 +0000 | [diff] [blame] | 268 | // Register makevars after other singletons so they can export values through makevars |
| Cole Faust | ae6cda6 | 2023-11-01 15:32:40 -0700 | [diff] [blame^] | 269 | singleton{parallel: false, name: "makevars", factory: makeVarsSingletonFunc}, |
| Paul Duffin | 42d0b93 | 2021-03-06 20:24:50 +0000 | [diff] [blame] | 270 | |
| 271 | // Register env and ninjadeps last so that they can track all used environment variables and |
| 272 | // Ninja file dependencies stored in the config. |
| Cole Faust | ae6cda6 | 2023-11-01 15:32:40 -0700 | [diff] [blame^] | 273 | singleton{parallel: false, name: "ninjadeps", factory: ninjaDepsSingletonFactory}, |
| Paul Duffin | 42d0b93 | 2021-03-06 20:24:50 +0000 | [diff] [blame] | 274 | ) |
| 275 | |
| 276 | return allSingletons |
| Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 277 | } |
| Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 278 | |
| 279 | func ModuleTypeFactories() map[string]ModuleFactory { |
| 280 | ret := make(map[string]ModuleFactory) |
| 281 | for _, t := range moduleTypes { |
| 282 | ret[t.name] = t.factory |
| 283 | } |
| 284 | return ret |
| 285 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 286 | |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 287 | func ModuleTypeFactoriesForDocs() map[string]reflect.Value { |
| 288 | return moduleTypesForDocs |
| 289 | } |
| 290 | |
| Sam Delmerico | 286bf26 | 2022-03-09 20:46:37 +0000 | [diff] [blame] | 291 | func ModuleTypeByFactory() map[reflect.Value]string { |
| 292 | return moduleTypeByFactory |
| 293 | } |
| 294 | |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 295 | // Interface for registering build components. |
| 296 | // |
| 297 | // Provided to allow registration of build components to be shared between the runtime |
| 298 | // and test environments. |
| 299 | type RegistrationContext interface { |
| 300 | RegisterModuleType(name string, factory ModuleFactory) |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 301 | RegisterSingletonModuleType(name string, factory SingletonModuleFactory) |
| LaMont Jones | e59c0db | 2023-05-15 21:50:29 +0000 | [diff] [blame] | 302 | RegisterParallelSingletonModuleType(name string, factory SingletonModuleFactory) |
| LaMont Jones | e59c0db | 2023-05-15 21:50:29 +0000 | [diff] [blame] | 303 | RegisterParallelSingletonType(name string, factory SingletonFactory) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 304 | RegisterSingletonType(name string, factory SingletonFactory) |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 305 | PreArchMutators(f RegisterMutatorFunc) |
| Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 306 | |
| 307 | // Register pre arch mutators that are hard coded into mutator.go. |
| 308 | // |
| 309 | // Only registers mutators for testing, is a noop on the InitRegistrationContext. |
| 310 | HardCodedPreArchMutators(f RegisterMutatorFunc) |
| 311 | |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 312 | PreDepsMutators(f RegisterMutatorFunc) |
| 313 | PostDepsMutators(f RegisterMutatorFunc) |
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 314 | FinalDepsMutators(f RegisterMutatorFunc) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // Used to register build components from an init() method, e.g. |
| 318 | // |
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 319 | // init() { |
| 320 | // RegisterBuildComponents(android.InitRegistrationContext) |
| 321 | // } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 322 | // |
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 323 | // func RegisterBuildComponents(ctx android.RegistrationContext) { |
| 324 | // ctx.RegisterModuleType(...) |
| 325 | // ... |
| 326 | // } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 327 | // |
| 328 | // Extracting the actual registration into a separate RegisterBuildComponents(ctx) function |
| 329 | // allows it to be used to initialize test context, e.g. |
| 330 | // |
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 331 | // ctx := android.NewTestContext(config) |
| 332 | // RegisterBuildComponents(ctx) |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 333 | var InitRegistrationContext RegistrationContext = &initRegistrationContext{ |
| Cole Faust | ae6cda6 | 2023-11-01 15:32:40 -0700 | [diff] [blame^] | 334 | moduleTypes: make(map[string]ModuleFactory), |
| 335 | singletonTypes: make(map[string]SingletonFactory), |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 336 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 337 | |
| 338 | // Make sure the TestContext implements RegistrationContext. |
| 339 | var _ RegistrationContext = (*TestContext)(nil) |
| 340 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 341 | type initRegistrationContext struct { |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 342 | moduleTypes map[string]ModuleFactory |
| 343 | singletonTypes map[string]SingletonFactory |
| 344 | moduleTypesForDocs map[string]reflect.Value |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 345 | } |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 346 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 347 | func (ctx *initRegistrationContext) RegisterModuleType(name string, factory ModuleFactory) { |
| 348 | if _, present := ctx.moduleTypes[name]; present { |
| 349 | panic(fmt.Sprintf("module type %q is already registered", name)) |
| 350 | } |
| 351 | ctx.moduleTypes[name] = factory |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 352 | RegisterModuleType(name, factory) |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 353 | RegisterModuleTypeForDocs(name, reflect.ValueOf(factory)) |
| 354 | } |
| 355 | |
| 356 | func (ctx *initRegistrationContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) { |
| LaMont Jones | e59c0db | 2023-05-15 21:50:29 +0000 | [diff] [blame] | 357 | ctx.registerSingletonModuleType(name, factory, false) |
| 358 | } |
| 359 | func (ctx *initRegistrationContext) RegisterParallelSingletonModuleType(name string, factory SingletonModuleFactory) { |
| 360 | ctx.registerSingletonModuleType(name, factory, true) |
| 361 | } |
| 362 | |
| 363 | func (ctx *initRegistrationContext) registerSingletonModuleType(name string, factory SingletonModuleFactory, parallel bool) { |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 364 | s, m := SingletonModuleFactoryAdaptor(name, factory) |
| LaMont Jones | e59c0db | 2023-05-15 21:50:29 +0000 | [diff] [blame] | 365 | ctx.registerSingletonType(name, s, parallel) |
| Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 366 | ctx.RegisterModuleType(name, m) |
| 367 | // Overwrite moduleTypesForDocs with the original factory instead of the lambda returned by |
| 368 | // SingletonModuleFactoryAdaptor so that docs can find the module type documentation on the |
| 369 | // factory method. |
| 370 | RegisterModuleTypeForDocs(name, reflect.ValueOf(factory)) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| LaMont Jones | e59c0db | 2023-05-15 21:50:29 +0000 | [diff] [blame] | 373 | func (ctx *initRegistrationContext) registerSingletonType(name string, factory SingletonFactory, parallel bool) { |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 374 | if _, present := ctx.singletonTypes[name]; present { |
| 375 | panic(fmt.Sprintf("singleton type %q is already registered", name)) |
| 376 | } |
| 377 | ctx.singletonTypes[name] = factory |
| LaMont Jones | e59c0db | 2023-05-15 21:50:29 +0000 | [diff] [blame] | 378 | registerSingletonType(name, factory, parallel) |
| 379 | } |
| 380 | |
| 381 | func (ctx *initRegistrationContext) RegisterSingletonType(name string, factory SingletonFactory) { |
| 382 | ctx.registerSingletonType(name, factory, false) |
| 383 | } |
| 384 | |
| 385 | func (ctx *initRegistrationContext) RegisterParallelSingletonType(name string, factory SingletonFactory) { |
| 386 | ctx.registerSingletonType(name, factory, true) |
| Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 387 | } |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 388 | |
| Paul Duffin | 0a28683 | 2019-12-19 12:23:01 +0000 | [diff] [blame] | 389 | func (ctx *initRegistrationContext) PreArchMutators(f RegisterMutatorFunc) { |
| Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 390 | PreArchMutators(f) |
| 391 | } |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 392 | |
| Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 393 | func (ctx *initRegistrationContext) HardCodedPreArchMutators(_ RegisterMutatorFunc) { |
| Paul Duffin | a80ef84 | 2020-01-14 12:09:36 +0000 | [diff] [blame] | 394 | // Nothing to do as the mutators are hard code in preArch in mutator.go |
| 395 | } |
| 396 | |
| Paul Duffin | 2ccaffd | 2019-12-19 15:12:58 +0000 | [diff] [blame] | 397 | func (ctx *initRegistrationContext) PreDepsMutators(f RegisterMutatorFunc) { |
| 398 | PreDepsMutators(f) |
| 399 | } |
| 400 | |
| 401 | func (ctx *initRegistrationContext) PostDepsMutators(f RegisterMutatorFunc) { |
| 402 | PostDepsMutators(f) |
| 403 | } |
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 404 | |
| 405 | func (ctx *initRegistrationContext) FinalDepsMutators(f RegisterMutatorFunc) { |
| 406 | FinalDepsMutators(f) |
| 407 | } |