host_snapshot.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2021 The Android Open Source Project
  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 snapshot
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "path/filepath"
  19. "sort"
  20. "strings"
  21. "github.com/google/blueprint"
  22. "github.com/google/blueprint/proptools"
  23. "android/soong/android"
  24. )
  25. //
  26. // The host_snapshot module creates a snapshot of the modules defined in
  27. // the deps property. The modules within the deps property (host tools)
  28. // are ones that return a valid path via HostToolPath() of the
  29. // HostToolProvider. The created snapshot contains the binaries and any
  30. // transitive PackagingSpecs of the included host tools, along with a JSON
  31. // meta file.
  32. //
  33. // The snapshot is installed into a source tree via
  34. // development/vendor_snapshot/update.py, the included modules are
  35. // provided as preferred prebuilts.
  36. //
  37. // To determine which tools to include in the host snapshot see
  38. // host_fake_snapshot.go.
  39. func init() {
  40. registerHostBuildComponents(android.InitRegistrationContext)
  41. }
  42. func registerHostBuildComponents(ctx android.RegistrationContext) {
  43. ctx.RegisterModuleType("host_snapshot", hostSnapshotFactory)
  44. }
  45. // Relative installation path
  46. type RelativeInstallPath interface {
  47. RelativeInstallPath() string
  48. }
  49. type hostSnapshot struct {
  50. android.ModuleBase
  51. android.PackagingBase
  52. outputFile android.OutputPath
  53. installDir android.InstallPath
  54. }
  55. type ProcMacro interface {
  56. ProcMacro() bool
  57. CrateName() string
  58. }
  59. func hostSnapshotFactory() android.Module {
  60. module := &hostSnapshot{}
  61. initHostToolsModule(module)
  62. return module
  63. }
  64. func initHostToolsModule(module *hostSnapshot) {
  65. android.InitPackageModule(module)
  66. android.InitAndroidMultiTargetsArchModule(module, android.HostSupported, android.MultilibCommon)
  67. }
  68. var dependencyTag = struct {
  69. blueprint.BaseDependencyTag
  70. android.InstallAlwaysNeededDependencyTag
  71. android.PackagingItemAlwaysDepTag
  72. }{}
  73. func (f *hostSnapshot) DepsMutator(ctx android.BottomUpMutatorContext) {
  74. f.AddDeps(ctx, dependencyTag)
  75. }
  76. func (f *hostSnapshot) installFileName() string {
  77. return f.Name() + ".zip"
  78. }
  79. // Create zipfile with JSON description, notice files... for dependent modules
  80. func (f *hostSnapshot) CreateMetaData(ctx android.ModuleContext, fileName string) android.OutputPath {
  81. var jsonData []SnapshotJsonFlags
  82. var metaPaths android.Paths
  83. installedNotices := make(map[string]bool)
  84. metaZipFile := android.PathForModuleOut(ctx, fileName).OutputPath
  85. // Create JSON file based on the direct dependencies
  86. ctx.VisitDirectDeps(func(dep android.Module) {
  87. desc := hostJsonDesc(dep)
  88. if desc != nil {
  89. jsonData = append(jsonData, *desc)
  90. }
  91. for _, notice := range dep.EffectiveLicenseFiles() {
  92. if _, ok := installedNotices[notice.String()]; !ok {
  93. installedNotices[notice.String()] = true
  94. noticeOut := android.PathForModuleOut(ctx, "NOTICE_FILES", notice.String()).OutputPath
  95. CopyFileToOutputPathRule(pctx, ctx, notice, noticeOut)
  96. metaPaths = append(metaPaths, noticeOut)
  97. }
  98. }
  99. })
  100. // Sort notice paths and json data for repeatble build
  101. sort.Slice(jsonData, func(i, j int) bool {
  102. return (jsonData[i].ModuleName < jsonData[j].ModuleName)
  103. })
  104. sort.Slice(metaPaths, func(i, j int) bool {
  105. return (metaPaths[i].String() < metaPaths[j].String())
  106. })
  107. marsh, err := json.Marshal(jsonData)
  108. if err != nil {
  109. ctx.ModuleErrorf("host snapshot json marshal failure: %#v", err)
  110. return android.OutputPath{}
  111. }
  112. jsonZipFile := android.PathForModuleOut(ctx, "host_snapshot.json").OutputPath
  113. metaPaths = append(metaPaths, jsonZipFile)
  114. rspFile := android.PathForModuleOut(ctx, "host_snapshot.rsp").OutputPath
  115. android.WriteFileRule(ctx, jsonZipFile, string(marsh))
  116. builder := android.NewRuleBuilder(pctx, ctx)
  117. builder.Command().
  118. BuiltTool("soong_zip").
  119. FlagWithArg("-C ", android.PathForModuleOut(ctx).OutputPath.String()).
  120. FlagWithOutput("-o ", metaZipFile).
  121. FlagWithRspFileInputList("-r ", rspFile, metaPaths)
  122. builder.Build("zip_meta", fmt.Sprintf("zipping meta data for %s", ctx.ModuleName()))
  123. return metaZipFile
  124. }
  125. // Create the host tool zip file
  126. func (f *hostSnapshot) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  127. // Create a zip file for the binaries, and a zip of the meta data, then merge zips
  128. depsZipFile := android.PathForModuleOut(ctx, f.Name()+"_deps.zip").OutputPath
  129. modsZipFile := android.PathForModuleOut(ctx, f.Name()+"_mods.zip").OutputPath
  130. f.outputFile = android.PathForModuleOut(ctx, f.installFileName()).OutputPath
  131. f.installDir = android.PathForModuleInstall(ctx)
  132. f.CopyDepsToZip(ctx, f.GatherPackagingSpecs(ctx), depsZipFile)
  133. builder := android.NewRuleBuilder(pctx, ctx)
  134. builder.Command().
  135. BuiltTool("zip2zip").
  136. FlagWithInput("-i ", depsZipFile).
  137. FlagWithOutput("-o ", modsZipFile).
  138. Text("**/*:" + proptools.ShellEscape(f.installDir.String()))
  139. metaZipFile := f.CreateMetaData(ctx, f.Name()+"_meta.zip")
  140. builder.Command().
  141. BuiltTool("merge_zips").
  142. Output(f.outputFile).
  143. Input(metaZipFile).
  144. Input(modsZipFile)
  145. builder.Build("manifest", fmt.Sprintf("Adding manifest %s", f.installFileName()))
  146. ctx.InstallFile(f.installDir, f.installFileName(), f.outputFile)
  147. }
  148. // Implements android.AndroidMkEntriesProvider
  149. func (f *hostSnapshot) AndroidMkEntries() []android.AndroidMkEntries {
  150. return []android.AndroidMkEntries{android.AndroidMkEntries{
  151. Class: "ETC",
  152. OutputFile: android.OptionalPathForPath(f.outputFile),
  153. DistFiles: android.MakeDefaultDistFiles(f.outputFile),
  154. ExtraEntries: []android.AndroidMkExtraEntriesFunc{
  155. func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
  156. entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
  157. entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
  158. },
  159. },
  160. }}
  161. }
  162. // Get host tools path and relative install string helpers
  163. func hostToolPath(m android.Module) android.OptionalPath {
  164. if provider, ok := m.(android.HostToolProvider); ok {
  165. return provider.HostToolPath()
  166. }
  167. return android.OptionalPath{}
  168. }
  169. func hostRelativePathString(m android.Module) string {
  170. var outString string
  171. if rel, ok := m.(RelativeInstallPath); ok {
  172. outString = rel.RelativeInstallPath()
  173. }
  174. return outString
  175. }
  176. // Create JSON description for given module, only create descriptions for binary modules
  177. // and rust_proc_macro modules which provide a valid HostToolPath
  178. func hostJsonDesc(m android.Module) *SnapshotJsonFlags {
  179. path := hostToolPath(m)
  180. relPath := hostRelativePathString(m)
  181. procMacro := false
  182. moduleStem := filepath.Base(path.String())
  183. crateName := ""
  184. if pm, ok := m.(ProcMacro); ok && pm.ProcMacro() {
  185. procMacro = pm.ProcMacro()
  186. moduleStem = strings.TrimSuffix(moduleStem, filepath.Ext(moduleStem))
  187. crateName = pm.CrateName()
  188. }
  189. if path.Valid() && path.String() != "" {
  190. props := &SnapshotJsonFlags{
  191. ModuleStemName: moduleStem,
  192. Filename: path.String(),
  193. Required: append(m.HostRequiredModuleNames(), m.RequiredModuleNames()...),
  194. RelativeInstallPath: relPath,
  195. RustProcMacro: procMacro,
  196. CrateName: crateName,
  197. }
  198. props.InitBaseSnapshotProps(m)
  199. return props
  200. }
  201. return nil
  202. }