Colin Cross | 1496fb1 | 2024-09-09 16:44:10 -0700 | [diff] [blame^] | 1 | // Copyright 2024 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 golang wraps the blueprint blueprint_go_binary and bootstrap_go_binary module types in versions |
| 16 | // that implement android.Module that are used when building in Soong. This simplifies the code in Soong |
| 17 | // so it can always assume modules are an android.Module. |
| 18 | // The original blueprint blueprint_go_binary and bootstrap_go_binary module types are still used during |
| 19 | // bootstrapping, so the Android.bp entries for these module types must be compatible with both the |
| 20 | // original blueprint module types and these wrapped module types. |
| 21 | package golang |
| 22 | |
| 23 | import ( |
| 24 | "android/soong/android" |
| 25 | "github.com/google/blueprint" |
| 26 | "github.com/google/blueprint/bootstrap" |
| 27 | ) |
| 28 | |
| 29 | func init() { |
| 30 | // Wrap the blueprint Go module types with Soong ones that interoperate with the rest of the Soong modules. |
| 31 | bootstrap.GoModuleTypesAreWrapped() |
| 32 | RegisterGoModuleTypes(android.InitRegistrationContext) |
| 33 | } |
| 34 | |
| 35 | func RegisterGoModuleTypes(ctx android.RegistrationContext) { |
| 36 | ctx.RegisterModuleType("bootstrap_go_package", goPackageModuleFactory) |
| 37 | ctx.RegisterModuleType("blueprint_go_binary", goBinaryModuleFactory) |
| 38 | } |
| 39 | |
| 40 | // A GoPackage is a module for building Go packages. |
| 41 | type GoPackage struct { |
| 42 | android.ModuleBase |
| 43 | bootstrap.GoPackage |
| 44 | } |
| 45 | |
| 46 | func goPackageModuleFactory() android.Module { |
| 47 | module := &GoPackage{} |
| 48 | module.AddProperties(module.Properties()...) |
| 49 | android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst) |
| 50 | return module |
| 51 | } |
| 52 | |
| 53 | func (g *GoPackage) GenerateBuildActions(ctx blueprint.ModuleContext) { |
| 54 | // The embedded ModuleBase and bootstrap.GoPackage each implement GenerateBuildActions, |
| 55 | // the delegation has to be implemented manually to disambiguate. Call ModuleBase's |
| 56 | // GenerateBuildActions, which will call GenerateAndroidBuildActions, which will call |
| 57 | // bootstrap.GoPackage.GenerateBuildActions. |
| 58 | g.ModuleBase.GenerateBuildActions(ctx) |
| 59 | } |
| 60 | |
| 61 | func (g *GoPackage) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 62 | g.GoPackage.GenerateBuildActions(ctx.BlueprintModuleContext()) |
| 63 | } |
| 64 | |
| 65 | // A GoBinary is a module for building executable binaries from Go sources. |
| 66 | type GoBinary struct { |
| 67 | android.ModuleBase |
| 68 | bootstrap.GoBinary |
| 69 | |
| 70 | outputFile android.Path |
| 71 | } |
| 72 | |
| 73 | func goBinaryModuleFactory() android.Module { |
| 74 | module := &GoBinary{} |
| 75 | module.AddProperties(module.Properties()...) |
| 76 | android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibFirst) |
| 77 | return module |
| 78 | } |
| 79 | |
| 80 | func (g *GoBinary) GenerateBuildActions(ctx blueprint.ModuleContext) { |
| 81 | // The embedded ModuleBase and bootstrap.GoBinary each implement GenerateBuildActions, |
| 82 | // the delegation has to be implemented manually to disambiguate. Call ModuleBase's |
| 83 | // GenerateBuildActions, which will call GenerateAndroidBuildActions, which will call |
| 84 | // bootstrap.GoBinary.GenerateBuildActions. |
| 85 | g.ModuleBase.GenerateBuildActions(ctx) |
| 86 | } |
| 87 | |
| 88 | func (g *GoBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 89 | // Install the file in Soong instead of blueprint so that Soong knows about the install rules. |
| 90 | g.GoBinary.SetSkipInstall() |
| 91 | |
| 92 | // Run the build actions from the wrapped blueprint bootstrap module. |
| 93 | g.GoBinary.GenerateBuildActions(ctx.BlueprintModuleContext()) |
| 94 | |
| 95 | // Translate the bootstrap module's string path into a Path |
| 96 | outputFile := android.PathForArbitraryOutput(ctx, android.Rel(ctx, ctx.Config().OutDir(), g.IntermediateFile())).WithoutRel() |
| 97 | g.outputFile = outputFile |
| 98 | |
| 99 | installPath := ctx.InstallFile(android.PathForModuleInstall(ctx, "bin"), ctx.ModuleName(), outputFile) |
| 100 | |
| 101 | if !ctx.Config().KatiEnabled() || g.ExportedToMake() { |
| 102 | // Modules in an unexported namespace have no install rule, only add modules in the exported namespaces |
| 103 | // to the blueprint_tools phony rules. |
| 104 | ctx.Phony("blueprint_tools", installPath) |
| 105 | } |
| 106 | |
| 107 | ctx.SetOutputFiles(android.Paths{outputFile}, "") |
| 108 | } |
| 109 | |
| 110 | func (g *GoBinary) HostToolPath() android.OptionalPath { |
| 111 | return android.OptionalPathForPath(g.outputFile) |
| 112 | } |
| 113 | |
| 114 | func (g *GoBinary) AndroidMkEntries() []android.AndroidMkEntries { |
| 115 | return []android.AndroidMkEntries{ |
| 116 | { |
| 117 | Class: "EXECUTABLES", |
| 118 | OutputFile: android.OptionalPathForPath(g.outputFile), |
| 119 | Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk", |
| 120 | }, |
| 121 | } |
| 122 | } |