• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15package android
16
17import (
18	"github.com/google/blueprint"
19	"github.com/google/blueprint/pool"
20)
21
22// Phases:
23//   run Pre-arch mutators
24//   run archMutator
25//   run Pre-deps mutators
26//   run depsMutator
27//   run PostDeps mutators
28//   run FinalDeps mutators (TransitionMutators disallowed in this phase)
29//   continue on to GenerateAndroidBuildActions
30
31// collateGloballyRegisteredMutators constructs the list of mutators that have been registered
32// with the InitRegistrationContext and will be used at runtime.
33func collateGloballyRegisteredMutators() sortableComponents {
34	return collateRegisteredMutators(preArch, preDeps, postDeps, postApex, finalDeps)
35}
36
37// collateRegisteredMutators constructs a single list of mutators from the separate lists.
38func collateRegisteredMutators(preArch, preDeps, postDeps, postApex, finalDeps []RegisterMutatorFunc) sortableComponents {
39	mctx := &registerMutatorsContext{}
40
41	register := func(funcs []RegisterMutatorFunc) {
42		for _, f := range funcs {
43			f(mctx)
44		}
45	}
46
47	register(preArch)
48
49	register(preDeps)
50
51	register([]RegisterMutatorFunc{registerDepsMutator})
52
53	register(postDeps)
54
55	register(postApex)
56
57	mctx.finalPhase = true
58	register(finalDeps)
59
60	return mctx.mutators
61}
62
63type registerMutatorsContext struct {
64	mutators   sortableComponents
65	finalPhase bool
66}
67
68type RegisterMutatorsContext interface {
69	BottomUp(name string, m BottomUpMutator) MutatorHandle
70	BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
71	Transition(name string, m VariationTransitionMutator) TransitionMutatorHandle
72	InfoBasedTransition(name string, m androidTransitionMutator) TransitionMutatorHandle
73}
74
75type RegisterMutatorFunc func(RegisterMutatorsContext)
76
77var preArch = []RegisterMutatorFunc{
78	RegisterNamespaceMutator,
79
80	// Check the visibility rules are valid.
81	//
82	// This must run after the package renamer mutators so that any issues found during
83	// validation of the package's default_visibility property are reported using the
84	// correct package name and not the synthetic name.
85	//
86	// This must also be run before defaults mutators as the rules for validation are
87	// different before checking the rules than they are afterwards. e.g.
88	//    visibility: ["//visibility:private", "//visibility:public"]
89	// would be invalid if specified in a module definition but is valid if it results
90	// from something like this:
91	//
92	//    defaults {
93	//        name: "defaults",
94	//        // Be inaccessible outside a package by default.
95	//        visibility: ["//visibility:private"]
96	//    }
97	//
98	//    defaultable_module {
99	//        name: "defaultable_module",
100	//        defaults: ["defaults"],
101	//        // Override the default.
102	//        visibility: ["//visibility:public"]
103	//    }
104	//
105	RegisterVisibilityRuleChecker,
106
107	// Record the default_applicable_licenses for each package.
108	//
109	// This must run before the defaults so that defaults modules can pick up the package default.
110	RegisterLicensesPackageMapper,
111
112	// Apply properties from defaults modules to the referencing modules.
113	//
114	// Any mutators that are added before this will not see any modules created by
115	// a DefaultableHook.
116	RegisterDefaultsPreArchMutators,
117
118	// Add dependencies on any components so that any component references can be
119	// resolved within the deps mutator.
120	//
121	// Must be run after defaults so it can be used to create dependencies on the
122	// component modules that are creating in a DefaultableHook.
123	//
124	// Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
125	// renamed. That is so that if a module creates components using a prebuilt module
126	// type that any dependencies (which must use prebuilt_ prefixes) are resolved to
127	// the prebuilt module and not the source module.
128	RegisterComponentsMutator,
129
130	// Create an association between prebuilt modules and their corresponding source
131	// modules (if any).
132	//
133	// Must be run after defaults mutators to ensure that any modules created by
134	// a DefaultableHook can be either a prebuilt or a source module with a matching
135	// prebuilt.
136	RegisterPrebuiltsPreArchMutators,
137
138	// Gather the licenses properties for all modules for use during expansion and enforcement.
139	//
140	// This must come after the defaults mutators to ensure that any licenses supplied
141	// in a defaults module has been successfully applied before the rules are gathered.
142	RegisterLicensesPropertyGatherer,
143
144	// Gather the visibility rules for all modules for us during visibility enforcement.
145	//
146	// This must come after the defaults mutators to ensure that any visibility supplied
147	// in a defaults module has been successfully applied before the rules are gathered.
148	RegisterVisibilityRuleGatherer,
149}
150
151func registerArchMutator(ctx RegisterMutatorsContext) {
152	ctx.Transition("os", &osTransitionMutator{})
153	ctx.BottomUp("image_begin", imageMutatorBeginMutator)
154	ctx.Transition("image", &imageTransitionMutator{})
155	ctx.Transition("arch", &archTransitionMutator{})
156}
157
158var preDeps = []RegisterMutatorFunc{
159	registerArchMutator,
160	RegisterPrebuiltsPreDepsMutators,
161}
162
163var postDeps = []RegisterMutatorFunc{
164	registerPathDepsMutator,
165	RegisterPrebuiltsPostDepsMutators,
166	RegisterVisibilityRuleEnforcer,
167	RegisterLicensesDependencyChecker,
168	registerNeverallowMutator,
169	RegisterOverridePostDepsMutators,
170}
171
172var postApex = []RegisterMutatorFunc{}
173
174var finalDeps = []RegisterMutatorFunc{}
175
176func PreArchMutators(f RegisterMutatorFunc) {
177	preArch = append(preArch, f)
178}
179
180func PreDepsMutators(f RegisterMutatorFunc) {
181	preDeps = append(preDeps, f)
182}
183
184func PostDepsMutators(f RegisterMutatorFunc) {
185	postDeps = append(postDeps, f)
186}
187
188func PostApexMutators(f RegisterMutatorFunc) {
189	postApex = append(postApex, f)
190}
191
192func FinalDepsMutators(f RegisterMutatorFunc) {
193	finalDeps = append(finalDeps, f)
194}
195
196type BottomUpMutator func(BottomUpMutatorContext)
197
198type BottomUpMutatorContext interface {
199	BaseModuleContext
200
201	// AddDependency adds a dependency to the given module.  It returns a slice of modules for each
202	// dependency (some entries may be nil).
203	//
204	// This method will pause until the new dependencies have had the current mutator called on them.
205	AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []Module
206
207	// AddReverseDependency adds a dependency from the destination to the given module.
208	// Does not affect the ordering of the current mutator pass, but will be ordered
209	// correctly for all future mutator passes.  All reverse dependencies for a destination module are
210	// collected until the end of the mutator pass, sorted by name, and then appended to the destination
211	// module's dependency list.  May only  be called by mutators that were marked with
212	// UsesReverseDependencies during registration.
213	AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
214
215	// AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
216	// argument to select which variant of the dependency to use.  It returns a slice of modules for
217	// each dependency (some entries may be nil).  A variant of the dependency must exist that matches
218	// all the non-local variations of the current module, plus the variations argument.
219	//
220	// This method will pause until the new dependencies have had the current mutator called on them.
221	AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []Module
222
223	// AddReverseVariationDependency adds a dependency from the named module to the current
224	// module. The given variations will be added to the current module's varations, and then the
225	// result will be used to find the correct variation of the depending module, which must exist.
226	//
227	// Does not affect the ordering of the current mutator pass, but will be ordered
228	// correctly for all future mutator passes.  All reverse dependencies for a destination module are
229	// collected until the end of the mutator pass, sorted by name, and then appended to the destination
230	// module's dependency list.  May only  be called by mutators that were marked with
231	// UsesReverseDependencies during registration.
232	AddReverseVariationDependency([]blueprint.Variation, blueprint.DependencyTag, string)
233
234	// AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
235	// variations argument to select which variant of the dependency to use.  It returns a slice of
236	// modules for each dependency (some entries may be nil).  A variant of the dependency must
237	// exist that matches the variations argument, but may also have other variations.
238	// For any unspecified variation the first variant will be used.
239	//
240	// Unlike AddVariationDependencies, the variations of the current module are ignored - the
241	// dependency only needs to match the supplied variations.
242	//
243	// This method will pause until the new dependencies have had the current mutator called on them.
244	AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []Module
245
246	// ReplaceDependencies finds all the variants of the module with the specified name, then
247	// replaces all dependencies onto those variants with the current variant of this module.
248	// Replacements don't take effect until after the mutator pass is finished.  May only
249	// be called by mutators that were marked with UsesReplaceDependencies during registration.
250	ReplaceDependencies(string)
251
252	// ReplaceDependenciesIf finds all the variants of the module with the specified name, then
253	// replaces all dependencies onto those variants with the current variant of this module
254	// as long as the supplied predicate returns true.
255	// Replacements don't take effect until after the mutator pass is finished.  May only
256	// be called by mutators that were marked with UsesReplaceDependencies during registration.
257	ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
258
259	// Rename all variants of a module.  The new name is not visible to calls to ModuleName,
260	// AddDependency or OtherModuleName until after this mutator pass is complete.  May only be called
261	// by mutators that were marked with UsesRename during registration.
262	Rename(name string)
263
264	// CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
265	// the specified property structs to it as if the properties were set in a blueprint file.  May only
266	// be called by mutators that were marked with UsesCreateModule during registration.
267	CreateModule(ModuleFactory, ...interface{}) Module
268}
269
270// An outgoingTransitionContextImpl and incomingTransitionContextImpl is created for every dependency of every module
271// for each transition mutator.  bottomUpMutatorContext is created once for every module for every BottomUp mutator.
272// Use a global pool for each to avoid reallocating every time.
273var (
274	outgoingTransitionContextPool = pool.New[outgoingTransitionContextImpl]()
275	incomingTransitionContextPool = pool.New[incomingTransitionContextImpl]()
276	bottomUpMutatorContextPool    = pool.New[bottomUpMutatorContext]()
277)
278
279type bottomUpMutatorContext struct {
280	bp blueprint.BottomUpMutatorContext
281	baseModuleContext
282	finalPhase bool
283}
284
285// callers must immediately follow the call to this function with defer bottomUpMutatorContextPool.Put(mctx).
286func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
287	finalPhase bool) *bottomUpMutatorContext {
288
289	moduleContext := a.base().baseModuleContextFactory(ctx)
290	mctx := bottomUpMutatorContextPool.Get()
291	*mctx = bottomUpMutatorContext{
292		bp:                ctx,
293		baseModuleContext: moduleContext,
294		finalPhase:        finalPhase,
295	}
296	return mctx
297}
298
299func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
300	finalPhase := x.finalPhase
301	f := func(ctx blueprint.BottomUpMutatorContext) {
302		if a, ok := ctx.Module().(Module); ok {
303			mctx := bottomUpMutatorContextFactory(ctx, a, finalPhase)
304			defer bottomUpMutatorContextPool.Put(mctx)
305			m(mctx)
306		}
307	}
308	mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
309	x.mutators = append(x.mutators, mutator)
310	return mutator
311}
312
313func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
314	mutator := &mutator{name: name, bottomUpMutator: m}
315	x.mutators = append(x.mutators, mutator)
316	return mutator
317}
318
319func (x *registerMutatorsContext) Transition(name string, m VariationTransitionMutator) TransitionMutatorHandle {
320	atm := &androidTransitionMutatorAdapter{
321		finalPhase: x.finalPhase,
322		mutator:    variationTransitionMutatorAdapter{m},
323		name:       name,
324	}
325	mutator := &mutator{
326		name:              name,
327		transitionMutator: atm,
328	}
329	x.mutators = append(x.mutators, mutator)
330	return mutator
331}
332
333func (x *registerMutatorsContext) InfoBasedTransition(name string, m androidTransitionMutator) TransitionMutatorHandle {
334	atm := &androidTransitionMutatorAdapter{
335		finalPhase: x.finalPhase,
336		mutator:    m,
337		name:       name,
338	}
339	mutator := &mutator{
340		name:              name,
341		transitionMutator: atm,
342	}
343	x.mutators = append(x.mutators, mutator)
344	return mutator
345}
346
347func (x *registerMutatorsContext) mutatorName(name string) string {
348	return name
349}
350
351func (mutator *mutator) componentName() string {
352	return mutator.name
353}
354
355func (mutator *mutator) register(ctx *Context) {
356	blueprintCtx := ctx.Context
357	var handle blueprint.MutatorHandle
358	if mutator.bottomUpMutator != nil {
359		handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
360	} else if mutator.transitionMutator != nil {
361		handle := blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
362		if mutator.neverFar {
363			handle.NeverFar()
364		}
365	}
366
367	// Forward booleans set on the MutatorHandle to the blueprint.MutatorHandle.
368	if mutator.usesRename {
369		handle.UsesRename()
370	}
371	if mutator.usesReverseDependencies {
372		handle.UsesReverseDependencies()
373	}
374	if mutator.usesReplaceDependencies {
375		handle.UsesReplaceDependencies()
376	}
377	if mutator.usesCreateModule {
378		handle.UsesCreateModule()
379	}
380	if mutator.mutatesDependencies {
381		handle.MutatesDependencies()
382	}
383	if mutator.mutatesGlobalState {
384		handle.MutatesGlobalState()
385	}
386}
387
388type MutatorHandle interface {
389	// Parallel sets the mutator to visit modules in parallel while maintaining ordering.  Calling any
390	// method on the mutator context is thread-safe, but the mutator must handle synchronization
391	// for any modifications to global state or any modules outside the one it was invoked on.
392	// Deprecated: all Mutators are parallel by default.
393	Parallel() MutatorHandle
394
395	// UsesRename marks the mutator as using the BottomUpMutatorContext.Rename method, which prevents
396	// coalescing adjacent mutators into a single mutator pass.
397	UsesRename() MutatorHandle
398
399	// UsesReverseDependencies marks the mutator as using the BottomUpMutatorContext.AddReverseDependency
400	// method, which prevents coalescing adjacent mutators into a single mutator pass.
401	UsesReverseDependencies() MutatorHandle
402
403	// UsesReplaceDependencies marks the mutator as using the BottomUpMutatorContext.ReplaceDependencies
404	// method, which prevents coalescing adjacent mutators into a single mutator pass.
405	UsesReplaceDependencies() MutatorHandle
406
407	// UsesCreateModule marks the mutator as using the BottomUpMutatorContext.CreateModule method,
408	// which prevents coalescing adjacent mutators into a single mutator pass.
409	UsesCreateModule() MutatorHandle
410
411	// MutatesDependencies marks the mutator as modifying properties in dependencies, which prevents
412	// coalescing adjacent mutators into a single mutator pass.
413	MutatesDependencies() MutatorHandle
414
415	// MutatesGlobalState marks the mutator as modifying global state, which prevents coalescing
416	// adjacent mutators into a single mutator pass.
417	MutatesGlobalState() MutatorHandle
418}
419
420type TransitionMutatorHandle interface {
421	// NeverFar causes the variations created by this mutator to never be ignored when adding
422	// far variation dependencies. Normally, far variation dependencies ignore all the variants
423	// of the source module, and only use the variants explicitly requested by the
424	// AddFarVariationDependencies call.
425	NeverFar() MutatorHandle
426}
427
428func (mutator *mutator) Parallel() MutatorHandle {
429	return mutator
430}
431
432func (mutator *mutator) UsesRename() MutatorHandle {
433	mutator.usesRename = true
434	return mutator
435}
436
437func (mutator *mutator) UsesReverseDependencies() MutatorHandle {
438	mutator.usesReverseDependencies = true
439	return mutator
440}
441
442func (mutator *mutator) UsesReplaceDependencies() MutatorHandle {
443	mutator.usesReplaceDependencies = true
444	return mutator
445}
446
447func (mutator *mutator) UsesCreateModule() MutatorHandle {
448	mutator.usesCreateModule = true
449	return mutator
450}
451
452func (mutator *mutator) MutatesDependencies() MutatorHandle {
453	mutator.mutatesDependencies = true
454	return mutator
455}
456
457func (mutator *mutator) MutatesGlobalState() MutatorHandle {
458	mutator.mutatesGlobalState = true
459	return mutator
460}
461
462func (mutator *mutator) NeverFar() MutatorHandle {
463	mutator.neverFar = true
464	return mutator
465}
466
467func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
468	ctx.BottomUp("component-deps", componentDepsMutator)
469}
470
471// A special mutator that runs just prior to the deps mutator to allow the dependencies
472// on component modules to be added so that they can depend directly on a prebuilt
473// module.
474func componentDepsMutator(ctx BottomUpMutatorContext) {
475	ctx.Module().ComponentDepsMutator(ctx)
476}
477
478func depsMutator(ctx BottomUpMutatorContext) {
479	if m := ctx.Module(); m.Enabled(ctx) {
480		m.base().baseDepsMutator(ctx)
481		m.DepsMutator(ctx)
482	}
483}
484
485func registerDepsMutator(ctx RegisterMutatorsContext) {
486	ctx.BottomUp("deps", depsMutator).UsesReverseDependencies()
487}
488
489// android.bottomUpMutatorContext either has to embed blueprint.BottomUpMutatorContext, in which case every method that
490// has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
491// ambiguous method errors, or it has to store a blueprint.BottomUpMutatorContext non-embedded, in which case every
492// non-overridden method has to be forwarded.  There are fewer non-overridden methods, so use the latter.  The following
493// methods forward to the identical blueprint versions for bottomUpMutatorContext.
494
495func (b *bottomUpMutatorContext) Rename(name string) {
496	b.bp.Rename(name)
497	b.Module().base().commonProperties.DebugName = name
498}
499
500func (b *bottomUpMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) Module {
501	return bpModuleToModule(b.bp.CreateModule(factory, name, props...))
502}
503
504func (b *bottomUpMutatorContext) createModuleInDirectory(factory blueprint.ModuleFactory, name string, _ string, props ...interface{}) Module {
505	panic("createModuleInDirectory is not implemented for bottomUpMutatorContext")
506}
507
508func (b *bottomUpMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
509	return createModule(b, factory, "_bottomUpMutatorModule", doesNotSpecifyDirectory(), props...)
510}
511
512func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []Module {
513	if b.baseModuleContext.checkedMissingDeps() {
514		panic("Adding deps not allowed after checking for missing deps")
515	}
516	return bpModulesToModules(b.bp.AddDependency(module, tag, name...))
517}
518
519func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
520	if b.baseModuleContext.checkedMissingDeps() {
521		panic("Adding deps not allowed after checking for missing deps")
522	}
523	b.bp.AddReverseDependency(module, tag, name)
524}
525
526func (b *bottomUpMutatorContext) AddReverseVariationDependency(variations []blueprint.Variation, tag blueprint.DependencyTag, name string) {
527	if b.baseModuleContext.checkedMissingDeps() {
528		panic("Adding deps not allowed after checking for missing deps")
529	}
530	b.bp.AddReverseVariationDependency(variations, tag, name)
531}
532
533func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
534	names ...string) []Module {
535	if b.baseModuleContext.checkedMissingDeps() {
536		panic("Adding deps not allowed after checking for missing deps")
537	}
538	return bpModulesToModules(b.bp.AddVariationDependencies(variations, tag, names...))
539}
540
541func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
542	tag blueprint.DependencyTag, names ...string) []Module {
543	if b.baseModuleContext.checkedMissingDeps() {
544		panic("Adding deps not allowed after checking for missing deps")
545	}
546
547	return bpModulesToModules(b.bp.AddFarVariationDependencies(variations, tag, names...))
548}
549
550func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
551	if b.baseModuleContext.checkedMissingDeps() {
552		panic("Adding deps not allowed after checking for missing deps")
553	}
554	b.bp.ReplaceDependencies(name)
555}
556
557func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
558	if b.baseModuleContext.checkedMissingDeps() {
559		panic("Adding deps not allowed after checking for missing deps")
560	}
561	b.bp.ReplaceDependenciesIf(name, predicate)
562}
563
564func bpModulesToModules(bpModules []blueprint.Module) []Module {
565	modules := make([]Module, len(bpModules))
566	for i, bpModule := range bpModules {
567		modules[i] = bpModuleToModule(bpModule)
568	}
569	return modules
570}
571
572func bpModuleToModule(bpModule blueprint.Module) Module {
573	if bpModule != nil {
574		return bpModule.(Module)
575	}
576	return nil
577}
578