blob: f571523a0311f1aff5863ca77d9eec17018515c0 [file] [log] [blame]
Dan Albert914449f2016-06-17 16:45:24 -07001// Copyright 2016 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
17// The platform needs to provide the following artifacts for the NDK:
18// 1. Bionic headers.
19// 2. Platform API headers.
20// 3. NDK stub shared libraries.
21// 4. Bionic static libraries.
22//
23// TODO(danalbert): All of the above need to include NOTICE files.
24//
25// Components 1 and 2: Headers
26// The bionic and platform API headers are generalized into a single
27// `ndk_headers` rule. This rule has a `from` property that indicates a base
28// directory from which headers are to be taken, and a `to` property that
29// indicates where in the sysroot they should reside relative to usr/include.
30// There is also a `srcs` property that is glob compatible for specifying which
31// headers to include.
32//
33// Component 3: Stub Libraries
34// The shared libraries in the NDK are not the actual shared libraries they
35// refer to (to prevent people from accidentally loading them), but stub
Joe Onoratob4638c12021-10-27 15:47:06 -070036// libraries with placeholder implementations of everything for use at build time
Dan Albert914449f2016-06-17 16:45:24 -070037// only.
38//
39// Since we don't actually need to know anything about the stub libraries aside
40// from a list of functions and globals to be exposed, we can create these for
41// every platform level in the current tree. This is handled by the
42// ndk_library rule.
43//
44// Component 4: Static Libraries
45// The NDK only provides static libraries for bionic, not the platform APIs.
46// Since these need to be the actual implementation, we can't build old versions
47// in the current platform tree. As such, legacy versions are checked in
48// prebuilt to development/ndk, and a current version is built and archived as
49// part of the platform build. The platfrom already builds these libraries, our
50// NDK build rules only need to archive them for retrieval so they can be added
51// to the prebuilts.
52//
53// TODO(danalbert): Write `ndk_static_library` rule.
54
55import (
Dan Albert914449f2016-06-17 16:45:24 -070056 "android/soong/android"
Dan Albert266f9912024-08-13 22:01:23 +000057 "fmt"
58 "path/filepath"
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +000059 "strings"
Dan Albert266f9912024-08-13 22:01:23 +000060
61 "github.com/google/blueprint"
62)
63
64var (
65 verifyCCompat = pctx.AndroidStaticRule("verifyCCompat",
66 blueprint.RuleParams{
67 Command: "$ccCmd -x c -fsyntax-only $flags $in && touch $out",
68 CommandDeps: []string{"$ccCmd"},
69 },
70 "ccCmd",
71 "flags",
72 )
Dan Albert914449f2016-06-17 16:45:24 -070073)
74
75func init() {
Spandan Das0773a602022-08-16 00:55:11 +000076 RegisterNdkModuleTypes(android.InitRegistrationContext)
Dan Albert914449f2016-06-17 16:45:24 -070077}
78
Spandan Das0773a602022-08-16 00:55:11 +000079func RegisterNdkModuleTypes(ctx android.RegistrationContext) {
Spandan Das319711b2023-09-19 19:04:41 +000080 ctx.RegisterModuleType("ndk_headers", NdkHeadersFactory)
Spandan Das0773a602022-08-16 00:55:11 +000081 ctx.RegisterModuleType("ndk_library", NdkLibraryFactory)
Spandan Dasa7da3f02023-09-28 19:30:51 +000082 ctx.RegisterModuleType("versioned_ndk_headers", VersionedNdkHeadersFactory)
Spandan Das0773a602022-08-16 00:55:11 +000083 ctx.RegisterModuleType("preprocessed_ndk_headers", preprocessedNdkHeadersFactory)
LaMont Jones0c10e4d2023-05-16 00:58:37 +000084 ctx.RegisterParallelSingletonType("ndk", NdkSingleton)
Spandan Das0773a602022-08-16 00:55:11 +000085}
86
Spandan Dasf280b232024-04-04 21:25:51 +000087func getNdkInstallBase(ctx android.PathContext) android.OutputPath {
Colin Cross70dda7e2019-10-01 22:05:35 -070088 return android.PathForNdkInstall(ctx)
Dan Albert914449f2016-06-17 16:45:24 -070089}
90
91// Returns the main install directory for the NDK sysroot. Usable with --sysroot.
Spandan Dasf280b232024-04-04 21:25:51 +000092func getNdkSysrootBase(ctx android.PathContext) android.OutputPath {
Dan Albert914449f2016-06-17 16:45:24 -070093 return getNdkInstallBase(ctx).Join(ctx, "sysroot")
94}
95
Dan Albert6ab43d82017-12-13 15:05:04 -080096// The base timestamp file depends on the NDK headers and stub shared libraries,
97// but not the static libraries. This distinction is needed because the static
98// libraries themselves might need to depend on the base sysroot.
99func getNdkBaseTimestampFile(ctx android.PathContext) android.WritablePath {
100 return android.PathForOutput(ctx, "ndk_base.timestamp")
101}
102
Chih-Hung Hsieh5b721532021-11-30 17:31:23 -0800103// The headers timestamp file depends only on the NDK headers.
104// This is used mainly for .tidy files that do not need any stub libraries.
105func getNdkHeadersTimestampFile(ctx android.PathContext) android.WritablePath {
106 return android.PathForOutput(ctx, "ndk_headers.timestamp")
107}
108
Dan Albert6ab43d82017-12-13 15:05:04 -0800109// The full timestamp file depends on the base timestamp *and* the static
110// libraries.
111func getNdkFullTimestampFile(ctx android.PathContext) android.WritablePath {
Dan Albert914449f2016-06-17 16:45:24 -0700112 return android.PathForOutput(ctx, "ndk.timestamp")
113}
114
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000115// The list of all NDK headers as they are located in the repo.
116// Used for ABI monitoring to track only structures defined in NDK headers.
117func getNdkABIHeadersFile(ctx android.PathContext) android.WritablePath {
118 return android.PathForOutput(ctx, "ndk_abi_headers.txt")
119}
120
Dan Albert266f9912024-08-13 22:01:23 +0000121func verifyNdkHeaderIsCCompatible(ctx android.SingletonContext,
122 src android.Path, dest android.Path) android.Path {
123 sysrootInclude := getCurrentIncludePath(ctx)
124 baseOutputDir := android.PathForOutput(ctx, "c-compat-verification")
125 installRelPath, err := filepath.Rel(sysrootInclude.String(), dest.String())
126 if err != nil {
127 ctx.Errorf("filepath.Rel(%q, %q) failed: %s", dest, sysrootInclude, err)
128 }
129 output := baseOutputDir.Join(ctx, installRelPath)
130 ctx.Build(pctx, android.BuildParams{
131 Rule: verifyCCompat,
132 Description: fmt.Sprintf("Verifying C compatibility of %s", src),
133 Output: output,
134 Input: dest,
135 // Ensures that all the headers in the sysroot are already installed
136 // before testing any of the headers for C compatibility, and also that
137 // the check will be re-run whenever the sysroot changes. This is
138 // necessary because many of the NDK headers depend on other NDK
139 // headers, but we don't have explicit dependency tracking for that.
140 Implicits: []android.Path{getNdkHeadersTimestampFile(ctx)},
141 Args: map[string]string{
142 "ccCmd": "${config.ClangBin}/clang",
143 "flags": fmt.Sprintf(
144 // Ideally we'd check each ABI, multiple API levels,
145 // fortify/non-fortify, and a handful of other variations. It's
146 // a lot more difficult to do that though, and would eat up more
147 // build time. All the problems we've seen so far that this
148 // check would catch have been in arch-generic and
149 // minSdkVersion-generic code in frameworks though, so this is a
150 // good place to start.
151 "-target aarch64-linux-android%d --sysroot %s",
152 android.FutureApiLevel.FinalOrFutureInt(),
153 getNdkSysrootBase(ctx).String(),
154 ),
155 },
156 })
157 return output
158}
159
Colin Cross0875c522017-11-28 17:34:01 -0800160func NdkSingleton() android.Singleton {
Dan Albert914449f2016-06-17 16:45:24 -0700161 return &ndkSingleton{}
162}
163
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000164// Collect all NDK exported headers paths into a file that is used to
165// detect public types that should be ABI monitored.
166//
167// Assume that we have the following code in exported header:
168//
169// typedef struct Context Context;
170// typedef struct Output {
171// ...
172// } Output;
173// void DoSomething(Context* ctx, Output* output);
174//
175// If none of public headers exported to end-users contain definition of
176// "struct Context", then "struct Context" layout and members shouldn't be
177// monitored. However we use DWARF information from a real library, which
178// may have access to the definition of "string Context" from
179// implementation headers, and it will leak to ABI.
180//
181// STG tool doesn't access source and header files, only DWARF information
182// from compiled library. And the DWARF contains file name where a type is
183// defined. So we need a rule to build a list of paths to public headers,
184// so STG can distinguish private types from public and do not monitor
185// private types that are not accessible to library users.
186func writeNdkAbiSrcFilter(ctx android.BuilderContext,
187 headerSrcPaths android.Paths, outputFile android.WritablePath) {
188 var filterBuilder strings.Builder
189 filterBuilder.WriteString("[decl_file_allowlist]\n")
190 for _, headerSrcPath := range headerSrcPaths {
191 filterBuilder.WriteString(headerSrcPath.String())
192 filterBuilder.WriteString("\n")
193 }
194
195 android.WriteFileRule(ctx, outputFile, filterBuilder.String())
196}
197
Dan Albert914449f2016-06-17 16:45:24 -0700198type ndkSingleton struct{}
199
Dan Albert266f9912024-08-13 22:01:23 +0000200type srcDestPair struct {
201 src android.Path
202 dest android.Path
203}
204
Colin Cross0875c522017-11-28 17:34:01 -0800205func (n *ndkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Dan Albert6ab43d82017-12-13 15:05:04 -0800206 var staticLibInstallPaths android.Paths
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000207 var headerSrcPaths android.Paths
208 var headerInstallPaths android.Paths
Dan Albert266f9912024-08-13 22:01:23 +0000209 var headersToVerify []srcDestPair
210 var headerCCompatVerificationTimestampPaths android.Paths
Colin Cross0875c522017-11-28 17:34:01 -0800211 var installPaths android.Paths
212 var licensePaths android.Paths
213 ctx.VisitAllModules(func(module android.Module) {
Cole Fausta963b942024-04-11 17:43:00 -0700214 if m, ok := module.(android.Module); ok && !m.Enabled(ctx) {
Dan Willemsen95f4dbb2017-05-05 23:26:01 -0700215 return
216 }
217
Dan Albert914449f2016-06-17 16:45:24 -0700218 if m, ok := module.(*headerModule); ok {
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000219 headerSrcPaths = append(headerSrcPaths, m.srcPaths...)
220 headerInstallPaths = append(headerInstallPaths, m.installPaths...)
Dan Albert266f9912024-08-13 22:01:23 +0000221 if !Bool(m.properties.Skip_verification) {
222 for i, installPath := range m.installPaths {
223 headersToVerify = append(headersToVerify, srcDestPair{
224 src: m.srcPaths[i],
225 dest: installPath,
226 })
227 }
228 }
Dan Albert914449f2016-06-17 16:45:24 -0700229 installPaths = append(installPaths, m.installPaths...)
Colin Cross0875c522017-11-28 17:34:01 -0800230 licensePaths = append(licensePaths, m.licensePath)
Dan Albert914449f2016-06-17 16:45:24 -0700231 }
Dan Albert914449f2016-06-17 16:45:24 -0700232
Dan Albert97f9c962018-05-24 15:02:16 -0700233 if m, ok := module.(*versionedHeaderModule); ok {
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000234 headerSrcPaths = append(headerSrcPaths, m.srcPaths...)
235 headerInstallPaths = append(headerInstallPaths, m.installPaths...)
Dan Albert266f9912024-08-13 22:01:23 +0000236 // Verification intentionally not done for headers that go through
237 // versioner. It'd be nice to have, but the only user is bionic, and
238 // that one module would also need to use skip_verification, so it
239 // wouldn't help at all.
Dan Albert269fab82017-02-15 17:31:33 -0800240 installPaths = append(installPaths, m.installPaths...)
Colin Cross0875c522017-11-28 17:34:01 -0800241 licensePaths = append(licensePaths, m.licensePath)
Dan Albert269fab82017-02-15 17:31:33 -0800242 }
243
Dan Albertcb1b4b22018-05-24 15:06:11 -0700244 if m, ok := module.(*preprocessedHeadersModule); ok {
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000245 headerSrcPaths = append(headerSrcPaths, m.srcPaths...)
246 headerInstallPaths = append(headerInstallPaths, m.installPaths...)
Dan Albert266f9912024-08-13 22:01:23 +0000247 if !Bool(m.properties.Skip_verification) {
248 for i, installPath := range m.installPaths {
249 headersToVerify = append(headersToVerify, srcDestPair{
250 src: m.srcPaths[i],
251 dest: installPath,
252 })
253 }
254 }
Dan Albertcb1b4b22018-05-24 15:06:11 -0700255 installPaths = append(installPaths, m.installPaths...)
256 licensePaths = append(licensePaths, m.licensePath)
257 }
258
Dan Albert914449f2016-06-17 16:45:24 -0700259 if m, ok := module.(*Module); ok {
Colin Cross31076b32020-10-23 17:22:06 -0700260 if installer, ok := m.installer.(*stubDecorator); ok && m.library.buildStubs() {
Dan Albert914449f2016-06-17 16:45:24 -0700261 installPaths = append(installPaths, installer.installPath)
262 }
Dan Albertf563d252017-10-13 00:29:00 -0700263
264 if library, ok := m.linker.(*libraryDecorator); ok {
Colin Cross0875c522017-11-28 17:34:01 -0800265 if library.ndkSysrootPath != nil {
Dan Albert6ab43d82017-12-13 15:05:04 -0800266 staticLibInstallPaths = append(
267 staticLibInstallPaths, library.ndkSysrootPath)
Dan Albertf563d252017-10-13 00:29:00 -0700268 }
269 }
Dan Albert5b0d4f32023-04-04 23:22:11 +0000270
271 if object, ok := m.linker.(*objectLinker); ok {
272 if object.ndkSysrootPath != nil {
273 staticLibInstallPaths = append(
274 staticLibInstallPaths, object.ndkSysrootPath)
275 }
276 }
Dan Albert914449f2016-06-17 16:45:24 -0700277 }
278 })
279
Ryan Prichardb1c9d402018-08-20 22:06:01 -0700280 // Include only a single copy of each license file. The Bionic NOTICE is
281 // long and is referenced by multiple Bionic modules.
282 licensePaths = android.FirstUniquePaths(licensePaths)
283
Dan Albertc6345fb2016-10-20 01:36:11 -0700284 combinedLicense := getNdkInstallBase(ctx).Join(ctx, "NOTICE")
Colin Cross0875c522017-11-28 17:34:01 -0800285 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700286 Rule: android.Cat,
287 Description: "combine licenses",
Colin Cross0875c522017-11-28 17:34:01 -0800288 Output: combinedLicense,
Colin Cross67a5c132017-05-09 13:45:28 -0700289 Inputs: licensePaths,
Dan Albertc6345fb2016-10-20 01:36:11 -0700290 })
291
Dan Albert49227032021-06-15 13:25:25 -0700292 baseDepPaths := append(installPaths, combinedLicense)
Dan Albertc6345fb2016-10-20 01:36:11 -0700293
Colin Cross0875c522017-11-28 17:34:01 -0800294 ctx.Build(pctx, android.BuildParams{
Dan Albert49227032021-06-15 13:25:25 -0700295 Rule: android.Touch,
296 Output: getNdkBaseTimestampFile(ctx),
297 Implicits: baseDepPaths,
298 Validation: getNdkAbiDiffTimestampFile(ctx),
Dan Albert6ab43d82017-12-13 15:05:04 -0800299 })
300
Chih-Hung Hsieh5b721532021-11-30 17:31:23 -0800301 ctx.Build(pctx, android.BuildParams{
302 Rule: android.Touch,
303 Output: getNdkHeadersTimestampFile(ctx),
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000304 Implicits: headerInstallPaths,
Chih-Hung Hsieh5b721532021-11-30 17:31:23 -0800305 })
306
Dan Albert266f9912024-08-13 22:01:23 +0000307 for _, srcDestPair := range headersToVerify {
308 headerCCompatVerificationTimestampPaths = append(
309 headerCCompatVerificationTimestampPaths,
310 verifyNdkHeaderIsCCompatible(ctx, srcDestPair.src, srcDestPair.dest))
311 }
312
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000313 writeNdkAbiSrcFilter(ctx, headerSrcPaths, getNdkABIHeadersFile(ctx))
314
Dan Albert6ab43d82017-12-13 15:05:04 -0800315 fullDepPaths := append(staticLibInstallPaths, getNdkBaseTimestampFile(ctx))
316
Dan Albertf1d14c72020-07-30 14:32:55 -0700317 // There's a phony "ndk" rule defined in core/main.mk that depends on this.
318 // `m ndk` will build the sysroots for the architectures in the current
319 // lunch target. `build/soong/scripts/build-ndk-prebuilts.sh` will build the
320 // sysroots for all the NDK architectures and package them so they can be
321 // imported into the NDK's build.
Dan Albert6ab43d82017-12-13 15:05:04 -0800322 ctx.Build(pctx, android.BuildParams{
323 Rule: android.Touch,
324 Output: getNdkFullTimestampFile(ctx),
Dan Albert266f9912024-08-13 22:01:23 +0000325 Implicits: append(fullDepPaths, headerCCompatVerificationTimestampPaths...),
Dan Albert914449f2016-06-17 16:45:24 -0700326 })
327}