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("launch_configs"), " ") { 60 if ctx.OtherModuleExists(dep) { 61 ctx.AddVariationDependencies(variations, cvdHostPackageDependencyTag, dep) 62 } 63 } 64 65 // If cvd_custom_action_config is set, include custom action servers in the 66 // host package as specified by cvd_custom_action_servers. 67 customActionConfig := ctx.Config().VendorConfig("cvd").String("custom_action_config") 68 if customActionConfig != "" && ctx.OtherModuleExists(customActionConfig) { 69 ctx.AddVariationDependencies(variations, cvdHostPackageDependencyTag, 70 customActionConfig) 71 for _, dep := range strings.Split( 72 ctx.Config().VendorConfig("cvd").String("custom_action_servers"), " ") { 73 if ctx.OtherModuleExists(dep) { 74 ctx.AddVariationDependencies(nil, cvdHostPackageDependencyTag, dep) 75 } 76 } 77 } 78} 79 80var pctx = android.NewPackageContext("android/soong/cuttlefish") 81 82func (c *cvdHostPackage) GenerateAndroidBuildActions(ctx android.ModuleContext) { 83 zipFile := android.PathForModuleOut(ctx, "package.zip") 84 c.CopyDepsToZip(ctx, zipFile) 85 86 // Dir where to extract the zip file and construct the final tar.gz from 87 packageDir := android.PathForModuleOut(ctx, ".temp") 88 builder := android.NewRuleBuilder(pctx, ctx) 89 builder.Command(). 90 BuiltTool("zipsync"). 91 FlagWithArg("-d ", packageDir.String()). 92 Input(zipFile) 93 94 output := android.PathForModuleOut(ctx, "package.tar.gz") 95 builder.Command().Text("tar Scfz"). 96 Output(output). 97 FlagWithArg("-C ", packageDir.String()). 98 Flag("--mtime='2020-01-01'"). // to have reproducible builds 99 Text(".") 100 101 builder.Command().Text("rm").Flag("-rf").Text(packageDir.String()) 102 103 builder.Build("cvd_host_package", fmt.Sprintf("Packaging %s", c.BaseModuleName())) 104 105 ctx.InstallFile(android.PathForModuleInstall(ctx), c.BaseModuleName()+".tar.gz", output) 106} 107