blob: 8f1ecbd6dfb5b18ebfd3102e53ced20cc945e450 [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.
Nan Zhang0007d812017-11-07 10:57:05 -080059 From *string
Dan Albert914449f2016-06-17 16:45:24 -070060
61 // Install path within the sysroot. This is relative to usr/include.
Nan Zhang0007d812017-11-07 10:57:05 -080062 To *string
Dan Albert914449f2016-06-17 16:45:24 -070063
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.
Nan Zhang0007d812017-11-07 10:57:05 -080068 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) {
Nan Zhang0007d812017-11-07 10:57:05 -0800116 if String(m.properties.License) == "" {
Dan Albertc6345fb2016-10-20 01:36:11 -0700117 ctx.PropertyErrorf("license", "field is required")
118 }
119
Nan Zhang0007d812017-11-07 10:57:05 -0800120 m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
Dan Albertc6345fb2016-10-20 01:36:11 -0700121
Dan Albert914449f2016-06-17 16:45:24 -0700122 srcFiles := ctx.ExpandSources(m.properties.Srcs, nil)
123 for _, header := range srcFiles {
Nan Zhang0007d812017-11-07 10:57:05 -0800124 installDir := getHeaderInstallDir(ctx, header, String(m.properties.From),
125 String(m.properties.To))
Colin Cross5c517922017-08-31 12:29:17 -0700126 installedPath := ctx.InstallFile(installDir, header.Base(), header)
Dan Albert269fab82017-02-15 17:31:33 -0800127 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
Colin Cross36242852017-06-23 15:06:31 -0700141func ndkHeadersFactory() android.Module {
Dan Albert914449f2016-06-17 16:45:24 -0700142 module := &headerModule{}
Colin Cross36242852017-06-23 15:06:31 -0700143 module.AddProperties(&module.properties)
144 android.InitAndroidModule(module)
145 return module
Dan Albert914449f2016-06-17 16:45:24 -0700146}
Dan Albert269fab82017-02-15 17:31:33 -0800147
148type preprocessedHeaderProperies struct {
149 // Base directory of the headers being installed. As an example:
150 //
151 // preprocessed_ndk_headers {
152 // name: "foo",
153 // from: "include",
154 // to: "",
155 // }
156 //
157 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
158 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
159 From string
160
161 // Install path within the sysroot. This is relative to usr/include.
162 To string
163
164 // Path to the NOTICE file associated with the headers.
165 License string
166}
167
168// Like ndk_headers, but preprocesses the headers with the bionic versioner:
169// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
170//
171// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
172// module does not have the srcs property, and operates on a full directory (the `from` property).
173//
174// Note that this is really only built to handle bionic/libc/include.
175type preprocessedHeaderModule struct {
176 android.ModuleBase
177
178 properties preprocessedHeaderProperies
179
180 installPaths []string
181 licensePath android.ModuleSrcPath
182}
183
184func (m *preprocessedHeaderModule) DepsMutator(ctx android.BottomUpMutatorContext) {
185}
186
187func (m *preprocessedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
188 if m.properties.License == "" {
189 ctx.PropertyErrorf("license", "field is required")
190 }
191
192 m.licensePath = android.PathForModuleSrc(ctx, m.properties.License)
193
194 fromSrcPath := android.PathForModuleSrc(ctx, m.properties.From)
195 toOutputPath := getCurrentIncludePath(ctx).Join(ctx, m.properties.To)
196 srcFiles := ctx.Glob(filepath.Join(fromSrcPath.String(), "**/*.h"), nil)
197 var installPaths []android.WritablePath
198 for _, header := range srcFiles {
199 installDir := getHeaderInstallDir(ctx, header, m.properties.From, m.properties.To)
200 installPath := installDir.Join(ctx, header.Base())
201 installPaths = append(installPaths, installPath)
202 m.installPaths = append(m.installPaths, installPath.String())
203 }
204
205 if len(m.installPaths) == 0 {
206 ctx.ModuleErrorf("glob %q matched zero files", m.properties.From)
207 }
208
Dan Willemsenb916b802017-03-19 13:44:32 -0700209 processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, srcFiles, installPaths)
210}
211
212func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path, srcFiles android.Paths, installPaths []android.WritablePath) android.Path {
Dan Albert269fab82017-02-15 17:31:33 -0800213 // The versioner depends on a dependencies directory to simplify determining include paths
214 // when parsing headers. This directory contains architecture specific directories as well
215 // as a common directory, each of which contains symlinks to the actually directories to
216 // be included.
217 //
218 // ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
219 // depend on these headers.
220 // TODO(http://b/35673191): Update the versioner to use a --sysroot.
221 depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
222 depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
223 for i, path := range depsGlob {
224 fileInfo, err := os.Lstat(path.String())
225 if err != nil {
226 ctx.ModuleErrorf("os.Lstat(%q) failed: %s", path.String, err)
227 }
228 if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
229 dest, err := os.Readlink(path.String())
230 if err != nil {
231 ctx.ModuleErrorf("os.Readlink(%q) failed: %s",
232 path.String, err)
233 }
234 // Additional .. to account for the symlink itself.
235 depsGlob[i] = android.PathForSource(
236 ctx, filepath.Clean(filepath.Join(path.String(), "..", dest)))
237 }
238 }
239
Dan Albertd2130a92017-03-29 18:33:28 -0700240 timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp")
Colin Crossae887032017-10-23 17:16:14 -0700241 ctx.Build(pctx, android.BuildParams{
Dan Albert269fab82017-02-15 17:31:33 -0800242 Rule: preprocessBionicHeaders,
Colin Cross67a5c132017-05-09 13:45:28 -0700243 Description: "versioner preprocess " + srcDir.Rel(),
Dan Albertd2130a92017-03-29 18:33:28 -0700244 Output: timestampFile,
Dan Albert269fab82017-02-15 17:31:33 -0800245 Implicits: append(srcFiles, depsGlob...),
246 ImplicitOutputs: installPaths,
247 Args: map[string]string{
248 "depsPath": depsPath.String(),
Dan Willemsenb916b802017-03-19 13:44:32 -0700249 "srcDir": srcDir.String(),
250 "outDir": outDir.String(),
Dan Albert269fab82017-02-15 17:31:33 -0800251 },
252 })
Dan Willemsenb916b802017-03-19 13:44:32 -0700253
254 return timestampFile
Dan Albert269fab82017-02-15 17:31:33 -0800255}
256
Colin Cross36242852017-06-23 15:06:31 -0700257func preprocessedNdkHeadersFactory() android.Module {
Dan Albert269fab82017-02-15 17:31:33 -0800258 module := &preprocessedHeaderModule{}
Colin Cross36242852017-06-23 15:06:31 -0700259
260 module.AddProperties(&module.properties)
261
Dan Albert269fab82017-02-15 17:31:33 -0800262 // Host module rather than device module because device module install steps
263 // do not get run when embedded in make. We're not any of the existing
264 // module types that can be exposed via the Android.mk exporter, so just use
265 // a host module.
Colin Cross36242852017-06-23 15:06:31 -0700266 android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibFirst)
267
268 return module
Dan Albert269fab82017-02-15 17:31:33 -0800269}