blob: 663bd2af208a14359666ea43770cd00c54d1e9c9 [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
Dan Albert19ff8b42018-05-24 15:00:48 -070068 // Source paths that should be excluded from the srcs glob.
69 Exclude_srcs []string
70
Dan Albertc6345fb2016-10-20 01:36:11 -070071 // Path to the NOTICE file associated with the headers.
Nan Zhang0007d812017-11-07 10:57:05 -080072 License *string
Dan Albert914449f2016-06-17 16:45:24 -070073}
74
75type headerModule struct {
76 android.ModuleBase
77
Dan Albert71222052018-05-24 15:00:05 -070078 properties headerProperties
Dan Albert914449f2016-06-17 16:45:24 -070079
Colin Cross0875c522017-11-28 17:34:01 -080080 installPaths android.Paths
Dan Albertc6345fb2016-10-20 01:36:11 -070081 licensePath android.ModuleSrcPath
Dan Albert914449f2016-06-17 16:45:24 -070082}
83
Colin Cross1e676be2016-10-12 14:38:15 -070084func (m *headerModule) DepsMutator(ctx android.BottomUpMutatorContext) {
85}
86
Dan Albert269fab82017-02-15 17:31:33 -080087func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string,
88 to string) android.OutputPath {
89 // Output path is the sysroot base + "usr/include" + to directory + directory component
90 // of the file without the leading from directory stripped.
91 //
92 // Given:
93 // sysroot base = "ndk/sysroot"
94 // from = "include/foo"
95 // to = "bar"
96 // header = "include/foo/woodly/doodly.h"
97 // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
98
99 // full/platform/path/to/include/foo
100 fullFromPath := android.PathForModuleSrc(ctx, from)
101
102 // full/platform/path/to/include/foo/woodly
103 headerDir := filepath.Dir(header.String())
104
105 // woodly
106 strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
107 if err != nil {
108 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
109 fullFromPath.String(), err)
110 }
111
112 // full/platform/path/to/sysroot/usr/include/bar/woodly
113 installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir)
114
115 // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
116 return installDir
117}
118
Dan Albert914449f2016-06-17 16:45:24 -0700119func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhang0007d812017-11-07 10:57:05 -0800120 if String(m.properties.License) == "" {
Dan Albertc6345fb2016-10-20 01:36:11 -0700121 ctx.PropertyErrorf("license", "field is required")
122 }
123
Nan Zhang0007d812017-11-07 10:57:05 -0800124 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albertc6345fb2016-10-20 01:36:11 -0700125
Lazar Trsiccdb710f2017-11-24 16:38:47 +0100126 // When generating NDK prebuilts, skip installing MIPS headers,
127 // but keep them when doing regular platform build.
128 // Ndk_abis property is only set to true with build/soong/scripts/build-ndk-prebuilts.sh
129 // TODO: Revert this once MIPS is supported in NDK again.
130 if Bool(ctx.AConfig().Ndk_abis) && strings.Contains(ctx.ModuleName(), "mips") {
131 return
132 }
133
Dan Albert19ff8b42018-05-24 15:00:48 -0700134 srcFiles := ctx.ExpandSources(m.properties.Srcs, m.properties.Exclude_srcs)
Dan Albert914449f2016-06-17 16:45:24 -0700135 for _, header := range srcFiles {
Nan Zhang0007d812017-11-07 10:57:05 -0800136 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From),
137 String(m.properties.To))
Colin Cross5c517922017-08-31 12:29:17 -0700138 installedPath := ctx.InstallFile(installDir, header.Base(), header)
Dan Albert269fab82017-02-15 17:31:33 -0800139 installPath := installDir.Join(ctx, header.Base())
140 if installPath != installedPath {
141 panic(fmt.Sprintf(
142 "expected header install path (%q) not equal to actual install path %q",
143 installPath, installedPath))
Dan Albert914449f2016-06-17 16:45:24 -0700144 }
Colin Cross0875c522017-11-28 17:34:01 -0800145 m.installPaths = append(m.installPaths, installPath)
Dan Albert914449f2016-06-17 16:45:24 -0700146 }
147
148 if len(m.installPaths) == 0 {
149 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
150 }
151}
152
Colin Cross36242852017-06-23 15:06:31 -0700153func ndkHeadersFactory() android.Module {
Dan Albert914449f2016-06-17 16:45:24 -0700154 module := &headerModule{}
Colin Cross36242852017-06-23 15:06:31 -0700155 module.AddProperties(&module.properties)
156 android.InitAndroidModule(module)
157 return module
Dan Albert914449f2016-06-17 16:45:24 -0700158}
Dan Albert269fab82017-02-15 17:31:33 -0800159
Dan Albert71222052018-05-24 15:00:05 -0700160type preprocessedHeaderProperties struct {
Dan Albert269fab82017-02-15 17:31:33 -0800161 // Base directory of the headers being installed. As an example:
162 //
163 // preprocessed_ndk_headers {
164 // name: "foo",
165 // from: "include",
166 // to: "",
167 // }
168 //
169 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
170 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800171 From *string
Dan Albert269fab82017-02-15 17:31:33 -0800172
173 // Install path within the sysroot. This is relative to usr/include.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800174 To *string
Dan Albert269fab82017-02-15 17:31:33 -0800175
176 // Path to the NOTICE file associated with the headers.
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800177 License *string
Dan Albert269fab82017-02-15 17:31:33 -0800178}
179
180// Like ndk_headers, but preprocesses the headers with the bionic versioner:
181// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
182//
183// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
184// module does not have the srcs property, and operates on a full directory (the `from` property).
185//
186// Note that this is really only built to handle bionic/libc/include.
187type preprocessedHeaderModule struct {
188 android.ModuleBase
189
Dan Albert71222052018-05-24 15:00:05 -0700190 properties preprocessedHeaderProperties
Dan Albert269fab82017-02-15 17:31:33 -0800191
Colin Cross0875c522017-11-28 17:34:01 -0800192 installPaths android.Paths
Dan Albert269fab82017-02-15 17:31:33 -0800193 licensePath android.ModuleSrcPath
194}
195
196func (m *preprocessedHeaderModule) DepsMutator(ctx android.BottomUpMutatorContext) {
197}
198
199func (m *preprocessedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800200 if String(m.properties.License) == "" {
Dan Albert269fab82017-02-15 17:31:33 -0800201 ctx.PropertyErrorf("license", "field is required")
202 }
203
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800204 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albert269fab82017-02-15 17:31:33 -0800205
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800206 fromSrcPath := android.PathForModuleSrc(ctx, String(m.properties.From))
207 toOutputPath := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
Dan Willemsen540a78c2018-02-26 21:50:08 -0800208 srcFiles := ctx.GlobFiles(filepath.Join(fromSrcPath.String(), "**/*.h"), nil)
Dan Albert269fab82017-02-15 17:31:33 -0800209 var installPaths []android.WritablePath
210 for _, header := range srcFiles {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800211 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), String(m.properties.To))
Dan Albert269fab82017-02-15 17:31:33 -0800212 installPath := installDir.Join(ctx, header.Base())
213 installPaths = append(installPaths, installPath)
Colin Cross0875c522017-11-28 17:34:01 -0800214 m.installPaths = append(m.installPaths, installPath)
Dan Albert269fab82017-02-15 17:31:33 -0800215 }
216
217 if len(m.installPaths) == 0 {
Nan Zhanga5e7cb42017-11-09 22:42:32 -0800218 ctx.ModuleErrorf("glob %q matched zero files", String(m.properties.From))
Dan Albert269fab82017-02-15 17:31:33 -0800219 }
220
Dan Willemsenb916b802017-03-19 13:44:32 -0700221 processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, srcFiles, installPaths)
222}
223
224func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path, srcFiles android.Paths, installPaths []android.WritablePath) android.Path {
Dan Albert269fab82017-02-15 17:31:33 -0800225 // The versioner depends on a dependencies directory to simplify determining include paths
226 // when parsing headers. This directory contains architecture specific directories as well
227 // as a common directory, each of which contains symlinks to the actually directories to
228 // be included.
229 //
230 // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
231 // depend on these headers.
232 // TODO(http://b/35673191): Update the versioner to use a --sysroot.
233 depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
234 depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
235 for i, path := range depsGlob {
236 fileInfo, err := os.Lstat(path.String())
237 if err != nil {
238 ctx.ModuleErrorf("os.Lstat(%q) failed: %s", path.String, err)
239 }
240 if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
241 dest, err := os.Readlink(path.String())
242 if err != nil {
243 ctx.ModuleErrorf("os.Readlink(%q) failed: %s",
244 path.String, err)
245 }
246 // 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 Albert269fab82017-02-15 17:31:33 -0800254 Rule: preprocessBionicHeaders,
Colin Cross67a5c132017-05-09 13:45:28 -0700255 Description: "versioner preprocess " + srcDir.Rel(),
Dan Albertd2130a92017-03-29 18:33:28 -0700256 Output: timestampFile,
Dan Albert269fab82017-02-15 17:31:33 -0800257 Implicits: append(srcFiles, depsGlob...),
258 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
Colin Cross36242852017-06-23 15:06:31 -0700269func preprocessedNdkHeadersFactory() android.Module {
Dan Albert269fab82017-02-15 17:31:33 -0800270 module := &preprocessedHeaderModule{}
Colin Cross36242852017-06-23 15:06:31 -0700271
272 module.AddProperties(&module.properties)
273
Dan Albert269fab82017-02-15 17:31:33 -0800274 // Host module rather than device module because device module install steps
275 // do not get run when embedded in make. We're not any of the existing
276 // module types that can be exposed via the Android.mk exporter, so just use
277 // a host module.
Colin Cross36242852017-06-23 15:06:31 -0700278 android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibFirst)
279
280 return module
Dan Albert269fab82017-02-15 17:31:33 -0800281}