blob: d457da4620d24f0300ab25c4b8d23450b2683a89 [file] [log] [blame]
Dan Willemsen13af8142020-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
17import (
18 "path"
19 "path/filepath"
20)
21
22func init() {
23 RegisterModuleType("prebuilt_build_tool", prebuiltBuildToolFactory)
24}
25
26type prebuiltBuildToolProperties struct {
27 // Source file to be executed for this build tool
28 Src *string `android:"path,arch_variant"`
29
30 // Extra files that should trigger rules using this tool to rebuild
31 Deps []string `android:"path,arch_variant"`
32}
33
34type prebuiltBuildTool struct {
35 ModuleBase
36 prebuilt Prebuilt
37
38 properties prebuiltBuildToolProperties
39
40 toolPath OptionalPath
41}
42
43func (t *prebuiltBuildTool) Name() string {
44 return t.prebuilt.Name(t.ModuleBase.Name())
45}
46
47func (t *prebuiltBuildTool) Prebuilt() *Prebuilt {
48 return &t.prebuilt
49}
50
51func (t *prebuiltBuildTool) DepsMutator(ctx BottomUpMutatorContext) {
52 if t.properties.Src == nil {
53 ctx.PropertyErrorf("src", "missing prebuilt source file")
54 }
55}
56
57func (t *prebuiltBuildTool) GenerateAndroidBuildActions(ctx ModuleContext) {
58 sourcePath := t.prebuilt.SingleSourcePath(ctx)
59 installedPath := PathForModuleOut(ctx, t.ModuleBase.Name())
60 deps := PathsForModuleSrc(ctx, t.properties.Deps)
61
Dan Willemsenb1d1e0e2020-07-23 16:10:51 -070062 var relPath string
63 if filepath.IsAbs(installedPath.String()) {
64 relPath = filepath.Join(absSrcDir, sourcePath.String())
65 } else {
66 var err error
67 relPath, err = filepath.Rel(path.Dir(installedPath.String()), sourcePath.String())
68 if err != nil {
69 ctx.ModuleErrorf("Unable to generate symlink between %q and %q: %s", installedPath.String(), sourcePath.String(), err)
70 }
Dan Willemsen13af8142020-07-16 17:49:05 -070071 }
72
73 ctx.Build(pctx, BuildParams{
74 Rule: Symlink,
75 Output: installedPath,
76 Input: sourcePath,
77 Implicits: deps,
78 Args: map[string]string{
79 "fromPath": relPath,
80 },
81 })
82
83 t.toolPath = OptionalPathForPath(installedPath)
84}
85
86func (t *prebuiltBuildTool) HostToolPath() OptionalPath {
87 return t.toolPath
88}
89
Dan Willemsen13af8142020-07-16 17:49:05 -070090var _ HostToolProvider = &prebuiltBuildTool{}
91
92// prebuilt_build_tool is to declare prebuilts to be used during the build, particularly for use
93// in genrules with the "tools" property.
94func prebuiltBuildToolFactory() Module {
95 module := &prebuiltBuildTool{}
96 module.AddProperties(&module.properties)
97 InitSingleSourcePrebuiltModule(module, &module.properties, "Src")
98 InitAndroidArchModule(module, HostSupportedNoCross, MultilibFirst)
99 return module
100}