blob: 2291153a2b49ee956b0cd8c4e96428d2836e5dc6 [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
Yu Liu68a70b72025-01-08 22:54:44 +000041func IsStubTarget(info *LinkableInfo) bool {
42 return info != nil && (info.IsStubs || info.HasStubsVariants)
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090043}
44
45// Get target file name to be installed from this module
Ivan Lozano9eaacc82024-10-30 14:28:17 +000046func getInstalledFileName(ctx android.SingletonContext, m LinkableInterface) string {
Yu Liud46e5ae2024-08-15 18:46:17 +000047 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) {
Ivan Lozano9eaacc82024-10-30 14:28:17 +000061 if m, ok := module.(VersionedLinkableInterface); ok {
62 // TODO(ivanlozano) remove this when Rust supports stubs
63 if m.RustLibraryInterface() {
64 return
65 }
Yu Liu68a70b72025-01-08 22:54:44 +000066 if IsStubTarget(android.OtherModuleProviderOrDefault(ctx, m, LinkableInfoProvider)) {
Yu Liud46e5ae2024-08-15 18:46:17 +000067 if name := getInstalledFileName(ctx, m); name != "" {
Cole Faustb43793e2024-12-13 16:53:36 -080068 stubLibraryMap[name] = true
Jooyung Han95821592023-12-01 16:33:30 +090069 if m.InVendor() {
Cole Faustb43793e2024-12-13 16:53:36 -080070 vendorStubLibraryMap[name] = true
Jooyung Han95821592023-12-01 16:33:30 +090071 }
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090072 }
73 }
Ivan Lozano9eaacc82024-10-30 14:28:17 +000074 if m.CcLibraryInterface() && android.IsModulePreferred(m) {
75 if p := m.VersionedInterface().GetAPIListCoverageXMLPath().String(); p != "" {
Colin Crossceaa5322021-09-28 16:37:50 -070076 s.apiListCoverageXmlPaths = append(s.apiListCoverageXmlPaths, p)
77 }
78 }
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090079 }
80 })
Cole Faustb43793e2024-12-13 16:53:36 -080081 s.stubLibraries = android.SortedKeys(stubLibraryMap)
82 s.vendorStubLibraries = android.SortedKeys(vendorStubLibraryMap)
83
84 android.WriteFileRule(ctx, StubLibrariesFile(ctx), strings.Join(s.stubLibraries, " "))
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090085}
86
Cole Faustb43793e2024-12-13 16:53:36 -080087func StubLibrariesFile(ctx android.PathContext) android.WritablePath {
88 return android.PathForIntermediates(ctx, "stub_libraries.txt")
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090089}
90
91func (s *stubLibraries) MakeVars(ctx android.MakeVarsContext) {
92 // Convert stub library file names into Makefile variable.
Cole Faustb43793e2024-12-13 16:53:36 -080093 ctx.Strict("STUB_LIBRARIES", strings.Join(s.stubLibraries, " "))
94 ctx.Strict("SOONG_STUB_VENDOR_LIBRARIES", strings.Join(s.vendorStubLibraries, " "))
Colin Crossceaa5322021-09-28 16:37:50 -070095
96 // Export the list of API XML files to Make.
97 sort.Strings(s.apiListCoverageXmlPaths)
98 ctx.Strict("SOONG_CC_API_XML", strings.Join(s.apiListCoverageXmlPaths, " "))
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090099}