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" |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 57 | "strings" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 58 | ) |
| 59 | |
| 60 | func init() { |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 61 | RegisterNdkModuleTypes(android.InitRegistrationContext) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 64 | func RegisterNdkModuleTypes(ctx android.RegistrationContext) { |
Spandan Das | 319711b | 2023-09-19 19:04:41 +0000 | [diff] [blame] | 65 | ctx.RegisterModuleType("ndk_headers", NdkHeadersFactory) |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 66 | ctx.RegisterModuleType("ndk_library", NdkLibraryFactory) |
Spandan Das | a7da3f0 | 2023-09-28 19:30:51 +0000 | [diff] [blame^] | 67 | ctx.RegisterModuleType("versioned_ndk_headers", VersionedNdkHeadersFactory) |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 68 | ctx.RegisterModuleType("preprocessed_ndk_headers", preprocessedNdkHeadersFactory) |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 69 | ctx.RegisterParallelSingletonType("ndk", NdkSingleton) |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 72 | func getNdkInstallBase(ctx android.PathContext) android.InstallPath { |
| 73 | return android.PathForNdkInstall(ctx) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // Returns the main install directory for the NDK sysroot. Usable with --sysroot. |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 77 | func getNdkSysrootBase(ctx android.PathContext) android.InstallPath { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 78 | return getNdkInstallBase(ctx).Join(ctx, "sysroot") |
| 79 | } |
| 80 | |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 81 | // The base timestamp file depends on the NDK headers and stub shared libraries, |
| 82 | // but not the static libraries. This distinction is needed because the static |
| 83 | // libraries themselves might need to depend on the base sysroot. |
| 84 | func getNdkBaseTimestampFile(ctx android.PathContext) android.WritablePath { |
| 85 | return android.PathForOutput(ctx, "ndk_base.timestamp") |
| 86 | } |
| 87 | |
Chih-Hung Hsieh | 5b72153 | 2021-11-30 17:31:23 -0800 | [diff] [blame] | 88 | // The headers timestamp file depends only on the NDK headers. |
| 89 | // This is used mainly for .tidy files that do not need any stub libraries. |
| 90 | func getNdkHeadersTimestampFile(ctx android.PathContext) android.WritablePath { |
| 91 | return android.PathForOutput(ctx, "ndk_headers.timestamp") |
| 92 | } |
| 93 | |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 94 | // The full timestamp file depends on the base timestamp *and* the static |
| 95 | // libraries. |
| 96 | func getNdkFullTimestampFile(ctx android.PathContext) android.WritablePath { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 97 | return android.PathForOutput(ctx, "ndk.timestamp") |
| 98 | } |
| 99 | |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 100 | // The list of all NDK headers as they are located in the repo. |
| 101 | // Used for ABI monitoring to track only structures defined in NDK headers. |
| 102 | func getNdkABIHeadersFile(ctx android.PathContext) android.WritablePath { |
| 103 | return android.PathForOutput(ctx, "ndk_abi_headers.txt") |
| 104 | } |
| 105 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 106 | func NdkSingleton() android.Singleton { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 107 | return &ndkSingleton{} |
| 108 | } |
| 109 | |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 110 | // Collect all NDK exported headers paths into a file that is used to |
| 111 | // detect public types that should be ABI monitored. |
| 112 | // |
| 113 | // Assume that we have the following code in exported header: |
| 114 | // |
| 115 | // typedef struct Context Context; |
| 116 | // typedef struct Output { |
| 117 | // ... |
| 118 | // } Output; |
| 119 | // void DoSomething(Context* ctx, Output* output); |
| 120 | // |
| 121 | // If none of public headers exported to end-users contain definition of |
| 122 | // "struct Context", then "struct Context" layout and members shouldn't be |
| 123 | // monitored. However we use DWARF information from a real library, which |
| 124 | // may have access to the definition of "string Context" from |
| 125 | // implementation headers, and it will leak to ABI. |
| 126 | // |
| 127 | // STG tool doesn't access source and header files, only DWARF information |
| 128 | // from compiled library. And the DWARF contains file name where a type is |
| 129 | // defined. So we need a rule to build a list of paths to public headers, |
| 130 | // so STG can distinguish private types from public and do not monitor |
| 131 | // private types that are not accessible to library users. |
| 132 | func writeNdkAbiSrcFilter(ctx android.BuilderContext, |
| 133 | headerSrcPaths android.Paths, outputFile android.WritablePath) { |
| 134 | var filterBuilder strings.Builder |
| 135 | filterBuilder.WriteString("[decl_file_allowlist]\n") |
| 136 | for _, headerSrcPath := range headerSrcPaths { |
| 137 | filterBuilder.WriteString(headerSrcPath.String()) |
| 138 | filterBuilder.WriteString("\n") |
| 139 | } |
| 140 | |
| 141 | android.WriteFileRule(ctx, outputFile, filterBuilder.String()) |
| 142 | } |
| 143 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 144 | type ndkSingleton struct{} |
| 145 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 146 | func (n *ndkSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 147 | var staticLibInstallPaths android.Paths |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 148 | var headerSrcPaths android.Paths |
| 149 | var headerInstallPaths android.Paths |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 150 | var installPaths android.Paths |
| 151 | var licensePaths android.Paths |
| 152 | ctx.VisitAllModules(func(module android.Module) { |
Dan Willemsen | 95f4dbb | 2017-05-05 23:26:01 -0700 | [diff] [blame] | 153 | if m, ok := module.(android.Module); ok && !m.Enabled() { |
| 154 | return |
| 155 | } |
| 156 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 157 | if m, ok := module.(*headerModule); ok { |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 158 | headerSrcPaths = append(headerSrcPaths, m.srcPaths...) |
| 159 | headerInstallPaths = append(headerInstallPaths, m.installPaths...) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 160 | installPaths = append(installPaths, m.installPaths...) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 161 | licensePaths = append(licensePaths, m.licensePath) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 162 | } |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 163 | |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 164 | if m, ok := module.(*versionedHeaderModule); ok { |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 165 | headerSrcPaths = append(headerSrcPaths, m.srcPaths...) |
| 166 | headerInstallPaths = append(headerInstallPaths, m.installPaths...) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 167 | installPaths = append(installPaths, m.installPaths...) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 168 | licensePaths = append(licensePaths, m.licensePath) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 171 | if m, ok := module.(*preprocessedHeadersModule); ok { |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 172 | headerSrcPaths = append(headerSrcPaths, m.srcPaths...) |
| 173 | headerInstallPaths = append(headerInstallPaths, m.installPaths...) |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 174 | installPaths = append(installPaths, m.installPaths...) |
| 175 | licensePaths = append(licensePaths, m.licensePath) |
| 176 | } |
| 177 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 178 | if m, ok := module.(*Module); ok { |
Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 179 | if installer, ok := m.installer.(*stubDecorator); ok && m.library.buildStubs() { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 180 | installPaths = append(installPaths, installer.installPath) |
| 181 | } |
Dan Albert | f563d25 | 2017-10-13 00:29:00 -0700 | [diff] [blame] | 182 | |
| 183 | if library, ok := m.linker.(*libraryDecorator); ok { |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 184 | if library.ndkSysrootPath != nil { |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 185 | staticLibInstallPaths = append( |
| 186 | staticLibInstallPaths, library.ndkSysrootPath) |
Dan Albert | f563d25 | 2017-10-13 00:29:00 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
Dan Albert | 5b0d4f3 | 2023-04-04 23:22:11 +0000 | [diff] [blame] | 189 | |
| 190 | if object, ok := m.linker.(*objectLinker); ok { |
| 191 | if object.ndkSysrootPath != nil { |
| 192 | staticLibInstallPaths = append( |
| 193 | staticLibInstallPaths, object.ndkSysrootPath) |
| 194 | } |
| 195 | } |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 196 | } |
| 197 | }) |
| 198 | |
Ryan Prichard | b1c9d40 | 2018-08-20 22:06:01 -0700 | [diff] [blame] | 199 | // Include only a single copy of each license file. The Bionic NOTICE is |
| 200 | // long and is referenced by multiple Bionic modules. |
| 201 | licensePaths = android.FirstUniquePaths(licensePaths) |
| 202 | |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 203 | combinedLicense := getNdkInstallBase(ctx).Join(ctx, "NOTICE") |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 204 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 205 | Rule: android.Cat, |
| 206 | Description: "combine licenses", |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 207 | Output: combinedLicense, |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 208 | Inputs: licensePaths, |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 209 | }) |
| 210 | |
Dan Albert | 4922703 | 2021-06-15 13:25:25 -0700 | [diff] [blame] | 211 | baseDepPaths := append(installPaths, combinedLicense) |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 212 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 213 | ctx.Build(pctx, android.BuildParams{ |
Dan Albert | 4922703 | 2021-06-15 13:25:25 -0700 | [diff] [blame] | 214 | Rule: android.Touch, |
| 215 | Output: getNdkBaseTimestampFile(ctx), |
| 216 | Implicits: baseDepPaths, |
| 217 | Validation: getNdkAbiDiffTimestampFile(ctx), |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 218 | }) |
| 219 | |
Chih-Hung Hsieh | 5b72153 | 2021-11-30 17:31:23 -0800 | [diff] [blame] | 220 | ctx.Build(pctx, android.BuildParams{ |
| 221 | Rule: android.Touch, |
| 222 | Output: getNdkHeadersTimestampFile(ctx), |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 223 | Implicits: headerInstallPaths, |
Chih-Hung Hsieh | 5b72153 | 2021-11-30 17:31:23 -0800 | [diff] [blame] | 224 | }) |
| 225 | |
Aleksei Vetrov | 262ed1a | 2023-08-23 10:06:35 +0000 | [diff] [blame] | 226 | writeNdkAbiSrcFilter(ctx, headerSrcPaths, getNdkABIHeadersFile(ctx)) |
| 227 | |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 228 | fullDepPaths := append(staticLibInstallPaths, getNdkBaseTimestampFile(ctx)) |
| 229 | |
Dan Albert | f1d14c7 | 2020-07-30 14:32:55 -0700 | [diff] [blame] | 230 | // There's a phony "ndk" rule defined in core/main.mk that depends on this. |
| 231 | // `m ndk` will build the sysroots for the architectures in the current |
| 232 | // lunch target. `build/soong/scripts/build-ndk-prebuilts.sh` will build the |
| 233 | // sysroots for all the NDK architectures and package them so they can be |
| 234 | // imported into the NDK's build. |
Dan Albert | 6ab43d8 | 2017-12-13 15:05:04 -0800 | [diff] [blame] | 235 | ctx.Build(pctx, android.BuildParams{ |
| 236 | Rule: android.Touch, |
| 237 | Output: getNdkFullTimestampFile(ctx), |
| 238 | Implicits: fullDepPaths, |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 239 | }) |
| 240 | } |