blob: 516f6e20050935492fe39194842c4be1d9d0c4e2 [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"},
Dan Albert269fab82017-02-15 17:31:33 -080034 },
Dan Albertd2130a92017-03-29 18:33:28 -070035 "depsPath", "srcDir", "outDir")
Dan Albert269fab82017-02-15 17:31:33 -080036)
37
38func init() {
39 pctx.HostBinToolVariable("versionerCmd", "versioner")
40}
41
Dan Albert914449f2016-06-17 16:45:24 -070042// Returns the NDK base include path for use with sdk_version current. Usable with -I.
43func getCurrentIncludePath(ctx android.ModuleContext) android.OutputPath {
44 return getNdkSysrootBase(ctx).Join(ctx, "usr/include")
45}
46
47type headerProperies struct {
48 // Base directory of the headers being installed. As an example:
49 //
50 // ndk_headers {
51 // name: "foo",
52 // from: "include",
53 // to: "",
54 // srcs: ["include/foo/bar/baz.h"],
55 // }
56 //
57 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
58 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
59 From string
60
61 // Install path within the sysroot. This is relative to usr/include.
62 To string
63
64 // List of headers to install. Glob compatible. Common case is "include/**/*.h".
65 Srcs []string
Dan Albertc6345fb2016-10-20 01:36:11 -070066
67 // Path to the NOTICE file associated with the headers.
68 License string
Dan Albert914449f2016-06-17 16:45:24 -070069}
70
71type headerModule struct {
72 android.ModuleBase
73
74 properties headerProperies
75
76 installPaths []string
Dan Albertc6345fb2016-10-20 01:36:11 -070077 licensePath android.ModuleSrcPath
Dan Albert914449f2016-06-17 16:45:24 -070078}
79
Colin Cross1e676be2016-10-12 14:38:15 -070080func (m *headerModule) DepsMutator(ctx android.BottomUpMutatorContext) {
81}
82
Dan Albert269fab82017-02-15 17:31:33 -080083func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string,
84 to string) android.OutputPath {
85 // Output path is the sysroot base + "usr/include" + to directory + directory component
86 // of the file without the leading from directory stripped.
87 //
88 // Given:
89 // sysroot base = "ndk/sysroot"
90 // from = "include/foo"
91 // to = "bar"
92 // header = "include/foo/woodly/doodly.h"
93 // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
94
95 // full/platform/path/to/include/foo
96 fullFromPath := android.PathForModuleSrc(ctx, from)
97
98 // full/platform/path/to/include/foo/woodly
99 headerDir := filepath.Dir(header.String())
100
101 // woodly
102 strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
103 if err != nil {
104 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
105 fullFromPath.String(), err)
106 }
107
108 // full/platform/path/to/sysroot/usr/include/bar/woodly
109 installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir)
110
111 // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
112 return installDir
113}
114
Dan Albert914449f2016-06-17 16:45:24 -0700115func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Albertc6345fb2016-10-20 01:36:11 -0700116 if m.properties.License == "" {
117 ctx.PropertyErrorf("license", "field is required")
118 }
119
120 m.licensePath = android.PathForModuleSrc(ctx, m.properties.License)
121
Dan Albert914449f2016-06-17 16:45:24 -0700122 srcFiles := ctx.ExpandSources(m.properties.Srcs, nil)
123 for _, header := range srcFiles {
Dan Albert269fab82017-02-15 17:31:33 -0800124 installDir := getHeaderInstallDir(ctx, header, m.properties.From, m.properties.To)
125 installedPath := ctx.InstallFile(installDir, header)
126 installPath := installDir.Join(ctx, header.Base())
127 if installPath != installedPath {
128 panic(fmt.Sprintf(
129 "expected header install path (%q) not equal to actual install path %q",
130 installPath, installedPath))
Dan Albert914449f2016-06-17 16:45:24 -0700131 }
Dan Albert914449f2016-06-17 16:45:24 -0700132 m.installPaths = append(m.installPaths, installPath.String())
133 }
134
135 if len(m.installPaths) == 0 {
136 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
137 }
138}
139
140func ndkHeadersFactory() (blueprint.Module, []interface{}) {
141 module := &headerModule{}
Dan Willemsen0e2d97b2016-11-28 17:50:06 -0800142 return android.InitAndroidModule(module, &module.properties)
Dan Albert914449f2016-06-17 16:45:24 -0700143}
Dan Albert269fab82017-02-15 17:31:33 -0800144
145type preprocessedHeaderProperies struct {
146 // Base directory of the headers being installed. As an example:
147 //
148 // preprocessed_ndk_headers {
149 // name: "foo",
150 // from: "include",
151 // to: "",
152 // }
153 //
154 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
155 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
156 From string
157
158 // Install path within the sysroot. This is relative to usr/include.
159 To string
160
161 // Path to the NOTICE file associated with the headers.
162 License string
163}
164
165// Like ndk_headers, but preprocesses the headers with the bionic versioner:
166// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
167//
168// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
169// module does not have the srcs property, and operates on a full directory (the `from` property).
170//
171// Note that this is really only built to handle bionic/libc/include.
172type preprocessedHeaderModule struct {
173 android.ModuleBase
174
175 properties preprocessedHeaderProperies
176
177 installPaths []string
178 licensePath android.ModuleSrcPath
179}
180
181func (m *preprocessedHeaderModule) DepsMutator(ctx android.BottomUpMutatorContext) {
182}
183
184func (m *preprocessedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
185 if m.properties.License == "" {
186 ctx.PropertyErrorf("license", "field is required")
187 }
188
189 m.licensePath = android.PathForModuleSrc(ctx, m.properties.License)
190
191 fromSrcPath := android.PathForModuleSrc(ctx, m.properties.From)
192 toOutputPath := getCurrentIncludePath(ctx).Join(ctx, m.properties.To)
193 srcFiles := ctx.Glob(filepath.Join(fromSrcPath.String(), "**/*.h"), nil)
194 var installPaths []android.WritablePath
195 for _, header := range srcFiles {
196 installDir := getHeaderInstallDir(ctx, header, m.properties.From, m.properties.To)
197 installPath := installDir.Join(ctx, header.Base())
198 installPaths = append(installPaths, installPath)
199 m.installPaths = append(m.installPaths, installPath.String())
200 }
201
202 if len(m.installPaths) == 0 {
203 ctx.ModuleErrorf("glob %q matched zero files", m.properties.From)
204 }
205
Dan Willemsenb916b802017-03-19 13:44:32 -0700206 processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, srcFiles, installPaths)
207}
208
209func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path, srcFiles android.Paths, installPaths []android.WritablePath) android.Path {
Dan Albert269fab82017-02-15 17:31:33 -0800210 // 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
Dan Albertd2130a92017-03-29 18:33:28 -0700237 timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp")
Dan Albert269fab82017-02-15 17:31:33 -0800238 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
239 Rule: preprocessBionicHeaders,
Colin Cross67a5c132017-05-09 13:45:28 -0700240 Description: "versioner preprocess " + srcDir.Rel(),
Dan Albertd2130a92017-03-29 18:33:28 -0700241 Output: timestampFile,
Dan Albert269fab82017-02-15 17:31:33 -0800242 Implicits: append(srcFiles, depsGlob...),
243 ImplicitOutputs: installPaths,
244 Args: map[string]string{
245 "depsPath": depsPath.String(),
Dan Willemsenb916b802017-03-19 13:44:32 -0700246 "srcDir": srcDir.String(),
247 "outDir": outDir.String(),
Dan Albert269fab82017-02-15 17:31:33 -0800248 },
249 })
Dan Willemsenb916b802017-03-19 13:44:32 -0700250
251 return timestampFile
Dan Albert269fab82017-02-15 17:31:33 -0800252}
253
254func preprocessedNdkHeadersFactory() (blueprint.Module, []interface{}) {
255 module := &preprocessedHeaderModule{}
256 // Host module rather than device module because device module install steps
257 // do not get run when embedded in make. We're not any of the existing
258 // module types that can be exposed via the Android.mk exporter, so just use
259 // a host module.
260 return android.InitAndroidArchModule(module, android.HostSupportedNoCross,
261 android.MultilibFirst, &module.properties)
262}