1// Copyright (C) 2020 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 cuttlefish 16 17import ( 18 "fmt" 19 "strings" 20 21 "github.com/google/blueprint" 22 23 "android/soong/android" 24) 25 26func init() { 27 android.RegisterModuleType("cvd_host_package", cvdHostPackageFactory) 28} 29 30type cvdHostPackage struct { 31 android.ModuleBase 32 android.PackagingBase 33} 34 35func cvdHostPackageFactory() android.Module { 36 module := &cvdHostPackage{} 37 android.InitPackageModule(module) 38 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst) 39 module.IgnoreMissingDependencies = true 40 return module 41} 42 43type dependencyTag struct { 44 blueprint.BaseDependencyTag 45 android.InstallAlwaysNeededDependencyTag // to force installation of both "deps" and manually added dependencies 46 android.PackagingItemAlwaysDepTag // to force packaging of both "deps" and manually added dependencies 47} 48 49var cvdHostPackageDependencyTag = dependencyTag{} 50 51func (c *cvdHostPackage) DepsMutator(ctx android.BottomUpMutatorContext) { 52 c.AddDeps(ctx, cvdHostPackageDependencyTag) 53 54 variations := []blueprint.Variation{ 55 {Mutator: "os", Variation: ctx.Target().Os.String()}, 56 {Mutator: "arch", Variation: android.Common.String()}, 57 } 58 for _, dep := range strings.Split( 59 ctx.Config().VendorConfig("cvd").String("grub_config"), " ") { 60 if ctx.OtherModuleExists(dep) { 61 ctx.AddVariationDependencies(variations, cvdHostPackageDependencyTag, dep) 62 } 63 } 64 for _, dep := range strings.Split( 65 ctx.Config().VendorConfig("cvd").String("launch_configs"), " ") { 66 if ctx.OtherModuleExists(dep) { 67 ctx.AddVariationDependencies(variations, cvdHostPackageDependencyTag, dep) 68 } 69 } 70 71 // If cvd_custom_action_config is set, include custom action servers in the 72 // host package as specified by cvd_custom_action_servers. 73 customActionConfig := ctx.Config().VendorConfig("cvd").String("custom_action_config") 74 if customActionConfig != "" && ctx.OtherModuleExists(customActionConfig) { 75 ctx.AddVariationDependencies(variations, cvdHostPackageDependencyTag, 76 customActionConfig) 77 for _, dep := range strings.Split( 78 ctx.Config().VendorConfig("cvd").String("custom_action_servers"), " ") { 79 if ctx.OtherModuleExists(dep) { 80 ctx.AddVariationDependencies(nil, cvdHostPackageDependencyTag, dep) 81 } 82 } 83 } 84} 85 86var pctx = android.NewPackageContext("android/soong/cuttlefish") 87 88func (c *cvdHostPackage) GenerateAndroidBuildActions(ctx android.ModuleContext) { 89 zipFile := android.PathForModuleOut(ctx, "package.zip") 90 c.CopyDepsToZip(ctx, c.GatherPackagingSpecs(ctx), zipFile) 91 92 // Dir where to extract the zip file and construct the final tar.gz from 93 packageDir := android.PathForModuleOut(ctx, ".temp") 94 builder := android.NewRuleBuilder(pctx, ctx) 95 builder.Command(). 96 BuiltTool("zipsync"). 97 FlagWithArg("-d ", packageDir.String()). 98 Input(zipFile) 99 100 output := android.PathForModuleOut(ctx, "package.tar.gz") 101 builder.Command().Text("tar Scfz"). 102 Output(output). 103 FlagWithArg("-C ", packageDir.String()). 104 Flag("--mtime='2020-01-01'"). // to have reproducible builds 105 Text(".") 106 107 builder.Command().Text("rm").Flag("-rf").Text(packageDir.String()) 108 109 builder.Build("cvd_host_package", fmt.Sprintf("Packaging %s", c.BaseModuleName())) 110 111 ctx.InstallFile(android.PathForModuleInstall(ctx), c.BaseModuleName()+".tar.gz", output) 112} 113