blob: a9f29ef6948cc1373d07ccacde68638a58e5af22 [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
62 relPath, err := filepath.Rel(path.Dir(installedPath.String()), sourcePath.String())
63 if err != nil {
64 ctx.ModuleErrorf("Unabled to generate symlink between %q and %q: %s", installedPath.String(), sourcePath.String())
65 }
66
67 ctx.Build(pctx, BuildParams{
68 Rule: Symlink,
69 Output: installedPath,
70 Input: sourcePath,
71 Implicits: deps,
72 Args: map[string]string{
73 "fromPath": relPath,
74 },
75 })
76
77 t.toolPath = OptionalPathForPath(installedPath)
78}
79
80func (t *prebuiltBuildTool) HostToolPath() OptionalPath {
81 return t.toolPath
82}
83
84var _ HostToolProvider = &prebuiltBuildTool{}
85
86// prebuilt_build_tool is to declare prebuilts to be used during the build, particularly for use
87// in genrules with the "tools" property.
88func prebuiltBuildToolFactory() Module {
89 module := &prebuiltBuildTool{}
90 module.AddProperties(&module.properties)
91 InitSingleSourcePrebuiltModule(module, &module.properties, "Src")
92 InitAndroidArchModule(module, HostSupportedNoCross, MultilibFirst)
93 return module
94}