blob: 9fa9e046137a1658fb9ba20ca9832fdf638399b3 [file] [log] [blame]
Jiyong Park9b409bc2019-10-11 14:59:13 +09001// Copyright (C) 2019 The Android Open Source Project
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 sdk
16
17import (
18 "fmt"
Jiyong Park9b409bc2019-10-11 14:59:13 +090019 "path/filepath"
Jiyong Park9b409bc2019-10-11 14:59:13 +090020 "strings"
21
22 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
Jiyong Park73c54ee2019-10-22 20:31:18 +090025 "android/soong/cc"
Jiyong Park9b409bc2019-10-11 14:59:13 +090026 "android/soong/java"
27)
28
29var pctx = android.NewPackageContext("android/soong/sdk")
30
31// generatedFile abstracts operations for writing contents into a file and emit a build rule
32// for the file.
33type generatedFile struct {
Jiyong Park73c54ee2019-10-22 20:31:18 +090034 path android.OutputPath
35 content strings.Builder
36 indentLevel int
Jiyong Park9b409bc2019-10-11 14:59:13 +090037}
38
Jiyong Park232e7852019-11-04 12:23:40 +090039func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile {
Jiyong Park9b409bc2019-10-11 14:59:13 +090040 return &generatedFile{
Jiyong Park232e7852019-11-04 12:23:40 +090041 path: android.PathForModuleOut(ctx, path...).OutputPath,
Jiyong Park73c54ee2019-10-22 20:31:18 +090042 indentLevel: 0,
Jiyong Park9b409bc2019-10-11 14:59:13 +090043 }
44}
45
Paul Duffin0e0cf1d2019-11-12 19:39:25 +000046func (gf *generatedFile) Indent() {
Jiyong Park73c54ee2019-10-22 20:31:18 +090047 gf.indentLevel++
48}
49
Paul Duffin0e0cf1d2019-11-12 19:39:25 +000050func (gf *generatedFile) Dedent() {
Jiyong Park73c54ee2019-10-22 20:31:18 +090051 gf.indentLevel--
52}
53
Paul Duffin0e0cf1d2019-11-12 19:39:25 +000054func (gf *generatedFile) Printfln(format string, args ...interface{}) {
Jiyong Park9b409bc2019-10-11 14:59:13 +090055 // ninja consumes newline characters in rspfile_content. Prevent it by
Paul Duffin0e0cf1d2019-11-12 19:39:25 +000056 // escaping the backslash in the newline character. The extra backslash
Jiyong Park9b409bc2019-10-11 14:59:13 +090057 // is removed when the rspfile is written to the actual script file
Jiyong Park73c54ee2019-10-22 20:31:18 +090058 fmt.Fprintf(&(gf.content), strings.Repeat(" ", gf.indentLevel)+format+"\\n", args...)
Jiyong Park9b409bc2019-10-11 14:59:13 +090059}
60
61func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) {
62 rb := android.NewRuleBuilder()
63 // convert \\n to \n
64 rb.Command().
65 Implicits(implicits).
66 Text("echo").Text(proptools.ShellEscape(gf.content.String())).
67 Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path)
68 rb.Command().
69 Text("chmod a+x").Output(gf.path)
70 rb.Build(pctx, ctx, gf.path.Base(), "Build "+gf.path.Base())
71}
72
Paul Duffin0e0cf1d2019-11-12 19:39:25 +000073func (s *sdk) javaLibs(ctx android.ModuleContext) []android.SdkAware {
74 result := []android.SdkAware{}
Jiyong Park9b409bc2019-10-11 14:59:13 +090075 ctx.VisitDirectDeps(func(m android.Module) {
Jiyong Park73c54ee2019-10-22 20:31:18 +090076 if j, ok := m.(*java.Library); ok {
77 result = append(result, j)
Jiyong Park9b409bc2019-10-11 14:59:13 +090078 }
79 })
80 return result
81}
82
Jiyong Park73c54ee2019-10-22 20:31:18 +090083// archSpecificNativeLibInfo represents an arch-specific variant of a native lib
84type archSpecificNativeLibInfo struct {
85 name string
86 archType string
87 exportedIncludeDirs android.Paths
88 exportedSystemIncludeDirs android.Paths
89 exportedFlags []string
Jiyong Park232e7852019-11-04 12:23:40 +090090 exportedDeps android.Paths
Jiyong Park73c54ee2019-10-22 20:31:18 +090091 outputFile android.Path
92}
Jiyong Park9b409bc2019-10-11 14:59:13 +090093
Jiyong Park73c54ee2019-10-22 20:31:18 +090094func (lib *archSpecificNativeLibInfo) signature() string {
95 return fmt.Sprintf("%v %v %v %v",
96 lib.name,
97 lib.exportedIncludeDirs.Strings(),
98 lib.exportedSystemIncludeDirs.Strings(),
99 lib.exportedFlags)
100}
101
102// nativeLibInfo represents a collection of arch-specific modules having the same name
103type nativeLibInfo struct {
104 name string
105 archVariants []archSpecificNativeLibInfo
106 // hasArchSpecificFlags is set to true if modules for each architecture all have the same
107 // include dirs, flags, etc, in which case only those of the first arch is selected.
108 hasArchSpecificFlags bool
109}
110
111// nativeMemberInfos collects all cc.Modules that are member of an SDK.
112func (s *sdk) nativeMemberInfos(ctx android.ModuleContext) []*nativeLibInfo {
113 infoMap := make(map[string]*nativeLibInfo)
114
115 // Collect cc.Modules
116 ctx.VisitDirectDeps(func(m android.Module) {
117 ccModule, ok := m.(*cc.Module)
118 if !ok {
119 return
120 }
121 depName := ctx.OtherModuleName(m)
122
123 if _, ok := infoMap[depName]; !ok {
124 infoMap[depName] = &nativeLibInfo{name: depName}
125 }
126
127 info := infoMap[depName]
128 info.archVariants = append(info.archVariants, archSpecificNativeLibInfo{
129 name: ccModule.BaseModuleName(),
130 archType: ccModule.Target().Arch.ArchType.String(),
131 exportedIncludeDirs: ccModule.ExportedIncludeDirs(),
132 exportedSystemIncludeDirs: ccModule.ExportedSystemIncludeDirs(),
133 exportedFlags: ccModule.ExportedFlags(),
Jiyong Park232e7852019-11-04 12:23:40 +0900134 exportedDeps: ccModule.ExportedDeps(),
Jiyong Park73c54ee2019-10-22 20:31:18 +0900135 outputFile: ccModule.OutputFile().Path(),
136 })
137 })
138
139 // Determine if include dirs and flags for each module are different across arch-specific
140 // modules or not. And set hasArchSpecificFlags accordingly
141 for _, info := range infoMap {
142 // by default, include paths and flags are assumed to be the same across arches
143 info.hasArchSpecificFlags = false
144 oldSignature := ""
145 for _, av := range info.archVariants {
146 newSignature := av.signature()
147 if oldSignature == "" {
148 oldSignature = newSignature
149 }
150 if oldSignature != newSignature {
151 info.hasArchSpecificFlags = true
152 break
153 }
154 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900155 }
156
Jiyong Park73c54ee2019-10-22 20:31:18 +0900157 var list []*nativeLibInfo
158 for _, v := range infoMap {
159 list = append(list, v)
160 }
161 return list
162}
Jiyong Park9b409bc2019-10-11 14:59:13 +0900163
Jiyong Park73c54ee2019-10-22 20:31:18 +0900164// SDK directory structure
165// <sdk_root>/
166// Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
167// <api_ver>/ : below this directory are all auto-generated
168// Android.bp : definition of 'sdk_snapshot' module is here
169// aidl/
170// frameworks/base/core/..../IFoo.aidl : an exported AIDL file
171// java/
Jiyong Park232e7852019-11-04 12:23:40 +0900172// <module_name>.jar : the stub jar for a java library 'module_name'
Jiyong Park73c54ee2019-10-22 20:31:18 +0900173// include/
174// bionic/libc/include/stdlib.h : an exported header file
175// include_gen/
Jiyong Park232e7852019-11-04 12:23:40 +0900176// <module_name>/com/android/.../IFoo.h : a generated header file
Jiyong Park73c54ee2019-10-22 20:31:18 +0900177// <arch>/include/ : arch-specific exported headers
178// <arch>/include_gen/ : arch-specific generated headers
179// <arch>/lib/
180// libFoo.so : a stub library
181
182const (
Jiyong Park73c54ee2019-10-22 20:31:18 +0900183 nativeIncludeDir = "include"
184 nativeGeneratedIncludeDir = "include_gen"
185 nativeStubDir = "lib"
186 nativeStubFileSuffix = ".so"
187)
188
Jiyong Park73c54ee2019-10-22 20:31:18 +0900189// path to the stub file of a native shared library. Relative to <sdk_root>/<api_dir>
190func nativeStubFilePathFor(lib archSpecificNativeLibInfo) string {
191 return filepath.Join(lib.archType,
192 nativeStubDir, lib.name+nativeStubFileSuffix)
193}
194
195// paths to the include dirs of a native shared library. Relative to <sdk_root>/<api_dir>
196func nativeIncludeDirPathsFor(ctx android.ModuleContext, lib archSpecificNativeLibInfo,
197 systemInclude bool, archSpecific bool) []string {
198 var result []string
Jiyong Park73c54ee2019-10-22 20:31:18 +0900199 var includeDirs []android.Path
200 if !systemInclude {
201 includeDirs = lib.exportedIncludeDirs
202 } else {
203 includeDirs = lib.exportedSystemIncludeDirs
204 }
205 for _, dir := range includeDirs {
206 var path string
Jiyong Park232e7852019-11-04 12:23:40 +0900207 if _, gen := dir.(android.WritablePath); gen {
208 path = filepath.Join(nativeGeneratedIncludeDir, lib.name)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900209 } else {
210 path = filepath.Join(nativeIncludeDir, dir.String())
211 }
212 if archSpecific {
213 path = filepath.Join(lib.archType, path)
214 }
215 result = append(result, path)
216 }
217 return result
218}
219
Jiyong Park232e7852019-11-04 12:23:40 +0900220// A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot
Jiyong Park73c54ee2019-10-22 20:31:18 +0900221// This isn't visible to users, so could be changed in future.
222func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string {
223 return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version
224}
225
Jiyong Park232e7852019-11-04 12:23:40 +0900226// buildSnapshot is the main function in this source file. It creates rules to copy
227// the contents (header files, stub libraries, etc) into the zip file.
228func (s *sdk) buildSnapshot(ctx android.ModuleContext) android.OutputPath {
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000229 snapshotDir := android.PathForModuleOut(ctx, "snapshot")
Jiyong Park9b409bc2019-10-11 14:59:13 +0900230
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000231 bp := newGeneratedFile(ctx, "snapshot", "Android.bp")
232 bp.Printfln("// This is auto-generated. DO NOT EDIT.")
233 bp.Printfln("")
234
235 builder := &snapshotBuilder{
236 ctx: ctx,
237 version: "current",
238 snapshotDir: snapshotDir.OutputPath,
239 androidBpFile: bp,
240 filesToZip: []android.Path{bp.path},
Jiyong Park73c54ee2019-10-22 20:31:18 +0900241 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900242
Jiyong Park232e7852019-11-04 12:23:40 +0900243 // copy exported AIDL files and stub jar files
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000244 javaLibs := s.javaLibs(ctx)
245 for _, m := range javaLibs {
246 m.BuildSnapshot(ctx, builder)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900247 }
248
Jiyong Park232e7852019-11-04 12:23:40 +0900249 // copy exported header files and stub *.so files
Jiyong Park73c54ee2019-10-22 20:31:18 +0900250 nativeLibInfos := s.nativeMemberInfos(ctx)
251 for _, info := range nativeLibInfos {
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000252 buildSharedNativeLibSnapshot(ctx, info, builder)
Jiyong Park73c54ee2019-10-22 20:31:18 +0900253 }
Jiyong Park9b409bc2019-10-11 14:59:13 +0900254
Jiyong Park232e7852019-11-04 12:23:40 +0900255 // generate Android.bp
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000256
257 bp.Printfln("sdk_snapshot {")
258 bp.Indent()
259 bp.Printfln("name: %q,", ctx.ModuleName()+string(android.SdkVersionSeparator)+builder.version)
260 if len(javaLibs) > 0 {
261 bp.Printfln("java_libs: [")
262 bp.Indent()
263 for _, m := range javaLibs {
264 bp.Printfln("%q,", builder.VersionedSdkMemberName(m.Name()))
265 }
266 bp.Dedent()
267 bp.Printfln("],") // java_libs
268 }
269 if len(nativeLibInfos) > 0 {
270 bp.Printfln("native_shared_libs: [")
271 bp.Indent()
272 for _, info := range nativeLibInfos {
273 bp.Printfln("%q,", builder.VersionedSdkMemberName(info.name))
274 }
275 bp.Dedent()
276 bp.Printfln("],") // native_shared_libs
277 }
278 bp.Dedent()
279 bp.Printfln("}") // sdk_snapshot
280 bp.Printfln("")
281
282 bp.build(pctx, ctx, nil)
283
284 filesToZip := builder.filesToZip
Jiyong Park9b409bc2019-10-11 14:59:13 +0900285
Jiyong Park232e7852019-11-04 12:23:40 +0900286 // zip them all
287 zipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
288 rb := android.NewRuleBuilder()
289 rb.Command().
290 BuiltTool(ctx, "soong_zip").
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000291 FlagWithArg("-C ", builder.snapshotDir.String()).
Jiyong Park232e7852019-11-04 12:23:40 +0900292 FlagWithRspFileInputList("-l ", filesToZip).
293 FlagWithOutput("-o ", zipFile)
294 rb.Build(pctx, ctx, "snapshot", "Building snapshot for "+ctx.ModuleName())
Jiyong Park9b409bc2019-10-11 14:59:13 +0900295
Jiyong Park232e7852019-11-04 12:23:40 +0900296 return zipFile
Jiyong Park9b409bc2019-10-11 14:59:13 +0900297}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000298
299func buildSharedNativeLibSnapshot(ctx android.ModuleContext, info *nativeLibInfo, builder android.SnapshotBuilder) {
300 // a function for emitting include dirs
301 printExportedDirCopyCommandsForNativeLibs := func(lib archSpecificNativeLibInfo) {
302 includeDirs := lib.exportedIncludeDirs
303 includeDirs = append(includeDirs, lib.exportedSystemIncludeDirs...)
304 if len(includeDirs) == 0 {
305 return
306 }
307 for _, dir := range includeDirs {
308 if _, gen := dir.(android.WritablePath); gen {
309 // generated headers are copied via exportedDeps. See below.
310 continue
311 }
312 targetDir := nativeIncludeDir
313 if info.hasArchSpecificFlags {
314 targetDir = filepath.Join(lib.archType, targetDir)
315 }
316
317 // TODO(jiyong) copy headers having other suffixes
318 headers, _ := ctx.GlobWithDeps(dir.String()+"/**/*.h", nil)
319 for _, file := range headers {
320 src := android.PathForSource(ctx, file)
321 dest := filepath.Join(targetDir, file)
322 builder.CopyToSnapshot(src, dest)
323 }
324 }
325
326 genHeaders := lib.exportedDeps
327 for _, file := range genHeaders {
328 targetDir := nativeGeneratedIncludeDir
329 if info.hasArchSpecificFlags {
330 targetDir = filepath.Join(lib.archType, targetDir)
331 }
332 dest := filepath.Join(targetDir, lib.name, file.Rel())
333 builder.CopyToSnapshot(file, dest)
334 }
335 }
336
337 if !info.hasArchSpecificFlags {
338 printExportedDirCopyCommandsForNativeLibs(info.archVariants[0])
339 }
340
341 // for each architecture
342 for _, av := range info.archVariants {
343 builder.CopyToSnapshot(av.outputFile, nativeStubFilePathFor(av))
344
345 if info.hasArchSpecificFlags {
346 printExportedDirCopyCommandsForNativeLibs(av)
347 }
348 }
349
350 bp := builder.AndroidBpFile()
351 bp.Printfln("cc_prebuilt_library_shared {")
352 bp.Indent()
353 bp.Printfln("name: %q,", builder.VersionedSdkMemberName(info.name))
354 bp.Printfln("sdk_member_name: %q,", info.name)
355
356 // a function for emitting include dirs
357 printExportedDirsForNativeLibs := func(lib archSpecificNativeLibInfo, systemInclude bool) {
358 includeDirs := nativeIncludeDirPathsFor(ctx, lib, systemInclude, info.hasArchSpecificFlags)
359 if len(includeDirs) == 0 {
360 return
361 }
362 if !systemInclude {
363 bp.Printfln("export_include_dirs: [")
364 } else {
365 bp.Printfln("export_system_include_dirs: [")
366 }
367 bp.Indent()
368 for _, dir := range includeDirs {
369 bp.Printfln("%q,", dir)
370 }
371 bp.Dedent()
372 bp.Printfln("],")
373 }
374
375 if !info.hasArchSpecificFlags {
376 printExportedDirsForNativeLibs(info.archVariants[0], false /*systemInclude*/)
377 printExportedDirsForNativeLibs(info.archVariants[0], true /*systemInclude*/)
378 }
379
380 bp.Printfln("arch: {")
381 bp.Indent()
382 for _, av := range info.archVariants {
383 bp.Printfln("%s: {", av.archType)
384 bp.Indent()
385 bp.Printfln("srcs: [%q],", nativeStubFilePathFor(av))
386 if info.hasArchSpecificFlags {
387 // export_* properties are added inside the arch: {<arch>: {...}} block
388 printExportedDirsForNativeLibs(av, false /*systemInclude*/)
389 printExportedDirsForNativeLibs(av, true /*systemInclude*/)
390 }
391 bp.Dedent()
392 bp.Printfln("},") // <arch>
393 }
394 bp.Dedent()
395 bp.Printfln("},") // arch
396 bp.Printfln("stl: \"none\",")
397 bp.Printfln("system_shared_libs: [],")
398 bp.Dedent()
399 bp.Printfln("}") // cc_prebuilt_library_shared
400 bp.Printfln("")
401}
402
403type snapshotBuilder struct {
404 ctx android.ModuleContext
405 version string
406 snapshotDir android.OutputPath
407 filesToZip android.Paths
408 androidBpFile *generatedFile
409}
410
411func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
412 path := s.snapshotDir.Join(s.ctx, dest)
413 s.ctx.Build(pctx, android.BuildParams{
414 Rule: android.Cp,
415 Input: src,
416 Output: path,
417 })
418 s.filesToZip = append(s.filesToZip, path)
419}
420
421func (s *snapshotBuilder) AndroidBpFile() android.GeneratedSnapshotFile {
422 return s.androidBpFile
423}
424
425func (s *snapshotBuilder) VersionedSdkMemberName(unversionedName string) interface{} {
426 return versionedSdkMemberName(s.ctx, unversionedName, s.version)
427}