blob: 0fbc8d0f3ce99fa5202f480793c9075d902905b4 [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
Dan Albert914449f2016-06-17 16:45:24 -070021 "android/soong/android"
Colin Cross8ff10582023-12-07 13:10:56 -080022 "github.com/google/blueprint"
Dan Albert914449f2016-06-17 16:45:24 -070023)
24
Dan Albert269fab82017-02-15 17:31:33 -080025var (
Dan Albert97f9c962018-05-24 15:02:16 -070026 versionBionicHeaders = pctx.AndroidStaticRule("versionBionicHeaders",
Dan Albert269fab82017-02-15 17:31:33 -080027 blueprint.RuleParams{
Dan Albertd2130a92017-03-29 18:33:28 -070028 // The `&& touch $out` isn't really necessary, but Blueprint won't
29 // let us have only implicit outputs.
30 Command: "$versionerCmd -o $outDir $srcDir $depsPath && touch $out",
Dan Albert269fab82017-02-15 17:31:33 -080031 CommandDeps: []string{"$versionerCmd"},
Dan Albert269fab82017-02-15 17:31:33 -080032 },
Dan Albertd2130a92017-03-29 18:33:28 -070033 "depsPath", "srcDir", "outDir")
Dan Albertcb1b4b22018-05-24 15:06:11 -070034
35 preprocessNdkHeader = pctx.AndroidStaticRule("preprocessNdkHeader",
36 blueprint.RuleParams{
37 Command: "$preprocessor -o $out $in",
38 CommandDeps: []string{"$preprocessor"},
39 },
40 "preprocessor")
Dan Albert269fab82017-02-15 17:31:33 -080041)
42
43func init() {
Logan Chien2f066352018-10-25 18:11:09 +080044 pctx.SourcePathVariable("versionerCmd", "prebuilts/clang-tools/${config.HostPrebuiltTag}/bin/versioner")
Dan Albert269fab82017-02-15 17:31:33 -080045}
46
Dan Albert914449f2016-06-17 16:45:24 -070047// Returns the NDK base include path for use with sdk_version current. Usable with -I.
Colin Cross70dda7e2019-10-01 22:05:35 -070048func getCurrentIncludePath(ctx android.ModuleContext) android.InstallPath {
Dan Albert914449f2016-06-17 16:45:24 -070049 return getNdkSysrootBase(ctx).Join(ctx, "usr/include")
50}
51
Dan Albert71222052018-05-24 15:00:05 -070052type headerProperties struct {
Dan Albert914449f2016-06-17 16:45:24 -070053 // Base directory of the headers being installed. As an example:
54 //
55 // ndk_headers {
56 // name: "foo",
57 // from: "include",
58 // to: "",
59 // srcs: ["include/foo/bar/baz.h"],
60 // }
61 //
62 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
63 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhang0007d812017-11-07 10:57:05 -080064 From *string
Dan Albert914449f2016-06-17 16:45:24 -070065
66 // Install path within the sysroot. This is relative to usr/include.
Nan Zhang0007d812017-11-07 10:57:05 -080067 To *string
Dan Albert914449f2016-06-17 16:45:24 -070068
69 // List of headers to install. Glob compatible. Common case is "include/**/*.h".
Colin Cross27b922f2019-03-04 22:35:41 -080070 Srcs []string `android:"path"`
Dan Albertc6345fb2016-10-20 01:36:11 -070071
Dan Albert19ff8b42018-05-24 15:00:48 -070072 // Source paths that should be excluded from the srcs glob.
Colin Cross27b922f2019-03-04 22:35:41 -080073 Exclude_srcs []string `android:"path"`
Dan Albert19ff8b42018-05-24 15:00:48 -070074
Dan Albertc6345fb2016-10-20 01:36:11 -070075 // Path to the NOTICE file associated with the headers.
Colin Cross27b922f2019-03-04 22:35:41 -080076 License *string `android:"path"`
Dan Albert914449f2016-06-17 16:45:24 -070077}
78
79type headerModule struct {
80 android.ModuleBase
81
Dan Albert71222052018-05-24 15:00:05 -070082 properties headerProperties
Dan Albert914449f2016-06-17 16:45:24 -070083
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +000084 srcPaths android.Paths
Colin Cross0875c522017-11-28 17:34:01 -080085 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -080086 licensePath android.Path
Dan Albert914449f2016-06-17 16:45:24 -070087}
88
Dan Albert269fab82017-02-15 17:31:33 -080089func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string,
Colin Cross70dda7e2019-10-01 22:05:35 -070090 to string) android.InstallPath {
Dan Albert269fab82017-02-15 17:31:33 -080091 // Output path is the sysroot base + "usr/include" + to directory + directory component
92 // of the file without the leading from directory stripped.
93 //
94 // Given:
95 // sysroot base = "ndk/sysroot"
96 // from = "include/foo"
97 // to = "bar"
98 // header = "include/foo/woodly/doodly.h"
99 // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
100
101 // full/platform/path/to/include/foo
102 fullFromPath := android.PathForModuleSrc(ctx, from)
103
104 // full/platform/path/to/include/foo/woodly
105 headerDir := filepath.Dir(header.String())
106
107 // woodly
108 strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
109 if err != nil {
110 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
111 fullFromPath.String(), err)
112 }
113
114 // full/platform/path/to/sysroot/usr/include/bar/woodly
115 installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir)
116
117 // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
118 return installDir
119}
120
Dan Albert914449f2016-06-17 16:45:24 -0700121func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhang0007d812017-11-07 10:57:05 -0800122 if String(m.properties.License) == "" {
Dan Albertc6345fb2016-10-20 01:36:11 -0700123 ctx.PropertyErrorf("license", "field is required")
124 }
125
Nan Zhang0007d812017-11-07 10:57:05 -0800126 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albertc6345fb2016-10-20 01:36:11 -0700127
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000128 m.srcPaths = android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
129 for _, header := range m.srcPaths {
Nan Zhang0007d812017-11-07 10:57:05 -0800130 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From),
131 String(m.properties.To))
Colin Cross5c517922017-08-31 12:29:17 -0700132 installedPath := ctx.InstallFile(installDir, header.Base(), header)
Dan Albert269fab82017-02-15 17:31:33 -0800133 installPath := installDir.Join(ctx, header.Base())
134 if installPath != installedPath {
135 panic(fmt.Sprintf(
136 "expected header install path (%q) not equal to actual install path %q",
137 installPath, installedPath))
Dan Albert914449f2016-06-17 16:45:24 -0700138 }
Colin Cross0875c522017-11-28 17:34:01 -0800139 m.installPaths = append(m.installPaths, installPath)
Dan Albert914449f2016-06-17 16:45:24 -0700140 }
141
142 if len(m.installPaths) == 0 {
143 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
144 }
145}
146
Patrice Arruda6ea42112019-04-03 08:43:30 -0700147// ndk_headers installs the sets of ndk headers defined in the srcs property
148// to the sysroot base + "usr/include" + to directory + directory component.
149// ndk_headers requires the license file to be specified. Example:
150//
Colin Crossd079e0b2022-08-16 10:27:33 -0700151// Given:
152// sysroot base = "ndk/sysroot"
153// from = "include/foo"
154// to = "bar"
155// header = "include/foo/woodly/doodly.h"
156// output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
Spandan Das319711b2023-09-19 19:04:41 +0000157func NdkHeadersFactory() android.Module {
Dan Albert914449f2016-06-17 16:45:24 -0700158 module := &headerModule{}
Colin Cross36242852017-06-23 15:06:31 -0700159 module.AddProperties(&module.properties)
160 android.InitAndroidModule(module)
161 return module
Dan Albert914449f2016-06-17 16:45:24 -0700162}
Dan Albert269fab82017-02-15 17:31:33 -0800163
Dan Albert97f9c962018-05-24 15:02:16 -0700164type versionedHeaderProperties struct {
Dan Albert269fab82017-02-15 17:31:33 -0800165 // Base directory of the headers being installed. As an example:
166 //
Dan Albert97f9c962018-05-24 15:02:16 -0700167 // versioned_ndk_headers {
Dan Albert269fab82017-02-15 17:31:33 -0800168 // name: "foo",
169 // from: "include",
170 // to: "",
171 // }
172 //
173 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
174 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800175 From *string
Dan Albert269fab82017-02-15 17:31:33 -0800176
177 // Install path within the sysroot. This is relative to usr/include.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800178 To *string
Dan Albert269fab82017-02-15 17:31:33 -0800179
180 // Path to the NOTICE file associated with the headers.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800181 License *string
Dan Albert269fab82017-02-15 17:31:33 -0800182}
183
184// Like ndk_headers, but preprocesses the headers with the bionic versioner:
185// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
186//
187// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
188// module does not have the srcs property, and operates on a full directory (the `from` property).
189//
190// Note that this is really only built to handle bionic/libc/include.
Dan Albert97f9c962018-05-24 15:02:16 -0700191type versionedHeaderModule struct {
Dan Albert269fab82017-02-15 17:31:33 -0800192 android.ModuleBase
193
Dan Albert97f9c962018-05-24 15:02:16 -0700194 properties versionedHeaderProperties
Dan Albert269fab82017-02-15 17:31:33 -0800195
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000196 srcPaths android.Paths
Colin Cross0875c522017-11-28 17:34:01 -0800197 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -0800198 licensePath android.Path
Dan Albert269fab82017-02-15 17:31:33 -0800199}
200
Spandan Das0773a602022-08-16 00:55:11 +0000201// Return the glob pattern to find all .h files beneath `dir`
202func headerGlobPattern(dir string) string {
203 return filepath.Join(dir, "**", "*.h")
204}
205
Dan Albert97f9c962018-05-24 15:02:16 -0700206func (m *versionedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800207 if String(m.properties.License) == "" {
Dan Albert269fab82017-02-15 17:31:33 -0800208 ctx.PropertyErrorf("license", "field is required")
209 }
210
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800211 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albert269fab82017-02-15 17:31:33 -0800212
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800213 fromSrcPath := android.PathForModuleSrc(ctx, String(m.properties.From))
214 toOutputPath := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000215 m.srcPaths = ctx.GlobFiles(headerGlobPattern(fromSrcPath.String()), nil)
Dan Albert269fab82017-02-15 17:31:33 -0800216 var installPaths []android.WritablePath
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000217 for _, header := range m.srcPaths {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800218 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), String(m.properties.To))
Dan Albert269fab82017-02-15 17:31:33 -0800219 installPath := installDir.Join(ctx, header.Base())
220 installPaths = append(installPaths, installPath)
Colin Cross0875c522017-11-28 17:34:01 -0800221 m.installPaths = append(m.installPaths, installPath)
Dan Albert269fab82017-02-15 17:31:33 -0800222 }
223
224 if len(m.installPaths) == 0 {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800225 ctx.ModuleErrorf("glob %q matched zero files", String(m.properties.From))
Dan Albert269fab82017-02-15 17:31:33 -0800226 }
227
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000228 processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, m.srcPaths, installPaths)
Dan Willemsenb916b802017-03-19 13:44:32 -0700229}
230
Colin Cross07e51612019-03-05 12:46:40 -0800231func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path,
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000232 srcPaths android.Paths, installPaths []android.WritablePath) android.Path {
Dan Albert269fab82017-02-15 17:31:33 -0800233 // The versioner depends on a dependencies directory to simplify determining include paths
234 // when parsing headers. This directory contains architecture specific directories as well
235 // as a common directory, each of which contains symlinks to the actually directories to
236 // be included.
237 //
238 // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
239 // depend on these headers.
240 // TODO(http://b/35673191): Update the versioner to use a --sysroot.
241 depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
242 depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
243 for i, path := range depsGlob {
Colin Cross988414c2020-01-11 01:11:46 +0000244 if ctx.IsSymlink(path) {
245 dest := ctx.Readlink(path)
Dan Albert269fab82017-02-15 17:31:33 -0800246 // Additional .. to account for the symlink itself.
247 depsGlob[i] = android.PathForSource(
248 ctx, filepath.Clean(filepath.Join(path.String(), "..", dest)))
249 }
250 }
251
Dan Albertd2130a92017-03-29 18:33:28 -0700252 timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp")
Colin Crossae887032017-10-23 17:16:14 -0700253 ctx.Build(pctx, android.BuildParams{
Dan Albert97f9c962018-05-24 15:02:16 -0700254 Rule: versionBionicHeaders,
Colin Cross67a5c132017-05-09 13:45:28 -0700255 Description: "versioner preprocess " + srcDir.Rel(),
Dan Albertd2130a92017-03-29 18:33:28 -0700256 Output: timestampFile,
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000257 Implicits: append(srcPaths, depsGlob...),
Dan Albert269fab82017-02-15 17:31:33 -0800258 ImplicitOutputs: installPaths,
259 Args: map[string]string{
260 "depsPath": depsPath.String(),
Dan Willemsenb916b802017-03-19 13:44:32 -0700261 "srcDir": srcDir.String(),
262 "outDir": outDir.String(),
Dan Albert269fab82017-02-15 17:31:33 -0800263 },
264 })
Dan Willemsenb916b802017-03-19 13:44:32 -0700265
266 return timestampFile
Dan Albert269fab82017-02-15 17:31:33 -0800267}
268
Patrice Arruda6ea42112019-04-03 08:43:30 -0700269// versioned_ndk_headers preprocesses the headers with the bionic versioner:
270// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
271// Unlike the ndk_headers soong module, versioned_ndk_headers operates on a
272// directory level specified in `from` property. This is only used to process
273// the bionic/libc/include directory.
Spandan Dasa7da3f02023-09-28 19:30:51 +0000274func VersionedNdkHeadersFactory() android.Module {
Dan Albert97f9c962018-05-24 15:02:16 -0700275 module := &versionedHeaderModule{}
Colin Cross36242852017-06-23 15:06:31 -0700276
277 module.AddProperties(&module.properties)
278
Dan Willemsen4f644da2018-10-10 17:18:08 -0700279 android.InitAndroidModule(module)
Colin Cross36242852017-06-23 15:06:31 -0700280
281 return module
Dan Albert269fab82017-02-15 17:31:33 -0800282}
Dan Albertcb1b4b22018-05-24 15:06:11 -0700283
Spandan Das0773a602022-08-16 00:55:11 +0000284// preprocessed_ndk_header {
285//
286// name: "foo",
287// preprocessor: "foo.sh",
288// srcs: [...],
289// to: "android",
290//
291// }
Dan Albertcb1b4b22018-05-24 15:06:11 -0700292//
293// Will invoke the preprocessor as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700294//
295// $preprocessor -o $SYSROOT/usr/include/android/needs_preproc.h $src
296//
Dan Albertcb1b4b22018-05-24 15:06:11 -0700297// For each src in srcs.
298type preprocessedHeadersProperties struct {
299 // The preprocessor to run. Must be a program inside the source directory
300 // with no dependencies.
301 Preprocessor *string
302
303 // Source path to the files to be preprocessed.
304 Srcs []string
305
306 // Source paths that should be excluded from the srcs glob.
307 Exclude_srcs []string
308
309 // Install path within the sysroot. This is relative to usr/include.
310 To *string
311
312 // Path to the NOTICE file associated with the headers.
313 License *string
314}
315
316type preprocessedHeadersModule struct {
317 android.ModuleBase
318
319 properties preprocessedHeadersProperties
320
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000321 srcPaths android.Paths
Dan Albertcb1b4b22018-05-24 15:06:11 -0700322 installPaths android.Paths
Colin Cross07e51612019-03-05 12:46:40 -0800323 licensePath android.Path
Dan Albertcb1b4b22018-05-24 15:06:11 -0700324}
325
Dan Albertcb1b4b22018-05-24 15:06:11 -0700326func (m *preprocessedHeadersModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
327 if String(m.properties.License) == "" {
328 ctx.PropertyErrorf("license", "field is required")
329 }
330
331 preprocessor := android.PathForModuleSrc(ctx, String(m.properties.Preprocessor))
332 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
333
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000334 m.srcPaths = android.PathsForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700335 installDir := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Aleksei Vetrov262ed1a2023-08-23 10:06:35 +0000336 for _, src := range m.srcPaths {
Dan Albertcb1b4b22018-05-24 15:06:11 -0700337 installPath := installDir.Join(ctx, src.Base())
338 m.installPaths = append(m.installPaths, installPath)
339
340 ctx.Build(pctx, android.BuildParams{
341 Rule: preprocessNdkHeader,
342 Description: "preprocess " + src.Rel(),
343 Input: src,
344 Output: installPath,
345 Args: map[string]string{
346 "preprocessor": preprocessor.String(),
347 },
348 })
349 }
350
351 if len(m.installPaths) == 0 {
352 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
353 }
354}
355
Patrice Arruda6ea42112019-04-03 08:43:30 -0700356// preprocessed_ndk_headers preprocesses all the ndk headers listed in the srcs
357// property by executing the command defined in the preprocessor property.
Dan Albertcb1b4b22018-05-24 15:06:11 -0700358func preprocessedNdkHeadersFactory() android.Module {
359 module := &preprocessedHeadersModule{}
360
361 module.AddProperties(&module.properties)
362
Dan Willemsen4f644da2018-10-10 17:18:08 -0700363 android.InitAndroidModule(module)
Dan Albertcb1b4b22018-05-24 15:06:11 -0700364
365 return module
366}