blob: 5911be017ee51ed295f38e2ff261b29af070bcaf [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.
LaMont Jones0c10e4d2023-05-16 00:58:37 +000026 android.RegisterParallelSingletonType("stublibraries", stubLibrariesSingleton)
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090027}
28
Cole Faustb43793e2024-12-13 16:53:36 -080029func stubLibrariesSingleton() android.Singleton {
30 return &stubLibraries{}
31}
32
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090033type stubLibraries struct {
Cole Faustb43793e2024-12-13 16:53:36 -080034 stubLibraries []string
35 vendorStubLibraries []string
Colin Crossceaa5322021-09-28 16:37:50 -070036
37 apiListCoverageXmlPaths []string
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090038}
39
40// Check if the module defines stub, or itself is stub
Jiyong Parkfa616132021-04-20 11:36:40 +090041func IsStubTarget(m *Module) bool {
Colin Cross203b4212021-04-26 17:19:41 -070042 return m.IsStubs() || m.HasStubsVariants()
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090043}
44
45// Get target file name to be installed from this module
Yu Liud46e5ae2024-08-15 18:46:17 +000046func getInstalledFileName(ctx android.SingletonContext, m *Module) string {
47 for _, ps := range android.OtherModuleProviderOrDefault(
48 ctx, m.Module(), android.InstallFilesProvider).PackagingSpecs {
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090049 if name := ps.FileName(); name != "" {
50 return name
51 }
52 }
53 return ""
54}
55
56func (s *stubLibraries) GenerateBuildActions(ctx android.SingletonContext) {
57 // Visit all generated soong modules and store stub library file names.
Cole Faustb43793e2024-12-13 16:53:36 -080058 stubLibraryMap := make(map[string]bool)
59 vendorStubLibraryMap := make(map[string]bool)
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090060 ctx.VisitAllModules(func(module android.Module) {
61 if m, ok := module.(*Module); ok {
Jiyong Parkfa616132021-04-20 11:36:40 +090062 if IsStubTarget(m) {
Yu Liud46e5ae2024-08-15 18:46:17 +000063 if name := getInstalledFileName(ctx, m); name != "" {
Cole Faustb43793e2024-12-13 16:53:36 -080064 stubLibraryMap[name] = true
Jooyung Han95821592023-12-01 16:33:30 +090065 if m.InVendor() {
Cole Faustb43793e2024-12-13 16:53:36 -080066 vendorStubLibraryMap[name] = true
Jooyung Han95821592023-12-01 16:33:30 +090067 }
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090068 }
69 }
Spandan Das24b3e532024-08-15 21:33:17 +000070 if m.library != nil && android.IsModulePreferred(m) {
Colin Crossceaa5322021-09-28 16:37:50 -070071 if p := m.library.getAPIListCoverageXMLPath().String(); p != "" {
72 s.apiListCoverageXmlPaths = append(s.apiListCoverageXmlPaths, p)
73 }
74 }
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090075 }
76 })
Cole Faustb43793e2024-12-13 16:53:36 -080077 s.stubLibraries = android.SortedKeys(stubLibraryMap)
78 s.vendorStubLibraries = android.SortedKeys(vendorStubLibraryMap)
79
80 android.WriteFileRule(ctx, StubLibrariesFile(ctx), strings.Join(s.stubLibraries, " "))
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090081}
82
Cole Faustb43793e2024-12-13 16:53:36 -080083func StubLibrariesFile(ctx android.PathContext) android.WritablePath {
84 return android.PathForIntermediates(ctx, "stub_libraries.txt")
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090085}
86
87func (s *stubLibraries) MakeVars(ctx android.MakeVarsContext) {
88 // Convert stub library file names into Makefile variable.
Cole Faustb43793e2024-12-13 16:53:36 -080089 ctx.Strict("STUB_LIBRARIES", strings.Join(s.stubLibraries, " "))
90 ctx.Strict("SOONG_STUB_VENDOR_LIBRARIES", strings.Join(s.vendorStubLibraries, " "))
Colin Crossceaa5322021-09-28 16:37:50 -070091
92 // Export the list of API XML files to Make.
93 sort.Strings(s.apiListCoverageXmlPaths)
94 ctx.Strict("SOONG_CC_API_XML", strings.Join(s.apiListCoverageXmlPaths, " "))
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090095}