blob: 5e06948806fed5e9cbc78370b35d1aa5a14f640d [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"
20
21 "github.com/google/blueprint"
Spandan Das0773a602022-08-16 00:55:11 +000022 "github.com/google/blueprint/proptools"
Dan Albert914449f2016-06-17 16:45:24 -070023
24 "android/soong/android"
Spandan Das0773a602022-08-16 00:55:11 +000025 "android/soong/bazel"
Dan Albert914449f2016-06-17 16:45:24 -070026)
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.
Colin Cross70dda7e2019-10-01 22:05:35 -070051func getCurrentIncludePath(ctx android.ModuleContext) android.InstallPath {
Dan Albert914449f2016-06-17 16:45:24 -070052 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".
Colin Cross27b922f2019-03-04 22:35:41 -080073 Srcs []string `android:"path"`
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.
Colin Cross27b922f2019-03-04 22:35:41 -080076 Exclude_srcs []string `android:"path"`
Dan Albert19ff8b42018-05-24 15:00:48 -070077
Dan Albertc6345fb2016-10-20 01:36:11 -070078 // Path to the NOTICE file associated with the headers.
Colin Cross27b922f2019-03-04 22:35:41 -080079 License *string `android:"path"`
Dan Albert914449f2016-06-17 16:45:24 -070080}
81
82type headerModule struct {
83 android.ModuleBase
Spandan Das0773a602022-08-16 00:55:11 +000084 android.BazelModuleBase
Dan Albert914449f2016-06-17 16:45:24 -070085
Dan Albert71222052018-05-24 15:00:05 -070086 properties headerProperties
Dan Albert914449f2016-06-17 16:45:24 -070087
Colin Cross0875c522017-11-28 17:34:01 -080088 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -080089 licensePath android.Path
Dan Albert914449f2016-06-17 16:45:24 -070090}
91
Dan Albert269fab82017-02-15 17:31:33 -080092func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string,
Colin Cross70dda7e2019-10-01 22:05:35 -070093 to string) android.InstallPath {
Dan Albert269fab82017-02-15 17:31:33 -080094 // Output path is the sysroot base + "usr/include" + to directory + directory component
95 // of the file without the leading from directory stripped.
96 //
97 // Given:
98 // sysroot base = "ndk/sysroot"
99 // from = "include/foo"
100 // to = "bar"
101 // header = "include/foo/woodly/doodly.h"
102 // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
103
104 // full/platform/path/to/include/foo
105 fullFromPath := android.PathForModuleSrc(ctx, from)
106
107 // full/platform/path/to/include/foo/woodly
108 headerDir := filepath.Dir(header.String())
109
110 // woodly
111 strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
112 if err != nil {
113 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
114 fullFromPath.String(), err)
115 }
116
117 // full/platform/path/to/sysroot/usr/include/bar/woodly
118 installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir)
119
120 // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
121 return installDir
122}
123
Dan Albert914449f2016-06-17 16:45:24 -0700124func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhang0007d812017-11-07 10:57:05 -0800125 if String(m.properties.License) == "" {
Dan Albertc6345fb2016-10-20 01:36:11 -0700126 ctx.PropertyErrorf("license", "field is required")
127 }
128
Nan Zhang0007d812017-11-07 10:57:05 -0800129 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albertc6345fb2016-10-20 01:36:11 -0700130
Colin Cross8a497952019-03-05 22:25:09 -0800131 srcFiles := android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
Dan Albert914449f2016-06-17 16:45:24 -0700132 for _, header := range srcFiles {
Nan Zhang0007d812017-11-07 10:57:05 -0800133 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From),
134 String(m.properties.To))
Colin Cross5c517922017-08-31 12:29:17 -0700135 installedPath := ctx.InstallFile(installDir, header.Base(), header)
Dan Albert269fab82017-02-15 17:31:33 -0800136 installPath := installDir.Join(ctx, header.Base())
137 if installPath != installedPath {
138 panic(fmt.Sprintf(
139 "expected header install path (%q) not equal to actual install path %q",
140 installPath, installedPath))
Dan Albert914449f2016-06-17 16:45:24 -0700141 }
Colin Cross0875c522017-11-28 17:34:01 -0800142 m.installPaths = append(m.installPaths, installPath)
Dan Albert914449f2016-06-17 16:45:24 -0700143 }
144
145 if len(m.installPaths) == 0 {
146 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
147 }
148}
149
Spandan Das0773a602022-08-16 00:55:11 +0000150const (
151 apiContributionSuffix = ".contribution"
152)
153
154// apiContributionTargetName returns the name of the cc_api(headers|contribution) bp2build target of ndk modules
155// A suffix is necessary to prevent a name collision with the base ndk_(library|header) target in the same bp2build bazel package
156func apiContributionTargetName(moduleName string) string {
157 return moduleName + apiContributionSuffix
158}
159
160// TODO(b/243196151): Populate `system` and `arch` metadata
161type bazelCcApiHeadersAttributes struct {
162 Hdrs bazel.LabelListAttribute
163 Include_dir *string
164}
165
166func createCcApiHeadersTarget(ctx android.TopDownMutatorContext, includes []string, excludes []string, include_dir *string) {
167 props := bazel.BazelTargetModuleProperties{
168 Rule_class: "cc_api_headers",
169 Bzl_load_location: "//build/bazel/rules/apis:cc_api_contribution.bzl",
170 }
171 attrs := &bazelCcApiHeadersAttributes{
172 Hdrs: bazel.MakeLabelListAttribute(
173 android.BazelLabelForModuleSrcExcludes(
174 ctx,
175 includes,
176 excludes,
177 ),
178 ),
179 Include_dir: include_dir,
180 }
181 ctx.CreateBazelTargetModule(props, android.CommonAttributes{
182 Name: apiContributionTargetName(ctx.ModuleName()),
183 }, attrs)
184}
185
186func (h *headerModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
187 // Generate `cc_api_headers` target for Multi-tree API export
188 createCcApiHeadersTarget(ctx, h.properties.Srcs, h.properties.Exclude_srcs, h.properties.From)
189}
190
Patrice Arruda6ea42112019-04-03 08:43:30 -0700191// ndk_headers installs the sets of ndk headers defined in the srcs property
192// to the sysroot base + "usr/include" + to directory + directory component.
193// ndk_headers requires the license file to be specified. Example:
194//
Colin Crossd079e0b2022-08-16 10:27:33 -0700195// Given:
196// sysroot base = "ndk/sysroot"
197// from = "include/foo"
198// to = "bar"
199// header = "include/foo/woodly/doodly.h"
200// output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
Colin Cross36242852017-06-23 15:06:31 -0700201func ndkHeadersFactory() android.Module {
Dan Albert914449f2016-06-17 16:45:24 -0700202 module := &headerModule{}
Colin Cross36242852017-06-23 15:06:31 -0700203 module.AddProperties(&module.properties)
204 android.InitAndroidModule(module)
Spandan Das0773a602022-08-16 00:55:11 +0000205 android.InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700206 return module
Dan Albert914449f2016-06-17 16:45:24 -0700207}
Dan Albert269fab82017-02-15 17:31:33 -0800208
Dan Albert97f9c962018-05-24 15:02:16 -0700209type versionedHeaderProperties struct {
Dan Albert269fab82017-02-15 17:31:33 -0800210 // Base directory of the headers being installed. As an example:
211 //
Dan Albert97f9c962018-05-24 15:02:16 -0700212 // versioned_ndk_headers {
Dan Albert269fab82017-02-15 17:31:33 -0800213 // name: "foo",
214 // from: "include",
215 // to: "",
216 // }
217 //
218 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
219 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800220 From *string
Dan Albert269fab82017-02-15 17:31:33 -0800221
222 // Install path within the sysroot. This is relative to usr/include.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800223 To *string
Dan Albert269fab82017-02-15 17:31:33 -0800224
225 // Path to the NOTICE file associated with the headers.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800226 License *string
Dan Albert269fab82017-02-15 17:31:33 -0800227}
228
229// Like ndk_headers, but preprocesses the headers with the bionic versioner:
230// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
231//
232// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
233// module does not have the srcs property, and operates on a full directory (the `from` property).
234//
235// Note that this is really only built to handle bionic/libc/include.
Dan Albert97f9c962018-05-24 15:02:16 -0700236type versionedHeaderModule struct {
Dan Albert269fab82017-02-15 17:31:33 -0800237 android.ModuleBase
Spandan Das0773a602022-08-16 00:55:11 +0000238 android.BazelModuleBase
Dan Albert269fab82017-02-15 17:31:33 -0800239
Dan Albert97f9c962018-05-24 15:02:16 -0700240 properties versionedHeaderProperties
Dan Albert269fab82017-02-15 17:31:33 -0800241
Colin Cross0875c522017-11-28 17:34:01 -0800242 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -0800243 licensePath android.Path
Dan Albert269fab82017-02-15 17:31:33 -0800244}
245
Spandan Das0773a602022-08-16 00:55:11 +0000246// Return the glob pattern to find all .h files beneath `dir`
247func headerGlobPattern(dir string) string {
248 return filepath.Join(dir, "**", "*.h")
249}
250
Dan Albert97f9c962018-05-24 15:02:16 -0700251func (m *versionedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800252 if String(m.properties.License) == "" {
Dan Albert269fab82017-02-15 17:31:33 -0800253 ctx.PropertyErrorf("license", "field is required")
254 }
255
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800256 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albert269fab82017-02-15 17:31:33 -0800257
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800258 fromSrcPath := android.PathForModuleSrc(ctx, String(m.properties.From))
259 toOutputPath := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Spandan Das0773a602022-08-16 00:55:11 +0000260 srcFiles := ctx.GlobFiles(headerGlobPattern(fromSrcPath.String()), nil)
Dan Albert269fab82017-02-15 17:31:33 -0800261 var installPaths []android.WritablePath
262 for _, header := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800263 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), String(m.properties.To))
Dan Albert269fab82017-02-15 17:31:33 -0800264 installPath := installDir.Join(ctx, header.Base())
265 installPaths = append(installPaths, installPath)
Colin Cross0875c522017-11-28 17:34:01 -0800266 m.installPaths = append(m.installPaths, installPath)
Dan Albert269fab82017-02-15 17:31:33 -0800267 }
268
269 if len(m.installPaths) == 0 {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800270 ctx.ModuleErrorf("glob %q matched zero files", String(m.properties.From))
Dan Albert269fab82017-02-15 17:31:33 -0800271 }
272
Dan Willemsenb916b802017-03-19 13:44:32 -0700273 processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, srcFiles, installPaths)
274}
275
Spandan Das0773a602022-08-16 00:55:11 +0000276func (h *versionedHeaderModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
277 // Glob all .h files under `From`
278 includePattern := headerGlobPattern(proptools.String(h.properties.From))
279 // Generate `cc_api_headers` target for Multi-tree API export
280 createCcApiHeadersTarget(ctx, []string{includePattern}, []string{}, h.properties.From)
281}
282
Colin Cross07e51612019-03-05 12:46:40 -0800283func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path,
284 srcFiles android.Paths, installPaths []android.WritablePath) android.Path {
Dan Albert269fab82017-02-15 17:31:33 -0800285 // The versioner depends on a dependencies directory to simplify determining include paths
286 // when parsing headers. This directory contains architecture specific directories as well
287 // as a common directory, each of which contains symlinks to the actually directories to
288 // be included.
289 //
290 // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
291 // depend on these headers.
292 // TODO(http://b/35673191): Update the versioner to use a --sysroot.
293 depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
294 depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
295 for i, path := range depsGlob {
Colin Cross988414c2020-01-11 01:11:46 +0000296 if ctx.IsSymlink(path) {
297 dest := ctx.Readlink(path)
Dan Albert269fab82017-02-15 17:31:33 -0800298 // Additional .. to account for the symlink itself.
299 depsGlob[i] = android.PathForSource(
300 ctx, filepath.Clean(filepath.Join(path.String(), "..", dest)))
301 }
302 }
303
Dan Albertd2130a92017-03-29 18:33:28 -0700304 timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp")
Colin Crossae887032017-10-23 17:16:14 -0700305 ctx.Build(pctx, android.BuildParams{
Dan Albert97f9c962018-05-24 15:02:16 -0700306 Rule: versionBionicHeaders,
Colin Cross67a5c132017-05-09 13:45:28 -0700307 Description: "versioner preprocess " + srcDir.Rel(),
Dan Albertd2130a92017-03-29 18:33:28 -0700308 Output: timestampFile,
Dan Albert269fab82017-02-15 17:31:33 -0800309 Implicits: append(srcFiles, depsGlob...),
310 ImplicitOutputs: installPaths,
311 Args: map[string]string{
312 "depsPath": depsPath.String(),
Dan Willemsenb916b802017-03-19 13:44:32 -0700313 "srcDir": srcDir.String(),
314 "outDir": outDir.String(),
Dan Albert269fab82017-02-15 17:31:33 -0800315 },
316 })
Dan Willemsenb916b802017-03-19 13:44:32 -0700317
318 return timestampFile
Dan Albert269fab82017-02-15 17:31:33 -0800319}
320
Patrice Arruda6ea42112019-04-03 08:43:30 -0700321// versioned_ndk_headers preprocesses the headers with the bionic versioner:
322// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
323// Unlike the ndk_headers soong module, versioned_ndk_headers operates on a
324// directory level specified in `from` property. This is only used to process
325// the bionic/libc/include directory.
Dan Albert97f9c962018-05-24 15:02:16 -0700326func versionedNdkHeadersFactory() android.Module {
327 module := &versionedHeaderModule{}
Colin Cross36242852017-06-23 15:06:31 -0700328
329 module.AddProperties(&module.properties)
330
Dan Willemsen4f644da2018-10-10 17:18:08 -0700331 android.InitAndroidModule(module)
Spandan Das0773a602022-08-16 00:55:11 +0000332 android.InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700333
334 return module
Dan Albert269fab82017-02-15 17:31:33 -0800335}
Dan Albertcb1b4b22018-05-24 15:06:11 -0700336
Spandan Das0773a602022-08-16 00:55:11 +0000337// preprocessed_ndk_header {
338//
339// name: "foo",
340// preprocessor: "foo.sh",
341// srcs: [...],
342// to: "android",
343//
344// }
Dan Albertcb1b4b22018-05-24 15:06:11 -0700345//
346// Will invoke the preprocessor as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700347//
348// $preprocessor -o $SYSROOT/usr/include/android/needs_preproc.h $src
349//
Dan Albertcb1b4b22018-05-24 15:06:11 -0700350// For each src in srcs.
351type preprocessedHeadersProperties struct {
352 // The preprocessor to run. Must be a program inside the source directory
353 // with no dependencies.
354 Preprocessor *string
355
356 // Source path to the files to be preprocessed.
357 Srcs []string
358
359 // Source paths that should be excluded from the srcs glob.
360 Exclude_srcs []string
361
362 // Install path within the sysroot. This is relative to usr/include.
363 To *string
364
365 // Path to the NOTICE file associated with the headers.
366 License *string
367}
368
369type preprocessedHeadersModule struct {
370 android.ModuleBase
371
372 properties preprocessedHeadersProperties
373
374 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -0800375 licensePath android.Path
Dan Albertcb1b4b22018-05-24 15:06:11 -0700376}
377
Dan Albertcb1b4b22018-05-24 15:06:11 -0700378func (m *preprocessedHeadersModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
379 if String(m.properties.License) == "" {
380 ctx.PropertyErrorf("license", "field is required")
381 }
382
383 preprocessor := android.PathForModuleSrc(ctx, String(m.properties.Preprocessor))
384 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
385
Colin Cross8a497952019-03-05 22:25:09 -0800386 srcFiles := android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700387 installDir := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
388 for _, src := range srcFiles {
389 installPath := installDir.Join(ctx, src.Base())
390 m.installPaths = append(m.installPaths, installPath)
391
392 ctx.Build(pctx, android.BuildParams{
393 Rule: preprocessNdkHeader,
394 Description: "preprocess " + src.Rel(),
395 Input: src,
396 Output: installPath,
397 Args: map[string]string{
398 "preprocessor": preprocessor.String(),
399 },
400 })
401 }
402
403 if len(m.installPaths) == 0 {
404 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
405 }
406}
407
Patrice Arruda6ea42112019-04-03 08:43:30 -0700408// preprocessed_ndk_headers preprocesses all the ndk headers listed in the srcs
409// property by executing the command defined in the preprocessor property.
Dan Albertcb1b4b22018-05-24 15:06:11 -0700410func preprocessedNdkHeadersFactory() android.Module {
411 module := &preprocessedHeadersModule{}
412
413 module.AddProperties(&module.properties)
414
Dan Willemsen4f644da2018-10-10 17:18:08 -0700415 android.InitAndroidModule(module)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700416
417 return module
418}