| Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 1 | // Copyright 2020 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 |  | 
|  | 15 | package cc | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "strings" | 
|  | 19 |  | 
|  | 20 | "android/soong/android" | 
|  | 21 | ) | 
|  | 22 |  | 
|  | 23 | func init() { | 
|  | 24 | // Use singleton type to gather all generated soong modules. | 
|  | 25 | android.RegisterSingletonType("stublibraries", stubLibrariesSingleton) | 
|  | 26 | } | 
|  | 27 |  | 
|  | 28 | type stubLibraries struct { | 
|  | 29 | stubLibraryMap map[string]bool | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | // Check if the module defines stub, or itself is stub | 
|  | 33 | func isStubTarget(m *Module) bool { | 
|  | 34 | if m.IsStubs() || m.HasStubsVariants() { | 
|  | 35 | return true | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | // Library which defines LLNDK Stub is also Stub target. | 
|  | 39 | // Pure LLNDK Stub target would not contain any packaging | 
|  | 40 | // with target file path. | 
|  | 41 | if library, ok := m.linker.(*libraryDecorator); ok { | 
|  | 42 | if library.Properties.Llndk_stubs != nil { | 
|  | 43 | return true | 
|  | 44 | } | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | return false | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | // Get target file name to be installed from this module | 
|  | 51 | func getInstalledFileName(m *Module) string { | 
|  | 52 | for _, ps := range m.PackagingSpecs() { | 
|  | 53 | if name := ps.FileName(); name != "" { | 
|  | 54 | return name | 
|  | 55 | } | 
|  | 56 | } | 
|  | 57 | return "" | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | func (s *stubLibraries) GenerateBuildActions(ctx android.SingletonContext) { | 
|  | 61 | // Visit all generated soong modules and store stub library file names. | 
|  | 62 | ctx.VisitAllModules(func(module android.Module) { | 
|  | 63 | if m, ok := module.(*Module); ok { | 
|  | 64 | if isStubTarget(m) { | 
|  | 65 | if name := getInstalledFileName(m); name != "" { | 
|  | 66 | s.stubLibraryMap[name] = true | 
|  | 67 | } | 
|  | 68 | } | 
|  | 69 | } | 
|  | 70 | }) | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | func stubLibrariesSingleton() android.Singleton { | 
|  | 74 | return &stubLibraries{ | 
|  | 75 | stubLibraryMap: make(map[string]bool), | 
|  | 76 | } | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | func (s *stubLibraries) MakeVars(ctx android.MakeVarsContext) { | 
|  | 80 | // Convert stub library file names into Makefile variable. | 
|  | 81 | ctx.Strict("STUB_LIBRARIES", strings.Join(android.SortedStringKeys(s.stubLibraryMap), " ")) | 
|  | 82 | } |