blob: 1a8e90fd9d254d1d4003648c5ac4ddbcf0b5d774 [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"
24)
25
Dan Albert269fab82017-02-15 17:31:33 -080026var (
Dan Albert97f9c962018-05-24 15:02:16 -070027 versionBionicHeaders = pctx.AndroidStaticRule("versionBionicHeaders",
Dan Albert269fab82017-02-15 17:31:33 -080028 blueprint.RuleParams{
Dan Albertd2130a92017-03-29 18:33:28 -070029 // The `&& touch $out` isn't really necessary, but Blueprint won't
30 // let us have only implicit outputs.
31 Command: "$versionerCmd -o $outDir $srcDir $depsPath && touch $out",
Dan Albert269fab82017-02-15 17:31:33 -080032 CommandDeps: []string{"$versionerCmd"},
Dan Albert269fab82017-02-15 17:31:33 -080033 },
Dan Albertd2130a92017-03-29 18:33:28 -070034 "depsPath", "srcDir", "outDir")
Dan Albertcb1b4b22018-05-24 15:06:11 -070035
36 preprocessNdkHeader = pctx.AndroidStaticRule("preprocessNdkHeader",
37 blueprint.RuleParams{
38 Command: "$preprocessor -o $out $in",
39 CommandDeps: []string{"$preprocessor"},
40 },
41 "preprocessor")
Dan Albert269fab82017-02-15 17:31:33 -080042)
43
44func init() {
Logan Chien2f066352018-10-25 18:11:09 +080045 pctx.SourcePathVariable("versionerCmd", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/versioner")
Dan Albert269fab82017-02-15 17:31:33 -080046}
47
Dan Albert914449f2016-06-17 16:45:24 -070048// Returns the NDK base include path for use with sdk_version current. Usable with -I.
Colin Cross70dda7e2019-10-01 22:05:35 -070049func getCurrentIncludePath(ctx android.ModuleContext) android.InstallPath {
Dan Albert914449f2016-06-17 16:45:24 -070050 return getNdkSysrootBase(ctx).Join(ctx, "usr/include")
51}
52
Dan Albert71222052018-05-24 15:00:05 -070053type headerProperties struct {
Dan Albert914449f2016-06-17 16:45:24 -070054 // Base directory of the headers being installed. As an example:
55 //
56 // ndk_headers {
57 // name: "foo",
58 // from: "include",
59 // to: "",
60 // srcs: ["include/foo/bar/baz.h"],
61 // }
62 //
63 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
64 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhang0007d812017-11-07 10:57:05 -080065 From *string
Dan Albert914449f2016-06-17 16:45:24 -070066
67 // Install path within the sysroot. This is relative to usr/include.
Nan Zhang0007d812017-11-07 10:57:05 -080068 To *string
Dan Albert914449f2016-06-17 16:45:24 -070069
70 // List of headers to install. Glob compatible. Common case is "include/**/*.h".
Colin Cross27b922f2019-03-04 22:35:41 -080071 Srcs []string `android:"path"`
Dan Albertc6345fb2016-10-20 01:36:11 -070072
Dan Albert19ff8b42018-05-24 15:00:48 -070073 // Source paths that should be excluded from the srcs glob.
Colin Cross27b922f2019-03-04 22:35:41 -080074 Exclude_srcs []string `android:"path"`
Dan Albert19ff8b42018-05-24 15:00:48 -070075
Dan Albertc6345fb2016-10-20 01:36:11 -070076 // Path to the NOTICE file associated with the headers.
Colin Cross27b922f2019-03-04 22:35:41 -080077 License *string `android:"path"`
Dan Albert914449f2016-06-17 16:45:24 -070078}
79
80type headerModule struct {
81 android.ModuleBase
82
Dan Albert71222052018-05-24 15:00:05 -070083 properties headerProperties
Dan Albert914449f2016-06-17 16:45:24 -070084
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +000085 srcPaths android.Paths
Colin Cross0875c522017-11-28 17:34:01 -080086 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -080087 licensePath android.Path
Dan Albert914449f2016-06-17 16:45:24 -070088}
89
Dan Albert269fab82017-02-15 17:31:33 -080090func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string,
Colin Cross70dda7e2019-10-01 22:05:35 -070091 to string) android.InstallPath {
Dan Albert269fab82017-02-15 17:31:33 -080092 // Output path is the sysroot base + "usr/include" + to directory + directory component
93 // of the file without the leading from directory stripped.
94 //
95 // Given:
96 // sysroot base = "ndk/sysroot"
97 // from = "include/foo"
98 // to = "bar"
99 // header = "include/foo/woodly/doodly.h"
100 // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
101
102 // full/platform/path/to/include/foo
103 fullFromPath := android.PathForModuleSrc(ctx, from)
104
105 // full/platform/path/to/include/foo/woodly
106 headerDir := filepath.Dir(header.String())
107
108 // woodly
109 strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
110 if err != nil {
111 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
112 fullFromPath.String(), err)
113 }
114
115 // full/platform/path/to/sysroot/usr/include/bar/woodly
116 installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir)
117
118 // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
119 return installDir
120}
121
Dan Albert914449f2016-06-17 16:45:24 -0700122func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhang0007d812017-11-07 10:57:05 -0800123 if String(m.properties.License) == "" {
Dan Albertc6345fb2016-10-20 01:36:11 -0700124 ctx.PropertyErrorf("license", "field is required")
125 }
126
Nan Zhang0007d812017-11-07 10:57:05 -0800127 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albertc6345fb2016-10-20 01:36:11 -0700128
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000129 m.srcPaths = android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
130 for _, header := range m.srcPaths {
Nan Zhang0007d812017-11-07 10:57:05 -0800131 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From),
132 String(m.properties.To))
Colin Cross5c517922017-08-31 12:29:17 -0700133 installedPath := ctx.InstallFile(installDir, header.Base(), header)
Dan Albert269fab82017-02-15 17:31:33 -0800134 installPath := installDir.Join(ctx, header.Base())
135 if installPath != installedPath {
136 panic(fmt.Sprintf(
137 "expected header install path (%q) not equal to actual install path %q",
138 installPath, installedPath))
Dan Albert914449f2016-06-17 16:45:24 -0700139 }
Colin Cross0875c522017-11-28 17:34:01 -0800140 m.installPaths = append(m.installPaths, installPath)
Dan Albert914449f2016-06-17 16:45:24 -0700141 }
142
143 if len(m.installPaths) == 0 {
144 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
145 }
146}
147
Patrice Arruda6ea42112019-04-03 08:43:30 -0700148// ndk_headers installs the sets of ndk headers defined in the srcs property
149// to the sysroot base + "usr/include" + to directory + directory component.
150// ndk_headers requires the license file to be specified. Example:
151//
Colin Crossd079e0b2022-08-16 10:27:33 -0700152// Given:
153// sysroot base = "ndk/sysroot"
154// from = "include/foo"
155// to = "bar"
156// header = "include/foo/woodly/doodly.h"
157// output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
Colin Cross36242852017-06-23 15:06:31 -0700158func ndkHeadersFactory() android.Module {
Dan Albert914449f2016-06-17 16:45:24 -0700159 module := &headerModule{}
Colin Cross36242852017-06-23 15:06:31 -0700160 module.AddProperties(&module.properties)
161 android.InitAndroidModule(module)
162 return module
Dan Albert914449f2016-06-17 16:45:24 -0700163}
Dan Albert269fab82017-02-15 17:31:33 -0800164
Dan Albert97f9c962018-05-24 15:02:16 -0700165type versionedHeaderProperties struct {
Dan Albert269fab82017-02-15 17:31:33 -0800166 // Base directory of the headers being installed. As an example:
167 //
Dan Albert97f9c962018-05-24 15:02:16 -0700168 // versioned_ndk_headers {
Dan Albert269fab82017-02-15 17:31:33 -0800169 // name: "foo",
170 // from: "include",
171 // to: "",
172 // }
173 //
174 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
175 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800176 From *string
Dan Albert269fab82017-02-15 17:31:33 -0800177
178 // Install path within the sysroot. This is relative to usr/include.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800179 To *string
Dan Albert269fab82017-02-15 17:31:33 -0800180
181 // Path to the NOTICE file associated with the headers.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800182 License *string
Dan Albert269fab82017-02-15 17:31:33 -0800183}
184
185// Like ndk_headers, but preprocesses the headers with the bionic versioner:
186// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
187//
188// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
189// module does not have the srcs property, and operates on a full directory (the `from` property).
190//
191// Note that this is really only built to handle bionic/libc/include.
Dan Albert97f9c962018-05-24 15:02:16 -0700192type versionedHeaderModule struct {
Dan Albert269fab82017-02-15 17:31:33 -0800193 android.ModuleBase
194
Dan Albert97f9c962018-05-24 15:02:16 -0700195 properties versionedHeaderProperties
Dan Albert269fab82017-02-15 17:31:33 -0800196
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000197 srcPaths android.Paths
Colin Cross0875c522017-11-28 17:34:01 -0800198 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -0800199 licensePath android.Path
Dan Albert269fab82017-02-15 17:31:33 -0800200}
201
Spandan Das0773a602022-08-16 00:55:11 +0000202// Return the glob pattern to find all .h files beneath `dir`
203func headerGlobPattern(dir string) string {
204 return filepath.Join(dir, "**", "*.h")
205}
206
Dan Albert97f9c962018-05-24 15:02:16 -0700207func (m *versionedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800208 if String(m.properties.License) == "" {
Dan Albert269fab82017-02-15 17:31:33 -0800209 ctx.PropertyErrorf("license", "field is required")
210 }
211
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800212 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albert269fab82017-02-15 17:31:33 -0800213
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800214 fromSrcPath := android.PathForModuleSrc(ctx, String(m.properties.From))
215 toOutputPath := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000216 m.srcPaths = ctx.GlobFiles(headerGlobPattern(fromSrcPath.String()), nil)
Dan Albert269fab82017-02-15 17:31:33 -0800217 var installPaths []android.WritablePath
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000218 for _, header := range m.srcPaths {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800219 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), String(m.properties.To))
Dan Albert269fab82017-02-15 17:31:33 -0800220 installPath := installDir.Join(ctx, header.Base())
221 installPaths = append(installPaths, installPath)
Colin Cross0875c522017-11-28 17:34:01 -0800222 m.installPaths = append(m.installPaths, installPath)
Dan Albert269fab82017-02-15 17:31:33 -0800223 }
224
225 if len(m.installPaths) == 0 {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800226 ctx.ModuleErrorf("glob %q matched zero files", String(m.properties.From))
Dan Albert269fab82017-02-15 17:31:33 -0800227 }
228
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000229 processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, m.srcPaths, installPaths)
Dan Willemsenb916b802017-03-19 13:44:32 -0700230}
231
Colin Cross07e51612019-03-05 12:46:40 -0800232func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path,
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000233 srcPaths android.Paths, installPaths []android.WritablePath) android.Path {
Dan Albert269fab82017-02-15 17:31:33 -0800234 // The versioner depends on a dependencies directory to simplify determining include paths
235 // when parsing headers. This directory contains architecture specific directories as well
236 // as a common directory, each of which contains symlinks to the actually directories to
237 // be included.
238 //
239 // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
240 // depend on these headers.
241 // TODO(http://b/35673191): Update the versioner to use a --sysroot.
242 depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
243 depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
244 for i, path := range depsGlob {
Colin Cross988414c2020-01-11 01:11:46 +0000245 if ctx.IsSymlink(path) {
246 dest := ctx.Readlink(path)
Dan Albert269fab82017-02-15 17:31:33 -0800247 // Additional .. to account for the symlink itself.
248 depsGlob[i] = android.PathForSource(
249 ctx, filepath.Clean(filepath.Join(path.String(), "..", dest)))
250 }
251 }
252
Dan Albertd2130a92017-03-29 18:33:28 -0700253 timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp")
Colin Crossae887032017-10-23 17:16:14 -0700254 ctx.Build(pctx, android.BuildParams{
Dan Albert97f9c962018-05-24 15:02:16 -0700255 Rule: versionBionicHeaders,
Colin Cross67a5c132017-05-09 13:45:28 -0700256 Description: "versioner preprocess " + srcDir.Rel(),
Dan Albertd2130a92017-03-29 18:33:28 -0700257 Output: timestampFile,
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000258 Implicits: append(srcPaths, depsGlob...),
Dan Albert269fab82017-02-15 17:31:33 -0800259 ImplicitOutputs: installPaths,
260 Args: map[string]string{
261 "depsPath": depsPath.String(),
Dan Willemsenb916b802017-03-19 13:44:32 -0700262 "srcDir": srcDir.String(),
263 "outDir": outDir.String(),
Dan Albert269fab82017-02-15 17:31:33 -0800264 },
265 })
Dan Willemsenb916b802017-03-19 13:44:32 -0700266
267 return timestampFile
Dan Albert269fab82017-02-15 17:31:33 -0800268}
269
Patrice Arruda6ea42112019-04-03 08:43:30 -0700270// versioned_ndk_headers preprocesses the headers with the bionic versioner:
271// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
272// Unlike the ndk_headers soong module, versioned_ndk_headers operates on a
273// directory level specified in `from` property. This is only used to process
274// the bionic/libc/include directory.
Dan Albert97f9c962018-05-24 15:02:16 -0700275func versionedNdkHeadersFactory() android.Module {
276 module := &versionedHeaderModule{}
Colin Cross36242852017-06-23 15:06:31 -0700277
278 module.AddProperties(&module.properties)
279
Dan Willemsen4f644da2018-10-10 17:18:08 -0700280 android.InitAndroidModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700281
282 return module
Dan Albert269fab82017-02-15 17:31:33 -0800283}
Dan Albertcb1b4b22018-05-24 15:06:11 -0700284
Spandan Das0773a602022-08-16 00:55:11 +0000285// preprocessed_ndk_header {
286//
287// name: "foo",
288// preprocessor: "foo.sh",
289// srcs: [...],
290// to: "android",
291//
292// }
Dan Albertcb1b4b22018-05-24 15:06:11 -0700293//
294// Will invoke the preprocessor as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700295//
296// $preprocessor -o $SYSROOT/usr/include/android/needs_preproc.h $src
297//
Dan Albertcb1b4b22018-05-24 15:06:11 -0700298// For each src in srcs.
299type preprocessedHeadersProperties struct {
300 // The preprocessor to run. Must be a program inside the source directory
301 // with no dependencies.
302 Preprocessor *string
303
304 // Source path to the files to be preprocessed.
305 Srcs []string
306
307 // Source paths that should be excluded from the srcs glob.
308 Exclude_srcs []string
309
310 // Install path within the sysroot. This is relative to usr/include.
311 To *string
312
313 // Path to the NOTICE file associated with the headers.
314 License *string
315}
316
317type preprocessedHeadersModule struct {
318 android.ModuleBase
319
320 properties preprocessedHeadersProperties
321
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000322 srcPaths android.Paths
Dan Albertcb1b4b22018-05-24 15:06:11 -0700323 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -0800324 licensePath android.Path
Dan Albertcb1b4b22018-05-24 15:06:11 -0700325}
326
Dan Albertcb1b4b22018-05-24 15:06:11 -0700327func (m *preprocessedHeadersModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
328 if String(m.properties.License) == "" {
329 ctx.PropertyErrorf("license", "field is required")
330 }
331
332 preprocessor := android.PathForModuleSrc(ctx, String(m.properties.Preprocessor))
333 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
334
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000335 m.srcPaths = android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700336 installDir := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000337 for _, src := range m.srcPaths {
Dan Albertcb1b4b22018-05-24 15:06:11 -0700338 installPath := installDir.Join(ctx, src.Base())
339 m.installPaths = append(m.installPaths, installPath)
340
341 ctx.Build(pctx, android.BuildParams{
342 Rule: preprocessNdkHeader,
343 Description: "preprocess " + src.Rel(),
344 Input: src,
345 Output: installPath,
346 Args: map[string]string{
347 "preprocessor": preprocessor.String(),
348 },
349 })
350 }
351
352 if len(m.installPaths) == 0 {
353 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
354 }
355}
356
Patrice Arruda6ea42112019-04-03 08:43:30 -0700357// preprocessed_ndk_headers preprocesses all the ndk headers listed in the srcs
358// property by executing the command defined in the preprocessor property.
Dan Albertcb1b4b22018-05-24 15:06:11 -0700359func preprocessedNdkHeadersFactory() android.Module {
360 module := &preprocessedHeadersModule{}
361
362 module.AddProperties(&module.properties)
363
Dan Willemsen4f644da2018-10-10 17:18:08 -0700364 android.InitAndroidModule(module)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700365
366 return module
367}