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