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