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
15package cc
16
17import (
18	"strings"
19
20	"android/soong/android"
21)
22
23func init() {
24	// Use singleton type to gather all generated soong modules.
25	android.RegisterSingletonType("stublibraries", stubLibrariesSingleton)
26}
27
28type stubLibraries struct {
29	stubLibraryMap map[string]bool
30}
31
32// Check if the module defines stub, or itself is stub
33func IsStubTarget(m *Module) bool {
34	return m.IsStubs() || m.HasStubsVariants()
35}
36
37// Get target file name to be installed from this module
38func getInstalledFileName(m *Module) string {
39	for _, ps := range m.PackagingSpecs() {
40		if name := ps.FileName(); name != "" {
41			return name
42		}
43	}
44	return ""
45}
46
47func (s *stubLibraries) GenerateBuildActions(ctx android.SingletonContext) {
48	// Visit all generated soong modules and store stub library file names.
49	ctx.VisitAllModules(func(module android.Module) {
50		if m, ok := module.(*Module); ok {
51			if IsStubTarget(m) {
52				if name := getInstalledFileName(m); name != "" {
53					s.stubLibraryMap[name] = true
54				}
55			}
56		}
57	})
58}
59
60func stubLibrariesSingleton() android.Singleton {
61	return &stubLibraries{
62		stubLibraryMap: make(map[string]bool),
63	}
64}
65
66func (s *stubLibraries) MakeVars(ctx android.MakeVarsContext) {
67	// Convert stub library file names into Makefile variable.
68	ctx.Strict("STUB_LIBRARIES", strings.Join(android.SortedStringKeys(s.stubLibraryMap), " "))
69}
70