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 packageDir := android.PathForModuleInstall(ctx, c.BaseModuleName()) 90 91 stamp := android.PathForModuleOut(ctx, "package.stamp") 92 dirBuilder := android.NewRuleBuilder(pctx, ctx) 93 dirBuilder.Command().Text("rm").Flag("-rf").Text(packageDir.String()) 94 dirBuilder.Command().Text("mkdir").Flag("-p").Text(packageDir.String()) 95 c.CopySpecsToDir(ctx, dirBuilder, c.GatherPackagingSpecs(ctx), packageDir) 96 dirBuilder.Command().Text("touch").Output(stamp) 97 dirBuilder.Build("cvd_host_package", fmt.Sprintf("Packaging %s", c.BaseModuleName())) 98 ctx.InstallFile(android.PathForModuleInstall(ctx), c.BaseModuleName()+".stamp", stamp) 99 100 tarball := android.PathForModuleOut(ctx, "package.tar.gz") 101 tarballBuilder := android.NewRuleBuilder(pctx, ctx) 102 tarballBuilder.Command().Text("tar Scfz"). 103 Output(tarball). 104 Flag("-C"). 105 Text(packageDir.String()). 106 Implicit(stamp). 107 Flag("--mtime='2020-01-01'"). // to have reproducible builds 108 Text(".") 109 tarballBuilder.Build("cvd_host_tarball", fmt.Sprintf("Creating tarball for %s", c.BaseModuleName())) 110 ctx.InstallFile(android.PathForModuleInstall(ctx), c.BaseModuleName()+".tar.gz", tarball) 111} 112