1// Copyright 2018 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	"sync"
20
21	"android/soong/android"
22)
23
24var (
25	vendorPublicLibrarySuffix = ".vendorpublic"
26
27	vendorPublicLibrariesKey  = android.NewOnceKey("vendorPublicLibraries")
28	vendorPublicLibrariesLock sync.Mutex
29)
30
31func vendorPublicLibraries(config android.Config) *[]string {
32	return config.Once(vendorPublicLibrariesKey, func() interface{} {
33		return &[]string{}
34	}).(*[]string)
35}
36
37// Creates a stub shared library for a vendor public library. Vendor public libraries
38// are vendor libraries (owned by them and installed to /vendor partition) that are
39// exposed to Android apps via JNI. The libraries are made public by being listed in
40// /vendor/etc/public.libraries.txt.
41//
42// This stub library is a build-time only artifact that provides symbols that are
43// exposed from a vendor public library.
44//
45// Example:
46//
47// vendor_public_library {
48//     name: "libfoo",
49//     symbol_file: "libfoo.map.txt",
50//     export_public_headers: ["libfoo_headers"],
51// }
52//
53// cc_headers {
54//     name: "libfoo_headers",
55//     export_include_dirs: ["include"],
56// }
57//
58type vendorPublicLibraryProperties struct {
59	// Relative path to the symbol map.
60	Symbol_file *string
61
62	// Whether the system library uses symbol versions.
63	Unversioned *bool
64
65	// list of header libs to re-export include directories from.
66	Export_public_headers []string `android:"arch_variant"`
67}
68
69type vendorPublicLibraryStubDecorator struct {
70	*libraryDecorator
71
72	Properties vendorPublicLibraryProperties
73
74	versionScriptPath android.ModuleGenPath
75}
76
77func (stub *vendorPublicLibraryStubDecorator) Name(name string) string {
78	return name + vendorPublicLibrarySuffix
79}
80
81func (stub *vendorPublicLibraryStubDecorator) compilerInit(ctx BaseModuleContext) {
82	stub.baseCompiler.compilerInit(ctx)
83
84	name := ctx.baseModuleName()
85	if strings.HasSuffix(name, vendorPublicLibrarySuffix) {
86		ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", vendorPublicLibrarySuffix)
87	}
88
89	vendorPublicLibrariesLock.Lock()
90	defer vendorPublicLibrariesLock.Unlock()
91	vendorPublicLibraries := vendorPublicLibraries(ctx.Config())
92	for _, lib := range *vendorPublicLibraries {
93		if lib == name {
94			return
95		}
96	}
97	*vendorPublicLibraries = append(*vendorPublicLibraries, name)
98}
99
100func (stub *vendorPublicLibraryStubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
101	flags = stub.baseCompiler.compilerFlags(ctx, flags, deps)
102	return addStubLibraryCompilerFlags(flags)
103}
104
105func (stub *vendorPublicLibraryStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
106	objs, versionScript := compileStubLibrary(ctx, flags, String(stub.Properties.Symbol_file), "current", "")
107	stub.versionScriptPath = versionScript
108	return objs
109}
110
111func (stub *vendorPublicLibraryStubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
112	headers := stub.Properties.Export_public_headers
113	deps.HeaderLibs = append(deps.HeaderLibs, headers...)
114	deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, headers...)
115	return deps
116}
117
118func (stub *vendorPublicLibraryStubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
119	stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), vendorPublicLibrarySuffix)
120	return stub.libraryDecorator.linkerFlags(ctx, flags)
121}
122
123func (stub *vendorPublicLibraryStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps,
124	objs Objects) android.Path {
125	if !Bool(stub.Properties.Unversioned) {
126		linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String()
127		flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlag)
128		flags.LdFlagsDeps = append(flags.LdFlagsDeps, stub.versionScriptPath)
129	}
130	return stub.libraryDecorator.link(ctx, flags, deps, objs)
131}
132
133// vendor_public_library creates a stub shared library for a vendor public
134// library. This stub library is a build-time only artifact that provides
135// symbols that are exposed from a vendor public library. Example:
136//
137//    vendor_public_library {
138//        name: "libfoo",
139//        symbol_file: "libfoo.map.txt",
140//        export_public_headers: ["libfoo_headers"],
141//    }
142func vendorPublicLibraryFactory() android.Module {
143	module, library := NewLibrary(android.DeviceSupported)
144	library.BuildOnlyShared()
145	module.stl = nil
146	module.sanitize = nil
147	library.StripProperties.Strip.None = BoolPtr(true)
148
149	stub := &vendorPublicLibraryStubDecorator{
150		libraryDecorator: library,
151	}
152	module.compiler = stub
153	module.linker = stub
154	module.installer = nil
155
156	module.AddProperties(
157		&stub.Properties,
158		&library.MutatedProperties,
159		&library.flagExporter.Properties)
160
161	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
162	return module
163}
164
165func init() {
166	android.RegisterModuleType("vendor_public_library", vendorPublicLibraryFactory)
167}
168