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 ( |
| 29 | preprocessBionicHeaders = pctx.AndroidStaticRule("preprocessBionicHeaders", |
| 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 | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 37 | ) |
| 38 | |
| 39 | func init() { |
| 40 | pctx.HostBinToolVariable("versionerCmd", "versioner") |
| 41 | } |
| 42 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 43 | // Returns the NDK base include path for use with sdk_version current. Usable with -I. |
| 44 | func getCurrentIncludePath(ctx android.ModuleContext) android.OutputPath { |
| 45 | return getNdkSysrootBase(ctx).Join(ctx, "usr/include") |
| 46 | } |
| 47 | |
Dan Albert | 7122205 | 2018-05-24 15:00:05 -0700 | [diff] [blame] | 48 | type headerProperties struct { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 49 | // Base directory of the headers being installed. As an example: |
| 50 | // |
| 51 | // ndk_headers { |
| 52 | // name: "foo", |
| 53 | // from: "include", |
| 54 | // to: "", |
| 55 | // srcs: ["include/foo/bar/baz.h"], |
| 56 | // } |
| 57 | // |
| 58 | // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead |
| 59 | // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 60 | From *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 61 | |
| 62 | // Install path within the sysroot. This is relative to usr/include. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 63 | To *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 64 | |
| 65 | // List of headers to install. Glob compatible. Common case is "include/**/*.h". |
| 66 | Srcs []string |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 67 | |
Dan Albert | 19ff8b4 | 2018-05-24 15:00:48 -0700 | [diff] [blame^] | 68 | // Source paths that should be excluded from the srcs glob. |
| 69 | Exclude_srcs []string |
| 70 | |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 71 | // Path to the NOTICE file associated with the headers. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 72 | License *string |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | type headerModule struct { |
| 76 | android.ModuleBase |
| 77 | |
Dan Albert | 7122205 | 2018-05-24 15:00:05 -0700 | [diff] [blame] | 78 | properties headerProperties |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 79 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 80 | installPaths android.Paths |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 81 | licensePath android.ModuleSrcPath |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 84 | func (m *headerModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 85 | } |
| 86 | |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 87 | func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string, |
| 88 | to string) android.OutputPath { |
| 89 | // Output path is the sysroot base + "usr/include" + to directory + directory component |
| 90 | // of the file without the leading from directory stripped. |
| 91 | // |
| 92 | // Given: |
| 93 | // sysroot base = "ndk/sysroot" |
| 94 | // from = "include/foo" |
| 95 | // to = "bar" |
| 96 | // header = "include/foo/woodly/doodly.h" |
| 97 | // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h" |
| 98 | |
| 99 | // full/platform/path/to/include/foo |
| 100 | fullFromPath := android.PathForModuleSrc(ctx, from) |
| 101 | |
| 102 | // full/platform/path/to/include/foo/woodly |
| 103 | headerDir := filepath.Dir(header.String()) |
| 104 | |
| 105 | // woodly |
| 106 | strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir) |
| 107 | if err != nil { |
| 108 | ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir, |
| 109 | fullFromPath.String(), err) |
| 110 | } |
| 111 | |
| 112 | // full/platform/path/to/sysroot/usr/include/bar/woodly |
| 113 | installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir) |
| 114 | |
| 115 | // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h |
| 116 | return installDir |
| 117 | } |
| 118 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 119 | func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 120 | if String(m.properties.License) == "" { |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 121 | ctx.PropertyErrorf("license", "field is required") |
| 122 | } |
| 123 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 124 | m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License)) |
Dan Albert | c6345fb | 2016-10-20 01:36:11 -0700 | [diff] [blame] | 125 | |
Lazar Trsic | cdb710f | 2017-11-24 16:38:47 +0100 | [diff] [blame] | 126 | // When generating NDK prebuilts, skip installing MIPS headers, |
| 127 | // but keep them when doing regular platform build. |
| 128 | // Ndk_abis property is only set to true with build/soong/scripts/build-ndk-prebuilts.sh |
| 129 | // TODO: Revert this once MIPS is supported in NDK again. |
| 130 | if Bool(ctx.AConfig().Ndk_abis) && strings.Contains(ctx.ModuleName(), "mips") { |
| 131 | return |
| 132 | } |
| 133 | |
Dan Albert | 19ff8b4 | 2018-05-24 15:00:48 -0700 | [diff] [blame^] | 134 | srcFiles := ctx.ExpandSources(m.properties.Srcs, m.properties.Exclude_srcs) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 135 | for _, header := range srcFiles { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 136 | installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), |
| 137 | String(m.properties.To)) |
Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 138 | installedPath := ctx.InstallFile(installDir, header.Base(), header) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 139 | installPath := installDir.Join(ctx, header.Base()) |
| 140 | if installPath != installedPath { |
| 141 | panic(fmt.Sprintf( |
| 142 | "expected header install path (%q) not equal to actual install path %q", |
| 143 | installPath, installedPath)) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 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 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 153 | func ndkHeadersFactory() android.Module { |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 154 | module := &headerModule{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 155 | module.AddProperties(&module.properties) |
| 156 | android.InitAndroidModule(module) |
| 157 | return module |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 158 | } |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 159 | |
Dan Albert | 7122205 | 2018-05-24 15:00:05 -0700 | [diff] [blame] | 160 | type preprocessedHeaderProperties struct { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 161 | // Base directory of the headers being installed. As an example: |
| 162 | // |
| 163 | // preprocessed_ndk_headers { |
| 164 | // name: "foo", |
| 165 | // from: "include", |
| 166 | // to: "", |
| 167 | // } |
| 168 | // |
| 169 | // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead |
| 170 | // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h. |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 171 | From *string |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 172 | |
| 173 | // Install path within the sysroot. This is relative to usr/include. |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 174 | To *string |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 175 | |
| 176 | // Path to the NOTICE file associated with the headers. |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 177 | License *string |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Like ndk_headers, but preprocesses the headers with the bionic versioner: |
| 181 | // https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md. |
| 182 | // |
| 183 | // Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the |
| 184 | // module does not have the srcs property, and operates on a full directory (the `from` property). |
| 185 | // |
| 186 | // Note that this is really only built to handle bionic/libc/include. |
| 187 | type preprocessedHeaderModule struct { |
| 188 | android.ModuleBase |
| 189 | |
Dan Albert | 7122205 | 2018-05-24 15:00:05 -0700 | [diff] [blame] | 190 | properties preprocessedHeaderProperties |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 191 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 192 | installPaths android.Paths |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 193 | licensePath android.ModuleSrcPath |
| 194 | } |
| 195 | |
| 196 | func (m *preprocessedHeaderModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 197 | } |
| 198 | |
| 199 | func (m *preprocessedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 200 | if String(m.properties.License) == "" { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 201 | ctx.PropertyErrorf("license", "field is required") |
| 202 | } |
| 203 | |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 204 | m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License)) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 205 | |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 206 | fromSrcPath := android.PathForModuleSrc(ctx, String(m.properties.From)) |
| 207 | toOutputPath := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To)) |
Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 208 | srcFiles := ctx.GlobFiles(filepath.Join(fromSrcPath.String(), "**/*.h"), nil) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 209 | var installPaths []android.WritablePath |
| 210 | for _, header := range srcFiles { |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 211 | installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), String(m.properties.To)) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 212 | installPath := installDir.Join(ctx, header.Base()) |
| 213 | installPaths = append(installPaths, installPath) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 214 | m.installPaths = append(m.installPaths, installPath) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | if len(m.installPaths) == 0 { |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 218 | ctx.ModuleErrorf("glob %q matched zero files", String(m.properties.From)) |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 221 | processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, srcFiles, installPaths) |
| 222 | } |
| 223 | |
| 224 | 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] | 225 | // The versioner depends on a dependencies directory to simplify determining include paths |
| 226 | // when parsing headers. This directory contains architecture specific directories as well |
| 227 | // as a common directory, each of which contains symlinks to the actually directories to |
| 228 | // be included. |
| 229 | // |
| 230 | // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly |
| 231 | // depend on these headers. |
| 232 | // TODO(http://b/35673191): Update the versioner to use a --sysroot. |
| 233 | depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies") |
| 234 | depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil) |
| 235 | for i, path := range depsGlob { |
| 236 | fileInfo, err := os.Lstat(path.String()) |
| 237 | if err != nil { |
| 238 | ctx.ModuleErrorf("os.Lstat(%q) failed: %s", path.String, err) |
| 239 | } |
| 240 | if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink { |
| 241 | dest, err := os.Readlink(path.String()) |
| 242 | if err != nil { |
| 243 | ctx.ModuleErrorf("os.Readlink(%q) failed: %s", |
| 244 | path.String, err) |
| 245 | } |
| 246 | // Additional .. to account for the symlink itself. |
| 247 | depsGlob[i] = android.PathForSource( |
| 248 | ctx, filepath.Clean(filepath.Join(path.String(), "..", dest))) |
| 249 | } |
| 250 | } |
| 251 | |
Dan Albert | d2130a9 | 2017-03-29 18:33:28 -0700 | [diff] [blame] | 252 | timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp") |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 253 | ctx.Build(pctx, android.BuildParams{ |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 254 | Rule: preprocessBionicHeaders, |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 255 | Description: "versioner preprocess " + srcDir.Rel(), |
Dan Albert | d2130a9 | 2017-03-29 18:33:28 -0700 | [diff] [blame] | 256 | Output: timestampFile, |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 257 | Implicits: append(srcFiles, depsGlob...), |
| 258 | ImplicitOutputs: installPaths, |
| 259 | Args: map[string]string{ |
| 260 | "depsPath": depsPath.String(), |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 261 | "srcDir": srcDir.String(), |
| 262 | "outDir": outDir.String(), |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 263 | }, |
| 264 | }) |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 265 | |
| 266 | return timestampFile |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 269 | func preprocessedNdkHeadersFactory() android.Module { |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 270 | module := &preprocessedHeaderModule{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 271 | |
| 272 | module.AddProperties(&module.properties) |
| 273 | |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 274 | // Host module rather than device module because device module install steps |
| 275 | // do not get run when embedded in make. We're not any of the existing |
| 276 | // module types that can be exposed via the Android.mk exporter, so just use |
| 277 | // a host module. |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 278 | android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibFirst) |
| 279 | |
| 280 | return module |
Dan Albert | 269fab8 | 2017-02-15 17:31:33 -0800 | [diff] [blame] | 281 | } |