blob: 76da782eef5e936890be6fea1560c5c87f30b676 [file] [log] [blame]
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +09001// 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
15package cc
16
17import (
Colin Crossceaa5322021-09-28 16:37:50 -070018 "sort"
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090019 "strings"
20
21 "android/soong/android"
22)
23
24func init() {
25 // Use singleton type to gather all generated soong modules.
26 android.RegisterSingletonType("stublibraries", stubLibrariesSingleton)
27}
28
29type stubLibraries struct {
30 stubLibraryMap map[string]bool
Colin Crossceaa5322021-09-28 16:37:50 -070031
32 apiListCoverageXmlPaths []string
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090033}
34
35// Check if the module defines stub, or itself is stub
Jiyong Parkfa616132021-04-20 11:36:40 +090036func IsStubTarget(m *Module) bool {
Colin Cross203b4212021-04-26 17:19:41 -070037 return m.IsStubs() || m.HasStubsVariants()
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090038}
39
40// Get target file name to be installed from this module
41func getInstalledFileName(m *Module) string {
42 for _, ps := range m.PackagingSpecs() {
43 if name := ps.FileName(); name != "" {
44 return name
45 }
46 }
47 return ""
48}
49
50func (s *stubLibraries) GenerateBuildActions(ctx android.SingletonContext) {
51 // Visit all generated soong modules and store stub library file names.
52 ctx.VisitAllModules(func(module android.Module) {
53 if m, ok := module.(*Module); ok {
Jiyong Parkfa616132021-04-20 11:36:40 +090054 if IsStubTarget(m) {
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090055 if name := getInstalledFileName(m); name != "" {
56 s.stubLibraryMap[name] = true
57 }
58 }
Colin Crossceaa5322021-09-28 16:37:50 -070059 if m.library != nil {
60 if p := m.library.getAPIListCoverageXMLPath().String(); p != "" {
61 s.apiListCoverageXmlPaths = append(s.apiListCoverageXmlPaths, p)
62 }
63 }
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090064 }
65 })
66}
67
68func stubLibrariesSingleton() android.Singleton {
69 return &stubLibraries{
70 stubLibraryMap: make(map[string]bool),
71 }
72}
73
74func (s *stubLibraries) MakeVars(ctx android.MakeVarsContext) {
75 // Convert stub library file names into Makefile variable.
76 ctx.Strict("STUB_LIBRARIES", strings.Join(android.SortedStringKeys(s.stubLibraryMap), " "))
Colin Crossceaa5322021-09-28 16:37:50 -070077
78 // Export the list of API XML files to Make.
79 sort.Strings(s.apiListCoverageXmlPaths)
80 ctx.Strict("SOONG_CC_API_XML", strings.Join(s.apiListCoverageXmlPaths, " "))
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090081}