1*333d2b36SAndroid Build Coastguard Worker// Copyright 2020 Google Inc. All rights reserved. 2*333d2b36SAndroid Build Coastguard Worker// 3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*333d2b36SAndroid Build Coastguard Worker// 7*333d2b36SAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*333d2b36SAndroid Build Coastguard Worker// 9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*333d2b36SAndroid Build Coastguard Worker// limitations under the License. 14*333d2b36SAndroid Build Coastguard Worker 15*333d2b36SAndroid Build Coastguard Workerpackage android 16*333d2b36SAndroid Build Coastguard Worker 17*333d2b36SAndroid Build Coastguard Workerimport "path/filepath" 18*333d2b36SAndroid Build Coastguard Worker 19*333d2b36SAndroid Build Coastguard Workerfunc init() { 20*333d2b36SAndroid Build Coastguard Worker RegisterModuleType("prebuilt_build_tool", NewPrebuiltBuildTool) 21*333d2b36SAndroid Build Coastguard Worker} 22*333d2b36SAndroid Build Coastguard Worker 23*333d2b36SAndroid Build Coastguard Workertype prebuiltBuildToolProperties struct { 24*333d2b36SAndroid Build Coastguard Worker // Source file to be executed for this build tool 25*333d2b36SAndroid Build Coastguard Worker Src *string `android:"path,arch_variant"` 26*333d2b36SAndroid Build Coastguard Worker 27*333d2b36SAndroid Build Coastguard Worker // Extra files that should trigger rules using this tool to rebuild 28*333d2b36SAndroid Build Coastguard Worker Deps []string `android:"path,arch_variant"` 29*333d2b36SAndroid Build Coastguard Worker 30*333d2b36SAndroid Build Coastguard Worker // Create a make variable with the specified name that contains the path to 31*333d2b36SAndroid Build Coastguard Worker // this prebuilt built tool, relative to the root of the source tree. 32*333d2b36SAndroid Build Coastguard Worker Export_to_make_var *string 33*333d2b36SAndroid Build Coastguard Worker} 34*333d2b36SAndroid Build Coastguard Worker 35*333d2b36SAndroid Build Coastguard Workertype prebuiltBuildTool struct { 36*333d2b36SAndroid Build Coastguard Worker ModuleBase 37*333d2b36SAndroid Build Coastguard Worker prebuilt Prebuilt 38*333d2b36SAndroid Build Coastguard Worker 39*333d2b36SAndroid Build Coastguard Worker properties prebuiltBuildToolProperties 40*333d2b36SAndroid Build Coastguard Worker 41*333d2b36SAndroid Build Coastguard Worker toolPath OptionalPath 42*333d2b36SAndroid Build Coastguard Worker} 43*333d2b36SAndroid Build Coastguard Worker 44*333d2b36SAndroid Build Coastguard Workerfunc (t *prebuiltBuildTool) Name() string { 45*333d2b36SAndroid Build Coastguard Worker return t.prebuilt.Name(t.ModuleBase.Name()) 46*333d2b36SAndroid Build Coastguard Worker} 47*333d2b36SAndroid Build Coastguard Worker 48*333d2b36SAndroid Build Coastguard Workerfunc (t *prebuiltBuildTool) Prebuilt() *Prebuilt { 49*333d2b36SAndroid Build Coastguard Worker return &t.prebuilt 50*333d2b36SAndroid Build Coastguard Worker} 51*333d2b36SAndroid Build Coastguard Worker 52*333d2b36SAndroid Build Coastguard Workerfunc (t *prebuiltBuildTool) DepsMutator(ctx BottomUpMutatorContext) { 53*333d2b36SAndroid Build Coastguard Worker if t.properties.Src == nil { 54*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf("src", "missing prebuilt source file") 55*333d2b36SAndroid Build Coastguard Worker } 56*333d2b36SAndroid Build Coastguard Worker} 57*333d2b36SAndroid Build Coastguard Worker 58*333d2b36SAndroid Build Coastguard Workerfunc (t *prebuiltBuildTool) GenerateAndroidBuildActions(ctx ModuleContext) { 59*333d2b36SAndroid Build Coastguard Worker sourcePath := t.prebuilt.SingleSourcePath(ctx) 60*333d2b36SAndroid Build Coastguard Worker installedPath := PathForModuleOut(ctx, t.BaseModuleName()) 61*333d2b36SAndroid Build Coastguard Worker deps := PathsForModuleSrc(ctx, t.properties.Deps) 62*333d2b36SAndroid Build Coastguard Worker 63*333d2b36SAndroid Build Coastguard Worker var fromPath = sourcePath.String() 64*333d2b36SAndroid Build Coastguard Worker if !filepath.IsAbs(fromPath) { 65*333d2b36SAndroid Build Coastguard Worker fromPath = "$$PWD/" + fromPath 66*333d2b36SAndroid Build Coastguard Worker } 67*333d2b36SAndroid Build Coastguard Worker 68*333d2b36SAndroid Build Coastguard Worker ctx.Build(pctx, BuildParams{ 69*333d2b36SAndroid Build Coastguard Worker Rule: Symlink, 70*333d2b36SAndroid Build Coastguard Worker Output: installedPath, 71*333d2b36SAndroid Build Coastguard Worker Input: sourcePath, 72*333d2b36SAndroid Build Coastguard Worker Implicits: deps, 73*333d2b36SAndroid Build Coastguard Worker Args: map[string]string{ 74*333d2b36SAndroid Build Coastguard Worker "fromPath": fromPath, 75*333d2b36SAndroid Build Coastguard Worker }, 76*333d2b36SAndroid Build Coastguard Worker }) 77*333d2b36SAndroid Build Coastguard Worker 78*333d2b36SAndroid Build Coastguard Worker packagingDir := PathForModuleInstall(ctx, t.BaseModuleName()) 79*333d2b36SAndroid Build Coastguard Worker ctx.PackageFile(packagingDir, sourcePath.String(), sourcePath) 80*333d2b36SAndroid Build Coastguard Worker for _, dep := range deps { 81*333d2b36SAndroid Build Coastguard Worker ctx.PackageFile(packagingDir, dep.String(), dep) 82*333d2b36SAndroid Build Coastguard Worker } 83*333d2b36SAndroid Build Coastguard Worker 84*333d2b36SAndroid Build Coastguard Worker t.toolPath = OptionalPathForPath(installedPath) 85*333d2b36SAndroid Build Coastguard Worker} 86*333d2b36SAndroid Build Coastguard Worker 87*333d2b36SAndroid Build Coastguard Workerfunc (t *prebuiltBuildTool) MakeVars(ctx MakeVarsModuleContext) { 88*333d2b36SAndroid Build Coastguard Worker if makeVar := String(t.properties.Export_to_make_var); makeVar != "" { 89*333d2b36SAndroid Build Coastguard Worker if t.Target().Os != ctx.Config().BuildOS { 90*333d2b36SAndroid Build Coastguard Worker return 91*333d2b36SAndroid Build Coastguard Worker } 92*333d2b36SAndroid Build Coastguard Worker ctx.StrictRaw(makeVar, t.toolPath.String()) 93*333d2b36SAndroid Build Coastguard Worker } 94*333d2b36SAndroid Build Coastguard Worker} 95*333d2b36SAndroid Build Coastguard Worker 96*333d2b36SAndroid Build Coastguard Workerfunc (t *prebuiltBuildTool) HostToolPath() OptionalPath { 97*333d2b36SAndroid Build Coastguard Worker return t.toolPath 98*333d2b36SAndroid Build Coastguard Worker} 99*333d2b36SAndroid Build Coastguard Worker 100*333d2b36SAndroid Build Coastguard Workervar _ HostToolProvider = &prebuiltBuildTool{} 101*333d2b36SAndroid Build Coastguard Worker 102*333d2b36SAndroid Build Coastguard Worker// prebuilt_build_tool is to declare prebuilts to be used during the build, particularly for use 103*333d2b36SAndroid Build Coastguard Worker// in genrules with the "tools" property. 104*333d2b36SAndroid Build Coastguard Workerfunc NewPrebuiltBuildTool() Module { 105*333d2b36SAndroid Build Coastguard Worker module := &prebuiltBuildTool{} 106*333d2b36SAndroid Build Coastguard Worker module.AddProperties(&module.properties) 107*333d2b36SAndroid Build Coastguard Worker InitSingleSourcePrebuiltModule(module, &module.properties, "Src") 108*333d2b36SAndroid Build Coastguard Worker InitAndroidArchModule(module, HostSupportedNoCross, MultilibFirst) 109*333d2b36SAndroid Build Coastguard Worker return module 110*333d2b36SAndroid Build Coastguard Worker} 111