| 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 |  | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 17 | import "path/filepath" | 
| Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 18 |  | 
| Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 19 | func init() { | 
| Sam Delmerico | f2b1606 | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 20 | RegisterModuleType("prebuilt_build_tool", NewPrebuiltBuildTool) | 
| Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 21 | } | 
|  | 22 |  | 
|  | 23 | type 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 |  | 
|  | 35 | type prebuiltBuildTool struct { | 
|  | 36 | ModuleBase | 
|  | 37 | prebuilt Prebuilt | 
|  | 38 |  | 
|  | 39 | properties prebuiltBuildToolProperties | 
|  | 40 |  | 
|  | 41 | toolPath OptionalPath | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | func (t *prebuiltBuildTool) Name() string { | 
|  | 45 | return t.prebuilt.Name(t.ModuleBase.Name()) | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | func (t *prebuiltBuildTool) Prebuilt() *Prebuilt { | 
|  | 49 | return &t.prebuilt | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | func (t *prebuiltBuildTool) DepsMutator(ctx BottomUpMutatorContext) { | 
|  | 53 | if t.properties.Src == nil { | 
|  | 54 | ctx.PropertyErrorf("src", "missing prebuilt source file") | 
|  | 55 | } | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | func (t *prebuiltBuildTool) GenerateAndroidBuildActions(ctx ModuleContext) { | 
|  | 59 | sourcePath := t.prebuilt.SingleSourcePath(ctx) | 
| Colin Cross | 4158950 | 2020-12-01 14:00:21 -0800 | [diff] [blame] | 60 | installedPath := PathForModuleOut(ctx, t.BaseModuleName()) | 
| Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 61 | deps := PathsForModuleSrc(ctx, t.properties.Deps) | 
|  | 62 |  | 
| Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 63 | var fromPath = sourcePath.String() | 
|  | 64 | if !filepath.IsAbs(fromPath) { | 
|  | 65 | fromPath = "$$PWD/" + fromPath | 
|  | 66 | } | 
|  | 67 |  | 
| Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 68 | ctx.Build(pctx, BuildParams{ | 
|  | 69 | Rule:      Symlink, | 
|  | 70 | Output:    installedPath, | 
|  | 71 | Input:     sourcePath, | 
|  | 72 | Implicits: deps, | 
|  | 73 | Args: map[string]string{ | 
| Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 74 | "fromPath": fromPath, | 
| Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 75 | }, | 
|  | 76 | }) | 
|  | 77 |  | 
| Colin Cross | 4158950 | 2020-12-01 14:00:21 -0800 | [diff] [blame] | 78 | packagingDir := PathForModuleInstall(ctx, t.BaseModuleName()) | 
|  | 79 | ctx.PackageFile(packagingDir, sourcePath.String(), sourcePath) | 
|  | 80 | for _, dep := range deps { | 
|  | 81 | ctx.PackageFile(packagingDir, dep.String(), dep) | 
|  | 82 | } | 
|  | 83 |  | 
| Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 84 | t.toolPath = OptionalPathForPath(installedPath) | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | func (t *prebuiltBuildTool) MakeVars(ctx MakeVarsModuleContext) { | 
|  | 88 | if makeVar := String(t.properties.Export_to_make_var); makeVar != "" { | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 89 | if t.Target().Os != ctx.Config().BuildOS { | 
| Jiyong Park | ad429d0 | 2020-12-17 19:31:17 +0900 | [diff] [blame] | 90 | return | 
|  | 91 | } | 
| Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 92 | ctx.StrictRaw(makeVar, t.toolPath.String()) | 
|  | 93 | } | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | func (t *prebuiltBuildTool) HostToolPath() OptionalPath { | 
|  | 97 | return t.toolPath | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | var _ HostToolProvider = &prebuiltBuildTool{} | 
|  | 101 |  | 
|  | 102 | // prebuilt_build_tool is to declare prebuilts to be used during the build, particularly for use | 
|  | 103 | // in genrules with the "tools" property. | 
| David Srbecky | 5d52dce | 2023-08-08 11:26:23 +0000 | [diff] [blame] | 104 | func NewPrebuiltBuildTool() Module { | 
| Dan Willemsen | 751ae87 | 2020-07-16 17:49:05 -0700 | [diff] [blame] | 105 | module := &prebuiltBuildTool{} | 
|  | 106 | module.AddProperties(&module.properties) | 
|  | 107 | InitSingleSourcePrebuiltModule(module, &module.properties, "Src") | 
|  | 108 | InitAndroidArchModule(module, HostSupportedNoCross, MultilibFirst) | 
|  | 109 | return module | 
|  | 110 | } |