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