blob: 504a6a0ea9486d89cc4f250cbffb9ebe439bb782 [file] [log] [blame]
Dan Albert914449f2016-06-17 16:45:24 -07001// 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
15package cc
16
17import (
Dan Albert269fab82017-02-15 17:31:33 -080018 "fmt"
19 "os"
Dan Albert914449f2016-06-17 16:45:24 -070020 "path/filepath"
Lazar Trsiccdb710f2017-11-24 16:38:47 +010021 "strings"
Dan Albert914449f2016-06-17 16:45:24 -070022
23 "github.com/google/blueprint"
24
25 "android/soong/android"
26)
27
Dan Albert269fab82017-02-15 17:31:33 -080028var (
Dan Albert97f9c962018-05-24 15:02:16 -070029 versionBionicHeaders = pctx.AndroidStaticRule("versionBionicHeaders",
Dan Albert269fab82017-02-15 17:31:33 -080030 blueprint.RuleParams{
Dan Albertd2130a92017-03-29 18:33:28 -070031 // 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 Albert269fab82017-02-15 17:31:33 -080034 CommandDeps: []string{"$versionerCmd"},
Dan Albert269fab82017-02-15 17:31:33 -080035 },
Dan Albertd2130a92017-03-29 18:33:28 -070036 "depsPath", "srcDir", "outDir")
Dan Albertcb1b4b22018-05-24 15:06:11 -070037
38 preprocessNdkHeader = pctx.AndroidStaticRule("preprocessNdkHeader",
39 blueprint.RuleParams{
40 Command: "$preprocessor -o $out $in",
41 CommandDeps: []string{"$preprocessor"},
42 },
43 "preprocessor")
Dan Albert269fab82017-02-15 17:31:33 -080044)
45
46func init() {
Logan Chien2f066352018-10-25 18:11:09 +080047 pctx.SourcePathVariable("versionerCmd", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/versioner")
Dan Albert269fab82017-02-15 17:31:33 -080048}
49
Dan Albert914449f2016-06-17 16:45:24 -070050// Returns the NDK base include path for use with sdk_version current. Usable with -I.
51func getCurrentIncludePath(ctx android.ModuleContext) android.OutputPath {
52 return getNdkSysrootBase(ctx).Join(ctx, "usr/include")
53}
54
Dan Albert71222052018-05-24 15:00:05 -070055type headerProperties struct {
Dan Albert914449f2016-06-17 16:45:24 -070056 // 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 Zhang0007d812017-11-07 10:57:05 -080067 From *string
Dan Albert914449f2016-06-17 16:45:24 -070068
69 // Install path within the sysroot. This is relative to usr/include.
Nan Zhang0007d812017-11-07 10:57:05 -080070 To *string
Dan Albert914449f2016-06-17 16:45:24 -070071
72 // List of headers to install. Glob compatible. Common case is "include/**/*.h".
73 Srcs []string
Dan Albertc6345fb2016-10-20 01:36:11 -070074
Dan Albert19ff8b42018-05-24 15:00:48 -070075 // Source paths that should be excluded from the srcs glob.
76 Exclude_srcs []string
77
Dan Albertc6345fb2016-10-20 01:36:11 -070078 // Path to the NOTICE file associated with the headers.
Nan Zhang0007d812017-11-07 10:57:05 -080079 License *string
Dan Albert23d37e02018-11-28 08:30:10 -080080
81 // True if this API is not yet ready to be shipped in the NDK. It will be
82 // available in the platform for testing, but will be excluded from the
83 // sysroot provided to the NDK proper.
84 Draft bool
Dan Albert914449f2016-06-17 16:45:24 -070085}
86
87type headerModule struct {
88 android.ModuleBase
89
Dan Albert71222052018-05-24 15:00:05 -070090 properties headerProperties
Dan Albert914449f2016-06-17 16:45:24 -070091
Colin Cross0875c522017-11-28 17:34:01 -080092 installPaths android.Paths
Dan Albertc6345fb2016-10-20 01:36:11 -070093 licensePath android.ModuleSrcPath
Dan Albert914449f2016-06-17 16:45:24 -070094}
95
Colin Cross1e676be2016-10-12 14:38:15 -070096func (m *headerModule) DepsMutator(ctx android.BottomUpMutatorContext) {
97}
98
Dan Albert269fab82017-02-15 17:31:33 -080099func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string,
100 to string) android.OutputPath {
101 // Output path is the sysroot base + "usr/include" + to directory + directory component
102 // of the file without the leading from directory stripped.
103 //
104 // Given:
105 // sysroot base = "ndk/sysroot"
106 // from = "include/foo"
107 // to = "bar"
108 // header = "include/foo/woodly/doodly.h"
109 // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
110
111 // full/platform/path/to/include/foo
112 fullFromPath := android.PathForModuleSrc(ctx, from)
113
114 // full/platform/path/to/include/foo/woodly
115 headerDir := filepath.Dir(header.String())
116
117 // woodly
118 strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
119 if err != nil {
120 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
121 fullFromPath.String(), err)
122 }
123
124 // full/platform/path/to/sysroot/usr/include/bar/woodly
125 installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir)
126
127 // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
128 return installDir
129}
130
Dan Albert914449f2016-06-17 16:45:24 -0700131func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhang0007d812017-11-07 10:57:05 -0800132 if String(m.properties.License) == "" {
Dan Albertc6345fb2016-10-20 01:36:11 -0700133 ctx.PropertyErrorf("license", "field is required")
134 }
135
Nan Zhang0007d812017-11-07 10:57:05 -0800136 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albertc6345fb2016-10-20 01:36:11 -0700137
Lazar Trsiccdb710f2017-11-24 16:38:47 +0100138 // When generating NDK prebuilts, skip installing MIPS headers,
139 // but keep them when doing regular platform build.
140 // Ndk_abis property is only set to true with build/soong/scripts/build-ndk-prebuilts.sh
141 // TODO: Revert this once MIPS is supported in NDK again.
Colin Cross395f2cf2018-10-24 16:10:32 -0700142 if ctx.Config().NdkAbis() && strings.Contains(ctx.ModuleName(), "mips") {
Lazar Trsiccdb710f2017-11-24 16:38:47 +0100143 return
144 }
145
Dan Albert19ff8b42018-05-24 15:00:48 -0700146 srcFiles := ctx.ExpandSources(m.properties.Srcs, m.properties.Exclude_srcs)
Dan Albert914449f2016-06-17 16:45:24 -0700147 for _, header := range srcFiles {
Nan Zhang0007d812017-11-07 10:57:05 -0800148 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From),
149 String(m.properties.To))
Colin Cross5c517922017-08-31 12:29:17 -0700150 installedPath := ctx.InstallFile(installDir, header.Base(), header)
Dan Albert269fab82017-02-15 17:31:33 -0800151 installPath := installDir.Join(ctx, header.Base())
152 if installPath != installedPath {
153 panic(fmt.Sprintf(
154 "expected header install path (%q) not equal to actual install path %q",
155 installPath, installedPath))
Dan Albert914449f2016-06-17 16:45:24 -0700156 }
Colin Cross0875c522017-11-28 17:34:01 -0800157 m.installPaths = append(m.installPaths, installPath)
Dan Albert914449f2016-06-17 16:45:24 -0700158 }
159
160 if len(m.installPaths) == 0 {
161 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
162 }
163}
164
Colin Cross36242852017-06-23 15:06:31 -0700165func ndkHeadersFactory() android.Module {
Dan Albert914449f2016-06-17 16:45:24 -0700166 module := &headerModule{}
Colin Cross36242852017-06-23 15:06:31 -0700167 module.AddProperties(&module.properties)
168 android.InitAndroidModule(module)
169 return module
Dan Albert914449f2016-06-17 16:45:24 -0700170}
Dan Albert269fab82017-02-15 17:31:33 -0800171
Dan Albert97f9c962018-05-24 15:02:16 -0700172type versionedHeaderProperties struct {
Dan Albert269fab82017-02-15 17:31:33 -0800173 // Base directory of the headers being installed. As an example:
174 //
Dan Albert97f9c962018-05-24 15:02:16 -0700175 // versioned_ndk_headers {
Dan Albert269fab82017-02-15 17:31:33 -0800176 // name: "foo",
177 // from: "include",
178 // to: "",
179 // }
180 //
181 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
182 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800183 From *string
Dan Albert269fab82017-02-15 17:31:33 -0800184
185 // Install path within the sysroot. This is relative to usr/include.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800186 To *string
Dan Albert269fab82017-02-15 17:31:33 -0800187
188 // Path to the NOTICE file associated with the headers.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800189 License *string
Dan Albert23d37e02018-11-28 08:30:10 -0800190
191 // True if this API is not yet ready to be shipped in the NDK. It will be
192 // available in the platform for testing, but will be excluded from the
193 // sysroot provided to the NDK proper.
194 Draft bool
Dan Albert269fab82017-02-15 17:31:33 -0800195}
196
197// Like ndk_headers, but preprocesses the headers with the bionic versioner:
198// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
199//
200// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
201// module does not have the srcs property, and operates on a full directory (the `from` property).
202//
203// Note that this is really only built to handle bionic/libc/include.
Dan Albert97f9c962018-05-24 15:02:16 -0700204type versionedHeaderModule struct {
Dan Albert269fab82017-02-15 17:31:33 -0800205 android.ModuleBase
206
Dan Albert97f9c962018-05-24 15:02:16 -0700207 properties versionedHeaderProperties
Dan Albert269fab82017-02-15 17:31:33 -0800208
Colin Cross0875c522017-11-28 17:34:01 -0800209 installPaths android.Paths
Dan Albert269fab82017-02-15 17:31:33 -0800210 licensePath android.ModuleSrcPath
211}
212
Dan Albert97f9c962018-05-24 15:02:16 -0700213func (m *versionedHeaderModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Albert269fab82017-02-15 17:31:33 -0800214}
215
Dan Albert97f9c962018-05-24 15:02:16 -0700216func (m *versionedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800217 if String(m.properties.License) == "" {
Dan Albert269fab82017-02-15 17:31:33 -0800218 ctx.PropertyErrorf("license", "field is required")
219 }
220
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800221 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albert269fab82017-02-15 17:31:33 -0800222
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800223 fromSrcPath := android.PathForModuleSrc(ctx, String(m.properties.From))
224 toOutputPath := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Dan Willemsen540a78c2018-02-26 21:50:08 -0800225 srcFiles := ctx.GlobFiles(filepath.Join(fromSrcPath.String(), "**/*.h"), nil)
Dan Albert269fab82017-02-15 17:31:33 -0800226 var installPaths []android.WritablePath
227 for _, header := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800228 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), String(m.properties.To))
Dan Albert269fab82017-02-15 17:31:33 -0800229 installPath := installDir.Join(ctx, header.Base())
230 installPaths = append(installPaths, installPath)
Colin Cross0875c522017-11-28 17:34:01 -0800231 m.installPaths = append(m.installPaths, installPath)
Dan Albert269fab82017-02-15 17:31:33 -0800232 }
233
234 if len(m.installPaths) == 0 {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800235 ctx.ModuleErrorf("glob %q matched zero files", String(m.properties.From))
Dan Albert269fab82017-02-15 17:31:33 -0800236 }
237
Dan Willemsenb916b802017-03-19 13:44:32 -0700238 processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, srcFiles, installPaths)
239}
240
241func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path, srcFiles android.Paths, installPaths []android.WritablePath) android.Path {
Dan Albert269fab82017-02-15 17:31:33 -0800242 // The versioner depends on a dependencies directory to simplify determining include paths
243 // when parsing headers. This directory contains architecture specific directories as well
244 // as a common directory, each of which contains symlinks to the actually directories to
245 // be included.
246 //
247 // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
248 // depend on these headers.
249 // TODO(http://b/35673191): Update the versioner to use a --sysroot.
250 depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
251 depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
252 for i, path := range depsGlob {
253 fileInfo, err := os.Lstat(path.String())
254 if err != nil {
255 ctx.ModuleErrorf("os.Lstat(%q) failed: %s", path.String, err)
256 }
257 if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
258 dest, err := os.Readlink(path.String())
259 if err != nil {
260 ctx.ModuleErrorf("os.Readlink(%q) failed: %s",
261 path.String, err)
262 }
263 // Additional .. to account for the symlink itself.
264 depsGlob[i] = android.PathForSource(
265 ctx, filepath.Clean(filepath.Join(path.String(), "..", dest)))
266 }
267 }
268
Dan Albertd2130a92017-03-29 18:33:28 -0700269 timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp")
Colin Crossae887032017-10-23 17:16:14 -0700270 ctx.Build(pctx, android.BuildParams{
Dan Albert97f9c962018-05-24 15:02:16 -0700271 Rule: versionBionicHeaders,
Colin Cross67a5c132017-05-09 13:45:28 -0700272 Description: "versioner preprocess " + srcDir.Rel(),
Dan Albertd2130a92017-03-29 18:33:28 -0700273 Output: timestampFile,
Dan Albert269fab82017-02-15 17:31:33 -0800274 Implicits: append(srcFiles, depsGlob...),
275 ImplicitOutputs: installPaths,
276 Args: map[string]string{
277 "depsPath": depsPath.String(),
Dan Willemsenb916b802017-03-19 13:44:32 -0700278 "srcDir": srcDir.String(),
279 "outDir": outDir.String(),
Dan Albert269fab82017-02-15 17:31:33 -0800280 },
281 })
Dan Willemsenb916b802017-03-19 13:44:32 -0700282
283 return timestampFile
Dan Albert269fab82017-02-15 17:31:33 -0800284}
285
Dan Albert97f9c962018-05-24 15:02:16 -0700286func versionedNdkHeadersFactory() android.Module {
287 module := &versionedHeaderModule{}
Colin Cross36242852017-06-23 15:06:31 -0700288
289 module.AddProperties(&module.properties)
290
Dan Willemsen4f644da2018-10-10 17:18:08 -0700291 android.InitAndroidModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700292
293 return module
Dan Albert269fab82017-02-15 17:31:33 -0800294}
Dan Albertcb1b4b22018-05-24 15:06:11 -0700295
296// preprocessed_ndk_header {
297// name: "foo",
298// preprocessor: "foo.sh",
299// srcs: [...],
300// to: "android",
301// }
302//
303// Will invoke the preprocessor as:
304// $preprocessor -o $SYSROOT/usr/include/android/needs_preproc.h $src
305// For each src in srcs.
306type preprocessedHeadersProperties struct {
307 // The preprocessor to run. Must be a program inside the source directory
308 // with no dependencies.
309 Preprocessor *string
310
311 // Source path to the files to be preprocessed.
312 Srcs []string
313
314 // Source paths that should be excluded from the srcs glob.
315 Exclude_srcs []string
316
317 // Install path within the sysroot. This is relative to usr/include.
318 To *string
319
320 // Path to the NOTICE file associated with the headers.
321 License *string
Dan Albert23d37e02018-11-28 08:30:10 -0800322
323 // True if this API is not yet ready to be shipped in the NDK. It will be
324 // available in the platform for testing, but will be excluded from the
325 // sysroot provided to the NDK proper.
326 Draft bool
Dan Albertcb1b4b22018-05-24 15:06:11 -0700327}
328
329type preprocessedHeadersModule struct {
330 android.ModuleBase
331
332 properties preprocessedHeadersProperties
333
334 installPaths android.Paths
335 licensePath android.ModuleSrcPath
336}
337
338func (m *preprocessedHeadersModule) DepsMutator(ctx android.BottomUpMutatorContext) {
339}
340
341func (m *preprocessedHeadersModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
342 if String(m.properties.License) == "" {
343 ctx.PropertyErrorf("license", "field is required")
344 }
345
346 preprocessor := android.PathForModuleSrc(ctx, String(m.properties.Preprocessor))
347 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
348
349 srcFiles := ctx.ExpandSources(m.properties.Srcs, m.properties.Exclude_srcs)
350 installDir := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
351 for _, src := range srcFiles {
352 installPath := installDir.Join(ctx, src.Base())
353 m.installPaths = append(m.installPaths, installPath)
354
355 ctx.Build(pctx, android.BuildParams{
356 Rule: preprocessNdkHeader,
357 Description: "preprocess " + src.Rel(),
358 Input: src,
359 Output: installPath,
360 Args: map[string]string{
361 "preprocessor": preprocessor.String(),
362 },
363 })
364 }
365
366 if len(m.installPaths) == 0 {
367 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
368 }
369}
370
371func preprocessedNdkHeadersFactory() android.Module {
372 module := &preprocessedHeadersModule{}
373
374 module.AddProperties(&module.properties)
375
Dan Willemsen4f644da2018-10-10 17:18:08 -0700376 android.InitAndroidModule(module)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700377
378 return module
379}