blob: e2555e4c3f72e0d7fb16448d0aac244f7387f9b7 [file] [log] [blame]
Dan Willemsen751ae872020-07-16 17:49:05 -07001// Copyright 2020 Google Inc. All rights reserved.
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 android
16
Martin Stjernholm14ee8322020-09-21 21:45:49 +010017import "path/filepath"
18
Dan Willemsen751ae872020-07-16 17:49:05 -070019func init() {
20 RegisterModuleType("prebuilt_build_tool", prebuiltBuildToolFactory)
21}
22
23type prebuiltBuildToolProperties struct {
24 // Source file to be executed for this build tool
25 Src *string `android:"path,arch_variant"`
26
27 // Extra files that should trigger rules using this tool to rebuild
28 Deps []string `android:"path,arch_variant"`
29
30 // Create a make variable with the specified name that contains the path to
31 // this prebuilt built tool, relative to the root of the source tree.
32 Export_to_make_var *string
33}
34
35type prebuiltBuildTool struct {
36 ModuleBase
37 prebuilt Prebuilt
38
39 properties prebuiltBuildToolProperties
40
41 toolPath OptionalPath
42}
43
44func (t *prebuiltBuildTool) Name() string {
45 return t.prebuilt.Name(t.ModuleBase.Name())
46}
47
48func (t *prebuiltBuildTool) Prebuilt() *Prebuilt {
49 return &t.prebuilt
50}
51
52func (t *prebuiltBuildTool) DepsMutator(ctx BottomUpMutatorContext) {
53 if t.properties.Src == nil {
54 ctx.PropertyErrorf("src", "missing prebuilt source file")
55 }
56}
57
58func (t *prebuiltBuildTool) GenerateAndroidBuildActions(ctx ModuleContext) {
59 sourcePath := t.prebuilt.SingleSourcePath(ctx)
60 installedPath := PathForModuleOut(ctx, t.ModuleBase.Name())
61 deps := PathsForModuleSrc(ctx, t.properties.Deps)
62
Martin Stjernholm14ee8322020-09-21 21:45:49 +010063 var fromPath = sourcePath.String()
64 if !filepath.IsAbs(fromPath) {
65 fromPath = "$$PWD/" + fromPath
66 }
67
Dan Willemsen751ae872020-07-16 17:49:05 -070068 ctx.Build(pctx, BuildParams{
69 Rule: Symlink,
70 Output: installedPath,
71 Input: sourcePath,
72 Implicits: deps,
73 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +010074 "fromPath": fromPath,
Dan Willemsen751ae872020-07-16 17:49:05 -070075 },
76 })
77
78 t.toolPath = OptionalPathForPath(installedPath)
79}
80
81func (t *prebuiltBuildTool) MakeVars(ctx MakeVarsModuleContext) {
82 if makeVar := String(t.properties.Export_to_make_var); makeVar != "" {
83 ctx.StrictRaw(makeVar, t.toolPath.String())
84 }
85}
86
87func (t *prebuiltBuildTool) HostToolPath() OptionalPath {
88 return t.toolPath
89}
90
91var _ HostToolProvider = &prebuiltBuildTool{}
92
93// prebuilt_build_tool is to declare prebuilts to be used during the build, particularly for use
94// in genrules with the "tools" property.
95func prebuiltBuildToolFactory() Module {
96 module := &prebuiltBuildTool{}
97 module.AddProperties(&module.properties)
98 InitSingleSourcePrebuiltModule(module, &module.properties, "Src")
99 InitAndroidArchModule(module, HostSupportedNoCross, MultilibFirst)
100 return module
101}