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	for _, dep := range strings.Split(
72		ctx.Config().VendorConfig("cvd").String("binary"), " ") {
73		if ctx.OtherModuleExists(dep) {
74			ctx.AddVariationDependencies(ctx.Target().Variations(), cvdHostPackageDependencyTag, dep)
75		}
76	}
77
78	// If cvd_custom_action_config is set, include custom action servers in the
79	// host package as specified by cvd_custom_action_servers.
80	customActionConfig := ctx.Config().VendorConfig("cvd").String("custom_action_config")
81	if customActionConfig != "" && ctx.OtherModuleExists(customActionConfig) {
82		ctx.AddVariationDependencies(variations, cvdHostPackageDependencyTag,
83			customActionConfig)
84		for _, dep := range strings.Split(
85			ctx.Config().VendorConfig("cvd").String("custom_action_servers"), " ") {
86			if ctx.OtherModuleExists(dep) {
87				ctx.AddVariationDependencies(nil, cvdHostPackageDependencyTag, dep)
88			}
89		}
90	}
91
92	// Include custom CSS file in host package if custom_style is set
93	custom_style := ctx.Config().VendorConfig("cvd").String("custom_style")
94	if custom_style == "" || !ctx.OtherModuleExists(custom_style) {
95		custom_style = "webrtc_custom_blank.css"
96	}
97	ctx.AddVariationDependencies(variations, cvdHostPackageDependencyTag, custom_style)
98}
99
100var pctx = android.NewPackageContext("android/soong/cuttlefish")
101
102func (c *cvdHostPackage) GenerateAndroidBuildActions(ctx android.ModuleContext) {
103	packageDir := android.PathForModuleInstall(ctx, c.BaseModuleName())
104
105	stamp := android.PathForModuleOut(ctx, "package.stamp")
106	dirBuilder := android.NewRuleBuilder(pctx, ctx)
107	dirBuilder.Command().Text("rm").Flag("-rf").Text(packageDir.String())
108	dirBuilder.Command().Text("mkdir").Flag("-p").Text(packageDir.String())
109	c.CopySpecsToDir(ctx, dirBuilder, c.GatherPackagingSpecs(ctx), packageDir)
110	dirBuilder.Command().Text("touch").Output(stamp)
111	dirBuilder.Build("cvd_host_package", fmt.Sprintf("Packaging %s", c.BaseModuleName()))
112	ctx.InstallFile(android.PathForModuleInstall(ctx), c.BaseModuleName()+".stamp", stamp)
113
114	tarball := android.PathForModuleOut(ctx, "package.tar.gz")
115	tarballBuilder := android.NewRuleBuilder(pctx, ctx)
116	tarballBuilder.Command().Text("tar Scfz").
117		Output(tarball).
118		Flag("-C").
119		Text(packageDir.String()).
120		Implicit(stamp).
121		Flag("--mtime='2020-01-01'"). // to have reproducible builds
122		Text(".")
123	tarballBuilder.Build("cvd_host_tarball", fmt.Sprintf("Creating tarball for %s", c.BaseModuleName()))
124	ctx.InstallFile(android.PathForModuleInstall(ctx), c.BaseModuleName()+".tar.gz", tarball)
125}
126