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" |
| 19 | "os" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 20 | "path/filepath" |
Lazar Trsic | cdb710f | 2017-11-24 16:38:47 +0100 | [diff] [blame] | 21 | "strings" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | |
| 25 | "android/soong/android" |
| 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() { |
| 47 | pctx.HostBinToolVariable("versionerCmd", "versioner") |
| 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. |
| 51 | func getCurrentIncludePath(ctx android.ModuleContext) android.OutputPath { |
| 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". |
| 73 | Srcs []string |
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. |
| 76 | Exclude_srcs []string |
| 77 | |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 78 | // Path to the NOTICE file associated with the headers. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 79 | License *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | type headerModule struct { |
| 83 | android.ModuleBase |
| 84 | |
Dan Albert | 7122205 | 2018-05-24 15:00:05 -0700 | [diff] [blame] | 85 | properties headerProperties |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 86 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 87 | installPaths android.Paths |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 88 | licensePath android.ModuleSrcPath |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 91 | func (m *headerModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 92 | } |
| 93 | |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 94 | func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string, |
| 95 | to string) android.OutputPath { |
| 96 | // Output path is the sysroot base + "usr/include" + to directory + directory component |
| 97 | // of the file without the leading from directory stripped. |
| 98 | // |
| 99 | // Given: |
| 100 | // sysroot base = "ndk/sysroot" |
| 101 | // from = "include/foo" |
| 102 | // to = "bar" |
| 103 | // header = "include/foo/woodly/doodly.h" |
| 104 | // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h" |
| 105 | |
| 106 | // full/platform/path/to/include/foo |
| 107 | fullFromPath := android.PathForModuleSrc(ctx, from) |
| 108 | |
| 109 | // full/platform/path/to/include/foo/woodly |
| 110 | headerDir := filepath.Dir(header.String()) |
| 111 | |
| 112 | // woodly |
| 113 | strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir) |
| 114 | if err != nil { |
| 115 | ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir, |
| 116 | fullFromPath.String(), err) |
| 117 | } |
| 118 | |
| 119 | // full/platform/path/to/sysroot/usr/include/bar/woodly |
| 120 | installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir) |
| 121 | |
| 122 | // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h |
| 123 | return installDir |
| 124 | } |
| 125 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 126 | func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 127 | if String(m.properties.License) == "" { |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 128 | ctx.PropertyErrorf("license", "field is required") |
| 129 | } |
| 130 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 131 | m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License)) |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 132 | |
Lazar Trsic | cdb710f | 2017-11-24 16:38:47 +0100 | [diff] [blame] | 133 | // When generating NDK prebuilts, skip installing MIPS headers, |
| 134 | // but keep them when doing regular platform build. |
| 135 | // Ndk_abis property is only set to true with build/soong/scripts/build-ndk-prebuilts.sh |
| 136 | // TODO: Revert this once MIPS is supported in NDK again. |
| 137 | if Bool(ctx.AConfig().Ndk_abis) && strings.Contains(ctx.ModuleName(), "mips") { |
| 138 | return |
| 139 | } |
| 140 | |
Dan Albert | 19ff8b4 | 2018-05-24 15:00:48 -0700 | [diff] [blame] | 141 | srcFiles := ctx.ExpandSources(m.properties.Srcs, m.properties.Exclude_srcs) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 142 | for _, header := range srcFiles { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 143 | installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), |
| 144 | String(m.properties.To)) |
Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 145 | installedPath := ctx.InstallFile(installDir, header.Base(), header) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 146 | installPath := installDir.Join(ctx, header.Base()) |
| 147 | if installPath != installedPath { |
| 148 | panic(fmt.Sprintf( |
| 149 | "expected header install path (%q) not equal to actual install path %q", |
| 150 | installPath, installedPath)) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 151 | } |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 152 | m.installPaths = append(m.installPaths, installPath) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | if len(m.installPaths) == 0 { |
| 156 | ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs) |
| 157 | } |
| 158 | } |
| 159 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 160 | func ndkHeadersFactory() android.Module { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 161 | module := &headerModule{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 162 | module.AddProperties(&module.properties) |
| 163 | android.InitAndroidModule(module) |
| 164 | return module |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 165 | } |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 166 | |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 167 | type versionedHeaderProperties struct { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 168 | // Base directory of the headers being installed. As an example: |
| 169 | // |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 170 | // versioned_ndk_headers { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 171 | // name: "foo", |
| 172 | // from: "include", |
| 173 | // to: "", |
| 174 | // } |
| 175 | // |
| 176 | // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead |
| 177 | // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h. |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 178 | From *string |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 179 | |
| 180 | // Install path within the sysroot. This is relative to usr/include. |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 181 | To *string |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 182 | |
| 183 | // Path to the NOTICE file associated with the headers. |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 184 | License *string |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Like ndk_headers, but preprocesses the headers with the bionic versioner: |
| 188 | // https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md. |
| 189 | // |
| 190 | // Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the |
| 191 | // module does not have the srcs property, and operates on a full directory (the `from` property). |
| 192 | // |
| 193 | // Note that this is really only built to handle bionic/libc/include. |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 194 | type versionedHeaderModule struct { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 195 | android.ModuleBase |
| 196 | |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 197 | properties versionedHeaderProperties |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 198 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 199 | installPaths android.Paths |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 200 | licensePath android.ModuleSrcPath |
| 201 | } |
| 202 | |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 203 | func (m *versionedHeaderModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 206 | func (m *versionedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 207 | if String(m.properties.License) == "" { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 208 | ctx.PropertyErrorf("license", "field is required") |
| 209 | } |
| 210 | |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 211 | m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License)) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 212 | |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 213 | fromSrcPath := android.PathForModuleSrc(ctx, String(m.properties.From)) |
| 214 | toOutputPath := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To)) |
Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 215 | srcFiles := ctx.GlobFiles(filepath.Join(fromSrcPath.String(), "**/*.h"), nil) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 216 | var installPaths []android.WritablePath |
| 217 | for _, header := range srcFiles { |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 218 | installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), String(m.properties.To)) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 219 | installPath := installDir.Join(ctx, header.Base()) |
| 220 | installPaths = append(installPaths, installPath) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 221 | m.installPaths = append(m.installPaths, installPath) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | if len(m.installPaths) == 0 { |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 225 | ctx.ModuleErrorf("glob %q matched zero files", String(m.properties.From)) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 228 | processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, srcFiles, installPaths) |
| 229 | } |
| 230 | |
| 231 | func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path, srcFiles android.Paths, installPaths []android.WritablePath) android.Path { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 232 | // The versioner depends on a dependencies directory to simplify determining include paths |
| 233 | // when parsing headers. This directory contains architecture specific directories as well |
| 234 | // as a common directory, each of which contains symlinks to the actually directories to |
| 235 | // be included. |
| 236 | // |
| 237 | // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly |
| 238 | // depend on these headers. |
| 239 | // TODO(http://b/35673191): Update the versioner to use a --sysroot. |
| 240 | depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies") |
| 241 | depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil) |
| 242 | for i, path := range depsGlob { |
| 243 | fileInfo, err := os.Lstat(path.String()) |
| 244 | if err != nil { |
| 245 | ctx.ModuleErrorf("os.Lstat(%q) failed: %s", path.String, err) |
| 246 | } |
| 247 | if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink { |
| 248 | dest, err := os.Readlink(path.String()) |
| 249 | if err != nil { |
| 250 | ctx.ModuleErrorf("os.Readlink(%q) failed: %s", |
| 251 | path.String, err) |
| 252 | } |
| 253 | // Additional .. to account for the symlink itself. |
| 254 | depsGlob[i] = android.PathForSource( |
| 255 | ctx, filepath.Clean(filepath.Join(path.String(), "..", dest))) |
| 256 | } |
| 257 | } |
| 258 | |
Dan Albert | d2130a9 | 2017-03-29 18:33:28 -0700 | [diff] [blame] | 259 | timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp") |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 260 | ctx.Build(pctx, android.BuildParams{ |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 261 | Rule: versionBionicHeaders, |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 262 | Description: "versioner preprocess " + srcDir.Rel(), |
Dan Albert | d2130a9 | 2017-03-29 18:33:28 -0700 | [diff] [blame] | 263 | Output: timestampFile, |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 264 | Implicits: append(srcFiles, depsGlob...), |
| 265 | ImplicitOutputs: installPaths, |
| 266 | Args: map[string]string{ |
| 267 | "depsPath": depsPath.String(), |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 268 | "srcDir": srcDir.String(), |
| 269 | "outDir": outDir.String(), |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 270 | }, |
| 271 | }) |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 272 | |
| 273 | return timestampFile |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Dan Albert | 97f9c96 | 2018-05-24 15:02:16 -0700 | [diff] [blame] | 276 | func versionedNdkHeadersFactory() android.Module { |
| 277 | module := &versionedHeaderModule{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 278 | |
| 279 | module.AddProperties(&module.properties) |
| 280 | |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 281 | // Host module rather than device module because device module install steps |
| 282 | // do not get run when embedded in make. We're not any of the existing |
| 283 | // module types that can be exposed via the Android.mk exporter, so just use |
| 284 | // a host module. |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 285 | android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibFirst) |
| 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 | |
| 290 | // preprocessed_ndk_header { |
| 291 | // name: "foo", |
| 292 | // preprocessor: "foo.sh", |
| 293 | // srcs: [...], |
| 294 | // to: "android", |
| 295 | // } |
| 296 | // |
| 297 | // Will invoke the preprocessor as: |
| 298 | // $preprocessor -o $SYSROOT/usr/include/android/needs_preproc.h $src |
| 299 | // For each src in srcs. |
| 300 | type preprocessedHeadersProperties struct { |
| 301 | // The preprocessor to run. Must be a program inside the source directory |
| 302 | // with no dependencies. |
| 303 | Preprocessor *string |
| 304 | |
| 305 | // Source path to the files to be preprocessed. |
| 306 | Srcs []string |
| 307 | |
| 308 | // Source paths that should be excluded from the srcs glob. |
| 309 | Exclude_srcs []string |
| 310 | |
| 311 | // Install path within the sysroot. This is relative to usr/include. |
| 312 | To *string |
| 313 | |
| 314 | // Path to the NOTICE file associated with the headers. |
| 315 | License *string |
| 316 | } |
| 317 | |
| 318 | type preprocessedHeadersModule struct { |
| 319 | android.ModuleBase |
| 320 | |
| 321 | properties preprocessedHeadersProperties |
| 322 | |
| 323 | installPaths android.Paths |
| 324 | licensePath android.ModuleSrcPath |
| 325 | } |
| 326 | |
| 327 | func (m *preprocessedHeadersModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 328 | } |
| 329 | |
| 330 | func (m *preprocessedHeadersModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 331 | if String(m.properties.License) == "" { |
| 332 | ctx.PropertyErrorf("license", "field is required") |
| 333 | } |
| 334 | |
| 335 | preprocessor := android.PathForModuleSrc(ctx, String(m.properties.Preprocessor)) |
| 336 | m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License)) |
| 337 | |
| 338 | srcFiles := ctx.ExpandSources(m.properties.Srcs, m.properties.Exclude_srcs) |
| 339 | installDir := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To)) |
| 340 | for _, src := range srcFiles { |
| 341 | installPath := installDir.Join(ctx, src.Base()) |
| 342 | m.installPaths = append(m.installPaths, installPath) |
| 343 | |
| 344 | ctx.Build(pctx, android.BuildParams{ |
| 345 | Rule: preprocessNdkHeader, |
| 346 | Description: "preprocess " + src.Rel(), |
| 347 | Input: src, |
| 348 | Output: installPath, |
| 349 | Args: map[string]string{ |
| 350 | "preprocessor": preprocessor.String(), |
| 351 | }, |
| 352 | }) |
| 353 | } |
| 354 | |
| 355 | if len(m.installPaths) == 0 { |
| 356 | ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | func preprocessedNdkHeadersFactory() android.Module { |
| 361 | module := &preprocessedHeadersModule{} |
| 362 | |
| 363 | module.AddProperties(&module.properties) |
| 364 | |
| 365 | // Host module rather than device module because device module install steps |
| 366 | // do not get run when embedded in make. We're not any of the existing |
| 367 | // module types that can be exposed via the Android.mk exporter, so just use |
| 368 | // a host module. |
| 369 | android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibFirst) |
| 370 | |
| 371 | return module |
| 372 | } |