sdk_repo_host.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Copyright (C) 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 android_sdk
  15. import (
  16. "fmt"
  17. "io"
  18. "path/filepath"
  19. "strings"
  20. "github.com/google/blueprint"
  21. "github.com/google/blueprint/pathtools"
  22. "github.com/google/blueprint/proptools"
  23. "android/soong/android"
  24. "android/soong/cc/config"
  25. )
  26. var pctx = android.NewPackageContext("android/soong/android_sdk")
  27. func init() {
  28. registerBuildComponents(android.InitRegistrationContext)
  29. }
  30. func registerBuildComponents(ctx android.RegistrationContext) {
  31. ctx.RegisterModuleType("android_sdk_repo_host", SdkRepoHostFactory)
  32. }
  33. type sdkRepoHost struct {
  34. android.ModuleBase
  35. android.PackagingBase
  36. properties sdkRepoHostProperties
  37. outputBaseName string
  38. outputFile android.OptionalPath
  39. }
  40. type remapProperties struct {
  41. From string
  42. To string
  43. }
  44. type sdkRepoHostProperties struct {
  45. // The top level directory to use for the SDK repo.
  46. Base_dir *string
  47. // List of src:dst mappings to rename files from `deps`.
  48. Deps_remap []remapProperties `android:"arch_variant"`
  49. // List of zip files to merge into the SDK repo.
  50. Merge_zips []string `android:"arch_variant,path"`
  51. // List of sources to include into the SDK repo. These are usually raw files, filegroups,
  52. // or genrules, as most built modules should be referenced via `deps`.
  53. Srcs []string `android:"arch_variant,path"`
  54. // List of files to strip. This should be a list of files, not modules. This happens after
  55. // `deps_remap` and `merge_zips` are applied, but before the `base_dir` is added.
  56. Strip_files []string `android:"arch_variant"`
  57. }
  58. // android_sdk_repo_host defines an Android SDK repo containing host tools.
  59. //
  60. // This implementation is trying to be a faithful reproduction of how these sdk-repos were produced
  61. // in the Make system, which may explain some of the oddities (like `strip_files` not being
  62. // automatic)
  63. func SdkRepoHostFactory() android.Module {
  64. return newSdkRepoHostModule()
  65. }
  66. func newSdkRepoHostModule() *sdkRepoHost {
  67. s := &sdkRepoHost{}
  68. s.AddProperties(&s.properties)
  69. android.InitPackageModule(s)
  70. android.InitAndroidMultiTargetsArchModule(s, android.HostSupported, android.MultilibCommon)
  71. return s
  72. }
  73. type dependencyTag struct {
  74. blueprint.BaseDependencyTag
  75. android.PackagingItemAlwaysDepTag
  76. }
  77. // TODO(b/201696252): Evaluate whether licenses should be propagated through this dependency.
  78. func (d dependencyTag) PropagateLicenses() bool {
  79. return false
  80. }
  81. var depTag = dependencyTag{}
  82. func (s *sdkRepoHost) DepsMutator(ctx android.BottomUpMutatorContext) {
  83. s.AddDeps(ctx, depTag)
  84. }
  85. func (s *sdkRepoHost) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  86. dir := android.PathForModuleOut(ctx, "zip")
  87. builder := android.NewRuleBuilder(pctx, ctx).
  88. Sbox(dir, android.PathForModuleOut(ctx, "out.sbox.textproto")).
  89. SandboxInputs()
  90. // Get files from modules listed in `deps`
  91. packageSpecs := s.GatherPackagingSpecs(ctx)
  92. // Handle `deps_remap` renames
  93. err := remapPackageSpecs(packageSpecs, s.properties.Deps_remap)
  94. if err != nil {
  95. ctx.PropertyErrorf("deps_remap", "%s", err.Error())
  96. }
  97. s.CopySpecsToDir(ctx, builder, packageSpecs, dir)
  98. // Collect licenses to write into NOTICE.txt
  99. noticeMap := map[string]android.Paths{}
  100. for path, pkgSpec := range packageSpecs {
  101. licenseFiles := pkgSpec.EffectiveLicenseFiles()
  102. if len(licenseFiles) > 0 {
  103. noticeMap[path] = pkgSpec.EffectiveLicenseFiles()
  104. }
  105. }
  106. notices := android.BuildNotices(ctx, noticeMap)
  107. builder.Command().Text("cp").
  108. Input(notices.TxtOutput.Path()).
  109. Text(filepath.Join(dir.String(), "NOTICE.txt"))
  110. // Handle `merge_zips` by extracting their contents into our tmpdir
  111. for _, zip := range android.PathsForModuleSrc(ctx, s.properties.Merge_zips) {
  112. builder.Command().
  113. Text("unzip").
  114. Flag("-DD").
  115. Flag("-q").
  116. FlagWithArg("-d ", dir.String()).
  117. Input(zip)
  118. }
  119. // Copy files from `srcs` into our tmpdir
  120. for _, src := range android.PathsForModuleSrc(ctx, s.properties.Srcs) {
  121. builder.Command().
  122. Text("cp").Input(src).Flag(dir.Join(ctx, src.Rel()).String())
  123. }
  124. // Handle `strip_files` by calling the necessary strip commands
  125. //
  126. // Note: this stripping logic was copied over from the old Make implementation
  127. // It's not using the same flags as the regular stripping support, nor does it
  128. // support the array of per-module stripping options. It would be nice if we
  129. // pulled the stripped versions from the CC modules, but that doesn't exist
  130. // for host tools today. (And not all the things we strip are CC modules today)
  131. if ctx.Darwin() {
  132. macStrip := config.MacStripPath(ctx)
  133. for _, strip := range s.properties.Strip_files {
  134. builder.Command().
  135. Text(macStrip).Flag("-x").
  136. Flag(dir.Join(ctx, strip).String())
  137. }
  138. } else {
  139. llvmStrip := config.ClangPath(ctx, "bin/llvm-strip")
  140. llvmLib := config.ClangPath(ctx, "lib64/libc++.so.1")
  141. for _, strip := range s.properties.Strip_files {
  142. cmd := builder.Command().Tool(llvmStrip).ImplicitTool(llvmLib)
  143. if !ctx.Windows() {
  144. cmd.Flag("-x")
  145. }
  146. cmd.Flag(dir.Join(ctx, strip).String())
  147. }
  148. }
  149. // Fix up the line endings of all text files. This also removes executable permissions.
  150. builder.Command().
  151. Text("find").
  152. Flag(dir.String()).
  153. Flag("-name '*.aidl' -o -name '*.css' -o -name '*.html' -o -name '*.java'").
  154. Flag("-o -name '*.js' -o -name '*.prop' -o -name '*.template'").
  155. Flag("-o -name '*.txt' -o -name '*.windows' -o -name '*.xml' -print0").
  156. // Using -n 500 for xargs to limit the max number of arguments per call to line_endings
  157. // to 500. This avoids line_endings failing with "arguments too long".
  158. Text("| xargs -0 -n 500 ").
  159. BuiltTool("line_endings").
  160. Flag("unix")
  161. // Exclude some file types (roughly matching sdk.exclude.atree)
  162. builder.Command().
  163. Text("find").
  164. Flag(dir.String()).
  165. Flag("'('").
  166. Flag("-name '.*' -o -name '*~' -o -name 'Makefile' -o -name 'Android.mk' -o").
  167. Flag("-name '.*.swp' -o -name '.DS_Store' -o -name '*.pyc' -o -name 'OWNERS' -o").
  168. Flag("-name 'MODULE_LICENSE_*' -o -name '*.ezt' -o -name 'Android.bp'").
  169. Flag("')' -print0").
  170. Text("| xargs -0 -r rm -rf")
  171. builder.Command().
  172. Text("find").
  173. Flag(dir.String()).
  174. Flag("-name '_*' ! -name '__*' -print0").
  175. Text("| xargs -0 -r rm -rf")
  176. if ctx.Windows() {
  177. // Fix EOL chars to make window users happy
  178. builder.Command().
  179. Text("find").
  180. Flag(dir.String()).
  181. Flag("-maxdepth 2 -name '*.bat' -type f -print0").
  182. Text("| xargs -0 -r unix2dos")
  183. }
  184. // Zip up our temporary directory as the sdk-repo
  185. outputZipFile := dir.Join(ctx, "output.zip")
  186. builder.Command().
  187. BuiltTool("soong_zip").
  188. FlagWithOutput("-o ", outputZipFile).
  189. FlagWithArg("-P ", proptools.StringDefault(s.properties.Base_dir, ".")).
  190. FlagWithArg("-C ", dir.String()).
  191. FlagWithArg("-D ", dir.String())
  192. builder.Command().Text("rm").Flag("-rf").Text(dir.String())
  193. builder.Build("build_sdk_repo", "Creating sdk-repo-"+s.BaseModuleName())
  194. osName := ctx.Os().String()
  195. if osName == "linux_glibc" {
  196. osName = "linux"
  197. }
  198. name := fmt.Sprintf("sdk-repo-%s-%s", osName, s.BaseModuleName())
  199. s.outputBaseName = name
  200. s.outputFile = android.OptionalPathForPath(outputZipFile)
  201. ctx.InstallFile(android.PathForModuleInstall(ctx, "sdk-repo"), name+".zip", outputZipFile)
  202. }
  203. func (s *sdkRepoHost) AndroidMk() android.AndroidMkData {
  204. return android.AndroidMkData{
  205. Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
  206. // TODO: per-OS PHONY
  207. fmt.Fprintln(w, ".PHONY:", name, "sdk_repo", "sdk-repo-"+name)
  208. fmt.Fprintln(w, "sdk_repo", "sdk-repo-"+name+":", strings.Join(s.FilesToInstall().Strings(), " "))
  209. fmt.Fprintf(w, "$(call dist-for-goals,sdk_repo sdk-repo-%s,%s:new_%s-$(FILE_NAME_TAG).zip)\n\n", s.BaseModuleName(), s.outputFile.String(), s.outputBaseName)
  210. },
  211. }
  212. }
  213. func remapPackageSpecs(specs map[string]android.PackagingSpec, remaps []remapProperties) error {
  214. for _, remap := range remaps {
  215. for path, spec := range specs {
  216. if match, err := pathtools.Match(remap.From, path); err != nil {
  217. return fmt.Errorf("Error parsing %q: %v", remap.From, err)
  218. } else if match {
  219. newPath := remap.To
  220. if pathtools.IsGlob(remap.From) {
  221. rel, err := filepath.Rel(constantPartOfPattern(remap.From), path)
  222. if err != nil {
  223. return fmt.Errorf("Error handling %q", path)
  224. }
  225. newPath = filepath.Join(remap.To, rel)
  226. }
  227. delete(specs, path)
  228. spec.SetRelPathInPackage(newPath)
  229. specs[newPath] = spec
  230. }
  231. }
  232. }
  233. return nil
  234. }
  235. func constantPartOfPattern(pattern string) string {
  236. ret := ""
  237. for pattern != "" {
  238. var first string
  239. first, pattern = splitFirst(pattern)
  240. if pathtools.IsGlob(first) {
  241. return ret
  242. }
  243. ret = filepath.Join(ret, first)
  244. }
  245. return ret
  246. }
  247. func splitFirst(path string) (string, string) {
  248. i := strings.IndexRune(path, filepath.Separator)
  249. if i < 0 {
  250. return path, ""
  251. }
  252. return path[:i], path[i+1:]
  253. }