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 | import ( |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 18 | "fmt" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 19 | "path/filepath" |
| 20 | |
| 21 | "github.com/google/blueprint" |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 23 | |
| 24 | "android/soong/android" |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 25 | "android/soong/bazel" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 28 | var ( |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 29 | versionBionicHeaders = pctx.AndroidStaticRule("versionBionicHeaders", |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 30 | blueprint.RuleParams{ |
Dan Albert | d2130a9 | 2017-03-29 18:33:28 -0700 | [diff] [blame] | 31 | // The `&& touch $out` isn't really necessary, but Blueprint won't |
| 32 | // let us have only implicit outputs. |
| 33 | Command: "$versionerCmd -o $outDir $srcDir $depsPath && touch $out", |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 34 | CommandDeps: []string{"$versionerCmd"}, |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 35 | }, |
Dan Albert | d2130a9 | 2017-03-29 18:33:28 -0700 | [diff] [blame] | 36 | "depsPath", "srcDir", "outDir") |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 37 | |
| 38 | preprocessNdkHeader = pctx.AndroidStaticRule("preprocessNdkHeader", |
| 39 | blueprint.RuleParams{ |
| 40 | Command: "$preprocessor -o $out $in", |
| 41 | CommandDeps: []string{"$preprocessor"}, |
| 42 | }, |
| 43 | "preprocessor") |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 44 | ) |
| 45 | |
| 46 | func init() { |
Logan Chien | 2f06635 | 2018-10-25 18:11:09 +0800 | [diff] [blame] | 47 | pctx.SourcePathVariable("versionerCmd", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/versioner") |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 50 | // Returns the NDK base include path for use with sdk_version current. Usable with -I. |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 51 | func getCurrentIncludePath(ctx android.ModuleContext) android.InstallPath { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 52 | return getNdkSysrootBase(ctx).Join(ctx, "usr/include") |
| 53 | } |
| 54 | |
Dan Albert | 7122205 | 2018-05-24 15:00:05 -0700 | [diff] [blame] | 55 | type headerProperties struct { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 56 | // Base directory of the headers being installed. As an example: |
| 57 | // |
| 58 | // ndk_headers { |
| 59 | // name: "foo", |
| 60 | // from: "include", |
| 61 | // to: "", |
| 62 | // srcs: ["include/foo/bar/baz.h"], |
| 63 | // } |
| 64 | // |
| 65 | // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead |
| 66 | // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 67 | From *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 68 | |
| 69 | // Install path within the sysroot. This is relative to usr/include. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 70 | To *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 71 | |
| 72 | // List of headers to install. Glob compatible. Common case is "include/**/*.h". |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 73 | Srcs []string `android:"path"` |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 74 | |
Dan Albert | 19ff8b4 | 2018-05-24 15:00:48 -0700 | [diff] [blame] | 75 | // Source paths that should be excluded from the srcs glob. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 76 | Exclude_srcs []string `android:"path"` |
Dan Albert | 19ff8b4 | 2018-05-24 15:00:48 -0700 | [diff] [blame] | 77 | |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 78 | // Path to the NOTICE file associated with the headers. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 79 | License *string `android:"path"` |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | type headerModule struct { |
| 83 | android.ModuleBase |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 84 | android.BazelModuleBase |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 85 | |
Dan Albert | 7122205 | 2018-05-24 15:00:05 -0700 | [diff] [blame] | 86 | properties headerProperties |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 87 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 88 | installPaths android.Paths |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 89 | licensePath android.Path |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 92 | func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string, |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 93 | to string) android.InstallPath { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 94 | // Output path is the sysroot base + "usr/include" + to directory + directory component |
| 95 | // of the file without the leading from directory stripped. |
| 96 | // |
| 97 | // Given: |
| 98 | // sysroot base = "ndk/sysroot" |
| 99 | // from = "include/foo" |
| 100 | // to = "bar" |
| 101 | // header = "include/foo/woodly/doodly.h" |
| 102 | // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h" |
| 103 | |
| 104 | // full/platform/path/to/include/foo |
| 105 | fullFromPath := android.PathForModuleSrc(ctx, from) |
| 106 | |
| 107 | // full/platform/path/to/include/foo/woodly |
| 108 | headerDir := filepath.Dir(header.String()) |
| 109 | |
| 110 | // woodly |
| 111 | strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir) |
| 112 | if err != nil { |
| 113 | ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir, |
| 114 | fullFromPath.String(), err) |
| 115 | } |
| 116 | |
| 117 | // full/platform/path/to/sysroot/usr/include/bar/woodly |
| 118 | installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir) |
| 119 | |
| 120 | // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h |
| 121 | return installDir |
| 122 | } |
| 123 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 124 | func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 125 | if String(m.properties.License) == "" { |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 126 | ctx.PropertyErrorf("license", "field is required") |
| 127 | } |
| 128 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 129 | m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License)) |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 130 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 131 | srcFiles := android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 132 | for _, header := range srcFiles { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 133 | installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), |
| 134 | String(m.properties.To)) |
Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 135 | installedPath := ctx.InstallFile(installDir, header.Base(), header) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 136 | installPath := installDir.Join(ctx, header.Base()) |
| 137 | if installPath != installedPath { |
| 138 | panic(fmt.Sprintf( |
| 139 | "expected header install path (%q) not equal to actual install path %q", |
| 140 | installPath, installedPath)) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 141 | } |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 142 | m.installPaths = append(m.installPaths, installPath) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if len(m.installPaths) == 0 { |
| 146 | ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs) |
| 147 | } |
| 148 | } |
| 149 | |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 150 | // TODO(b/243196151): Populate `system` and `arch` metadata |
| 151 | type bazelCcApiHeadersAttributes struct { |
| 152 | Hdrs bazel.LabelListAttribute |
| 153 | Include_dir *string |
| 154 | } |
| 155 | |
| 156 | func createCcApiHeadersTarget(ctx android.TopDownMutatorContext, includes []string, excludes []string, include_dir *string) { |
| 157 | props := bazel.BazelTargetModuleProperties{ |
| 158 | Rule_class: "cc_api_headers", |
| 159 | Bzl_load_location: "//build/bazel/rules/apis:cc_api_contribution.bzl", |
| 160 | } |
| 161 | attrs := &bazelCcApiHeadersAttributes{ |
| 162 | Hdrs: bazel.MakeLabelListAttribute( |
| 163 | android.BazelLabelForModuleSrcExcludes( |
| 164 | ctx, |
| 165 | includes, |
| 166 | excludes, |
| 167 | ), |
| 168 | ), |
| 169 | Include_dir: include_dir, |
| 170 | } |
| 171 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{ |
Spandan Das | 8159389 | 2022-09-06 17:31:48 +0000 | [diff] [blame] | 172 | Name: android.ApiContributionTargetName(ctx.ModuleName()), |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 173 | }, attrs) |
| 174 | } |
| 175 | |
Spandan Das | 8b4a5f3 | 2022-09-29 00:28:24 +0000 | [diff] [blame^] | 176 | var _ android.ApiProvider = (*headerModule)(nil) |
| 177 | |
| 178 | func (h *headerModule) ConvertWithApiBp2build(ctx android.TopDownMutatorContext) { |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 179 | // Generate `cc_api_headers` target for Multi-tree API export |
| 180 | createCcApiHeadersTarget(ctx, h.properties.Srcs, h.properties.Exclude_srcs, h.properties.From) |
| 181 | } |
| 182 | |
Patrice Arruda | 6ea4211 | 2019-04-03 08:43:30 -0700 | [diff] [blame] | 183 | // ndk_headers installs the sets of ndk headers defined in the srcs property |
| 184 | // to the sysroot base + "usr/include" + to directory + directory component. |
| 185 | // ndk_headers requires the license file to be specified. Example: |
| 186 | // |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 187 | // Given: |
| 188 | // sysroot base = "ndk/sysroot" |
| 189 | // from = "include/foo" |
| 190 | // to = "bar" |
| 191 | // header = "include/foo/woodly/doodly.h" |
| 192 | // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h" |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 193 | func ndkHeadersFactory() android.Module { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 194 | module := &headerModule{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 195 | module.AddProperties(&module.properties) |
| 196 | android.InitAndroidModule(module) |
| 197 | return module |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 198 | } |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 199 | |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 200 | type versionedHeaderProperties struct { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 201 | // Base directory of the headers being installed. As an example: |
| 202 | // |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 203 | // versioned_ndk_headers { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 204 | // name: "foo", |
| 205 | // from: "include", |
| 206 | // to: "", |
| 207 | // } |
| 208 | // |
| 209 | // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead |
| 210 | // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h. |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 211 | From *string |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 212 | |
| 213 | // Install path within the sysroot. This is relative to usr/include. |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 214 | To *string |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 215 | |
| 216 | // Path to the NOTICE file associated with the headers. |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 217 | License *string |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // Like ndk_headers, but preprocesses the headers with the bionic versioner: |
| 221 | // https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md. |
| 222 | // |
| 223 | // Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the |
| 224 | // module does not have the srcs property, and operates on a full directory (the `from` property). |
| 225 | // |
| 226 | // Note that this is really only built to handle bionic/libc/include. |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 227 | type versionedHeaderModule struct { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 228 | android.ModuleBase |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 229 | android.BazelModuleBase |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 230 | |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 231 | properties versionedHeaderProperties |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 232 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 233 | installPaths android.Paths |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 234 | licensePath android.Path |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 237 | // Return the glob pattern to find all .h files beneath `dir` |
| 238 | func headerGlobPattern(dir string) string { |
| 239 | return filepath.Join(dir, "**", "*.h") |
| 240 | } |
| 241 | |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 242 | func (m *versionedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 243 | if String(m.properties.License) == "" { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 244 | ctx.PropertyErrorf("license", "field is required") |
| 245 | } |
| 246 | |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 247 | m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License)) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 248 | |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 249 | fromSrcPath := android.PathForModuleSrc(ctx, String(m.properties.From)) |
| 250 | toOutputPath := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To)) |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 251 | srcFiles := ctx.GlobFiles(headerGlobPattern(fromSrcPath.String()), nil) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 252 | var installPaths []android.WritablePath |
| 253 | for _, header := range srcFiles { |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 254 | installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), String(m.properties.To)) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 255 | installPath := installDir.Join(ctx, header.Base()) |
| 256 | installPaths = append(installPaths, installPath) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 257 | m.installPaths = append(m.installPaths, installPath) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if len(m.installPaths) == 0 { |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 261 | ctx.ModuleErrorf("glob %q matched zero files", String(m.properties.From)) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 264 | processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, srcFiles, installPaths) |
| 265 | } |
| 266 | |
Spandan Das | 8b4a5f3 | 2022-09-29 00:28:24 +0000 | [diff] [blame^] | 267 | var _ android.ApiProvider = (*versionedHeaderModule)(nil) |
| 268 | |
| 269 | func (h *versionedHeaderModule) ConvertWithApiBp2build(ctx android.TopDownMutatorContext) { |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 270 | // Glob all .h files under `From` |
| 271 | includePattern := headerGlobPattern(proptools.String(h.properties.From)) |
| 272 | // Generate `cc_api_headers` target for Multi-tree API export |
| 273 | createCcApiHeadersTarget(ctx, []string{includePattern}, []string{}, h.properties.From) |
| 274 | } |
| 275 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 276 | func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path, |
| 277 | srcFiles android.Paths, installPaths []android.WritablePath) android.Path { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 278 | // The versioner depends on a dependencies directory to simplify determining include paths |
| 279 | // when parsing headers. This directory contains architecture specific directories as well |
| 280 | // as a common directory, each of which contains symlinks to the actually directories to |
| 281 | // be included. |
| 282 | // |
| 283 | // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly |
| 284 | // depend on these headers. |
| 285 | // TODO(http://b/35673191): Update the versioner to use a --sysroot. |
| 286 | depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies") |
| 287 | depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil) |
| 288 | for i, path := range depsGlob { |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 289 | if ctx.IsSymlink(path) { |
| 290 | dest := ctx.Readlink(path) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 291 | // Additional .. to account for the symlink itself. |
| 292 | depsGlob[i] = android.PathForSource( |
| 293 | ctx, filepath.Clean(filepath.Join(path.String(), "..", dest))) |
| 294 | } |
| 295 | } |
| 296 | |
Dan Albert | d2130a9 | 2017-03-29 18:33:28 -0700 | [diff] [blame] | 297 | timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp") |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 298 | ctx.Build(pctx, android.BuildParams{ |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 299 | Rule: versionBionicHeaders, |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 300 | Description: "versioner preprocess " + srcDir.Rel(), |
Dan Albert | d2130a9 | 2017-03-29 18:33:28 -0700 | [diff] [blame] | 301 | Output: timestampFile, |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 302 | Implicits: append(srcFiles, depsGlob...), |
| 303 | ImplicitOutputs: installPaths, |
| 304 | Args: map[string]string{ |
| 305 | "depsPath": depsPath.String(), |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 306 | "srcDir": srcDir.String(), |
| 307 | "outDir": outDir.String(), |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 308 | }, |
| 309 | }) |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 310 | |
| 311 | return timestampFile |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Patrice Arruda | 6ea4211 | 2019-04-03 08:43:30 -0700 | [diff] [blame] | 314 | // versioned_ndk_headers preprocesses the headers with the bionic versioner: |
| 315 | // https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md. |
| 316 | // Unlike the ndk_headers soong module, versioned_ndk_headers operates on a |
| 317 | // directory level specified in `from` property. This is only used to process |
| 318 | // the bionic/libc/include directory. |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 319 | func versionedNdkHeadersFactory() android.Module { |
| 320 | module := &versionedHeaderModule{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 321 | |
| 322 | module.AddProperties(&module.properties) |
| 323 | |
Dan Willemsen | 4f644da | 2018-10-10 17:18:08 -0700 | [diff] [blame] | 324 | android.InitAndroidModule(module) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 325 | |
| 326 | return module |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 327 | } |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 328 | |
Spandan Das | 0773a60 | 2022-08-16 00:55:11 +0000 | [diff] [blame] | 329 | // preprocessed_ndk_header { |
| 330 | // |
| 331 | // name: "foo", |
| 332 | // preprocessor: "foo.sh", |
| 333 | // srcs: [...], |
| 334 | // to: "android", |
| 335 | // |
| 336 | // } |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 337 | // |
| 338 | // Will invoke the preprocessor as: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 339 | // |
| 340 | // $preprocessor -o $SYSROOT/usr/include/android/needs_preproc.h $src |
| 341 | // |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 342 | // For each src in srcs. |
| 343 | type preprocessedHeadersProperties struct { |
| 344 | // The preprocessor to run. Must be a program inside the source directory |
| 345 | // with no dependencies. |
| 346 | Preprocessor *string |
| 347 | |
| 348 | // Source path to the files to be preprocessed. |
| 349 | Srcs []string |
| 350 | |
| 351 | // Source paths that should be excluded from the srcs glob. |
| 352 | Exclude_srcs []string |
| 353 | |
| 354 | // Install path within the sysroot. This is relative to usr/include. |
| 355 | To *string |
| 356 | |
| 357 | // Path to the NOTICE file associated with the headers. |
| 358 | License *string |
| 359 | } |
| 360 | |
| 361 | type preprocessedHeadersModule struct { |
| 362 | android.ModuleBase |
| 363 | |
| 364 | properties preprocessedHeadersProperties |
| 365 | |
| 366 | installPaths android.Paths |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 367 | licensePath android.Path |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 370 | func (m *preprocessedHeadersModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 371 | if String(m.properties.License) == "" { |
| 372 | ctx.PropertyErrorf("license", "field is required") |
| 373 | } |
| 374 | |
| 375 | preprocessor := android.PathForModuleSrc(ctx, String(m.properties.Preprocessor)) |
| 376 | m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License)) |
| 377 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 378 | srcFiles := android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs) |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 379 | installDir := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To)) |
| 380 | for _, src := range srcFiles { |
| 381 | installPath := installDir.Join(ctx, src.Base()) |
| 382 | m.installPaths = append(m.installPaths, installPath) |
| 383 | |
| 384 | ctx.Build(pctx, android.BuildParams{ |
| 385 | Rule: preprocessNdkHeader, |
| 386 | Description: "preprocess " + src.Rel(), |
| 387 | Input: src, |
| 388 | Output: installPath, |
| 389 | Args: map[string]string{ |
| 390 | "preprocessor": preprocessor.String(), |
| 391 | }, |
| 392 | }) |
| 393 | } |
| 394 | |
| 395 | if len(m.installPaths) == 0 { |
| 396 | ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs) |
| 397 | } |
| 398 | } |
| 399 | |
Patrice Arruda | 6ea4211 | 2019-04-03 08:43:30 -0700 | [diff] [blame] | 400 | // preprocessed_ndk_headers preprocesses all the ndk headers listed in the srcs |
| 401 | // property by executing the command defined in the preprocessor property. |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 402 | func preprocessedNdkHeadersFactory() android.Module { |
| 403 | module := &preprocessedHeadersModule{} |
| 404 | |
| 405 | module.AddProperties(&module.properties) |
| 406 | |
Dan Willemsen | 4f644da | 2018-10-10 17:18:08 -0700 | [diff] [blame] | 407 | android.InitAndroidModule(module) |
Dan Albert | cb1b4b2 | 2018-05-24 15:06:11 -0700 | [diff] [blame] | 408 | |
| 409 | return module |
| 410 | } |