blob: 57a3b3a9c4cddf28ead672bc7adf7288fc6c59ba [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 (
18 "path/filepath"
19
Dan Albert914449f2016-06-17 16:45:24 -070020 "android/soong/android"
Colin Cross8ff10582023-12-07 13:10:56 -080021 "github.com/google/blueprint"
Dan Albert914449f2016-06-17 16:45:24 -070022)
23
Dan Albert269fab82017-02-15 17:31:33 -080024var (
Dan Albert97f9c962018-05-24 15:02:16 -070025 versionBionicHeaders = pctx.AndroidStaticRule("versionBionicHeaders",
Dan Albert269fab82017-02-15 17:31:33 -080026 blueprint.RuleParams{
Dan Albertd2130a92017-03-29 18:33:28 -070027 // The `&& touch $out` isn't really necessary, but Blueprint won't
28 // let us have only implicit outputs.
29 Command: "$versionerCmd -o $outDir $srcDir $depsPath && touch $out",
Dan Albert269fab82017-02-15 17:31:33 -080030 CommandDeps: []string{"$versionerCmd"},
Dan Albert269fab82017-02-15 17:31:33 -080031 },
Dan Albertd2130a92017-03-29 18:33:28 -070032 "depsPath", "srcDir", "outDir")
Dan Albertcb1b4b22018-05-24 15:06:11 -070033
34 preprocessNdkHeader = pctx.AndroidStaticRule("preprocessNdkHeader",
35 blueprint.RuleParams{
36 Command: "$preprocessor -o $out $in",
37 CommandDeps: []string{"$preprocessor"},
38 },
39 "preprocessor")
Dan Albert269fab82017-02-15 17:31:33 -080040)
41
42func init() {
Logan Chien2f066352018-10-25 18:11:09 +080043 pctx.SourcePathVariable("versionerCmd", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/versioner")
Dan Albert269fab82017-02-15 17:31:33 -080044}
45
Dan Albert914449f2016-06-17 16:45:24 -070046// Returns the NDK base include path for use with sdk_version current. Usable with -I.
Spandan Dasf280b232024-04-04 21:25:51 +000047func getCurrentIncludePath(ctx android.ModuleContext) android.OutputPath {
Dan Albert914449f2016-06-17 16:45:24 -070048 return getNdkSysrootBase(ctx).Join(ctx, "usr/include")
49}
50
Dan Albert71222052018-05-24 15:00:05 -070051type headerProperties struct {
Dan Albert914449f2016-06-17 16:45:24 -070052 // Base directory of the headers being installed. As an example:
53 //
54 // ndk_headers {
55 // name: "foo",
56 // from: "include",
57 // to: "",
58 // srcs: ["include/foo/bar/baz.h"],
59 // }
60 //
61 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
62 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhang0007d812017-11-07 10:57:05 -080063 From *string
Dan Albert914449f2016-06-17 16:45:24 -070064
65 // Install path within the sysroot. This is relative to usr/include.
Nan Zhang0007d812017-11-07 10:57:05 -080066 To *string
Dan Albert914449f2016-06-17 16:45:24 -070067
68 // List of headers to install. Glob compatible. Common case is "include/**/*.h".
Colin Cross27b922f2019-03-04 22:35:41 -080069 Srcs []string `android:"path"`
Dan Albertc6345fb2016-10-20 01:36:11 -070070
Dan Albert19ff8b42018-05-24 15:00:48 -070071 // Source paths that should be excluded from the srcs glob.
Colin Cross27b922f2019-03-04 22:35:41 -080072 Exclude_srcs []string `android:"path"`
Dan Albert19ff8b42018-05-24 15:00:48 -070073
Dan Albertc6345fb2016-10-20 01:36:11 -070074 // Path to the NOTICE file associated with the headers.
Colin Cross27b922f2019-03-04 22:35:41 -080075 License *string `android:"path"`
Dan Albert914449f2016-06-17 16:45:24 -070076}
77
78type headerModule struct {
79 android.ModuleBase
80
Dan Albert71222052018-05-24 15:00:05 -070081 properties headerProperties
Dan Albert914449f2016-06-17 16:45:24 -070082
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +000083 srcPaths android.Paths
Colin Cross0875c522017-11-28 17:34:01 -080084 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -080085 licensePath android.Path
Dan Albert914449f2016-06-17 16:45:24 -070086}
87
Dan Albert269fab82017-02-15 17:31:33 -080088func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string,
Spandan Dasf280b232024-04-04 21:25:51 +000089 to string) android.OutputPath {
Dan Albert269fab82017-02-15 17:31:33 -080090 // Output path is the sysroot base + "usr/include" + to directory + directory component
91 // of the file without the leading from directory stripped.
92 //
93 // Given:
94 // sysroot base = "ndk/sysroot"
95 // from = "include/foo"
96 // to = "bar"
97 // header = "include/foo/woodly/doodly.h"
98 // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
99
100 // full/platform/path/to/include/foo
101 fullFromPath := android.PathForModuleSrc(ctx, from)
102
103 // full/platform/path/to/include/foo/woodly
104 headerDir := filepath.Dir(header.String())
105
106 // woodly
107 strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
108 if err != nil {
109 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
110 fullFromPath.String(), err)
111 }
112
113 // full/platform/path/to/sysroot/usr/include/bar/woodly
114 installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir)
115
116 // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
117 return installDir
118}
119
Dan Albert914449f2016-06-17 16:45:24 -0700120func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhang0007d812017-11-07 10:57:05 -0800121 if String(m.properties.License) == "" {
Dan Albertc6345fb2016-10-20 01:36:11 -0700122 ctx.PropertyErrorf("license", "field is required")
123 }
124
Nan Zhang0007d812017-11-07 10:57:05 -0800125 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albertc6345fb2016-10-20 01:36:11 -0700126
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000127 m.srcPaths = android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
128 for _, header := range m.srcPaths {
Nan Zhang0007d812017-11-07 10:57:05 -0800129 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From),
130 String(m.properties.To))
Dan Albert269fab82017-02-15 17:31:33 -0800131 installPath := installDir.Join(ctx, header.Base())
Spandan Dasf280b232024-04-04 21:25:51 +0000132 ctx.Build(pctx, android.BuildParams{
133 Rule: android.Cp,
134 Input: header,
135 Output: installPath,
136 })
Colin Cross0875c522017-11-28 17:34:01 -0800137 m.installPaths = append(m.installPaths, installPath)
Dan Albert914449f2016-06-17 16:45:24 -0700138 }
139
140 if len(m.installPaths) == 0 {
141 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
142 }
143}
144
Patrice Arruda6ea42112019-04-03 08:43:30 -0700145// ndk_headers installs the sets of ndk headers defined in the srcs property
146// to the sysroot base + "usr/include" + to directory + directory component.
147// ndk_headers requires the license file to be specified. Example:
148//
Colin Crossd079e0b2022-08-16 10:27:33 -0700149// Given:
150// sysroot base = "ndk/sysroot"
151// from = "include/foo"
152// to = "bar"
153// header = "include/foo/woodly/doodly.h"
154// output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
Spandan Das319711b2023-09-19 19:04:41 +0000155func NdkHeadersFactory() android.Module {
Dan Albert914449f2016-06-17 16:45:24 -0700156 module := &headerModule{}
Colin Cross36242852017-06-23 15:06:31 -0700157 module.AddProperties(&module.properties)
158 android.InitAndroidModule(module)
159 return module
Dan Albert914449f2016-06-17 16:45:24 -0700160}
Dan Albert269fab82017-02-15 17:31:33 -0800161
Dan Albert97f9c962018-05-24 15:02:16 -0700162type versionedHeaderProperties struct {
Dan Albert269fab82017-02-15 17:31:33 -0800163 // Base directory of the headers being installed. As an example:
164 //
Dan Albert97f9c962018-05-24 15:02:16 -0700165 // versioned_ndk_headers {
Dan Albert269fab82017-02-15 17:31:33 -0800166 // name: "foo",
167 // from: "include",
168 // to: "",
169 // }
170 //
171 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
172 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800173 From *string
Dan Albert269fab82017-02-15 17:31:33 -0800174
175 // Install path within the sysroot. This is relative to usr/include.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800176 To *string
Dan Albert269fab82017-02-15 17:31:33 -0800177
178 // Path to the NOTICE file associated with the headers.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800179 License *string
Dan Albert269fab82017-02-15 17:31:33 -0800180}
181
182// Like ndk_headers, but preprocesses the headers with the bionic versioner:
Elliott Hughes10363162024-01-09 22:02:03 +0000183// https://android.googlesource.com/platform/bionic/+/main/tools/versioner/README.md.
Dan Albert269fab82017-02-15 17:31:33 -0800184//
185// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
186// module does not have the srcs property, and operates on a full directory (the `from` property).
187//
188// Note that this is really only built to handle bionic/libc/include.
Dan Albert97f9c962018-05-24 15:02:16 -0700189type versionedHeaderModule struct {
Dan Albert269fab82017-02-15 17:31:33 -0800190 android.ModuleBase
191
Dan Albert97f9c962018-05-24 15:02:16 -0700192 properties versionedHeaderProperties
Dan Albert269fab82017-02-15 17:31:33 -0800193
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000194 srcPaths android.Paths
Colin Cross0875c522017-11-28 17:34:01 -0800195 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -0800196 licensePath android.Path
Dan Albert269fab82017-02-15 17:31:33 -0800197}
198
Spandan Das0773a602022-08-16 00:55:11 +0000199// Return the glob pattern to find all .h files beneath `dir`
200func headerGlobPattern(dir string) string {
201 return filepath.Join(dir, "**", "*.h")
202}
203
Dan Albert97f9c962018-05-24 15:02:16 -0700204func (m *versionedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800205 if String(m.properties.License) == "" {
Dan Albert269fab82017-02-15 17:31:33 -0800206 ctx.PropertyErrorf("license", "field is required")
207 }
208
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800209 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albert269fab82017-02-15 17:31:33 -0800210
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800211 fromSrcPath := android.PathForModuleSrc(ctx, String(m.properties.From))
212 toOutputPath := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000213 m.srcPaths = ctx.GlobFiles(headerGlobPattern(fromSrcPath.String()), nil)
Dan Albert269fab82017-02-15 17:31:33 -0800214 var installPaths []android.WritablePath
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000215 for _, header := range m.srcPaths {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800216 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), String(m.properties.To))
Dan Albert269fab82017-02-15 17:31:33 -0800217 installPath := installDir.Join(ctx, header.Base())
218 installPaths = append(installPaths, installPath)
Colin Cross0875c522017-11-28 17:34:01 -0800219 m.installPaths = append(m.installPaths, installPath)
Dan Albert269fab82017-02-15 17:31:33 -0800220 }
221
222 if len(m.installPaths) == 0 {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800223 ctx.ModuleErrorf("glob %q matched zero files", String(m.properties.From))
Dan Albert269fab82017-02-15 17:31:33 -0800224 }
225
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000226 processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, m.srcPaths, installPaths)
Dan Willemsenb916b802017-03-19 13:44:32 -0700227}
228
Colin Cross07e51612019-03-05 12:46:40 -0800229func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path,
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000230 srcPaths android.Paths, installPaths []android.WritablePath) android.Path {
Dan Albert269fab82017-02-15 17:31:33 -0800231 // The versioner depends on a dependencies directory to simplify determining include paths
232 // when parsing headers. This directory contains architecture specific directories as well
233 // as a common directory, each of which contains symlinks to the actually directories to
234 // be included.
235 //
236 // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
237 // depend on these headers.
238 // TODO(http://b/35673191): Update the versioner to use a --sysroot.
239 depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
240 depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
241 for i, path := range depsGlob {
Colin Cross988414c2020-01-11 01:11:46 +0000242 if ctx.IsSymlink(path) {
243 dest := ctx.Readlink(path)
Dan Albert269fab82017-02-15 17:31:33 -0800244 // Additional .. to account for the symlink itself.
245 depsGlob[i] = android.PathForSource(
246 ctx, filepath.Clean(filepath.Join(path.String(), "..", dest)))
247 }
248 }
249
Dan Albertd2130a92017-03-29 18:33:28 -0700250 timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp")
Colin Crossae887032017-10-23 17:16:14 -0700251 ctx.Build(pctx, android.BuildParams{
Dan Albert97f9c962018-05-24 15:02:16 -0700252 Rule: versionBionicHeaders,
Colin Cross67a5c132017-05-09 13:45:28 -0700253 Description: "versioner preprocess " + srcDir.Rel(),
Dan Albertd2130a92017-03-29 18:33:28 -0700254 Output: timestampFile,
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000255 Implicits: append(srcPaths, depsGlob...),
Dan Albert269fab82017-02-15 17:31:33 -0800256 ImplicitOutputs: installPaths,
257 Args: map[string]string{
258 "depsPath": depsPath.String(),
Dan Willemsenb916b802017-03-19 13:44:32 -0700259 "srcDir": srcDir.String(),
260 "outDir": outDir.String(),
Dan Albert269fab82017-02-15 17:31:33 -0800261 },
262 })
Dan Willemsenb916b802017-03-19 13:44:32 -0700263
264 return timestampFile
Dan Albert269fab82017-02-15 17:31:33 -0800265}
266
Patrice Arruda6ea42112019-04-03 08:43:30 -0700267// versioned_ndk_headers preprocesses the headers with the bionic versioner:
Elliott Hughes10363162024-01-09 22:02:03 +0000268// https://android.googlesource.com/platform/bionic/+/main/tools/versioner/README.md.
Patrice Arruda6ea42112019-04-03 08:43:30 -0700269// Unlike the ndk_headers soong module, versioned_ndk_headers operates on a
270// directory level specified in `from` property. This is only used to process
271// the bionic/libc/include directory.
Spandan Dasa7da3f02023-09-28 19:30:51 +0000272func VersionedNdkHeadersFactory() android.Module {
Dan Albert97f9c962018-05-24 15:02:16 -0700273 module := &versionedHeaderModule{}
Colin Cross36242852017-06-23 15:06:31 -0700274
275 module.AddProperties(&module.properties)
276
Dan Willemsen4f644da2018-10-10 17:18:08 -0700277 android.InitAndroidModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700278
279 return module
Dan Albert269fab82017-02-15 17:31:33 -0800280}
Dan Albertcb1b4b22018-05-24 15:06:11 -0700281
Spandan Das0773a602022-08-16 00:55:11 +0000282// preprocessed_ndk_header {
283//
284// name: "foo",
285// preprocessor: "foo.sh",
286// srcs: [...],
287// to: "android",
288//
289// }
Dan Albertcb1b4b22018-05-24 15:06:11 -0700290//
291// Will invoke the preprocessor as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700292//
293// $preprocessor -o $SYSROOT/usr/include/android/needs_preproc.h $src
294//
Dan Albertcb1b4b22018-05-24 15:06:11 -0700295// For each src in srcs.
296type preprocessedHeadersProperties struct {
297 // The preprocessor to run. Must be a program inside the source directory
298 // with no dependencies.
299 Preprocessor *string
300
301 // Source path to the files to be preprocessed.
302 Srcs []string
303
304 // Source paths that should be excluded from the srcs glob.
305 Exclude_srcs []string
306
307 // Install path within the sysroot. This is relative to usr/include.
308 To *string
309
310 // Path to the NOTICE file associated with the headers.
311 License *string
312}
313
314type preprocessedHeadersModule struct {
315 android.ModuleBase
316
317 properties preprocessedHeadersProperties
318
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000319 srcPaths android.Paths
Dan Albertcb1b4b22018-05-24 15:06:11 -0700320 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -0800321 licensePath android.Path
Dan Albertcb1b4b22018-05-24 15:06:11 -0700322}
323
Dan Albertcb1b4b22018-05-24 15:06:11 -0700324func (m *preprocessedHeadersModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
325 if String(m.properties.License) == "" {
326 ctx.PropertyErrorf("license", "field is required")
327 }
328
329 preprocessor := android.PathForModuleSrc(ctx, String(m.properties.Preprocessor))
330 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
331
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000332 m.srcPaths = android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700333 installDir := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000334 for _, src := range m.srcPaths {
Dan Albertcb1b4b22018-05-24 15:06:11 -0700335 installPath := installDir.Join(ctx, src.Base())
336 m.installPaths = append(m.installPaths, installPath)
337
338 ctx.Build(pctx, android.BuildParams{
339 Rule: preprocessNdkHeader,
340 Description: "preprocess " + src.Rel(),
341 Input: src,
342 Output: installPath,
343 Args: map[string]string{
344 "preprocessor": preprocessor.String(),
345 },
346 })
347 }
348
349 if len(m.installPaths) == 0 {
350 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
351 }
352}
353
Patrice Arruda6ea42112019-04-03 08:43:30 -0700354// preprocessed_ndk_headers preprocesses all the ndk headers listed in the srcs
355// property by executing the command defined in the preprocessor property.
Dan Albertcb1b4b22018-05-24 15:06:11 -0700356func preprocessedNdkHeadersFactory() android.Module {
357 module := &preprocessedHeadersModule{}
358
359 module.AddProperties(&module.properties)
360
Dan Willemsen4f644da2018-10-10 17:18:08 -0700361 android.InitAndroidModule(module)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700362
363 return module
364}