blob: a40fc72c82ca422c00412d378467dbbd125ea941 [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"
21
22 "github.com/google/blueprint"
23
24 "android/soong/android"
25)
26
Dan Albert269fab82017-02-15 17:31:33 -080027var (
28 preprocessBionicHeaders = pctx.AndroidStaticRule("preprocessBionicHeaders",
29 blueprint.RuleParams{
30 Command: "$versionerCmd $srcDir $depsPath -o $out",
31 CommandDeps: []string{"$versionerCmd"},
32 Description: "versioner preprocess $in",
33 },
34 "depsPath", "srcDir")
35)
36
37func init() {
38 pctx.HostBinToolVariable("versionerCmd", "versioner")
39}
40
Dan Albert914449f2016-06-17 16:45:24 -070041// Returns the NDK base include path for use with sdk_version current. Usable with -I.
42func getCurrentIncludePath(ctx android.ModuleContext) android.OutputPath {
43 return getNdkSysrootBase(ctx).Join(ctx, "usr/include")
44}
45
46type headerProperies struct {
47 // Base directory of the headers being installed. As an example:
48 //
49 // ndk_headers {
50 // name: "foo",
51 // from: "include",
52 // to: "",
53 // srcs: ["include/foo/bar/baz.h"],
54 // }
55 //
56 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
57 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
58 From string
59
60 // Install path within the sysroot. This is relative to usr/include.
61 To string
62
63 // List of headers to install. Glob compatible. Common case is "include/**/*.h".
64 Srcs []string
Dan Albertc6345fb2016-10-20 01:36:11 -070065
66 // Path to the NOTICE file associated with the headers.
67 License string
Dan Albert914449f2016-06-17 16:45:24 -070068}
69
70type headerModule struct {
71 android.ModuleBase
72
73 properties headerProperies
74
75 installPaths []string
Dan Albertc6345fb2016-10-20 01:36:11 -070076 licensePath android.ModuleSrcPath
Dan Albert914449f2016-06-17 16:45:24 -070077}
78
Colin Cross1e676be2016-10-12 14:38:15 -070079func (m *headerModule) DepsMutator(ctx android.BottomUpMutatorContext) {
80}
81
Dan Albert269fab82017-02-15 17:31:33 -080082func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string,
83 to string) android.OutputPath {
84 // Output path is the sysroot base + "usr/include" + to directory + directory component
85 // of the file without the leading from directory stripped.
86 //
87 // Given:
88 // sysroot base = "ndk/sysroot"
89 // from = "include/foo"
90 // to = "bar"
91 // header = "include/foo/woodly/doodly.h"
92 // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
93
94 // full/platform/path/to/include/foo
95 fullFromPath := android.PathForModuleSrc(ctx, from)
96
97 // full/platform/path/to/include/foo/woodly
98 headerDir := filepath.Dir(header.String())
99
100 // woodly
101 strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
102 if err != nil {
103 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
104 fullFromPath.String(), err)
105 }
106
107 // full/platform/path/to/sysroot/usr/include/bar/woodly
108 installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir)
109
110 // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
111 return installDir
112}
113
Dan Albert914449f2016-06-17 16:45:24 -0700114func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Albertc6345fb2016-10-20 01:36:11 -0700115 if m.properties.License == "" {
116 ctx.PropertyErrorf("license", "field is required")
117 }
118
119 m.licensePath = android.PathForModuleSrc(ctx, m.properties.License)
120
Dan Albert914449f2016-06-17 16:45:24 -0700121 srcFiles := ctx.ExpandSources(m.properties.Srcs, nil)
122 for _, header := range srcFiles {
Dan Albert269fab82017-02-15 17:31:33 -0800123 installDir := getHeaderInstallDir(ctx, header, m.properties.From, m.properties.To)
124 installedPath := ctx.InstallFile(installDir, header)
125 installPath := installDir.Join(ctx, header.Base())
126 if installPath != installedPath {
127 panic(fmt.Sprintf(
128 "expected header install path (%q) not equal to actual install path %q",
129 installPath, installedPath))
Dan Albert914449f2016-06-17 16:45:24 -0700130 }
Dan Albert914449f2016-06-17 16:45:24 -0700131 m.installPaths = append(m.installPaths, installPath.String())
132 }
133
134 if len(m.installPaths) == 0 {
135 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
136 }
137}
138
139func ndkHeadersFactory() (blueprint.Module, []interface{}) {
140 module := &headerModule{}
Dan Albertc6345fb2016-10-20 01:36:11 -0700141 // Host module rather than device module because device module install steps
142 // do not get run when embedded in make. We're not any of the existing
143 // module types that can be exposed via the Android.mk exporter, so just use
144 // a host module.
145 return android.InitAndroidArchModule(module, android.HostSupportedNoCross,
146 android.MultilibFirst, &module.properties)
Dan Albert914449f2016-06-17 16:45:24 -0700147}
Dan Albert269fab82017-02-15 17:31:33 -0800148
149type preprocessedHeaderProperies struct {
150 // Base directory of the headers being installed. As an example:
151 //
152 // preprocessed_ndk_headers {
153 // name: "foo",
154 // from: "include",
155 // to: "",
156 // }
157 //
158 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
159 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
160 From string
161
162 // Install path within the sysroot. This is relative to usr/include.
163 To string
164
165 // Path to the NOTICE file associated with the headers.
166 License string
167}
168
169// Like ndk_headers, but preprocesses the headers with the bionic versioner:
170// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
171//
172// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
173// module does not have the srcs property, and operates on a full directory (the `from` property).
174//
175// Note that this is really only built to handle bionic/libc/include.
176type preprocessedHeaderModule struct {
177 android.ModuleBase
178
179 properties preprocessedHeaderProperies
180
181 installPaths []string
182 licensePath android.ModuleSrcPath
183}
184
185func (m *preprocessedHeaderModule) DepsMutator(ctx android.BottomUpMutatorContext) {
186}
187
188func (m *preprocessedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
189 if m.properties.License == "" {
190 ctx.PropertyErrorf("license", "field is required")
191 }
192
193 m.licensePath = android.PathForModuleSrc(ctx, m.properties.License)
194
195 fromSrcPath := android.PathForModuleSrc(ctx, m.properties.From)
196 toOutputPath := getCurrentIncludePath(ctx).Join(ctx, m.properties.To)
197 srcFiles := ctx.Glob(filepath.Join(fromSrcPath.String(), "**/*.h"), nil)
198 var installPaths []android.WritablePath
199 for _, header := range srcFiles {
200 installDir := getHeaderInstallDir(ctx, header, m.properties.From, m.properties.To)
201 installPath := installDir.Join(ctx, header.Base())
202 installPaths = append(installPaths, installPath)
203 m.installPaths = append(m.installPaths, installPath.String())
204 }
205
206 if len(m.installPaths) == 0 {
207 ctx.ModuleErrorf("glob %q matched zero files", m.properties.From)
208 }
209
210 // The versioner depends on a dependencies directory to simplify determining include paths
211 // when parsing headers. This directory contains architecture specific directories as well
212 // as a common directory, each of which contains symlinks to the actually directories to
213 // be included.
214 //
215 // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
216 // depend on these headers.
217 // TODO(http://b/35673191): Update the versioner to use a --sysroot.
218 depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
219 depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
220 for i, path := range depsGlob {
221 fileInfo, err := os.Lstat(path.String())
222 if err != nil {
223 ctx.ModuleErrorf("os.Lstat(%q) failed: %s", path.String, err)
224 }
225 if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
226 dest, err := os.Readlink(path.String())
227 if err != nil {
228 ctx.ModuleErrorf("os.Readlink(%q) failed: %s",
229 path.String, err)
230 }
231 // Additional .. to account for the symlink itself.
232 depsGlob[i] = android.PathForSource(
233 ctx, filepath.Clean(filepath.Join(path.String(), "..", dest)))
234 }
235 }
236
237 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
238 Rule: preprocessBionicHeaders,
239 Output: toOutputPath,
240 Implicits: append(srcFiles, depsGlob...),
241 ImplicitOutputs: installPaths,
242 Args: map[string]string{
243 "depsPath": depsPath.String(),
244 "srcDir": fromSrcPath.String(),
245 },
246 })
247}
248
249func preprocessedNdkHeadersFactory() (blueprint.Module, []interface{}) {
250 module := &preprocessedHeaderModule{}
251 // Host module rather than device module because device module install steps
252 // do not get run when embedded in make. We're not any of the existing
253 // module types that can be exposed via the Android.mk exporter, so just use
254 // a host module.
255 return android.InitAndroidArchModule(module, android.HostSupportedNoCross,
256 android.MultilibFirst, &module.properties)
257}