Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame^] | 17 | import ( |
| 18 | "path/filepath" |
| 19 | |
| 20 | "github.com/google/blueprint" |
| 21 | ) |
Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 22 | |
Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 23 | func init() { |
Sam Delmerico | 9333ac1 | 2023-09-11 17:18:08 +0000 | [diff] [blame] | 24 | RegisterModuleType("prebuilt_build_tool", NewPrebuiltBuildTool) |
Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | type 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 | |
| 39 | type prebuiltBuildTool struct { |
| 40 | ModuleBase |
| 41 | prebuilt Prebuilt |
| 42 | |
| 43 | properties prebuiltBuildToolProperties |
| 44 | |
| 45 | toolPath OptionalPath |
| 46 | } |
| 47 | |
| 48 | func (t *prebuiltBuildTool) Name() string { |
| 49 | return t.prebuilt.Name(t.ModuleBase.Name()) |
| 50 | } |
| 51 | |
| 52 | func (t *prebuiltBuildTool) Prebuilt() *Prebuilt { |
| 53 | return &t.prebuilt |
| 54 | } |
| 55 | |
| 56 | func (t *prebuiltBuildTool) DepsMutator(ctx BottomUpMutatorContext) { |
| 57 | if t.properties.Src == nil { |
| 58 | ctx.PropertyErrorf("src", "missing prebuilt source file") |
| 59 | } |
| 60 | } |
| 61 | |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame^] | 62 | type PrebuiltBuildToolInfo struct { |
| 63 | Src Path |
| 64 | Deps Paths |
| 65 | } |
| 66 | |
| 67 | var PrebuiltBuildToolInfoProvider = blueprint.NewProvider(PrebuiltBuildToolInfo{}) |
| 68 | |
Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 69 | func (t *prebuiltBuildTool) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 70 | sourcePath := t.prebuilt.SingleSourcePath(ctx) |
Colin Cross | 4158950 | 2020-12-01 14:00:21 -0800 | [diff] [blame] | 71 | installedPath := PathForModuleOut(ctx, t.BaseModuleName()) |
Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 72 | deps := PathsForModuleSrc(ctx, t.properties.Deps) |
| 73 | |
Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 74 | var fromPath = sourcePath.String() |
| 75 | if !filepath.IsAbs(fromPath) { |
| 76 | fromPath = "$$PWD/" + fromPath |
| 77 | } |
| 78 | |
Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 79 | ctx.Build(pctx, BuildParams{ |
| 80 | Rule: Symlink, |
| 81 | Output: installedPath, |
| 82 | Input: sourcePath, |
| 83 | Implicits: deps, |
| 84 | Args: map[string]string{ |
Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 85 | "fromPath": fromPath, |
Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 86 | }, |
| 87 | }) |
| 88 | |
Colin Cross | 4158950 | 2020-12-01 14:00:21 -0800 | [diff] [blame] | 89 | 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 Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 95 | t.toolPath = OptionalPathForPath(installedPath) |
Sam Delmerico | a588d15 | 2023-06-16 10:28:04 -0400 | [diff] [blame^] | 96 | |
| 97 | ctx.SetProvider(PrebuiltBuildToolInfoProvider, PrebuiltBuildToolInfo{ |
| 98 | Src: sourcePath, |
| 99 | Deps: deps, |
| 100 | }) |
Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | func (t *prebuiltBuildTool) MakeVars(ctx MakeVarsModuleContext) { |
| 104 | if makeVar := String(t.properties.Export_to_make_var); makeVar != "" { |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 105 | if t.Target().Os != ctx.Config().BuildOS { |
Jiyong Park | ad429d0 | 2020-12-17 19:31:17 +0900 | [diff] [blame] | 106 | return |
| 107 | } |
Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 108 | ctx.StrictRaw(makeVar, t.toolPath.String()) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func (t *prebuiltBuildTool) HostToolPath() OptionalPath { |
| 113 | return t.toolPath |
| 114 | } |
| 115 | |
| 116 | var _ 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 Srbecky | 5d52dce | 2023-08-08 11:26:23 +0000 | [diff] [blame] | 120 | func NewPrebuiltBuildTool() Module { |
Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 121 | module := &prebuiltBuildTool{} |
| 122 | module.AddProperties(&module.properties) |
| 123 | InitSingleSourcePrebuiltModule(module, &module.properties, "Src") |
| 124 | InitAndroidArchModule(module, HostSupportedNoCross, MultilibFirst) |
| 125 | return module |
| 126 | } |