Skip to content

Commit 49e5403

Browse files
author
Jonathan Watson
committed
refactor: create common interface for external library rules
Change-Id: I9bcdf51b63804af6703ba1bcd3e245984ec06dd7
1 parent bf3d192 commit 49e5403

7 files changed

Lines changed: 89 additions & 116 deletions

File tree

core/androidninja_backend.go

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -184,53 +184,44 @@ func (g *androidNinjaGenerator) getSharedLibFlags(m BackendCommonLibraryInterfac
184184
ctx.VisitDirectDepsIf(
185185
func(m blueprint.Module) bool { return ctx.OtherModuleDependencyTag(m) == tag.SharedTag },
186186
func(m blueprint.Module) {
187-
if sl, ok := m.(*ModuleSharedLibrary); ok {
188-
if isExternal(m.(*ModuleSharedLibrary)) {
189-
ldlibs = append(ldlibs, sl.FlagsOut().Filtered(func(f flag.Flag) bool {
190-
return f.MatchesType(flag.TypeLinkLibrary)
191-
}).ToStringSlice()...)
192-
193-
ldflags = append(ldflags, sl.FlagsOut().Filtered(func(f flag.Flag) bool {
194-
return f.MatchesType(flag.TypeLinker)
195-
}).ToStringSlice()...)
196-
} else {
197-
b := &sl.ModuleLibrary.Properties.Build
198-
if b.isForwardingSharedLibrary() {
199-
hasForwardingLib = true
200-
ldlibs = append(ldlibs, tc.GetLinker().KeepSharedLibraryTransitivity())
201-
if useNoAsNeeded {
202-
ldlibs = append(ldlibs, tc.GetLinker().KeepUnusedDependencies())
203-
}
204-
}
205-
ldlibs = append(ldlibs, pathToLibFlagAndroid(sl.outputName()))
206-
if b.isForwardingSharedLibrary() {
207-
if useNoAsNeeded {
208-
ldlibs = append(ldlibs, tc.GetLinker().DropUnusedDependencies())
209-
}
210-
ldlibs = append(ldlibs, tc.GetLinker().DropSharedLibraryTransitivity())
187+
if ex, ok := m.(externableLibrary); ok && ex.isExternal() {
188+
ldlibs = append(ldlibs, ex.FlagsOut().Filtered(func(f flag.Flag) bool {
189+
return f.MatchesType(flag.TypeLinkLibrary)
190+
}).ToStringSlice()...)
191+
192+
ldflags = append(ldflags, ex.FlagsOut().Filtered(func(f flag.Flag) bool {
193+
return f.MatchesType(flag.TypeLinker)
194+
}).ToStringSlice()...)
195+
} else if sl, ok := m.(*ModuleSharedLibrary); ok {
196+
b := &sl.ModuleLibrary.Properties.Build
197+
if b.isForwardingSharedLibrary() {
198+
hasForwardingLib = true
199+
ldlibs = append(ldlibs, tc.GetLinker().KeepSharedLibraryTransitivity())
200+
if useNoAsNeeded {
201+
ldlibs = append(ldlibs, tc.GetLinker().KeepUnusedDependencies())
211202
}
212-
if installPath, ok := sl.Properties.InstallableProps.getInstallPath(); ok {
213-
libPaths = utils.AppendIfUnique(libPaths, installPath)
203+
}
204+
ldlibs = append(ldlibs, pathToLibFlagAndroid(sl.outputName()))
205+
if b.isForwardingSharedLibrary() {
206+
if useNoAsNeeded {
207+
ldlibs = append(ldlibs, tc.GetLinker().DropUnusedDependencies())
214208
}
209+
ldlibs = append(ldlibs, tc.GetLinker().DropSharedLibraryTransitivity())
210+
}
211+
if installPath, ok := sl.Properties.InstallableProps.getInstallPath(); ok {
212+
libPaths = utils.AppendIfUnique(libPaths, installPath)
215213
}
216214
} else if sl, ok := m.(*generateSharedLibrary); ok {
217215
ldlibs = append(ldlibs, pathToLibFlagAndroid(sl.outputName()))
218216
if installPath, ok := sl.ModuleGenerateCommon.Properties.InstallableProps.getInstallPath(); ok {
219217
libPaths = utils.AppendIfUnique(libPaths, installPath)
220218
}
221-
} else if el, ok := m.(*ModuleExternalLibrary); ok {
222-
ldlibs = append(ldlibs, el.FlagsOut().Filtered(func(f flag.Flag) bool {
223-
return f.MatchesType(flag.TypeLinkLibrary)
224-
}).ToStringSlice()...)
225-
226-
ldflags = append(ldflags, el.FlagsOut().Filtered(func(f flag.Flag) bool {
227-
return f.MatchesType(flag.TypeLinker)
228-
}).ToStringSlice()...)
229219
} else if sl, ok := m.(*ModuleStrictLibrary); ok {
230220
ldlibs = append(ldlibs, pathToLibFlagAndroid(sl.Name()+".so"))
231221
} else {
232222
utils.Die("%s is not a shared library", ctx.OtherModuleName(m))
233223
}
224+
234225
})
235226

236227
ctx.VisitDirectDepsIf(
@@ -288,19 +279,17 @@ func (g *androidNinjaGenerator) getSharedLibTocPaths(ctx blueprint.ModuleContext
288279
ctx.VisitDirectDepsIf(
289280
func(m blueprint.Module) bool { return ctx.OtherModuleDependencyTag(m) == tag.SharedTag },
290281
func(m blueprint.Module) {
291-
if _, ok := m.(sharedLibProducer); ok { //Remove this check and replace it with an API call
292-
if m, ok := m.(file.Provider); ok {
293-
if e, ok := m.(externable); !ok || !isExternal(e) {
294-
if toc, ok := m.OutFiles().FindSingle(
295-
func(p file.Path) bool { return p.IsType(file.TypeToc) }); ok {
296-
libs = append(libs, toc.BuildPath())
297-
}
298-
}
299-
}
300-
} else if _, ok := m.(*ModuleExternalLibrary); ok {
282+
if e, ok := m.(externableLibrary); ok && e.isExternal() {
301283
// Don't try and guess the path to external libraries,
302284
// and as they are outside of the build we don't need to
303285
// add a dependency on them anyway.
286+
} else if _, ok := m.(sharedLibProducer); ok { //Remove this check and replace it with an API call
287+
if m, ok := m.(file.Provider); ok {
288+
if toc, ok := m.OutFiles().FindSingle(
289+
func(p file.Path) bool { return p.IsType(file.TypeToc) }); ok {
290+
libs = append(libs, toc.BuildPath())
291+
}
292+
}
304293
} else {
305294
utils.Die("%s doesn't produce a shared library", ctx.OtherModuleName(m))
306295
}
@@ -312,14 +301,12 @@ func (g *androidNinjaGenerator) getSharedLibLinkPaths(ctx blueprint.ModuleContex
312301
ctx.VisitDirectDepsIf(
313302
func(m blueprint.Module) bool { return ctx.OtherModuleDependencyTag(m) == tag.SharedTag },
314303
func(m blueprint.Module) {
315-
if t, ok := m.(targetableModule); ok {
316-
if e, ok := t.(externable); !ok || !isExternal(e) {
317-
libs = append(libs, g.getSharedLibLinkPath(t))
318-
}
319-
} else if _, ok := m.(*ModuleExternalLibrary); ok {
304+
if e, ok := m.(externableLibrary); ok && e.isExternal() {
320305
// Don't try and guess the path to external libraries,
321306
// and as they are outside of the build we don't need to
322307
// add a dependency on them anyway.
308+
} else if t, ok := m.(targetableModule); ok {
309+
libs = append(libs, g.getSharedLibLinkPath(t))
323310
} else {
324311
utils.Die("%s doesn't support targets", ctx.OtherModuleName(m))
325312
}

core/binary.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (m *ModuleBinary) strip() bool {
3535
}
3636

3737
func (m *ModuleBinary) GenerateBuildActions(ctx blueprint.ModuleContext) {
38-
if isEnabled(m) && !isExternal(m) {
38+
if isEnabled(m) && !m.isExternal() {
3939
getGenerator(ctx).binaryActions(m, ctx)
4040
}
4141
}

core/external_library.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,8 @@ func externalLibFactory(config *BobConfig) (blueprint.Module, []interface{}) {
183183
module.Properties.Target.init(&config.Properties, ExternalLibProps{})
184184
return module, []interface{}{&module.Properties, &module.SimpleName.Properties}
185185
}
186+
187+
var _ externableLibrary = (*ModuleExternalLibrary)(nil) // impl check
188+
func (m *ModuleExternalLibrary) isExternal() bool {
189+
return true
190+
}

core/library.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ func (m *ModuleLibrary) getInstallDepPhonyNames(ctx blueprint.ModuleContext) []s
120120
depTag := ctx.OtherModuleDependencyTag(m)
121121
// External libraries do not have a build target so don't
122122
// try to add a dependency on them.
123-
if _, ok := m.(*ModuleExternalLibrary); ok {
124-
return false
125-
}
126-
if ml, ok := m.(*ModuleLibrary); ok && isExternal(ml) {
123+
if e, ok := m.(externableLibrary); ok && e.isExternal() {
127124
return false
128125
}
129126
if depTag == tag.InstallTag || depTag == tag.SharedTag {
@@ -137,16 +134,13 @@ func (m *ModuleLibrary) getEnableableProps() *EnableableProps {
137134
return &m.Properties.Build.EnableableProps
138135
}
139136

140-
type externable interface {
141-
getExternalableProps() *ExternalableProps
142-
}
143-
144-
func (m *ModuleLibrary) getExternalableProps() *ExternalableProps {
145-
return &m.Properties.ExternalableProps
137+
type externableLibrary interface {
138+
isExternal() bool
139+
flag.Provider
146140
}
147141

148-
func isExternal(e externable) bool {
149-
props := e.getExternalableProps()
142+
func (m *ModuleLibrary) isExternal() bool {
143+
props := &m.Properties.ExternalableProps
150144
if props.External != nil {
151145
return *props.External
152146
}
@@ -625,7 +619,7 @@ func checkLibraryFieldsMutator(ctx blueprint.BottomUpMutatorContext) {
625619
b.checkField(props.Forwarding_shlib == nil, "forwarding_shlib")
626620
} else if sl, ok := m.(*ModuleSharedLibrary); ok {
627621
props := sl.Properties
628-
if !isExternal(sl) {
622+
if !sl.isExternal() {
629623
sl.checkField(len(props.Export_ldflags) == 0, "export_ldflags")
630624
}
631625
sl.checkField(props.Mte.Memtag_heap == nil, "memtag_heap")
@@ -750,7 +744,7 @@ func exportLibFlagsMutator(ctx blueprint.TopDownMutatorContext) {
750744
}
751745

752746
if depLib, ok := dep.(*ModuleStaticLibrary); ok {
753-
if !isExternal(depLib) {
747+
if !depLib.isExternal() {
754748
// TODO: whole static libs should use a tag with relevant information.
755749
for _, subLib := range depLib.Properties.Whole_static_libs {
756750
if firstContainingLib, ok := insideWholeLibs[subLib]; ok {

core/library_shared.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (m *ModuleSharedLibrary) GetBackendConfiguration(ctx blueprint.ModuleContex
9696
}
9797

9898
func (m *ModuleSharedLibrary) GenerateBuildActions(ctx blueprint.ModuleContext) {
99-
if isEnabled(m) && !isExternal(m) {
99+
if isEnabled(m) && !m.isExternal() {
100100
getGenerator(ctx).sharedActions(m, ctx)
101101
}
102102
}

core/library_static.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var _ BackendConfiguration = (*ModuleStaticLibrary)(nil) // impl check
1414
var _ BackendConfigurationProvider = (*ModuleStaticLibrary)(nil) // impl check
1515

1616
func (m *ModuleStaticLibrary) GenerateBuildActions(ctx blueprint.ModuleContext) {
17-
if isEnabled(m) && !isExternal(m) {
17+
if isEnabled(m) && !m.isExternal() {
1818
getGenerator(ctx).staticActions(m, ctx)
1919
}
2020
}
@@ -28,7 +28,7 @@ func (m ModuleStaticLibrary) GetProperties() interface{} {
2828
}
2929

3030
func (m *ModuleStaticLibrary) OutFiles() (srcs file.Paths) {
31-
if !isExternal(m) {
31+
if !m.isExternal() {
3232
fp := file.NewPath(m.outputFileName(), string(m.getTarget()), file.TypeArchive|file.TypeInstallable)
3333
srcs = srcs.AppendIfUnique(fp)
3434
}

core/linux_cclibs.go

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,12 @@ func (g *linuxGenerator) getSharedLibLinkPaths(ctx blueprint.ModuleContext) (lib
319319
ctx.VisitDirectDepsIf(
320320
func(m blueprint.Module) bool { return ctx.OtherModuleDependencyTag(m) == tag.SharedTag },
321321
func(m blueprint.Module) {
322-
if t, ok := m.(targetableModule); ok {
323-
if e, ok := m.(externable); !ok || !isExternal(e) {
324-
libs = append(libs, g.getSharedLibLinkPath(t))
325-
}
326-
} else if _, ok := m.(*ModuleExternalLibrary); ok {
322+
if e, ok := m.(externableLibrary); ok && e.isExternal() {
327323
// Don't try and guess the path to external libraries,
328324
// and as they are outside of the build we don't need to
329325
// add a dependency on them anyway.
326+
} else if t, ok := m.(targetableModule); ok {
327+
libs = append(libs, g.getSharedLibLinkPath(t))
330328
} else {
331329
utils.Die("%s doesn't support targets", ctx.OtherModuleName(m))
332330
}
@@ -338,19 +336,17 @@ func (g *linuxGenerator) getSharedLibTocPaths(ctx blueprint.ModuleContext) (libs
338336
ctx.VisitDirectDepsIf(
339337
func(m blueprint.Module) bool { return ctx.OtherModuleDependencyTag(m) == tag.SharedTag },
340338
func(m blueprint.Module) {
341-
if _, ok := m.(sharedLibProducer); ok { //Remove this check and replace it with an API call
342-
if m, ok := m.(file.Provider); ok {
343-
if e, ok := m.(externable); !ok || !isExternal(e) {
344-
if toc, ok := m.OutFiles().FindSingle(
345-
func(p file.Path) bool { return p.IsType(file.TypeToc) }); ok {
346-
libs = append(libs, toc.BuildPath())
347-
}
348-
}
349-
}
350-
} else if _, ok := m.(*ModuleExternalLibrary); ok {
339+
if e, ok := m.(externableLibrary); ok && e.isExternal() {
351340
// Don't try and guess the path to external libraries,
352341
// and as they are outside of the build we don't need to
353342
// add a dependency on them anyway.
343+
} else if _, ok := m.(sharedLibProducer); ok { //Remove this check and replace it with an API call
344+
if m, ok := m.(file.Provider); ok {
345+
if toc, ok := m.OutFiles().FindSingle(
346+
func(p file.Path) bool { return p.IsType(file.TypeToc) }); ok {
347+
libs = append(libs, toc.BuildPath())
348+
}
349+
}
354350
} else {
355351
utils.Die("%s doesn't produce a shared library", ctx.OtherModuleName(m))
356352
}
@@ -370,48 +366,39 @@ func (g *linuxGenerator) getSharedLibFlags(m BackendCommonLibraryInterface, ctx
370366
func(m blueprint.Module) bool { return ctx.OtherModuleDependencyTag(m) == tag.SharedTag },
371367
func(m blueprint.Module) {
372368

373-
if sl, ok := m.(*ModuleSharedLibrary); ok {
374-
if isExternal(m.(*ModuleSharedLibrary)) {
375-
ldlibs = append(ldlibs, sl.FlagsOut().Filtered(func(f flag.Flag) bool {
376-
return f.MatchesType(flag.TypeLinkLibrary)
377-
}).ToStringSlice()...)
378-
379-
ldflags = append(ldflags, sl.FlagsOut().Filtered(func(f flag.Flag) bool {
380-
return f.MatchesType(flag.TypeLinker)
381-
}).ToStringSlice()...)
382-
} else {
383-
b := &sl.ModuleLibrary.Properties.Build
384-
if b.isForwardingSharedLibrary() {
385-
hasForwardingLib = true
386-
ldlibs = append(ldlibs, tc.GetLinker().KeepSharedLibraryTransitivity())
387-
if useNoAsNeeded {
388-
ldlibs = append(ldlibs, tc.GetLinker().KeepUnusedDependencies())
389-
}
390-
}
391-
ldlibs = append(ldlibs, pathToLibFlag(sl.outputName()))
392-
if b.isForwardingSharedLibrary() {
393-
if useNoAsNeeded {
394-
ldlibs = append(ldlibs, tc.GetLinker().DropUnusedDependencies())
395-
}
396-
ldlibs = append(ldlibs, tc.GetLinker().DropSharedLibraryTransitivity())
369+
if ex, ok := m.(externableLibrary); ok && ex.isExternal() {
370+
ldlibs = append(ldlibs, ex.FlagsOut().Filtered(func(f flag.Flag) bool {
371+
return f.MatchesType(flag.TypeLinkLibrary)
372+
}).ToStringSlice()...)
373+
374+
ldflags = append(ldflags, ex.FlagsOut().Filtered(func(f flag.Flag) bool {
375+
return f.MatchesType(flag.TypeLinker)
376+
}).ToStringSlice()...)
377+
} else if sl, ok := m.(*ModuleSharedLibrary); ok {
378+
b := &sl.ModuleLibrary.Properties.Build
379+
if b.isForwardingSharedLibrary() {
380+
hasForwardingLib = true
381+
ldlibs = append(ldlibs, tc.GetLinker().KeepSharedLibraryTransitivity())
382+
if useNoAsNeeded {
383+
ldlibs = append(ldlibs, tc.GetLinker().KeepUnusedDependencies())
397384
}
398-
if installPath, ok := sl.Properties.InstallableProps.getInstallPath(); ok {
399-
libPaths = utils.AppendIfUnique(libPaths, installPath)
385+
}
386+
ldlibs = append(ldlibs, pathToLibFlag(sl.outputName()))
387+
if b.isForwardingSharedLibrary() {
388+
if useNoAsNeeded {
389+
ldlibs = append(ldlibs, tc.GetLinker().DropUnusedDependencies())
400390
}
391+
ldlibs = append(ldlibs, tc.GetLinker().DropSharedLibraryTransitivity())
401392
}
393+
if installPath, ok := sl.Properties.InstallableProps.getInstallPath(); ok {
394+
libPaths = utils.AppendIfUnique(libPaths, installPath)
395+
}
396+
402397
} else if sl, ok := m.(*generateSharedLibrary); ok {
403398
ldlibs = append(ldlibs, pathToLibFlag(sl.outputName()))
404399
if installPath, ok := sl.ModuleGenerateCommon.Properties.InstallableProps.getInstallPath(); ok {
405400
libPaths = utils.AppendIfUnique(libPaths, installPath)
406401
}
407-
} else if el, ok := m.(*ModuleExternalLibrary); ok {
408-
ldlibs = append(ldlibs, el.FlagsOut().Filtered(func(f flag.Flag) bool {
409-
return f.MatchesType(flag.TypeLinkLibrary)
410-
}).ToStringSlice()...)
411-
412-
ldflags = append(ldflags, el.FlagsOut().Filtered(func(f flag.Flag) bool {
413-
return f.MatchesType(flag.TypeLinker)
414-
}).ToStringSlice()...)
415402
} else if sl, ok := m.(*ModuleStrictLibrary); ok {
416403
ldlibs = append(ldlibs, pathToLibFlag(sl.Name()+".so"))
417404
} else {

0 commit comments

Comments
 (0)