blob: 5744bb285fff41582ab60b188436ba29e7bff31a [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"
Dan Albert914449f2016-06-17 16:45:24 -070019 "path/filepath"
Lazar Trsiccdb710f2017-11-24 16:38:47 +010020 "strings"
Dan Albert914449f2016-06-17 16:45:24 -070021
22 "github.com/google/blueprint"
23
24 "android/soong/android"
25)
26
Dan Albert269fab82017-02-15 17:31:33 -080027var (
Dan Albert97f9c962018-05-24 15:02:16 -070028 versionBionicHeaders = pctx.AndroidStaticRule("versionBionicHeaders",
Dan Albert269fab82017-02-15 17:31:33 -080029 blueprint.RuleParams{
Dan Albertd2130a92017-03-29 18:33:28 -070030 // 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 Albert269fab82017-02-15 17:31:33 -080033 CommandDeps: []string{"$versionerCmd"},
Dan Albert269fab82017-02-15 17:31:33 -080034 },
Dan Albertd2130a92017-03-29 18:33:28 -070035 "depsPath", "srcDir", "outDir")
Dan Albertcb1b4b22018-05-24 15:06:11 -070036
37 preprocessNdkHeader = pctx.AndroidStaticRule("preprocessNdkHeader",
38 blueprint.RuleParams{
39 Command: "$preprocessor -o $out $in",
40 CommandDeps: []string{"$preprocessor"},
41 },
42 "preprocessor")
Dan Albert269fab82017-02-15 17:31:33 -080043)
44
45func init() {
Logan Chien2f066352018-10-25 18:11:09 +080046 pctx.SourcePathVariable("versionerCmd", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/versioner")
Dan Albert269fab82017-02-15 17:31:33 -080047}
48
Dan Albert914449f2016-06-17 16:45:24 -070049// Returns the NDK base include path for use with sdk_version current. Usable with -I.
Colin Cross70dda7e2019-10-01 22:05:35 -070050func getCurrentIncludePath(ctx android.ModuleContext) android.InstallPath {
Dan Albert914449f2016-06-17 16:45:24 -070051 return getNdkSysrootBase(ctx).Join(ctx, "usr/include")
52}
53
Dan Albert71222052018-05-24 15:00:05 -070054type headerProperties struct {
Dan Albert914449f2016-06-17 16:45:24 -070055 // Base directory of the headers being installed. As an example:
56 //
57 // ndk_headers {
58 // name: "foo",
59 // from: "include",
60 // to: "",
61 // srcs: ["include/foo/bar/baz.h"],
62 // }
63 //
64 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
65 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhang0007d812017-11-07 10:57:05 -080066 From *string
Dan Albert914449f2016-06-17 16:45:24 -070067
68 // Install path within the sysroot. This is relative to usr/include.
Nan Zhang0007d812017-11-07 10:57:05 -080069 To *string
Dan Albert914449f2016-06-17 16:45:24 -070070
71 // List of headers to install. Glob compatible. Common case is "include/**/*.h".
Colin Cross27b922f2019-03-04 22:35:41 -080072 Srcs []string `android:"path"`
Dan Albertc6345fb2016-10-20 01:36:11 -070073
Dan Albert19ff8b42018-05-24 15:00:48 -070074 // Source paths that should be excluded from the srcs glob.
Colin Cross27b922f2019-03-04 22:35:41 -080075 Exclude_srcs []string `android:"path"`
Dan Albert19ff8b42018-05-24 15:00:48 -070076
Dan Albertc6345fb2016-10-20 01:36:11 -070077 // Path to the NOTICE file associated with the headers.
Colin Cross27b922f2019-03-04 22:35:41 -080078 License *string `android:"path"`
Dan Albert23d37e02018-11-28 08:30:10 -080079
80 // True if this API is not yet ready to be shipped in the NDK. It will be
81 // available in the platform for testing, but will be excluded from the
82 // sysroot provided to the NDK proper.
83 Draft bool
Dan Albert914449f2016-06-17 16:45:24 -070084}
85
86type headerModule struct {
87 android.ModuleBase
88
Dan Albert71222052018-05-24 15:00:05 -070089 properties headerProperties
Dan Albert914449f2016-06-17 16:45:24 -070090
Colin Cross0875c522017-11-28 17:34:01 -080091 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -080092 licensePath android.Path
Dan Albert914449f2016-06-17 16:45:24 -070093}
94
Dan Albert269fab82017-02-15 17:31:33 -080095func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string,
Colin Cross70dda7e2019-10-01 22:05:35 -070096 to string) android.InstallPath {
Dan Albert269fab82017-02-15 17:31:33 -080097 // Output path is the sysroot base + "usr/include" + to directory + directory component
98 // of the file without the leading from directory stripped.
99 //
100 // Given:
101 // sysroot base = "ndk/sysroot"
102 // from = "include/foo"
103 // to = "bar"
104 // header = "include/foo/woodly/doodly.h"
105 // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
106
107 // full/platform/path/to/include/foo
108 fullFromPath := android.PathForModuleSrc(ctx, from)
109
110 // full/platform/path/to/include/foo/woodly
111 headerDir := filepath.Dir(header.String())
112
113 // woodly
114 strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
115 if err != nil {
116 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
117 fullFromPath.String(), err)
118 }
119
120 // full/platform/path/to/sysroot/usr/include/bar/woodly
121 installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir)
122
123 // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
124 return installDir
125}
126
Dan Albert914449f2016-06-17 16:45:24 -0700127func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhang0007d812017-11-07 10:57:05 -0800128 if String(m.properties.License) == "" {
Dan Albertc6345fb2016-10-20 01:36:11 -0700129 ctx.PropertyErrorf("license", "field is required")
130 }
131
Nan Zhang0007d812017-11-07 10:57:05 -0800132 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albertc6345fb2016-10-20 01:36:11 -0700133
Lazar Trsiccdb710f2017-11-24 16:38:47 +0100134 // When generating NDK prebuilts, skip installing MIPS headers,
135 // but keep them when doing regular platform build.
136 // Ndk_abis property is only set to true with build/soong/scripts/build-ndk-prebuilts.sh
137 // TODO: Revert this once MIPS is supported in NDK again.
Colin Cross395f2cf2018-10-24 16:10:32 -0700138 if ctx.Config().NdkAbis() && strings.Contains(ctx.ModuleName(), "mips") {
Lazar Trsiccdb710f2017-11-24 16:38:47 +0100139 return
140 }
141
Colin Cross8a497952019-03-05 22:25:09 -0800142 srcFiles := android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
Dan Albert914449f2016-06-17 16:45:24 -0700143 for _, header := range srcFiles {
Nan Zhang0007d812017-11-07 10:57:05 -0800144 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From),
145 String(m.properties.To))
Colin Cross5c517922017-08-31 12:29:17 -0700146 installedPath := ctx.InstallFile(installDir, header.Base(), header)
Dan Albert269fab82017-02-15 17:31:33 -0800147 installPath := installDir.Join(ctx, header.Base())
148 if installPath != installedPath {
149 panic(fmt.Sprintf(
150 "expected header install path (%q) not equal to actual install path %q",
151 installPath, installedPath))
Dan Albert914449f2016-06-17 16:45:24 -0700152 }
Colin Cross0875c522017-11-28 17:34:01 -0800153 m.installPaths = append(m.installPaths, installPath)
Dan Albert914449f2016-06-17 16:45:24 -0700154 }
155
156 if len(m.installPaths) == 0 {
157 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
158 }
159}
160
Patrice Arruda6ea42112019-04-03 08:43:30 -0700161// ndk_headers installs the sets of ndk headers defined in the srcs property
162// to the sysroot base + "usr/include" + to directory + directory component.
163// ndk_headers requires the license file to be specified. Example:
164//
165// Given:
166// sysroot base = "ndk/sysroot"
167// from = "include/foo"
168// to = "bar"
169// header = "include/foo/woodly/doodly.h"
170// output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
Colin Cross36242852017-06-23 15:06:31 -0700171func ndkHeadersFactory() android.Module {
Dan Albert914449f2016-06-17 16:45:24 -0700172 module := &headerModule{}
Colin Cross36242852017-06-23 15:06:31 -0700173 module.AddProperties(&module.properties)
174 android.InitAndroidModule(module)
175 return module
Dan Albert914449f2016-06-17 16:45:24 -0700176}
Dan Albert269fab82017-02-15 17:31:33 -0800177
Dan Albert97f9c962018-05-24 15:02:16 -0700178type versionedHeaderProperties struct {
Dan Albert269fab82017-02-15 17:31:33 -0800179 // Base directory of the headers being installed. As an example:
180 //
Dan Albert97f9c962018-05-24 15:02:16 -0700181 // versioned_ndk_headers {
Dan Albert269fab82017-02-15 17:31:33 -0800182 // name: "foo",
183 // from: "include",
184 // to: "",
185 // }
186 //
187 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
188 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800189 From *string
Dan Albert269fab82017-02-15 17:31:33 -0800190
191 // Install path within the sysroot. This is relative to usr/include.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800192 To *string
Dan Albert269fab82017-02-15 17:31:33 -0800193
194 // Path to the NOTICE file associated with the headers.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800195 License *string
Dan Albert23d37e02018-11-28 08:30:10 -0800196
197 // True if this API is not yet ready to be shipped in the NDK. It will be
198 // available in the platform for testing, but will be excluded from the
199 // sysroot provided to the NDK proper.
200 Draft bool
Dan Albert269fab82017-02-15 17:31:33 -0800201}
202
203// Like ndk_headers, but preprocesses the headers with the bionic versioner:
204// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
205//
206// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
207// module does not have the srcs property, and operates on a full directory (the `from` property).
208//
209// Note that this is really only built to handle bionic/libc/include.
Dan Albert97f9c962018-05-24 15:02:16 -0700210type versionedHeaderModule struct {
Dan Albert269fab82017-02-15 17:31:33 -0800211 android.ModuleBase
212
Dan Albert97f9c962018-05-24 15:02:16 -0700213 properties versionedHeaderProperties
Dan Albert269fab82017-02-15 17:31:33 -0800214
Colin Cross0875c522017-11-28 17:34:01 -0800215 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -0800216 licensePath android.Path
Dan Albert269fab82017-02-15 17:31:33 -0800217}
218
Dan Albert97f9c962018-05-24 15:02:16 -0700219func (m *versionedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800220 if String(m.properties.License) == "" {
Dan Albert269fab82017-02-15 17:31:33 -0800221 ctx.PropertyErrorf("license", "field is required")
222 }
223
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800224 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albert269fab82017-02-15 17:31:33 -0800225
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800226 fromSrcPath := android.PathForModuleSrc(ctx, String(m.properties.From))
227 toOutputPath := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Dan Willemsen540a78c2018-02-26 21:50:08 -0800228 srcFiles := ctx.GlobFiles(filepath.Join(fromSrcPath.String(), "**/*.h"), nil)
Dan Albert269fab82017-02-15 17:31:33 -0800229 var installPaths []android.WritablePath
230 for _, header := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800231 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), String(m.properties.To))
Dan Albert269fab82017-02-15 17:31:33 -0800232 installPath := installDir.Join(ctx, header.Base())
233 installPaths = append(installPaths, installPath)
Colin Cross0875c522017-11-28 17:34:01 -0800234 m.installPaths = append(m.installPaths, installPath)
Dan Albert269fab82017-02-15 17:31:33 -0800235 }
236
237 if len(m.installPaths) == 0 {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800238 ctx.ModuleErrorf("glob %q matched zero files", String(m.properties.From))
Dan Albert269fab82017-02-15 17:31:33 -0800239 }
240
Dan Willemsenb916b802017-03-19 13:44:32 -0700241 processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, srcFiles, installPaths)
242}
243
Colin Cross07e51612019-03-05 12:46:40 -0800244func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path,
245 srcFiles android.Paths, installPaths []android.WritablePath) android.Path {
Dan Albert269fab82017-02-15 17:31:33 -0800246 // The versioner depends on a dependencies directory to simplify determining include paths
247 // when parsing headers. This directory contains architecture specific directories as well
248 // as a common directory, each of which contains symlinks to the actually directories to
249 // be included.
250 //
251 // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
252 // depend on these headers.
253 // TODO(http://b/35673191): Update the versioner to use a --sysroot.
254 depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
255 depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
256 for i, path := range depsGlob {
Colin Cross988414c2020-01-11 01:11:46 +0000257 if ctx.IsSymlink(path) {
258 dest := ctx.Readlink(path)
Dan Albert269fab82017-02-15 17:31:33 -0800259 // Additional .. to account for the symlink itself.
260 depsGlob[i] = android.PathForSource(
261 ctx, filepath.Clean(filepath.Join(path.String(), "..", dest)))
262 }
263 }
264
Dan Albertd2130a92017-03-29 18:33:28 -0700265 timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp")
Colin Crossae887032017-10-23 17:16:14 -0700266 ctx.Build(pctx, android.BuildParams{
Dan Albert97f9c962018-05-24 15:02:16 -0700267 Rule: versionBionicHeaders,
Colin Cross67a5c132017-05-09 13:45:28 -0700268 Description: "versioner preprocess " + srcDir.Rel(),
Dan Albertd2130a92017-03-29 18:33:28 -0700269 Output: timestampFile,
Dan Albert269fab82017-02-15 17:31:33 -0800270 Implicits: append(srcFiles, depsGlob...),
271 ImplicitOutputs: installPaths,
272 Args: map[string]string{
273 "depsPath": depsPath.String(),
Dan Willemsenb916b802017-03-19 13:44:32 -0700274 "srcDir": srcDir.String(),
275 "outDir": outDir.String(),
Dan Albert269fab82017-02-15 17:31:33 -0800276 },
277 })
Dan Willemsenb916b802017-03-19 13:44:32 -0700278
279 return timestampFile
Dan Albert269fab82017-02-15 17:31:33 -0800280}
281
Patrice Arruda6ea42112019-04-03 08:43:30 -0700282// versioned_ndk_headers preprocesses the headers with the bionic versioner:
283// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
284// Unlike the ndk_headers soong module, versioned_ndk_headers operates on a
285// directory level specified in `from` property. This is only used to process
286// the bionic/libc/include directory.
Dan Albert97f9c962018-05-24 15:02:16 -0700287func versionedNdkHeadersFactory() android.Module {
288 module := &versionedHeaderModule{}
Colin Cross36242852017-06-23 15:06:31 -0700289
290 module.AddProperties(&module.properties)
291
Dan Willemsen4f644da2018-10-10 17:18:08 -0700292 android.InitAndroidModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700293
294 return module
Dan Albert269fab82017-02-15 17:31:33 -0800295}
Dan Albertcb1b4b22018-05-24 15:06:11 -0700296
297// preprocessed_ndk_header {
298// name: "foo",
299// preprocessor: "foo.sh",
300// srcs: [...],
301// to: "android",
302// }
303//
304// Will invoke the preprocessor as:
305// $preprocessor -o $SYSROOT/usr/include/android/needs_preproc.h $src
306// For each src in srcs.
307type preprocessedHeadersProperties struct {
308 // The preprocessor to run. Must be a program inside the source directory
309 // with no dependencies.
310 Preprocessor *string
311
312 // Source path to the files to be preprocessed.
313 Srcs []string
314
315 // Source paths that should be excluded from the srcs glob.
316 Exclude_srcs []string
317
318 // Install path within the sysroot. This is relative to usr/include.
319 To *string
320
321 // Path to the NOTICE file associated with the headers.
322 License *string
Dan Albert23d37e02018-11-28 08:30:10 -0800323
324 // True if this API is not yet ready to be shipped in the NDK. It will be
325 // available in the platform for testing, but will be excluded from the
326 // sysroot provided to the NDK proper.
327 Draft bool
Dan Albertcb1b4b22018-05-24 15:06:11 -0700328}
329
330type preprocessedHeadersModule struct {
331 android.ModuleBase
332
333 properties preprocessedHeadersProperties
334
335 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -0800336 licensePath android.Path
Dan Albertcb1b4b22018-05-24 15:06:11 -0700337}
338
Dan Albertcb1b4b22018-05-24 15:06:11 -0700339func (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
Colin Cross8a497952019-03-05 22:25:09 -0800347 srcFiles := android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700348 installDir := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
349 for _, src := range srcFiles {
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 Arruda6ea42112019-04-03 08:43:30 -0700369// preprocessed_ndk_headers preprocesses all the ndk headers listed in the srcs
370// property by executing the command defined in the preprocessor property.
Dan Albertcb1b4b22018-05-24 15:06:11 -0700371func 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}