blob: da5db1c19b4b6797103f735804b07ba47d4532f7 [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"
22
23 "android/soong/android"
Spandan Das319711b2023-09-19 19:04:41 +000024 "android/soong/bazel"
Dan Albert914449f2016-06-17 16:45:24 -070025)
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 Albert914449f2016-06-17 16:45:24 -070079}
80
81type headerModule struct {
82 android.ModuleBase
Spandan Das319711b2023-09-19 19:04:41 +000083 android.BazelModuleBase
Dan Albert914449f2016-06-17 16:45:24 -070084
Dan Albert71222052018-05-24 15:00:05 -070085 properties headerProperties
Dan Albert914449f2016-06-17 16:45:24 -070086
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +000087 srcPaths android.Paths
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
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000131 m.srcPaths = android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
132 for _, header := range m.srcPaths {
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 Das319711b2023-09-19 19:04:41 +0000150type bazelNdkHeadersAttributes struct {
151 Strip_import_prefix *string
152 Import_prefix *string
153 Hdrs bazel.LabelListAttribute
154}
155
156func (h *headerModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
157 props := bazel.BazelTargetModuleProperties{
158 Rule_class: "ndk_headers",
159 Bzl_load_location: "//build/bazel/rules/cc:ndk_headers.bzl",
160 }
161 attrs := &bazelNdkHeadersAttributes{
162 Strip_import_prefix: h.properties.From,
163 Import_prefix: h.properties.To,
164 Hdrs: bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, h.properties.Srcs, h.properties.Exclude_srcs)),
165 }
166 ctx.CreateBazelTargetModule(
167 props,
168 android.CommonAttributes{Name: h.Name()},
169 attrs,
170 )
171}
172
Patrice Arruda6ea42112019-04-03 08:43:30 -0700173// ndk_headers installs the sets of ndk headers defined in the srcs property
174// to the sysroot base + "usr/include" + to directory + directory component.
175// ndk_headers requires the license file to be specified. Example:
176//
Colin Crossd079e0b2022-08-16 10:27:33 -0700177// Given:
178// sysroot base = "ndk/sysroot"
179// from = "include/foo"
180// to = "bar"
181// header = "include/foo/woodly/doodly.h"
182// output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
Spandan Das319711b2023-09-19 19:04:41 +0000183func NdkHeadersFactory() android.Module {
Dan Albert914449f2016-06-17 16:45:24 -0700184 module := &headerModule{}
Colin Cross36242852017-06-23 15:06:31 -0700185 module.AddProperties(&module.properties)
186 android.InitAndroidModule(module)
Spandan Das319711b2023-09-19 19:04:41 +0000187 android.InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700188 return module
Dan Albert914449f2016-06-17 16:45:24 -0700189}
Dan Albert269fab82017-02-15 17:31:33 -0800190
Dan Albert97f9c962018-05-24 15:02:16 -0700191type versionedHeaderProperties struct {
Dan Albert269fab82017-02-15 17:31:33 -0800192 // Base directory of the headers being installed. As an example:
193 //
Dan Albert97f9c962018-05-24 15:02:16 -0700194 // versioned_ndk_headers {
Dan Albert269fab82017-02-15 17:31:33 -0800195 // name: "foo",
196 // from: "include",
197 // to: "",
198 // }
199 //
200 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
201 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800202 From *string
Dan Albert269fab82017-02-15 17:31:33 -0800203
204 // Install path within the sysroot. This is relative to usr/include.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800205 To *string
Dan Albert269fab82017-02-15 17:31:33 -0800206
207 // Path to the NOTICE file associated with the headers.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800208 License *string
Dan Albert269fab82017-02-15 17:31:33 -0800209}
210
211// Like ndk_headers, but preprocesses the headers with the bionic versioner:
212// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
213//
214// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
215// module does not have the srcs property, and operates on a full directory (the `from` property).
216//
217// Note that this is really only built to handle bionic/libc/include.
Dan Albert97f9c962018-05-24 15:02:16 -0700218type versionedHeaderModule struct {
Dan Albert269fab82017-02-15 17:31:33 -0800219 android.ModuleBase
220
Dan Albert97f9c962018-05-24 15:02:16 -0700221 properties versionedHeaderProperties
Dan Albert269fab82017-02-15 17:31:33 -0800222
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000223 srcPaths android.Paths
Colin Cross0875c522017-11-28 17:34:01 -0800224 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -0800225 licensePath android.Path
Dan Albert269fab82017-02-15 17:31:33 -0800226}
227
Spandan Das0773a602022-08-16 00:55:11 +0000228// Return the glob pattern to find all .h files beneath `dir`
229func headerGlobPattern(dir string) string {
230 return filepath.Join(dir, "**", "*.h")
231}
232
Dan Albert97f9c962018-05-24 15:02:16 -0700233func (m *versionedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800234 if String(m.properties.License) == "" {
Dan Albert269fab82017-02-15 17:31:33 -0800235 ctx.PropertyErrorf("license", "field is required")
236 }
237
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800238 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albert269fab82017-02-15 17:31:33 -0800239
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800240 fromSrcPath := android.PathForModuleSrc(ctx, String(m.properties.From))
241 toOutputPath := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000242 m.srcPaths = ctx.GlobFiles(headerGlobPattern(fromSrcPath.String()), nil)
Dan Albert269fab82017-02-15 17:31:33 -0800243 var installPaths []android.WritablePath
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000244 for _, header := range m.srcPaths {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800245 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), String(m.properties.To))
Dan Albert269fab82017-02-15 17:31:33 -0800246 installPath := installDir.Join(ctx, header.Base())
247 installPaths = append(installPaths, installPath)
Colin Cross0875c522017-11-28 17:34:01 -0800248 m.installPaths = append(m.installPaths, installPath)
Dan Albert269fab82017-02-15 17:31:33 -0800249 }
250
251 if len(m.installPaths) == 0 {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800252 ctx.ModuleErrorf("glob %q matched zero files", String(m.properties.From))
Dan Albert269fab82017-02-15 17:31:33 -0800253 }
254
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000255 processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, m.srcPaths, installPaths)
Dan Willemsenb916b802017-03-19 13:44:32 -0700256}
257
Colin Cross07e51612019-03-05 12:46:40 -0800258func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path,
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000259 srcPaths android.Paths, installPaths []android.WritablePath) android.Path {
Dan Albert269fab82017-02-15 17:31:33 -0800260 // The versioner depends on a dependencies directory to simplify determining include paths
261 // when parsing headers. This directory contains architecture specific directories as well
262 // as a common directory, each of which contains symlinks to the actually directories to
263 // be included.
264 //
265 // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
266 // depend on these headers.
267 // TODO(http://b/35673191): Update the versioner to use a --sysroot.
268 depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
269 depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
270 for i, path := range depsGlob {
Colin Cross988414c2020-01-11 01:11:46 +0000271 if ctx.IsSymlink(path) {
272 dest := ctx.Readlink(path)
Dan Albert269fab82017-02-15 17:31:33 -0800273 // Additional .. to account for the symlink itself.
274 depsGlob[i] = android.PathForSource(
275 ctx, filepath.Clean(filepath.Join(path.String(), "..", dest)))
276 }
277 }
278
Dan Albertd2130a92017-03-29 18:33:28 -0700279 timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp")
Colin Crossae887032017-10-23 17:16:14 -0700280 ctx.Build(pctx, android.BuildParams{
Dan Albert97f9c962018-05-24 15:02:16 -0700281 Rule: versionBionicHeaders,
Colin Cross67a5c132017-05-09 13:45:28 -0700282 Description: "versioner preprocess " + srcDir.Rel(),
Dan Albertd2130a92017-03-29 18:33:28 -0700283 Output: timestampFile,
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000284 Implicits: append(srcPaths, depsGlob...),
Dan Albert269fab82017-02-15 17:31:33 -0800285 ImplicitOutputs: installPaths,
286 Args: map[string]string{
287 "depsPath": depsPath.String(),
Dan Willemsenb916b802017-03-19 13:44:32 -0700288 "srcDir": srcDir.String(),
289 "outDir": outDir.String(),
Dan Albert269fab82017-02-15 17:31:33 -0800290 },
291 })
Dan Willemsenb916b802017-03-19 13:44:32 -0700292
293 return timestampFile
Dan Albert269fab82017-02-15 17:31:33 -0800294}
295
Patrice Arruda6ea42112019-04-03 08:43:30 -0700296// versioned_ndk_headers preprocesses the headers with the bionic versioner:
297// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
298// Unlike the ndk_headers soong module, versioned_ndk_headers operates on a
299// directory level specified in `from` property. This is only used to process
300// the bionic/libc/include directory.
Dan Albert97f9c962018-05-24 15:02:16 -0700301func versionedNdkHeadersFactory() android.Module {
302 module := &versionedHeaderModule{}
Colin Cross36242852017-06-23 15:06:31 -0700303
304 module.AddProperties(&module.properties)
305
Dan Willemsen4f644da2018-10-10 17:18:08 -0700306 android.InitAndroidModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700307
308 return module
Dan Albert269fab82017-02-15 17:31:33 -0800309}
Dan Albertcb1b4b22018-05-24 15:06:11 -0700310
Spandan Das0773a602022-08-16 00:55:11 +0000311// preprocessed_ndk_header {
312//
313// name: "foo",
314// preprocessor: "foo.sh",
315// srcs: [...],
316// to: "android",
317//
318// }
Dan Albertcb1b4b22018-05-24 15:06:11 -0700319//
320// Will invoke the preprocessor as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700321//
322// $preprocessor -o $SYSROOT/usr/include/android/needs_preproc.h $src
323//
Dan Albertcb1b4b22018-05-24 15:06:11 -0700324// For each src in srcs.
325type preprocessedHeadersProperties struct {
326 // The preprocessor to run. Must be a program inside the source directory
327 // with no dependencies.
328 Preprocessor *string
329
330 // Source path to the files to be preprocessed.
331 Srcs []string
332
333 // Source paths that should be excluded from the srcs glob.
334 Exclude_srcs []string
335
336 // Install path within the sysroot. This is relative to usr/include.
337 To *string
338
339 // Path to the NOTICE file associated with the headers.
340 License *string
341}
342
343type preprocessedHeadersModule struct {
344 android.ModuleBase
345
346 properties preprocessedHeadersProperties
347
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000348 srcPaths android.Paths
Dan Albertcb1b4b22018-05-24 15:06:11 -0700349 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -0800350 licensePath android.Path
Dan Albertcb1b4b22018-05-24 15:06:11 -0700351}
352
Dan Albertcb1b4b22018-05-24 15:06:11 -0700353func (m *preprocessedHeadersModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
354 if String(m.properties.License) == "" {
355 ctx.PropertyErrorf("license", "field is required")
356 }
357
358 preprocessor := android.PathForModuleSrc(ctx, String(m.properties.Preprocessor))
359 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
360
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000361 m.srcPaths = android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700362 installDir := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000363 for _, src := range m.srcPaths {
Dan Albertcb1b4b22018-05-24 15:06:11 -0700364 installPath := installDir.Join(ctx, src.Base())
365 m.installPaths = append(m.installPaths, installPath)
366
367 ctx.Build(pctx, android.BuildParams{
368 Rule: preprocessNdkHeader,
369 Description: "preprocess " + src.Rel(),
370 Input: src,
371 Output: installPath,
372 Args: map[string]string{
373 "preprocessor": preprocessor.String(),
374 },
375 })
376 }
377
378 if len(m.installPaths) == 0 {
379 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
380 }
381}
382
Patrice Arruda6ea42112019-04-03 08:43:30 -0700383// preprocessed_ndk_headers preprocesses all the ndk headers listed in the srcs
384// property by executing the command defined in the preprocessor property.
Dan Albertcb1b4b22018-05-24 15:06:11 -0700385func preprocessedNdkHeadersFactory() android.Module {
386 module := &preprocessedHeadersModule{}
387
388 module.AddProperties(&module.properties)
389
Dan Willemsen4f644da2018-10-10 17:18:08 -0700390 android.InitAndroidModule(module)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700391
392 return module
393}