blob: 4d0148df4e5551bf7ac34731ac800e9b8c53f6be [file] [log] [blame]
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001// Copyright 2021 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 "android/soong/android"
19 "android/soong/multitree"
20)
21
22func init() {
23 RegisterLibraryStubBuildComponents(android.InitRegistrationContext)
24}
25
26func RegisterLibraryStubBuildComponents(ctx android.RegistrationContext) {
27 // cc_api_stub_library shares a lot of ndk_library, and this will be refactored later
28 ctx.RegisterModuleType("cc_api_stub_library", CcApiStubLibraryFactory)
29 ctx.RegisterModuleType("cc_api_contribution", CcApiContributionFactory)
30}
31
32func CcApiStubLibraryFactory() android.Module {
33 module, decorator := NewLibrary(android.DeviceSupported)
34 apiStubDecorator := &apiStubDecorator{
35 libraryDecorator: decorator,
36 }
37 apiStubDecorator.BuildOnlyShared()
38
39 module.compiler = apiStubDecorator
40 module.linker = apiStubDecorator
41 module.installer = nil
42 module.library = apiStubDecorator
43 module.Properties.HideFromMake = true // TODO: remove
44
45 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
46 module.AddProperties(&module.Properties,
47 &apiStubDecorator.properties,
48 &apiStubDecorator.MutatedProperties,
49 &apiStubDecorator.apiStubLibraryProperties)
50 return module
51}
52
53type apiStubLiraryProperties struct {
54 Imported_includes []string `android:"path"`
55}
56
57type apiStubDecorator struct {
58 *libraryDecorator
59 properties libraryProperties
60 apiStubLibraryProperties apiStubLiraryProperties
61}
62
63func (compiler *apiStubDecorator) stubsVersions(ctx android.BaseMutatorContext) []string {
64 firstVersion := String(compiler.properties.First_version)
65 return ndkLibraryVersions(ctx, android.ApiLevelOrPanic(ctx, firstVersion))
66}
67
68func (decorator *apiStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
69 if decorator.stubsVersion() == "" {
70 decorator.setStubsVersion("current")
71 } // TODO: fix
72 symbolFile := String(decorator.properties.Symbol_file)
73 nativeAbiResult := parseNativeAbiDefinition(ctx, symbolFile,
74 android.ApiLevelOrPanic(ctx, decorator.stubsVersion()),
75 "")
76 return compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
77}
78
79func (decorator *apiStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objects Objects) android.Path {
80 decorator.reexportDirs(android.PathsForModuleSrc(ctx, decorator.apiStubLibraryProperties.Imported_includes)...)
81 return decorator.libraryDecorator.link(ctx, flags, deps, objects)
82}
83
84func init() {
85 pctx.HostBinToolVariable("gen_api_surface_build_files", "gen_api_surface_build_files")
86}
87
88type CcApiContribution struct {
89 android.ModuleBase
90 properties ccApiContributionProperties
91}
92
93type ccApiContributionProperties struct {
94 Symbol_file *string `android:"path"`
95 First_version *string
96 Export_include_dir *string
97}
98
99func CcApiContributionFactory() android.Module {
100 module := &CcApiContribution{}
101 module.AddProperties(&module.properties)
102 android.InitAndroidModule(module)
103 return module
104}
105
106// Do some simple validations
107// Majority of the build rules will be created in the ctx of the api surface this module contributes to
108func (contrib *CcApiContribution) GenerateAndroidBuildActions(ctx android.ModuleContext) {
109 if contrib.properties.Symbol_file == nil {
110 ctx.PropertyErrorf("symbol_file", "%v does not have symbol file", ctx.ModuleName())
111 }
112 if contrib.properties.First_version == nil {
113 ctx.PropertyErrorf("first_version", "%v does not have first_version for stub variants", ctx.ModuleName())
114 }
115}
116
117// Path is out/soong/.export/ but will be different in final multi-tree layout
118func outPathApiSurface(ctx android.ModuleContext, myModuleName string, pathComponent string) android.OutputPath {
119 return android.PathForOutput(ctx, ".export", ctx.ModuleName(), myModuleName, pathComponent)
120}
121
122func (contrib *CcApiContribution) CopyFilesWithTag(apiSurfaceContext android.ModuleContext) map[string]android.Paths {
123 // copy map.txt for now
124 // hardlinks cannot be created since nsjail creates a different mountpoint for out/
125 myDir := apiSurfaceContext.OtherModuleDir(contrib)
126 genMapTxt := outPathApiSurface(apiSurfaceContext, contrib.Name(), String(contrib.properties.Symbol_file))
127 apiSurfaceContext.Build(pctx, android.BuildParams{
128 Rule: android.Cp,
129 Description: "import map.txt file",
130 Input: android.PathForSource(apiSurfaceContext, myDir, String(contrib.properties.Symbol_file)),
131 Output: genMapTxt,
132 })
133
134 outputs := make(map[string]android.Paths)
135 outputs["map"] = []android.Path{genMapTxt}
136
137 if contrib.properties.Export_include_dir != nil {
138 includeDir := android.PathForSource(apiSurfaceContext, myDir, String(contrib.properties.Export_include_dir))
139 outputs["export_include_dir"] = []android.Path{includeDir}
140 }
141 return outputs
142}
143
144var _ multitree.ApiContribution = (*CcApiContribution)(nil)
145
146/*
147func (contrib *CcApiContribution) GenerateBuildFiles(apiSurfaceContext android.ModuleContext) android.Paths {
148 genAndroidBp := outPathApiSurface(apiSurfaceContext, contrib.Name(), "Android.bp")
149
150 // generate Android.bp
151 apiSurfaceContext.Build(pctx, android.BuildParams{
152 Rule: genApiSurfaceBuildFiles,
153 Description: "generate API surface build files",
154 Outputs: []android.WritablePath{genAndroidBp},
155 Args: map[string]string{
156 "name": contrib.Name() + "." + apiSurfaceContext.ModuleName(), //e.g. liblog.ndk
157 "symbol_file": String(contrib.properties.Symbol_file),
158 "first_version": String(contrib.properties.First_version),
159 },
160 })
161 return []android.Path{genAndroidBp}
162}
163*/