Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package sdk |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 19 | "path/filepath" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 20 | "strings" |
| 21 | |
| 22 | "github.com/google/blueprint/proptools" |
| 23 | |
| 24 | "android/soong/android" |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 25 | "android/soong/cc" |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 26 | "android/soong/java" |
| 27 | ) |
| 28 | |
| 29 | var 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. |
| 33 | type generatedFile struct { |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 34 | path android.OutputPath |
| 35 | content strings.Builder |
| 36 | indentLevel int |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 37 | } |
| 38 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 39 | func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 40 | return &generatedFile{ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 41 | path: android.PathForModuleOut(ctx, path...).OutputPath, |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 42 | indentLevel: 0, |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 46 | func (gf *generatedFile) Indent() { |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 47 | gf.indentLevel++ |
| 48 | } |
| 49 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 50 | func (gf *generatedFile) Dedent() { |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 51 | gf.indentLevel-- |
| 52 | } |
| 53 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 54 | func (gf *generatedFile) Printfln(format string, args ...interface{}) { |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 55 | // ninja consumes newline characters in rspfile_content. Prevent it by |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 56 | // escaping the backslash in the newline character. The extra backslash |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 57 | // is removed when the rspfile is written to the actual script file |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 58 | fmt.Fprintf(&(gf.content), strings.Repeat(" ", gf.indentLevel)+format+"\\n", args...) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func (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 Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 73 | func (s *sdk) javaLibs(ctx android.ModuleContext) []android.SdkAware { |
| 74 | result := []android.SdkAware{} |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 75 | ctx.VisitDirectDeps(func(m android.Module) { |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 76 | if j, ok := m.(*java.Library); ok { |
| 77 | result = append(result, j) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 78 | } |
| 79 | }) |
| 80 | return result |
| 81 | } |
| 82 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 83 | func (s *sdk) stubsSources(ctx android.ModuleContext) []android.SdkAware { |
| 84 | result := []android.SdkAware{} |
| 85 | ctx.VisitDirectDeps(func(m android.Module) { |
| 86 | if j, ok := m.(*java.Droidstubs); ok { |
| 87 | result = append(result, j) |
| 88 | } |
| 89 | }) |
| 90 | return result |
| 91 | } |
| 92 | |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 93 | // archSpecificNativeLibInfo represents an arch-specific variant of a native lib |
| 94 | type archSpecificNativeLibInfo struct { |
| 95 | name string |
| 96 | archType string |
| 97 | exportedIncludeDirs android.Paths |
| 98 | exportedSystemIncludeDirs android.Paths |
| 99 | exportedFlags []string |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 100 | exportedDeps android.Paths |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 101 | outputFile android.Path |
| 102 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 103 | |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 104 | func (lib *archSpecificNativeLibInfo) signature() string { |
| 105 | return fmt.Sprintf("%v %v %v %v", |
| 106 | lib.name, |
| 107 | lib.exportedIncludeDirs.Strings(), |
| 108 | lib.exportedSystemIncludeDirs.Strings(), |
| 109 | lib.exportedFlags) |
| 110 | } |
| 111 | |
| 112 | // nativeLibInfo represents a collection of arch-specific modules having the same name |
| 113 | type nativeLibInfo struct { |
| 114 | name string |
| 115 | archVariants []archSpecificNativeLibInfo |
| 116 | // hasArchSpecificFlags is set to true if modules for each architecture all have the same |
| 117 | // include dirs, flags, etc, in which case only those of the first arch is selected. |
| 118 | hasArchSpecificFlags bool |
| 119 | } |
| 120 | |
| 121 | // nativeMemberInfos collects all cc.Modules that are member of an SDK. |
| 122 | func (s *sdk) nativeMemberInfos(ctx android.ModuleContext) []*nativeLibInfo { |
| 123 | infoMap := make(map[string]*nativeLibInfo) |
| 124 | |
| 125 | // Collect cc.Modules |
| 126 | ctx.VisitDirectDeps(func(m android.Module) { |
| 127 | ccModule, ok := m.(*cc.Module) |
| 128 | if !ok { |
| 129 | return |
| 130 | } |
| 131 | depName := ctx.OtherModuleName(m) |
| 132 | |
| 133 | if _, ok := infoMap[depName]; !ok { |
| 134 | infoMap[depName] = &nativeLibInfo{name: depName} |
| 135 | } |
| 136 | |
| 137 | info := infoMap[depName] |
| 138 | info.archVariants = append(info.archVariants, archSpecificNativeLibInfo{ |
| 139 | name: ccModule.BaseModuleName(), |
| 140 | archType: ccModule.Target().Arch.ArchType.String(), |
| 141 | exportedIncludeDirs: ccModule.ExportedIncludeDirs(), |
| 142 | exportedSystemIncludeDirs: ccModule.ExportedSystemIncludeDirs(), |
| 143 | exportedFlags: ccModule.ExportedFlags(), |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 144 | exportedDeps: ccModule.ExportedDeps(), |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 145 | outputFile: ccModule.OutputFile().Path(), |
| 146 | }) |
| 147 | }) |
| 148 | |
| 149 | // Determine if include dirs and flags for each module are different across arch-specific |
| 150 | // modules or not. And set hasArchSpecificFlags accordingly |
| 151 | for _, info := range infoMap { |
| 152 | // by default, include paths and flags are assumed to be the same across arches |
| 153 | info.hasArchSpecificFlags = false |
| 154 | oldSignature := "" |
| 155 | for _, av := range info.archVariants { |
| 156 | newSignature := av.signature() |
| 157 | if oldSignature == "" { |
| 158 | oldSignature = newSignature |
| 159 | } |
| 160 | if oldSignature != newSignature { |
| 161 | info.hasArchSpecificFlags = true |
| 162 | break |
| 163 | } |
| 164 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 165 | } |
| 166 | |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 167 | var list []*nativeLibInfo |
| 168 | for _, v := range infoMap { |
| 169 | list = append(list, v) |
| 170 | } |
| 171 | return list |
| 172 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 173 | |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 174 | // SDK directory structure |
| 175 | // <sdk_root>/ |
| 176 | // Android.bp : definition of a 'sdk' module is here. This is a hand-made one. |
| 177 | // <api_ver>/ : below this directory are all auto-generated |
| 178 | // Android.bp : definition of 'sdk_snapshot' module is here |
| 179 | // aidl/ |
| 180 | // frameworks/base/core/..../IFoo.aidl : an exported AIDL file |
| 181 | // java/ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 182 | // <module_name>.jar : the stub jar for a java library 'module_name' |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 183 | // include/ |
| 184 | // bionic/libc/include/stdlib.h : an exported header file |
| 185 | // include_gen/ |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 186 | // <module_name>/com/android/.../IFoo.h : a generated header file |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 187 | // <arch>/include/ : arch-specific exported headers |
| 188 | // <arch>/include_gen/ : arch-specific generated headers |
| 189 | // <arch>/lib/ |
| 190 | // libFoo.so : a stub library |
| 191 | |
| 192 | const ( |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 193 | nativeIncludeDir = "include" |
| 194 | nativeGeneratedIncludeDir = "include_gen" |
| 195 | nativeStubDir = "lib" |
| 196 | nativeStubFileSuffix = ".so" |
| 197 | ) |
| 198 | |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 199 | // path to the stub file of a native shared library. Relative to <sdk_root>/<api_dir> |
| 200 | func nativeStubFilePathFor(lib archSpecificNativeLibInfo) string { |
| 201 | return filepath.Join(lib.archType, |
| 202 | nativeStubDir, lib.name+nativeStubFileSuffix) |
| 203 | } |
| 204 | |
| 205 | // paths to the include dirs of a native shared library. Relative to <sdk_root>/<api_dir> |
| 206 | func nativeIncludeDirPathsFor(ctx android.ModuleContext, lib archSpecificNativeLibInfo, |
| 207 | systemInclude bool, archSpecific bool) []string { |
| 208 | var result []string |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 209 | var includeDirs []android.Path |
| 210 | if !systemInclude { |
| 211 | includeDirs = lib.exportedIncludeDirs |
| 212 | } else { |
| 213 | includeDirs = lib.exportedSystemIncludeDirs |
| 214 | } |
| 215 | for _, dir := range includeDirs { |
| 216 | var path string |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 217 | if _, gen := dir.(android.WritablePath); gen { |
| 218 | path = filepath.Join(nativeGeneratedIncludeDir, lib.name) |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 219 | } else { |
| 220 | path = filepath.Join(nativeIncludeDir, dir.String()) |
| 221 | } |
| 222 | if archSpecific { |
| 223 | path = filepath.Join(lib.archType, path) |
| 224 | } |
| 225 | result = append(result, path) |
| 226 | } |
| 227 | return result |
| 228 | } |
| 229 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 230 | // A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 231 | // This isn't visible to users, so could be changed in future. |
| 232 | func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string { |
| 233 | return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version |
| 234 | } |
| 235 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 236 | // buildSnapshot is the main function in this source file. It creates rules to copy |
| 237 | // the contents (header files, stub libraries, etc) into the zip file. |
| 238 | func (s *sdk) buildSnapshot(ctx android.ModuleContext) android.OutputPath { |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 239 | snapshotDir := android.PathForModuleOut(ctx, "snapshot") |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 240 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 241 | bp := newGeneratedFile(ctx, "snapshot", "Android.bp") |
| 242 | bp.Printfln("// This is auto-generated. DO NOT EDIT.") |
| 243 | bp.Printfln("") |
| 244 | |
| 245 | builder := &snapshotBuilder{ |
| 246 | ctx: ctx, |
| 247 | version: "current", |
| 248 | snapshotDir: snapshotDir.OutputPath, |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 249 | filesToZip: []android.Path{bp.path}, |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 250 | androidBpFile: bp, |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 251 | } |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 252 | s.builderForTests = builder |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 253 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 254 | // copy exported AIDL files and stub jar files |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 255 | javaLibs := s.javaLibs(ctx) |
| 256 | for _, m := range javaLibs { |
| 257 | m.BuildSnapshot(ctx, builder) |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 258 | } |
| 259 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 260 | // copy stubs sources |
| 261 | stubsSources := s.stubsSources(ctx) |
| 262 | for _, m := range stubsSources { |
| 263 | m.BuildSnapshot(ctx, builder) |
| 264 | } |
| 265 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 266 | // copy exported header files and stub *.so files |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 267 | nativeLibInfos := s.nativeMemberInfos(ctx) |
| 268 | for _, info := range nativeLibInfos { |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 269 | buildSharedNativeLibSnapshot(ctx, info, builder) |
Jiyong Park | 73c54ee | 2019-10-22 20:31:18 +0900 | [diff] [blame] | 270 | } |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 271 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 272 | // generate Android.bp |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 273 | |
| 274 | bp.Printfln("sdk_snapshot {") |
| 275 | bp.Indent() |
| 276 | bp.Printfln("name: %q,", ctx.ModuleName()+string(android.SdkVersionSeparator)+builder.version) |
Paul Duffin | 66905ed | 2019-11-28 13:44:47 +0000 | [diff] [blame] | 277 | if len(s.properties.Java_libs) > 0 { |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 278 | bp.Printfln("java_libs: [") |
| 279 | bp.Indent() |
Paul Duffin | 66905ed | 2019-11-28 13:44:47 +0000 | [diff] [blame] | 280 | for _, m := range s.properties.Java_libs { |
| 281 | bp.Printfln("%q,", builder.VersionedSdkMemberName(m)) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 282 | } |
| 283 | bp.Dedent() |
| 284 | bp.Printfln("],") // java_libs |
| 285 | } |
Paul Duffin | 66905ed | 2019-11-28 13:44:47 +0000 | [diff] [blame] | 286 | if len(s.properties.Stubs_sources) > 0 { |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 287 | bp.Printfln("stubs_sources: [") |
| 288 | bp.Indent() |
Paul Duffin | 66905ed | 2019-11-28 13:44:47 +0000 | [diff] [blame] | 289 | for _, m := range s.properties.Stubs_sources { |
| 290 | bp.Printfln("%q,", builder.VersionedSdkMemberName(m)) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 291 | } |
| 292 | bp.Dedent() |
| 293 | bp.Printfln("],") // stubs_sources |
| 294 | } |
Paul Duffin | 66905ed | 2019-11-28 13:44:47 +0000 | [diff] [blame] | 295 | if len(s.properties.Native_shared_libs) > 0 { |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 296 | bp.Printfln("native_shared_libs: [") |
| 297 | bp.Indent() |
Paul Duffin | 66905ed | 2019-11-28 13:44:47 +0000 | [diff] [blame] | 298 | for _, m := range s.properties.Native_shared_libs { |
| 299 | bp.Printfln("%q,", builder.VersionedSdkMemberName(m)) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 300 | } |
| 301 | bp.Dedent() |
| 302 | bp.Printfln("],") // native_shared_libs |
| 303 | } |
| 304 | bp.Dedent() |
| 305 | bp.Printfln("}") // sdk_snapshot |
| 306 | bp.Printfln("") |
| 307 | |
| 308 | bp.build(pctx, ctx, nil) |
| 309 | |
| 310 | filesToZip := builder.filesToZip |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 311 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 312 | // zip them all |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 313 | outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath |
| 314 | outputRuleName := "snapshot" |
| 315 | outputDesc := "Building snapshot for " + ctx.ModuleName() |
| 316 | |
| 317 | // If there are no zips to merge then generate the output zip directly. |
| 318 | // Otherwise, generate an intermediate zip file into which other zips can be |
| 319 | // merged. |
| 320 | var zipFile android.OutputPath |
| 321 | var ruleName string |
| 322 | var desc string |
| 323 | if len(builder.zipsToMerge) == 0 { |
| 324 | zipFile = outputZipFile |
| 325 | ruleName = outputRuleName |
| 326 | desc = outputDesc |
| 327 | } else { |
| 328 | zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath |
| 329 | ruleName = "intermediate snapshot" |
| 330 | desc = "Building intermediate snapshot for " + ctx.ModuleName() |
| 331 | } |
| 332 | |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 333 | rb := android.NewRuleBuilder() |
| 334 | rb.Command(). |
| 335 | BuiltTool(ctx, "soong_zip"). |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 336 | FlagWithArg("-C ", builder.snapshotDir.String()). |
Jiyong Park | 232e785 | 2019-11-04 12:23:40 +0900 | [diff] [blame] | 337 | FlagWithRspFileInputList("-l ", filesToZip). |
| 338 | FlagWithOutput("-o ", zipFile) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 339 | rb.Build(pctx, ctx, ruleName, desc) |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 340 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 341 | if len(builder.zipsToMerge) != 0 { |
| 342 | rb := android.NewRuleBuilder() |
| 343 | rb.Command(). |
| 344 | BuiltTool(ctx, "merge_zips"). |
| 345 | Output(outputZipFile). |
| 346 | Input(zipFile). |
| 347 | Inputs(builder.zipsToMerge) |
| 348 | rb.Build(pctx, ctx, outputRuleName, outputDesc) |
| 349 | } |
| 350 | |
| 351 | return outputZipFile |
Jiyong Park | 9b409bc | 2019-10-11 14:59:13 +0900 | [diff] [blame] | 352 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 353 | |
Paul Duffin | ac37c50 | 2019-11-26 18:02:20 +0000 | [diff] [blame] | 354 | func (s *sdk) GetAndroidBpContentsForTests() string { |
| 355 | return s.builderForTests.androidBpFile.content.String() |
| 356 | } |
| 357 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 358 | func buildSharedNativeLibSnapshot(ctx android.ModuleContext, info *nativeLibInfo, builder android.SnapshotBuilder) { |
| 359 | // a function for emitting include dirs |
| 360 | printExportedDirCopyCommandsForNativeLibs := func(lib archSpecificNativeLibInfo) { |
| 361 | includeDirs := lib.exportedIncludeDirs |
| 362 | includeDirs = append(includeDirs, lib.exportedSystemIncludeDirs...) |
| 363 | if len(includeDirs) == 0 { |
| 364 | return |
| 365 | } |
| 366 | for _, dir := range includeDirs { |
| 367 | if _, gen := dir.(android.WritablePath); gen { |
| 368 | // generated headers are copied via exportedDeps. See below. |
| 369 | continue |
| 370 | } |
| 371 | targetDir := nativeIncludeDir |
| 372 | if info.hasArchSpecificFlags { |
| 373 | targetDir = filepath.Join(lib.archType, targetDir) |
| 374 | } |
| 375 | |
| 376 | // TODO(jiyong) copy headers having other suffixes |
| 377 | headers, _ := ctx.GlobWithDeps(dir.String()+"/**/*.h", nil) |
| 378 | for _, file := range headers { |
| 379 | src := android.PathForSource(ctx, file) |
| 380 | dest := filepath.Join(targetDir, file) |
| 381 | builder.CopyToSnapshot(src, dest) |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | genHeaders := lib.exportedDeps |
| 386 | for _, file := range genHeaders { |
| 387 | targetDir := nativeGeneratedIncludeDir |
| 388 | if info.hasArchSpecificFlags { |
| 389 | targetDir = filepath.Join(lib.archType, targetDir) |
| 390 | } |
| 391 | dest := filepath.Join(targetDir, lib.name, file.Rel()) |
| 392 | builder.CopyToSnapshot(file, dest) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | if !info.hasArchSpecificFlags { |
| 397 | printExportedDirCopyCommandsForNativeLibs(info.archVariants[0]) |
| 398 | } |
| 399 | |
| 400 | // for each architecture |
| 401 | for _, av := range info.archVariants { |
| 402 | builder.CopyToSnapshot(av.outputFile, nativeStubFilePathFor(av)) |
| 403 | |
| 404 | if info.hasArchSpecificFlags { |
| 405 | printExportedDirCopyCommandsForNativeLibs(av) |
| 406 | } |
| 407 | } |
| 408 | |
Paul Duffin | 9010250 | 2019-11-25 19:49:33 +0000 | [diff] [blame] | 409 | info.generatePrebuiltLibrary(ctx, builder, true) |
| 410 | |
| 411 | // This module is for the case when the source tree for the unversioned module |
| 412 | // doesn't exist (i.e. building in an unbundled tree). "prefer:" is set to false |
| 413 | // so that this module does not eclipse the unversioned module if it exists. |
| 414 | info.generatePrebuiltLibrary(ctx, builder, false) |
| 415 | } |
| 416 | |
| 417 | func (info *nativeLibInfo) generatePrebuiltLibrary(ctx android.ModuleContext, builder android.SnapshotBuilder, versioned bool) { |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 418 | bp := builder.AndroidBpFile() |
| 419 | bp.Printfln("cc_prebuilt_library_shared {") |
| 420 | bp.Indent() |
Paul Duffin | 9010250 | 2019-11-25 19:49:33 +0000 | [diff] [blame] | 421 | name := info.name |
| 422 | if versioned { |
| 423 | bp.Printfln("name: %q,", builder.VersionedSdkMemberName(name)) |
| 424 | bp.Printfln("sdk_member_name: %q,", name) |
| 425 | } else { |
| 426 | bp.Printfln("name: %q,", name) |
| 427 | bp.Printfln("prefer: false,") |
| 428 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 429 | |
| 430 | // a function for emitting include dirs |
| 431 | printExportedDirsForNativeLibs := func(lib archSpecificNativeLibInfo, systemInclude bool) { |
| 432 | includeDirs := nativeIncludeDirPathsFor(ctx, lib, systemInclude, info.hasArchSpecificFlags) |
| 433 | if len(includeDirs) == 0 { |
| 434 | return |
| 435 | } |
| 436 | if !systemInclude { |
| 437 | bp.Printfln("export_include_dirs: [") |
| 438 | } else { |
| 439 | bp.Printfln("export_system_include_dirs: [") |
| 440 | } |
| 441 | bp.Indent() |
| 442 | for _, dir := range includeDirs { |
| 443 | bp.Printfln("%q,", dir) |
| 444 | } |
| 445 | bp.Dedent() |
| 446 | bp.Printfln("],") |
| 447 | } |
| 448 | |
| 449 | if !info.hasArchSpecificFlags { |
| 450 | printExportedDirsForNativeLibs(info.archVariants[0], false /*systemInclude*/) |
| 451 | printExportedDirsForNativeLibs(info.archVariants[0], true /*systemInclude*/) |
| 452 | } |
| 453 | |
| 454 | bp.Printfln("arch: {") |
| 455 | bp.Indent() |
| 456 | for _, av := range info.archVariants { |
| 457 | bp.Printfln("%s: {", av.archType) |
| 458 | bp.Indent() |
| 459 | bp.Printfln("srcs: [%q],", nativeStubFilePathFor(av)) |
| 460 | if info.hasArchSpecificFlags { |
| 461 | // export_* properties are added inside the arch: {<arch>: {...}} block |
| 462 | printExportedDirsForNativeLibs(av, false /*systemInclude*/) |
| 463 | printExportedDirsForNativeLibs(av, true /*systemInclude*/) |
| 464 | } |
| 465 | bp.Dedent() |
| 466 | bp.Printfln("},") // <arch> |
| 467 | } |
| 468 | bp.Dedent() |
| 469 | bp.Printfln("},") // arch |
| 470 | bp.Printfln("stl: \"none\",") |
| 471 | bp.Printfln("system_shared_libs: [],") |
| 472 | bp.Dedent() |
| 473 | bp.Printfln("}") // cc_prebuilt_library_shared |
| 474 | bp.Printfln("") |
| 475 | } |
| 476 | |
| 477 | type snapshotBuilder struct { |
| 478 | ctx android.ModuleContext |
| 479 | version string |
| 480 | snapshotDir android.OutputPath |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 481 | androidBpFile *generatedFile |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 482 | filesToZip android.Paths |
| 483 | zipsToMerge android.Paths |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) { |
| 487 | path := s.snapshotDir.Join(s.ctx, dest) |
| 488 | s.ctx.Build(pctx, android.BuildParams{ |
| 489 | Rule: android.Cp, |
| 490 | Input: src, |
| 491 | Output: path, |
| 492 | }) |
| 493 | s.filesToZip = append(s.filesToZip, path) |
| 494 | } |
| 495 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 496 | func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) { |
| 497 | ctx := s.ctx |
| 498 | |
| 499 | // Repackage the zip file so that the entries are in the destDir directory. |
| 500 | // This will allow the zip file to be merged into the snapshot. |
| 501 | tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath |
| 502 | rb := android.NewRuleBuilder() |
| 503 | rb.Command(). |
| 504 | BuiltTool(ctx, "zip2zip"). |
| 505 | FlagWithInput("-i ", zipPath). |
| 506 | FlagWithOutput("-o ", tmpZipPath). |
| 507 | Flag("**/*:" + destDir) |
| 508 | rb.Build(pctx, ctx, "repackaging "+destDir, |
| 509 | "Repackaging zip file "+destDir+" for snapshot "+ctx.ModuleName()) |
| 510 | |
| 511 | // Add the repackaged zip file to the files to merge. |
| 512 | s.zipsToMerge = append(s.zipsToMerge, tmpZipPath) |
| 513 | } |
| 514 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 515 | func (s *snapshotBuilder) AndroidBpFile() android.GeneratedSnapshotFile { |
| 516 | return s.androidBpFile |
| 517 | } |
| 518 | |
| 519 | func (s *snapshotBuilder) VersionedSdkMemberName(unversionedName string) interface{} { |
| 520 | return versionedSdkMemberName(s.ctx, unversionedName, s.version) |
| 521 | } |