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