blob: 866479dc0d593a82a0b1316f7af84114d6ba9696 [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"
19 "os"
Dan Albert914449f2016-06-17 16:45:24 -070020 "path/filepath"
Lazar Trsiccdb710f2017-11-24 16:38:47 +010021 "strings"
Dan Albert914449f2016-06-17 16:45:24 -070022
23 "github.com/google/blueprint"
24
25 "android/soong/android"
26)
27
Dan Albert269fab82017-02-15 17:31:33 -080028var (
29 preprocessBionicHeaders = pctx.AndroidStaticRule("preprocessBionicHeaders",
30 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 Albert269fab82017-02-15 17:31:33 -080037)
38
39func init() {
40 pctx.HostBinToolVariable("versionerCmd", "versioner")
41}
42
Dan Albert914449f2016-06-17 16:45:24 -070043// Returns the NDK base include path for use with sdk_version current. Usable with -I.
44func getCurrentIncludePath(ctx android.ModuleContext) android.OutputPath {
45 return getNdkSysrootBase(ctx).Join(ctx, "usr/include")
46}
47
Dan Albert71222052018-05-24 15:00:05 -070048type headerProperties struct {
Dan Albert914449f2016-06-17 16:45:24 -070049 // Base directory of the headers being installed. As an example:
50 //
51 // ndk_headers {
52 // name: "foo",
53 // from: "include",
54 // to: "",
55 // srcs: ["include/foo/bar/baz.h"],
56 // }
57 //
58 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
59 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhang0007d812017-11-07 10:57:05 -080060 From *string
Dan Albert914449f2016-06-17 16:45:24 -070061
62 // Install path within the sysroot. This is relative to usr/include.
Nan Zhang0007d812017-11-07 10:57:05 -080063 To *string
Dan Albert914449f2016-06-17 16:45:24 -070064
65 // List of headers to install. Glob compatible. Common case is "include/**/*.h".
66 Srcs []string
Dan Albertc6345fb2016-10-20 01:36:11 -070067
68 // Path to the NOTICE file associated with the headers.
Nan Zhang0007d812017-11-07 10:57:05 -080069 License *string
Dan Albert914449f2016-06-17 16:45:24 -070070}
71
72type headerModule struct {
73 android.ModuleBase
74
Dan Albert71222052018-05-24 15:00:05 -070075 properties headerProperties
Dan Albert914449f2016-06-17 16:45:24 -070076
Colin Cross0875c522017-11-28 17:34:01 -080077 installPaths android.Paths
Dan Albertc6345fb2016-10-20 01:36:11 -070078 licensePath android.ModuleSrcPath
Dan Albert914449f2016-06-17 16:45:24 -070079}
80
Colin Cross1e676be2016-10-12 14:38:15 -070081func (m *headerModule) DepsMutator(ctx android.BottomUpMutatorContext) {
82}
83
Dan Albert269fab82017-02-15 17:31:33 -080084func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string,
85 to string) android.OutputPath {
86 // Output path is the sysroot base + "usr/include" + to directory + directory component
87 // of the file without the leading from directory stripped.
88 //
89 // Given:
90 // sysroot base = "ndk/sysroot"
91 // from = "include/foo"
92 // to = "bar"
93 // header = "include/foo/woodly/doodly.h"
94 // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
95
96 // full/platform/path/to/include/foo
97 fullFromPath := android.PathForModuleSrc(ctx, from)
98
99 // full/platform/path/to/include/foo/woodly
100 headerDir := filepath.Dir(header.String())
101
102 // woodly
103 strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
104 if err != nil {
105 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
106 fullFromPath.String(), err)
107 }
108
109 // full/platform/path/to/sysroot/usr/include/bar/woodly
110 installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir)
111
112 // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
113 return installDir
114}
115
Dan Albert914449f2016-06-17 16:45:24 -0700116func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhang0007d812017-11-07 10:57:05 -0800117 if String(m.properties.License) == "" {
Dan Albertc6345fb2016-10-20 01:36:11 -0700118 ctx.PropertyErrorf("license", "field is required")
119 }
120
Nan Zhang0007d812017-11-07 10:57:05 -0800121 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albertc6345fb2016-10-20 01:36:11 -0700122
Lazar Trsiccdb710f2017-11-24 16:38:47 +0100123 // When generating NDK prebuilts, skip installing MIPS headers,
124 // but keep them when doing regular platform build.
125 // Ndk_abis property is only set to true with build/soong/scripts/build-ndk-prebuilts.sh
126 // TODO: Revert this once MIPS is supported in NDK again.
127 if Bool(ctx.AConfig().Ndk_abis) && strings.Contains(ctx.ModuleName(), "mips") {
128 return
129 }
130
Dan Albert914449f2016-06-17 16:45:24 -0700131 srcFiles := ctx.ExpandSources(m.properties.Srcs, nil)
132 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
Colin Cross36242852017-06-23 15:06:31 -0700150func ndkHeadersFactory() android.Module {
Dan Albert914449f2016-06-17 16:45:24 -0700151 module := &headerModule{}
Colin Cross36242852017-06-23 15:06:31 -0700152 module.AddProperties(&module.properties)
153 android.InitAndroidModule(module)
154 return module
Dan Albert914449f2016-06-17 16:45:24 -0700155}
Dan Albert269fab82017-02-15 17:31:33 -0800156
Dan Albert71222052018-05-24 15:00:05 -0700157type preprocessedHeaderProperties struct {
Dan Albert269fab82017-02-15 17:31:33 -0800158 // Base directory of the headers being installed. As an example:
159 //
160 // preprocessed_ndk_headers {
161 // name: "foo",
162 // from: "include",
163 // to: "",
164 // }
165 //
166 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
167 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800168 From *string
Dan Albert269fab82017-02-15 17:31:33 -0800169
170 // Install path within the sysroot. This is relative to usr/include.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800171 To *string
Dan Albert269fab82017-02-15 17:31:33 -0800172
173 // Path to the NOTICE file associated with the headers.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800174 License *string
Dan Albert269fab82017-02-15 17:31:33 -0800175}
176
177// Like ndk_headers, but preprocesses the headers with the bionic versioner:
178// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
179//
180// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
181// module does not have the srcs property, and operates on a full directory (the `from` property).
182//
183// Note that this is really only built to handle bionic/libc/include.
184type preprocessedHeaderModule struct {
185 android.ModuleBase
186
Dan Albert71222052018-05-24 15:00:05 -0700187 properties preprocessedHeaderProperties
Dan Albert269fab82017-02-15 17:31:33 -0800188
Colin Cross0875c522017-11-28 17:34:01 -0800189 installPaths android.Paths
Dan Albert269fab82017-02-15 17:31:33 -0800190 licensePath android.ModuleSrcPath
191}
192
193func (m *preprocessedHeaderModule) DepsMutator(ctx android.BottomUpMutatorContext) {
194}
195
196func (m *preprocessedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800197 if String(m.properties.License) == "" {
Dan Albert269fab82017-02-15 17:31:33 -0800198 ctx.PropertyErrorf("license", "field is required")
199 }
200
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800201 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albert269fab82017-02-15 17:31:33 -0800202
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800203 fromSrcPath := android.PathForModuleSrc(ctx, String(m.properties.From))
204 toOutputPath := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Dan Willemsen540a78c2018-02-26 21:50:08 -0800205 srcFiles := ctx.GlobFiles(filepath.Join(fromSrcPath.String(), "**/*.h"), nil)
Dan Albert269fab82017-02-15 17:31:33 -0800206 var installPaths []android.WritablePath
207 for _, header := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800208 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), String(m.properties.To))
Dan Albert269fab82017-02-15 17:31:33 -0800209 installPath := installDir.Join(ctx, header.Base())
210 installPaths = append(installPaths, installPath)
Colin Cross0875c522017-11-28 17:34:01 -0800211 m.installPaths = append(m.installPaths, installPath)
Dan Albert269fab82017-02-15 17:31:33 -0800212 }
213
214 if len(m.installPaths) == 0 {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800215 ctx.ModuleErrorf("glob %q matched zero files", String(m.properties.From))
Dan Albert269fab82017-02-15 17:31:33 -0800216 }
217
Dan Willemsenb916b802017-03-19 13:44:32 -0700218 processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, srcFiles, installPaths)
219}
220
221func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path, srcFiles android.Paths, installPaths []android.WritablePath) android.Path {
Dan Albert269fab82017-02-15 17:31:33 -0800222 // The versioner depends on a dependencies directory to simplify determining include paths
223 // when parsing headers. This directory contains architecture specific directories as well
224 // as a common directory, each of which contains symlinks to the actually directories to
225 // be included.
226 //
227 // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
228 // depend on these headers.
229 // TODO(http://b/35673191): Update the versioner to use a --sysroot.
230 depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
231 depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
232 for i, path := range depsGlob {
233 fileInfo, err := os.Lstat(path.String())
234 if err != nil {
235 ctx.ModuleErrorf("os.Lstat(%q) failed: %s", path.String, err)
236 }
237 if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
238 dest, err := os.Readlink(path.String())
239 if err != nil {
240 ctx.ModuleErrorf("os.Readlink(%q) failed: %s",
241 path.String, err)
242 }
243 // Additional .. to account for the symlink itself.
244 depsGlob[i] = android.PathForSource(
245 ctx, filepath.Clean(filepath.Join(path.String(), "..", dest)))
246 }
247 }
248
Dan Albertd2130a92017-03-29 18:33:28 -0700249 timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp")
Colin Crossae887032017-10-23 17:16:14 -0700250 ctx.Build(pctx, android.BuildParams{
Dan Albert269fab82017-02-15 17:31:33 -0800251 Rule: preprocessBionicHeaders,
Colin Cross67a5c132017-05-09 13:45:28 -0700252 Description: "versioner preprocess " + srcDir.Rel(),
Dan Albertd2130a92017-03-29 18:33:28 -0700253 Output: timestampFile,
Dan Albert269fab82017-02-15 17:31:33 -0800254 Implicits: append(srcFiles, depsGlob...),
255 ImplicitOutputs: installPaths,
256 Args: map[string]string{
257 "depsPath": depsPath.String(),
Dan Willemsenb916b802017-03-19 13:44:32 -0700258 "srcDir": srcDir.String(),
259 "outDir": outDir.String(),
Dan Albert269fab82017-02-15 17:31:33 -0800260 },
261 })
Dan Willemsenb916b802017-03-19 13:44:32 -0700262
263 return timestampFile
Dan Albert269fab82017-02-15 17:31:33 -0800264}
265
Colin Cross36242852017-06-23 15:06:31 -0700266func preprocessedNdkHeadersFactory() android.Module {
Dan Albert269fab82017-02-15 17:31:33 -0800267 module := &preprocessedHeaderModule{}
Colin Cross36242852017-06-23 15:06:31 -0700268
269 module.AddProperties(&module.properties)
270
Dan Albert269fab82017-02-15 17:31:33 -0800271 // Host module rather than device module because device module install steps
272 // do not get run when embedded in make. We're not any of the existing
273 // module types that can be exposed via the Android.mk exporter, so just use
274 // a host module.
Colin Cross36242852017-06-23 15:06:31 -0700275 android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibFirst)
276
277 return module
Dan Albert269fab82017-02-15 17:31:33 -0800278}