Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package 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 Onorato | b4638c1 | 2021-10-27 15:47:06 -0700 | [diff] [blame] | 36 | // libraries with placeholder implementations of everything for use at build time |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 37 | // 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 | |
| 55 | import ( |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 56 | "android/soong/android" |
Dan Albert | 266f991 | 2024-08-13 22:01:23 +0000 | [diff] [blame] | 57 | "fmt" |
| 58 | "path/filepath" |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 59 | "strings" |
Dan Albert | 266f991 | 2024-08-13 22:01:23 +0000 | [diff] [blame] | 60 | |
| 61 | "github.com/google/blueprint" |
| 62 | ) |
| 63 | |
| 64 | var ( |
| 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 Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 73 | ) |
| 74 | |
| 75 | func init() { |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 76 | RegisterNdkModuleTypes(android.InitRegistrationContext) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 79 | func RegisterNdkModuleTypes(ctx android.RegistrationContext) { |
Spandan Das | 319711b | 2023-09-19 19:04:41 +0000 | [diff] [blame] | 80 | ctx.RegisterModuleType("ndk_headers", NdkHeadersFactory) |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 81 | ctx.RegisterModuleType("ndk_library", NdkLibraryFactory) |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 82 | ctx.RegisterModuleType("preprocessed_ndk_headers", preprocessedNdkHeadersFactory) |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 83 | ctx.RegisterParallelSingletonType("ndk", NdkSingleton) |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Spandan Das | f280b23 | 2024-04-04 21:25:51 +0000 | [diff] [blame] | 86 | func getNdkInstallBase(ctx android.PathContext) android.OutputPath { |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 87 | return android.PathForNdkInstall(ctx) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Returns the main install directory for the NDK sysroot. Usable with --sysroot. |
Spandan Das | f280b23 | 2024-04-04 21:25:51 +0000 | [diff] [blame] | 91 | func getNdkSysrootBase(ctx android.PathContext) android.OutputPath { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 92 | return getNdkInstallBase(ctx).Join(ctx, "sysroot") |
| 93 | } |
| 94 | |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 95 | // The base timestamp file depends on the NDK headers and stub shared libraries, |
| 96 | // but not the static libraries. This distinction is needed because the static |
| 97 | // libraries themselves might need to depend on the base sysroot. |
| 98 | func getNdkBaseTimestampFile(ctx android.PathContext) android.WritablePath { |
| 99 | return android.PathForOutput(ctx, "ndk_base.timestamp") |
| 100 | } |
| 101 | |
Chih-Hung Hsieh | 5b72153 | 2021-11-30 17:31:23 -0800 | [diff] [blame] | 102 | // The headers timestamp file depends only on the NDK headers. |
| 103 | // This is used mainly for .tidy files that do not need any stub libraries. |
| 104 | func getNdkHeadersTimestampFile(ctx android.PathContext) android.WritablePath { |
| 105 | return android.PathForOutput(ctx, "ndk_headers.timestamp") |
| 106 | } |
| 107 | |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 108 | // The full timestamp file depends on the base timestamp *and* the static |
| 109 | // libraries. |
| 110 | func getNdkFullTimestampFile(ctx android.PathContext) android.WritablePath { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 111 | return android.PathForOutput(ctx, "ndk.timestamp") |
| 112 | } |
| 113 | |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 114 | // The list of all NDK headers as they are located in the repo. |
| 115 | // Used for ABI monitoring to track only structures defined in NDK headers. |
| 116 | func getNdkABIHeadersFile(ctx android.PathContext) android.WritablePath { |
| 117 | return android.PathForOutput(ctx, "ndk_abi_headers.txt") |
| 118 | } |
| 119 | |
Dan Albert | 266f991 | 2024-08-13 22:01:23 +0000 | [diff] [blame] | 120 | func verifyNdkHeaderIsCCompatible(ctx android.SingletonContext, |
| 121 | src android.Path, dest android.Path) android.Path { |
| 122 | sysrootInclude := getCurrentIncludePath(ctx) |
| 123 | baseOutputDir := android.PathForOutput(ctx, "c-compat-verification") |
| 124 | installRelPath, err := filepath.Rel(sysrootInclude.String(), dest.String()) |
| 125 | if err != nil { |
| 126 | ctx.Errorf("filepath.Rel(%q, %q) failed: %s", dest, sysrootInclude, err) |
| 127 | } |
| 128 | output := baseOutputDir.Join(ctx, installRelPath) |
| 129 | ctx.Build(pctx, android.BuildParams{ |
| 130 | Rule: verifyCCompat, |
| 131 | Description: fmt.Sprintf("Verifying C compatibility of %s", src), |
| 132 | Output: output, |
| 133 | Input: dest, |
| 134 | // Ensures that all the headers in the sysroot are already installed |
| 135 | // before testing any of the headers for C compatibility, and also that |
| 136 | // the check will be re-run whenever the sysroot changes. This is |
| 137 | // necessary because many of the NDK headers depend on other NDK |
| 138 | // headers, but we don't have explicit dependency tracking for that. |
| 139 | Implicits: []android.Path{getNdkHeadersTimestampFile(ctx)}, |
| 140 | Args: map[string]string{ |
| 141 | "ccCmd": "${config.ClangBin}/clang", |
| 142 | "flags": fmt.Sprintf( |
| 143 | // Ideally we'd check each ABI, multiple API levels, |
| 144 | // fortify/non-fortify, and a handful of other variations. It's |
| 145 | // a lot more difficult to do that though, and would eat up more |
| 146 | // build time. All the problems we've seen so far that this |
| 147 | // check would catch have been in arch-generic and |
| 148 | // minSdkVersion-generic code in frameworks though, so this is a |
| 149 | // good place to start. |
| 150 | "-target aarch64-linux-android%d --sysroot %s", |
| 151 | android.FutureApiLevel.FinalOrFutureInt(), |
| 152 | getNdkSysrootBase(ctx).String(), |
| 153 | ), |
| 154 | }, |
| 155 | }) |
| 156 | return output |
| 157 | } |
| 158 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 159 | func NdkSingleton() android.Singleton { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 160 | return &ndkSingleton{} |
| 161 | } |
| 162 | |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 163 | // Collect all NDK exported headers paths into a file that is used to |
| 164 | // detect public types that should be ABI monitored. |
| 165 | // |
| 166 | // Assume that we have the following code in exported header: |
| 167 | // |
| 168 | // typedef struct Context Context; |
| 169 | // typedef struct Output { |
| 170 | // ... |
| 171 | // } Output; |
| 172 | // void DoSomething(Context* ctx, Output* output); |
| 173 | // |
| 174 | // If none of public headers exported to end-users contain definition of |
| 175 | // "struct Context", then "struct Context" layout and members shouldn't be |
| 176 | // monitored. However we use DWARF information from a real library, which |
| 177 | // may have access to the definition of "string Context" from |
| 178 | // implementation headers, and it will leak to ABI. |
| 179 | // |
| 180 | // STG tool doesn't access source and header files, only DWARF information |
| 181 | // from compiled library. And the DWARF contains file name where a type is |
| 182 | // defined. So we need a rule to build a list of paths to public headers, |
| 183 | // so STG can distinguish private types from public and do not monitor |
| 184 | // private types that are not accessible to library users. |
| 185 | func writeNdkAbiSrcFilter(ctx android.BuilderContext, |
| 186 | headerSrcPaths android.Paths, outputFile android.WritablePath) { |
| 187 | var filterBuilder strings.Builder |
| 188 | filterBuilder.WriteString("[decl_file_allowlist]\n") |
| 189 | for _, headerSrcPath := range headerSrcPaths { |
| 190 | filterBuilder.WriteString(headerSrcPath.String()) |
| 191 | filterBuilder.WriteString("\n") |
| 192 | } |
| 193 | |
| 194 | android.WriteFileRule(ctx, outputFile, filterBuilder.String()) |
| 195 | } |
| 196 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 197 | type ndkSingleton struct{} |
| 198 | |
Dan Albert | 266f991 | 2024-08-13 22:01:23 +0000 | [diff] [blame] | 199 | type srcDestPair struct { |
| 200 | src android.Path |
| 201 | dest android.Path |
| 202 | } |
| 203 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 204 | func (n *ndkSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 205 | var staticLibInstallPaths android.Paths |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 206 | var headerSrcPaths android.Paths |
| 207 | var headerInstallPaths android.Paths |
Dan Albert | 266f991 | 2024-08-13 22:01:23 +0000 | [diff] [blame] | 208 | var headersToVerify []srcDestPair |
| 209 | var headerCCompatVerificationTimestampPaths android.Paths |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 210 | var installPaths android.Paths |
| 211 | var licensePaths android.Paths |
| 212 | ctx.VisitAllModules(func(module android.Module) { |
Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 213 | if m, ok := module.(android.Module); ok && !m.Enabled(ctx) { |
Dan Willemsen | 95f4dbb | 2017-05-05 23:26:01 -0700 | [diff] [blame] | 214 | return |
| 215 | } |
| 216 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 217 | if m, ok := module.(*headerModule); ok { |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 218 | headerSrcPaths = append(headerSrcPaths, m.srcPaths...) |
| 219 | headerInstallPaths = append(headerInstallPaths, m.installPaths...) |
Dan Albert | 266f991 | 2024-08-13 22:01:23 +0000 | [diff] [blame] | 220 | if !Bool(m.properties.Skip_verification) { |
| 221 | for i, installPath := range m.installPaths { |
| 222 | headersToVerify = append(headersToVerify, srcDestPair{ |
| 223 | src: m.srcPaths[i], |
| 224 | dest: installPath, |
| 225 | }) |
| 226 | } |
| 227 | } |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 228 | installPaths = append(installPaths, m.installPaths...) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 229 | licensePaths = append(licensePaths, m.licensePath) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 230 | } |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 231 | |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 232 | if m, ok := module.(*preprocessedHeadersModule); ok { |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 233 | headerSrcPaths = append(headerSrcPaths, m.srcPaths...) |
| 234 | headerInstallPaths = append(headerInstallPaths, m.installPaths...) |
Dan Albert | 266f991 | 2024-08-13 22:01:23 +0000 | [diff] [blame] | 235 | if !Bool(m.properties.Skip_verification) { |
| 236 | for i, installPath := range m.installPaths { |
| 237 | headersToVerify = append(headersToVerify, srcDestPair{ |
| 238 | src: m.srcPaths[i], |
| 239 | dest: installPath, |
| 240 | }) |
| 241 | } |
| 242 | } |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 243 | installPaths = append(installPaths, m.installPaths...) |
| 244 | licensePaths = append(licensePaths, m.licensePath) |
| 245 | } |
| 246 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 247 | if m, ok := module.(*Module); ok { |
Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 248 | if installer, ok := m.installer.(*stubDecorator); ok && m.library.buildStubs() { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 249 | installPaths = append(installPaths, installer.installPath) |
| 250 | } |
Dan Albert | f563d25 | 2017-10-13 00:29:00 -0700 | [diff] [blame] | 251 | |
| 252 | if library, ok := m.linker.(*libraryDecorator); ok { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 253 | if library.ndkSysrootPath != nil { |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 254 | staticLibInstallPaths = append( |
| 255 | staticLibInstallPaths, library.ndkSysrootPath) |
Dan Albert | f563d25 | 2017-10-13 00:29:00 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
Dan Albert | 5b0d4f3 | 2023-04-04 23:22:11 +0000 | [diff] [blame] | 258 | |
| 259 | if object, ok := m.linker.(*objectLinker); ok { |
| 260 | if object.ndkSysrootPath != nil { |
| 261 | staticLibInstallPaths = append( |
| 262 | staticLibInstallPaths, object.ndkSysrootPath) |
| 263 | } |
| 264 | } |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 265 | } |
| 266 | }) |
| 267 | |
Ryan Prichard | b1c9d40 | 2018-08-20 22:06:01 -0700 | [diff] [blame] | 268 | // Include only a single copy of each license file. The Bionic NOTICE is |
| 269 | // long and is referenced by multiple Bionic modules. |
| 270 | licensePaths = android.FirstUniquePaths(licensePaths) |
| 271 | |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 272 | combinedLicense := getNdkInstallBase(ctx).Join(ctx, "NOTICE") |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 273 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 274 | Rule: android.Cat, |
| 275 | Description: "combine licenses", |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 276 | Output: combinedLicense, |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 277 | Inputs: licensePaths, |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 278 | }) |
| 279 | |
Dan Albert | 4922703 | 2021-06-15 13:25:25 -0700 | [diff] [blame] | 280 | baseDepPaths := append(installPaths, combinedLicense) |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 281 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 282 | ctx.Build(pctx, android.BuildParams{ |
Dan Albert | 4922703 | 2021-06-15 13:25:25 -0700 | [diff] [blame] | 283 | Rule: android.Touch, |
| 284 | Output: getNdkBaseTimestampFile(ctx), |
| 285 | Implicits: baseDepPaths, |
| 286 | Validation: getNdkAbiDiffTimestampFile(ctx), |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 287 | }) |
| 288 | |
Chih-Hung Hsieh | 5b72153 | 2021-11-30 17:31:23 -0800 | [diff] [blame] | 289 | ctx.Build(pctx, android.BuildParams{ |
| 290 | Rule: android.Touch, |
| 291 | Output: getNdkHeadersTimestampFile(ctx), |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 292 | Implicits: headerInstallPaths, |
Chih-Hung Hsieh | 5b72153 | 2021-11-30 17:31:23 -0800 | [diff] [blame] | 293 | }) |
| 294 | |
Dan Albert | 266f991 | 2024-08-13 22:01:23 +0000 | [diff] [blame] | 295 | for _, srcDestPair := range headersToVerify { |
| 296 | headerCCompatVerificationTimestampPaths = append( |
| 297 | headerCCompatVerificationTimestampPaths, |
| 298 | verifyNdkHeaderIsCCompatible(ctx, srcDestPair.src, srcDestPair.dest)) |
| 299 | } |
| 300 | |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 301 | writeNdkAbiSrcFilter(ctx, headerSrcPaths, getNdkABIHeadersFile(ctx)) |
| 302 | |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 303 | fullDepPaths := append(staticLibInstallPaths, getNdkBaseTimestampFile(ctx)) |
| 304 | |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 305 | // There's a phony "ndk" rule defined in core/main.mk that depends on this. |
| 306 | // `m ndk` will build the sysroots for the architectures in the current |
| 307 | // lunch target. `build/soong/scripts/build-ndk-prebuilts.sh` will build the |
| 308 | // sysroots for all the NDK architectures and package them so they can be |
| 309 | // imported into the NDK's build. |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 310 | ctx.Build(pctx, android.BuildParams{ |
| 311 | Rule: android.Touch, |
| 312 | Output: getNdkFullTimestampFile(ctx), |
Dan Albert | 266f991 | 2024-08-13 22:01:23 +0000 | [diff] [blame] | 313 | Implicits: append(fullDepPaths, headerCCompatVerificationTimestampPaths...), |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 314 | }) |
| 315 | } |