blob: 6a083aed1280dda009d2ddbba4d0076c9ba032fd [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{
Dan Albertd2130a92017-03-29 18:33:28 -070030 // The `&& touch $out` isn't really necessary, but Blueprint won't
31 // let us have only implicit outputs.
32 Command: "$versionerCmd -o $outDir $srcDir $depsPath && touch $out",
Dan Albert269fab82017-02-15 17:31:33 -080033 CommandDeps: []string{"$versionerCmd"},
34 Description: "versioner preprocess $in",
35 },
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
48type headerProperies struct {
49 // 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.
60 From string
61
62 // Install path within the sysroot. This is relative to usr/include.
63 To string
64
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.
69 License string
Dan Albert914449f2016-06-17 16:45:24 -070070}
71
72type headerModule struct {
73 android.ModuleBase
74
75 properties headerProperies
76
77 installPaths []string
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) {
Dan Albertc6345fb2016-10-20 01:36:11 -0700117 if m.properties.License == "" {
118 ctx.PropertyErrorf("license", "field is required")
119 }
120
121 m.licensePath = android.PathForModuleSrc(ctx, m.properties.License)
122
Dan Albert914449f2016-06-17 16:45:24 -0700123 srcFiles := ctx.ExpandSources(m.properties.Srcs, nil)
124 for _, header := range srcFiles {
Dan Albert269fab82017-02-15 17:31:33 -0800125 installDir := getHeaderInstallDir(ctx, header, m.properties.From, m.properties.To)
126 installedPath := ctx.InstallFile(installDir, header)
127 installPath := installDir.Join(ctx, header.Base())
128 if installPath != installedPath {
129 panic(fmt.Sprintf(
130 "expected header install path (%q) not equal to actual install path %q",
131 installPath, installedPath))
Dan Albert914449f2016-06-17 16:45:24 -0700132 }
Dan Albert914449f2016-06-17 16:45:24 -0700133 m.installPaths = append(m.installPaths, installPath.String())
134 }
135
136 if len(m.installPaths) == 0 {
137 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
138 }
139}
140
141func ndkHeadersFactory() (blueprint.Module, []interface{}) {
142 module := &headerModule{}
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800143 return android.InitAndroidModule(module, &module.properties)
Dan Albert914449f2016-06-17 16:45:24 -0700144}
Dan Albert269fab82017-02-15 17:31:33 -0800145
146type preprocessedHeaderProperies struct {
147 // Base directory of the headers being installed. As an example:
148 //
149 // preprocessed_ndk_headers {
150 // name: "foo",
151 // from: "include",
152 // to: "",
153 // }
154 //
155 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
156 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
157 From string
158
159 // Install path within the sysroot. This is relative to usr/include.
160 To string
161
162 // Path to the NOTICE file associated with the headers.
163 License string
164}
165
166// Like ndk_headers, but preprocesses the headers with the bionic versioner:
167// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
168//
169// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
170// module does not have the srcs property, and operates on a full directory (the `from` property).
171//
172// Note that this is really only built to handle bionic/libc/include.
173type preprocessedHeaderModule struct {
174 android.ModuleBase
175
176 properties preprocessedHeaderProperies
177
178 installPaths []string
179 licensePath android.ModuleSrcPath
180}
181
182func (m *preprocessedHeaderModule) DepsMutator(ctx android.BottomUpMutatorContext) {
183}
184
185func (m *preprocessedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
186 if m.properties.License == "" {
187 ctx.PropertyErrorf("license", "field is required")
188 }
189
190 m.licensePath = android.PathForModuleSrc(ctx, m.properties.License)
191
192 fromSrcPath := android.PathForModuleSrc(ctx, m.properties.From)
193 toOutputPath := getCurrentIncludePath(ctx).Join(ctx, m.properties.To)
194 srcFiles := ctx.Glob(filepath.Join(fromSrcPath.String(), "**/*.h"), nil)
195 var installPaths []android.WritablePath
196 for _, header := range srcFiles {
197 installDir := getHeaderInstallDir(ctx, header, m.properties.From, m.properties.To)
198 installPath := installDir.Join(ctx, header.Base())
199 installPaths = append(installPaths, installPath)
200 m.installPaths = append(m.installPaths, installPath.String())
201 }
202
203 if len(m.installPaths) == 0 {
204 ctx.ModuleErrorf("glob %q matched zero files", m.properties.From)
205 }
206
207 // The versioner depends on a dependencies directory to simplify determining include paths
208 // when parsing headers. This directory contains architecture specific directories as well
209 // as a common directory, each of which contains symlinks to the actually directories to
210 // be included.
211 //
212 // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
213 // depend on these headers.
214 // TODO(http://b/35673191): Update the versioner to use a --sysroot.
215 depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
216 depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
217 for i, path := range depsGlob {
218 fileInfo, err := os.Lstat(path.String())
219 if err != nil {
220 ctx.ModuleErrorf("os.Lstat(%q) failed: %s", path.String, err)
221 }
222 if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
223 dest, err := os.Readlink(path.String())
224 if err != nil {
225 ctx.ModuleErrorf("os.Readlink(%q) failed: %s",
226 path.String, err)
227 }
228 // Additional .. to account for the symlink itself.
229 depsGlob[i] = android.PathForSource(
230 ctx, filepath.Clean(filepath.Join(path.String(), "..", dest)))
231 }
232 }
233
Dan Albertd2130a92017-03-29 18:33:28 -0700234 timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp")
Dan Albert269fab82017-02-15 17:31:33 -0800235 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
236 Rule: preprocessBionicHeaders,
Dan Albertd2130a92017-03-29 18:33:28 -0700237 Output: timestampFile,
Dan Albert269fab82017-02-15 17:31:33 -0800238 Implicits: append(srcFiles, depsGlob...),
239 ImplicitOutputs: installPaths,
240 Args: map[string]string{
241 "depsPath": depsPath.String(),
242 "srcDir": fromSrcPath.String(),
Dan Albertd2130a92017-03-29 18:33:28 -0700243 "outDir": toOutputPath.String(),
Dan Albert269fab82017-02-15 17:31:33 -0800244 },
245 })
246}
247
248func preprocessedNdkHeadersFactory() (blueprint.Module, []interface{}) {
249 module := &preprocessedHeaderModule{}
250 // Host module rather than device module because device module install steps
251 // do not get run when embedded in make. We're not any of the existing
252 // module types that can be exposed via the Android.mk exporter, so just use
253 // a host module.
254 return android.InitAndroidArchModule(module, android.HostSupportedNoCross,
255 android.MultilibFirst, &module.properties)
256}