prebuilt_build_tool.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. package android
  15. import "path/filepath"
  16. func init() {
  17. RegisterModuleType("prebuilt_build_tool", prebuiltBuildToolFactory)
  18. }
  19. type prebuiltBuildToolProperties struct {
  20. // Source file to be executed for this build tool
  21. Src *string `android:"path,arch_variant"`
  22. // Extra files that should trigger rules using this tool to rebuild
  23. Deps []string `android:"path,arch_variant"`
  24. // Create a make variable with the specified name that contains the path to
  25. // this prebuilt built tool, relative to the root of the source tree.
  26. Export_to_make_var *string
  27. }
  28. type prebuiltBuildTool struct {
  29. ModuleBase
  30. prebuilt Prebuilt
  31. properties prebuiltBuildToolProperties
  32. toolPath OptionalPath
  33. }
  34. func (t *prebuiltBuildTool) Name() string {
  35. return t.prebuilt.Name(t.ModuleBase.Name())
  36. }
  37. func (t *prebuiltBuildTool) Prebuilt() *Prebuilt {
  38. return &t.prebuilt
  39. }
  40. func (t *prebuiltBuildTool) DepsMutator(ctx BottomUpMutatorContext) {
  41. if t.properties.Src == nil {
  42. ctx.PropertyErrorf("src", "missing prebuilt source file")
  43. }
  44. }
  45. func (t *prebuiltBuildTool) GenerateAndroidBuildActions(ctx ModuleContext) {
  46. sourcePath := t.prebuilt.SingleSourcePath(ctx)
  47. installedPath := PathForModuleOut(ctx, t.BaseModuleName())
  48. deps := PathsForModuleSrc(ctx, t.properties.Deps)
  49. var fromPath = sourcePath.String()
  50. if !filepath.IsAbs(fromPath) {
  51. fromPath = "$$PWD/" + fromPath
  52. }
  53. ctx.Build(pctx, BuildParams{
  54. Rule: Symlink,
  55. Output: installedPath,
  56. Input: sourcePath,
  57. Implicits: deps,
  58. Args: map[string]string{
  59. "fromPath": fromPath,
  60. },
  61. })
  62. packagingDir := PathForModuleInstall(ctx, t.BaseModuleName())
  63. ctx.PackageFile(packagingDir, sourcePath.String(), sourcePath)
  64. for _, dep := range deps {
  65. ctx.PackageFile(packagingDir, dep.String(), dep)
  66. }
  67. t.toolPath = OptionalPathForPath(installedPath)
  68. }
  69. func (t *prebuiltBuildTool) MakeVars(ctx MakeVarsModuleContext) {
  70. if makeVar := String(t.properties.Export_to_make_var); makeVar != "" {
  71. if t.Target().Os != ctx.Config().BuildOS {
  72. return
  73. }
  74. ctx.StrictRaw(makeVar, t.toolPath.String())
  75. }
  76. }
  77. func (t *prebuiltBuildTool) HostToolPath() OptionalPath {
  78. return t.toolPath
  79. }
  80. var _ HostToolProvider = &prebuiltBuildTool{}
  81. // prebuilt_build_tool is to declare prebuilts to be used during the build, particularly for use
  82. // in genrules with the "tools" property.
  83. func prebuiltBuildToolFactory() Module {
  84. module := &prebuiltBuildTool{}
  85. module.AddProperties(&module.properties)
  86. InitSingleSourcePrebuiltModule(module, &module.properties, "Src")
  87. InitAndroidArchModule(module, HostSupportedNoCross, MultilibFirst)
  88. return module
  89. }