blob: c00b22ba476013a3ed21456829ed42fae9f1fb4c [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
Sam Delmericoa588d152023-06-16 10:28:04 -040017import (
18 "path/filepath"
19
20 "github.com/google/blueprint"
21)
Martin Stjernholm14ee8322020-09-21 21:45:49 +010022
Dan Willemsen751ae872020-07-16 17:49:05 -070023func init() {
Sam Delmerico9333ac12023-09-11 17:18:08 +000024 RegisterModuleType("prebuilt_build_tool", NewPrebuiltBuildTool)
Dan Willemsen751ae872020-07-16 17:49:05 -070025}
26
27type prebuiltBuildToolProperties struct {
28 // Source file to be executed for this build tool
29 Src *string `android:"path,arch_variant"`
30
31 // Extra files that should trigger rules using this tool to rebuild
32 Deps []string `android:"path,arch_variant"`
33
34 // Create a make variable with the specified name that contains the path to
35 // this prebuilt built tool, relative to the root of the source tree.
36 Export_to_make_var *string
37}
38
39type prebuiltBuildTool struct {
40 ModuleBase
41 prebuilt Prebuilt
42
43 properties prebuiltBuildToolProperties
44
45 toolPath OptionalPath
46}
47
48func (t *prebuiltBuildTool) Name() string {
49 return t.prebuilt.Name(t.ModuleBase.Name())
50}
51
52func (t *prebuiltBuildTool) Prebuilt() *Prebuilt {
53 return &t.prebuilt
54}
55
56func (t *prebuiltBuildTool) DepsMutator(ctx BottomUpMutatorContext) {
57 if t.properties.Src == nil {
58 ctx.PropertyErrorf("src", "missing prebuilt source file")
59 }
60}
61
Sam Delmericoa588d152023-06-16 10:28:04 -040062type PrebuiltBuildToolInfo struct {
63 Src Path
64 Deps Paths
65}
66
67var PrebuiltBuildToolInfoProvider = blueprint.NewProvider(PrebuiltBuildToolInfo{})
68
Dan Willemsen751ae872020-07-16 17:49:05 -070069func (t *prebuiltBuildTool) GenerateAndroidBuildActions(ctx ModuleContext) {
70 sourcePath := t.prebuilt.SingleSourcePath(ctx)
Colin Cross41589502020-12-01 14:00:21 -080071 installedPath := PathForModuleOut(ctx, t.BaseModuleName())
Dan Willemsen751ae872020-07-16 17:49:05 -070072 deps := PathsForModuleSrc(ctx, t.properties.Deps)
73
Martin Stjernholm14ee8322020-09-21 21:45:49 +010074 var fromPath = sourcePath.String()
75 if !filepath.IsAbs(fromPath) {
76 fromPath = "$$PWD/" + fromPath
77 }
78
Dan Willemsen751ae872020-07-16 17:49:05 -070079 ctx.Build(pctx, BuildParams{
80 Rule: Symlink,
81 Output: installedPath,
82 Input: sourcePath,
83 Implicits: deps,
84 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +010085 "fromPath": fromPath,
Dan Willemsen751ae872020-07-16 17:49:05 -070086 },
87 })
88
Colin Cross41589502020-12-01 14:00:21 -080089 packagingDir := PathForModuleInstall(ctx, t.BaseModuleName())
90 ctx.PackageFile(packagingDir, sourcePath.String(), sourcePath)
91 for _, dep := range deps {
92 ctx.PackageFile(packagingDir, dep.String(), dep)
93 }
94
Dan Willemsen751ae872020-07-16 17:49:05 -070095 t.toolPath = OptionalPathForPath(installedPath)
Sam Delmericoa588d152023-06-16 10:28:04 -040096
97 ctx.SetProvider(PrebuiltBuildToolInfoProvider, PrebuiltBuildToolInfo{
98 Src: sourcePath,
99 Deps: deps,
100 })
Dan Willemsen751ae872020-07-16 17:49:05 -0700101}
102
103func (t *prebuiltBuildTool) MakeVars(ctx MakeVarsModuleContext) {
104 if makeVar := String(t.properties.Export_to_make_var); makeVar != "" {
Colin Cross0c66bc62021-07-20 09:47:41 -0700105 if t.Target().Os != ctx.Config().BuildOS {
Jiyong Parkad429d02020-12-17 19:31:17 +0900106 return
107 }
Dan Willemsen751ae872020-07-16 17:49:05 -0700108 ctx.StrictRaw(makeVar, t.toolPath.String())
109 }
110}
111
112func (t *prebuiltBuildTool) HostToolPath() OptionalPath {
113 return t.toolPath
114}
115
116var _ HostToolProvider = &prebuiltBuildTool{}
117
118// prebuilt_build_tool is to declare prebuilts to be used during the build, particularly for use
119// in genrules with the "tools" property.
David Srbecky5d52dce2023-08-08 11:26:23 +0000120func NewPrebuiltBuildTool() Module {
Dan Willemsen751ae872020-07-16 17:49:05 -0700121 module := &prebuiltBuildTool{}
122 module.AddProperties(&module.properties)
123 InitSingleSourcePrebuiltModule(module, &module.properties, "Src")
124 InitAndroidArchModule(module, HostSupportedNoCross, MultilibFirst)
125 return module
126}