• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2017 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 hidl
16
17import (
18	"sync"
19
20	"github.com/google/blueprint"
21	"github.com/google/blueprint/proptools"
22
23	"android/soong/android"
24)
25
26var (
27	currentTxtRule = pctx.StaticRule("currentTxtRule", blueprint.RuleParams{
28		Command:     "cp -f ${in} ${output}",
29		Description: "copy current.txt: ${in} => ${output}",
30	}, "output")
31)
32
33type hidlPackageRootProperties struct {
34	// Path to the package root from android build root. It is recommended not to set this and
35	// use the current path. This will be deprecated in the future.
36	Path *string
37
38	// True to require a current.txt API file here.
39	//
40	// When false, it uses the file only when it exists.
41	Use_current *bool
42
43	// True to require all things referenced by this package root to be frozen.
44	Require_frozen *bool
45}
46
47func init() {
48	android.RegisterModuleType("hidl_package_root", hidlPackageRootFactory)
49}
50
51type hidlPackageRoot struct {
52	android.ModuleBase
53
54	properties hidlPackageRootProperties
55
56	currentPath android.OptionalPath
57	genOutputs  android.Paths
58}
59
60var _ android.SourceFileProducer = (*hidlPackageRoot)(nil)
61
62func (r *hidlPackageRoot) getFullPackageRoot() string {
63	return "-r" + r.Name() + ":" + *r.properties.Path
64}
65
66func (r *hidlPackageRoot) getCurrentPath() android.OptionalPath {
67	return r.currentPath
68}
69
70func (r *hidlPackageRoot) requireFrozen() bool {
71	return proptools.BoolDefault(r.properties.Require_frozen, false)
72}
73
74func (r *hidlPackageRoot) generateCurrentFile(ctx android.ModuleContext) {
75	if !r.currentPath.Valid() {
76		return
77	}
78
79	output := android.PathForModuleGen(ctx, r.Name()+".txt")
80	r.genOutputs = append(r.genOutputs, output)
81
82	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
83		Rule:   currentTxtRule,
84		Input:  r.currentPath.Path(),
85		Output: output,
86		Args: map[string]string{
87			"output": output.String(),
88		},
89	})
90}
91
92func (r *hidlPackageRoot) Srcs() android.Paths {
93	return r.genOutputs
94}
95
96func (r *hidlPackageRoot) GenerateAndroidBuildActions(ctx android.ModuleContext) {
97	if r.properties.Path == nil {
98		r.properties.Path = proptools.StringPtr(ctx.ModuleDir())
99	}
100
101	if proptools.BoolDefault(r.properties.Use_current, false) {
102		if *r.properties.Path != ctx.ModuleDir() {
103			ctx.PropertyErrorf("path", "Cannot use unrelated path with use_current. "+
104				"Presumably this hidl_package_root should be at %s. Otherwise, current.txt "+
105				"could be located at %s, but use_current must be removed. path is by default "+
106				"the path of hidl_package_root.", *r.properties.Path, ctx.ModuleDir())
107			return
108		}
109
110		r.currentPath = android.OptionalPathForPath(android.PathForModuleSrc(ctx, "current.txt"))
111	} else {
112		r.currentPath = android.ExistentPathForSource(ctx, ctx.ModuleDir(), "current.txt")
113	}
114
115	r.generateCurrentFile(ctx)
116}
117
118func (r *hidlPackageRoot) DepsMutator(ctx android.BottomUpMutatorContext) {
119}
120
121var packageRootsMutex sync.Mutex
122var packageRoots []*hidlPackageRoot
123
124func hidlPackageRootFactory() android.Module {
125	r := &hidlPackageRoot{}
126	r.AddProperties(&r.properties)
127	android.InitAndroidModule(r)
128
129	packageRootsMutex.Lock()
130	packageRoots = append(packageRoots, r)
131	packageRootsMutex.Unlock()
132
133	return r
134}
135
136func lookupPackageRoot(name string) *hidlPackageRoot {
137	for _, i := range packageRoots {
138		if i.ModuleBase.Name() == name {
139			return i
140		}
141	}
142	return nil
143}
144